Search in sources :

Example 11 with TableNode

use of net.sourceforge.sqlexplorer.dbstructure.nodes.TableNode in project tdq-studio-se by Talend.

the class CreateTableScriptAction method run.

/**
 * Create table script for selected node.
 *
 * @see org.eclipse.jface.action.IAction#run()
 */
public void run() {
    TableNode tableNode = (TableNode) _selectedNodes[0];
    ITableInfo info = tableNode.getTableInfo();
    StringBuffer buf = new StringBuffer(4 * 1024);
    String sep = System.getProperty("line.separator");
    try {
        SQLDatabaseMetaData metaData = tableNode.getSession().getMetaData();
        ArrayList<String> pks = new ArrayList<String>();
        PrimaryKeyInfo[] pksInfo = metaData.getPrimaryKey(info);
        for (PrimaryKeyInfo pkInfo : pksInfo) pks.add(pkInfo.getColumnName());
        TableColumnInfo[] columnsInfo = metaData.getColumnInfo(info);
        String tableName = _selectedNodes[0].getQualifiedName();
        buf.append("CREATE TABLE ");
        buf.append(tableName);
        buf.append("(");
        for (TableColumnInfo col : columnsInfo) {
            // String columnName = resultSet.getString(4);
            // String typeName = resultSet.getString(6);
            // String columnSize = resultSet.getString(7);
            // String decimalDigits = resultSet.getString(9);
            // String defaultValue = resultSet.getString(13);
            boolean notNull = "NO".equalsIgnoreCase(col.isNullable());
            String sLower = col.getColumnName().toLowerCase();
            buf.append(sep);
            buf.append(col.getColumnName() + " ");
            buf.append(col.getTypeName());
            boolean bNumeric = false;
            if (sLower.equals("numeric") || sLower.equals("number") || sLower.equals("decimal"))
                bNumeric = true;
            if (sLower.indexOf("char") != -1 || sLower.indexOf("int") != -1) {
                buf.append("(");
                buf.append(col.getColumnSize());
                buf.append(")");
            } else if (bNumeric) {
                buf.append("(");
                buf.append(col.getColumnSize());
                if (col.getDecimalDigits() > 0)
                    buf.append(col.getDecimalDigits());
                buf.append(")");
            }
            if (pks.size() == 1 && pks.get(0).equals(col.getColumnName())) {
                buf.append(" PRIMARY KEY");
            }
            String defaultValue = col.getDefaultValue();
            if (defaultValue != null && !defaultValue.equals("")) {
                buf.append(" default ");
                boolean isSystemValue = bNumeric;
                if (defaultValue.equalsIgnoreCase("CURRENT_TIMESTAMP")) {
                    isSystemValue = true;
                }
                if (!isSystemValue)
                    buf.append("'");
                buf.append(defaultValue);
                if (!isSystemValue)
                    buf.append("'");
            }
            if (notNull) {
                buf.append(" not null");
            }
            buf.append(",");
        }
        buf.deleteCharAt(buf.length() - 1);
        buf.append(")" + sep);
        SQLEditorInput input = new SQLEditorInput("SQL Editor (" + SQLExplorerPlugin.getDefault().getEditorSerialNo() + ").sql");
        input.setUser(_selectedNodes[0].getSession().getUser());
        IWorkbenchPage page = SQLExplorerPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
        SQLEditor editorPart = (SQLEditor) page.openEditor((IEditorInput) input, "net.sourceforge.sqlexplorer.plugin.editors.SQLEditor");
        editorPart.setText(buf.toString());
    } catch (SQLException e) {
        SQLExplorerPlugin.error("Error creating export script", e);
    } catch (PartInitException e) {
        SQLExplorerPlugin.error("Error creating export script", e);
    }
}
Also used : SQLEditor(net.sourceforge.sqlexplorer.plugin.editors.SQLEditor) SQLDatabaseMetaData(net.sourceforge.squirrel_sql.fw.sql.SQLDatabaseMetaData) ITableInfo(net.sourceforge.squirrel_sql.fw.sql.ITableInfo) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PrimaryKeyInfo(net.sourceforge.squirrel_sql.fw.sql.PrimaryKeyInfo) SQLEditorInput(net.sourceforge.sqlexplorer.plugin.editors.SQLEditorInput) TableNode(net.sourceforge.sqlexplorer.dbstructure.nodes.TableNode) TableColumnInfo(net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) PartInitException(org.eclipse.ui.PartInitException) IEditorInput(org.eclipse.ui.IEditorInput)

Example 12 with TableNode

use of net.sourceforge.sqlexplorer.dbstructure.nodes.TableNode in project tdq-studio-se by Talend.

the class GenerateSelectSQLAction method createTableSelect.

/**
 * @return query string for full table select
 */
private String createTableSelect() {
    TableNode node = (TableNode) _selectedNodes[0];
    // $NON-NLS-1$
    StringBuffer query = new StringBuffer("select ");
    // $NON-NLS-1$
    String sep = "";
    List columnNames = node.getColumnNames();
    Iterator it = columnNames.iterator();
    while (it.hasNext()) {
        query.append(sep);
        String column = (String) it.next();
        query.append(quote(column, getQuoteString(node)));
        // $NON-NLS-1$
        sep = ", ";
    }
    // $NON-NLS-1$
    query.append(" from ");
    query.append(fixTableName(node.getQualifiedName()));
    return query.toString();
}
Also used : TableNode(net.sourceforge.sqlexplorer.dbstructure.nodes.TableNode) Iterator(java.util.Iterator) List(java.util.List)

