/**
   * Create table script for selected node.
   *
   * @see org.eclipse.jface.action.IAction#run()
   */
  @Override
  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(input, "com.safi.workshop.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);
    }
  }
  /** Let fw get the list of default values for the columns to be used when creating a new row */
  public String[] getDefaultValues(ColumnDisplayDefinition[] colDefs) {

    // we return something valid even if there is a DB error
    final String[] defaultValues = new String[colDefs.length];

    // if we could not identify which table to edit, just return
    if (ti == null) {
      return defaultValues;
    }

    final ISession session = _session;
    final ISQLConnection conn = session.getSQLConnection();

    try {
      SQLDatabaseMetaData md = conn.getSQLMetaData();
      TableColumnInfo[] infos = md.getColumnInfo(ti);

      // read the DB MetaData info and fill in the value, if any
      // Note that the ResultSet info and the colDefs should be
      // in the same order, but we cannot guarantee that.
      int expectedColDefIndex = 0;

      for (int idx = 0; idx < infos.length; idx++) {
        String colName = infos[idx].getColumnName();
        String defValue = infos[idx].getDefaultValue();

        // if value was null, we do not need to do
        // anything else with this column.
        // Also assume that a value of "" is equivilent to null
        if (defValue != null && defValue.length() > 0) {
          // find the entry in colDefs matching this column
          if (colDefs[expectedColDefIndex].getColumnName().equals(colName)) {
            // DB cols are in same order as colDefs
            defaultValues[expectedColDefIndex] = defValue;
          } else {
            // colDefs not in same order as DB, so search for
            // matching colDef entry
            // Note: linear search here will NORMALLY be not too bad
            // because most tables do not have huge numbers of columns.
            for (int i = 0; i < colDefs.length; i++) {
              if (colDefs[i].getColumnName().equals(colName)) {
                defaultValues[i] = defValue;
                break;
              }
            }
          }
        }

        // assuming that the columns in table match colDefs,
        // bump the index to point to the next colDef entry
        expectedColDefIndex++;
      }
    } catch (Exception ex) {
      // i18n[DataSetUpdateableTableModelImpl.error.retrievingdefaultvalues=Error retrieving default
      // column values]
      s_log.error(
          s_stringMgr.getString("DataSetUpdateableTableModelImpl.error.retrievingdefaultvalues"),
          ex);
    }

    return defaultValues;
  }