Search in sources :

Example 1 with PrimaryKeyInfo

use of net.sourceforge.squirrel_sql.fw.sql.PrimaryKeyInfo 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)

Aggregations

SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 TableNode (net.sourceforge.sqlexplorer.dbstructure.nodes.TableNode)1 SQLEditor (net.sourceforge.sqlexplorer.plugin.editors.SQLEditor)1 SQLEditorInput (net.sourceforge.sqlexplorer.plugin.editors.SQLEditorInput)1 ITableInfo (net.sourceforge.squirrel_sql.fw.sql.ITableInfo)1 PrimaryKeyInfo (net.sourceforge.squirrel_sql.fw.sql.PrimaryKeyInfo)1 SQLDatabaseMetaData (net.sourceforge.squirrel_sql.fw.sql.SQLDatabaseMetaData)1 TableColumnInfo (net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo)1 IEditorInput (org.eclipse.ui.IEditorInput)1 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)1 PartInitException (org.eclipse.ui.PartInitException)1