Example 13 with TableNode

use of net.sourceforge.sqlexplorer.dbstructure.nodes.TableNode in project tdq-studio-se by Talend.

the class ImportedKeysTab method getDataSet.

public DataSet getDataSet() throws Exception {
    INode node = getNode();
    if (node == null) {
        return null;
    }
    if (node instanceof TableNode) {
        TableNode tableNode = (TableNode) node;
        ResultSet resultSet = node.getSession().getMetaData().getImportedKeys(tableNode.getTableInfo());
        DataSet dataSet = new DataSet(resultSet, new int[] { 3, 4, 8, 9, 10, 11, 12, 13, 14 });
        resultSet.close();
        return dataSet;
    }
    return null;
}
Also used : INode(net.sourceforge.sqlexplorer.dbstructure.nodes.INode) DataSet(net.sourceforge.sqlexplorer.dataset.DataSet) TableNode(net.sourceforge.sqlexplorer.dbstructure.nodes.TableNode) ResultSet(java.sql.ResultSet)

Example 14 with TableNode

use of net.sourceforge.sqlexplorer.dbstructure.nodes.TableNode in project tdq-studio-se by Talend.

the class PreviewTab method getDataSet.

public DataSet getDataSet() throws Exception {
    INode node = getNode();
    if (node == null) {
        return null;
    }
    if (node instanceof TableNode) {
        TableNode tableNode = (TableNode) node;
        int maxResults = SQLExplorerPlugin.getDefault().getPluginPreferences().getInt(IConstants.PRE_ROW_COUNT);
        if (maxResults == 0) {
            maxResults = 50;
        }
        SQLConnection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        DataSet dataSet = null;
        try {
            connection = tableNode.getSession().grabConnection();
            statement = connection.createStatement();
            statement.setMaxRows(maxResults);
            // $NON-NLS-1$
            statement.execute("select * from " + tableNode.getQualifiedName());
            resultSet = statement.getResultSet();
            dataSet = new DataSet(resultSet, null);
        } finally {
            if (resultSet != null)
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    SQLExplorerPlugin.error(Messages.getString("DataSet.errorCloseRs"), e);
                }
            if (statement != null)
                try {
                    statement.close();
                } catch (SQLException e) {
                    SQLExplorerPlugin.error(Messages.getString("DataSet.errorCloseStmt"), e);
                }
            if (connection != null)
                getNode().getSession().releaseConnection(connection);
        }
        return dataSet;
    }
    return null;
}
Also used : INode(net.sourceforge.sqlexplorer.dbstructure.nodes.INode) DataSet(net.sourceforge.sqlexplorer.dataset.DataSet) SQLException(java.sql.SQLException) Statement(java.sql.Statement) SQLConnection(net.sourceforge.sqlexplorer.dbproduct.SQLConnection) TableNode(net.sourceforge.sqlexplorer.dbstructure.nodes.TableNode) ResultSet(java.sql.ResultSet)

Example 15 with TableNode

use of net.sourceforge.sqlexplorer.dbstructure.nodes.TableNode in project tdq-studio-se by Talend.

the class PrimaryKeysTab method getDataSet.

public DataSet getDataSet() throws Exception {
    INode node = getNode();
    if (node == null) {
        return null;
    }
    if (node instanceof TableNode) {
        TableNode tableNode = (TableNode) node;
        ResultSet resultSet = node.getSession().getMetaData().getPrimaryKeys(tableNode.getTableInfo());
        DataSet dataSet = new DataSet(resultSet, new int[] { 4, 5, 6 });
        resultSet.close();
        return dataSet;
    }
    return null;
}
Also used : INode(net.sourceforge.sqlexplorer.dbstructure.nodes.INode) DataSet(net.sourceforge.sqlexplorer.dataset.DataSet) TableNode(net.sourceforge.sqlexplorer.dbstructure.nodes.TableNode) ResultSet(java.sql.ResultSet)

Aggregations

TableNode (net.sourceforge.sqlexplorer.dbstructure.nodes.TableNode)21 INode (net.sourceforge.sqlexplorer.dbstructure.nodes.INode)15 DataSet (net.sourceforge.sqlexplorer.dataset.DataSet)12 ResultSet (java.sql.ResultSet)7 ArrayList (java.util.ArrayList)7 SQLException (java.sql.SQLException)5 Iterator (java.util.Iterator)3 TreeSet (java.util.TreeSet)3 List (java.util.List)2 DatabaseNode (net.sourceforge.sqlexplorer.dbstructure.nodes.DatabaseNode)2 SQLEditor (net.sourceforge.sqlexplorer.plugin.editors.SQLEditor)2 SQLEditorInput (net.sourceforge.sqlexplorer.plugin.editors.SQLEditorInput)2 ITableInfo (net.sourceforge.squirrel_sql.fw.sql.ITableInfo)2 TableColumnInfo (net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo)2 Point (org.eclipse.swt.graphics.Point)2 Statement (java.sql.Statement)1 ExplorerException (net.sourceforge.sqlexplorer.ExplorerException)1 SQLCannotConnectException (net.sourceforge.sqlexplorer.SQLCannotConnectException)1 ColumnInfoTab (net.sourceforge.sqlexplorer.dbdetail.tab.ColumnInfoTab)1 ColumnPriviligesTab (net.sourceforge.sqlexplorer.dbdetail.tab.ColumnPriviligesTab)1