コード例 #1
0
  public void executeSQL(String sql) {
    if (sql != null && sql.trim().length() > 0) {
      removeErrorPanels();

      String origSQL = sql;
      sql = fireSQLToBeExecutedEvent(sql);

      // This can happen if an impl of ISQLExecutionListener returns null
      // from the statementExecuting API method, to indicate that the SQL
      // shouldn't be executed.
      if (sql == null) {
        s_log.info(
            "executeSQL: An ISQLExecutionListener veto'd execution of "
                + "the following SQL: "
                + origSQL);
        return;
      }

      ISQLExecutionListener[] executionListeners =
          _listeners.getListeners(ISQLExecutionListener.class);

      ISQLExecutionHandlerListener executionHandlerListener = createSQLExecutionHandlerListener();

      new SQLExecutionHandler(
          (IResultTab) null, _session, sql, executionHandlerListener, executionListeners);
    }
  }
コード例 #2
0
 /**
  * @see
  *     net.sourceforge.squirrel_sql.fw.datasetviewer.cellcomponent.IDataTypeComponent#getDefaultValue(java.lang.String)
  */
 public Object getDefaultValue(String dbDefaultValue) {
   // At the moment, no default value
   if (s_log.isInfoEnabled()) {
     s_log.info("getDefaultValue: not yet implemented");
   }
   return null;
 }
コード例 #3
0
  /** Load from preferences file. */
  private void loadPrefs() {
    try {
      XMLBeanReader doc = new XMLBeanReader();

      FileWrapper prefFile = PreferenceUtil.getPreferenceFileToReadFrom(plugin);

      doc.load(prefFile, _prefs.getClass().getClassLoader());

      Iterator<Object> it = doc.iterator();

      if (it.hasNext()) {
        _prefs = (IQueryTokenizerPreferenceBean) it.next();
      }
    } catch (FileNotFoundException ignore) {
      s_log.info(USER_PREFS_FILE_NAME + " not found - will be created");
    } catch (Exception ex) {
      s_log.error("Error occurred reading from preferences file: " + USER_PREFS_FILE_NAME, ex);
    }

    _prefs.setClientName(Version.getApplicationName() + "/" + plugin.getDescriptiveName());
    _prefs.setClientVersion(Version.getShortVersion() + "/" + plugin.getVersion());
  }
コード例 #4
0
  /** Insert a row into the DB. If the insert succeeds this returns a null string. */
  public String insertRow(Object[] values, ColumnDisplayDefinition[] colDefs) {

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

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

    int count = -1;

    try {
      // start the string for use in the prepared statment
      StringBuilder buf = new StringBuilder("INSERT INTO ");
      buf.append(ti.getQualifiedName());

      // Add the list of column names we will be inserting into - be sure
      // to skip the rowId column and any auto increment columns.
      buf.append(" ( ");
      for (int i = 0; i < colDefs.length; i++) {
        if (i == _rowIDcol) {
          continue;
        }
        if (colDefs[i].isAutoIncrement()) {
          if (s_log.isInfoEnabled()) {
            s_log.info("insertRow: skipping auto-increment column " + colDefs[i].getColumnName());
          }
          continue;
        }
        buf.append(colDefs[i].getColumnName());
        buf.append(",");
      }
      buf.setCharAt(buf.length() - 1, ')');
      buf.append(" VALUES (");

      // add a variable position for each of the columns
      for (int i = 0; i < colDefs.length; i++) {
        if (i != _rowIDcol && !colDefs[i].isAutoIncrement()) buf.append(" ?,");
      }

      // replace the last "," with ")"
      buf.setCharAt(buf.length() - 1, ')');

      String pstmtSQL = buf.toString();
      if (s_log.isInfoEnabled()) {
        s_log.info("insertRow: pstmt sql = " + pstmtSQL);
      }
      final PreparedStatement pstmt = conn.prepareStatement(pstmtSQL);

      try {
        // We need to keep track of the bind var index separately, since
        // the number of column defs may not be the number of bind vars
        // (For example: auto-increment columns are excluded)
        int bindVarIdx = 1;

        // have the DataType object fill in the appropriate kind of value
        // into the appropriate variable position in the prepared stmt
        for (int i = 0; i < colDefs.length; i++) {
          if (i != _rowIDcol && !colDefs[i].isAutoIncrement()) {
            CellComponentFactory.setPreparedStatementValue(
                colDefs[i], pstmt, values[i], bindVarIdx);
            bindVarIdx++;
          }
        }
        count = pstmt.executeUpdate();
      } finally {
        pstmt.close();
      }
    } catch (SQLException ex) {
      // i18n[DataSetUpdateableTableModelImpl.error.duringInsert=Exception seen during check on DB.
      // Exception was:\n{0}\nInsert was probably not completed correctly.  DB may be corrupted!]
      return s_stringMgr.getString(
          "DataSetUpdateableTableModelImpl.error.duringInsert", ex.getMessage());
    }

    if (count != 1)
      // i18n[DataSetUpdateableTableModelImpl.error.unknownerrorupdate=Unknown problem during
      // update.\nNo count of inserted rows was returned.\nDatabase may be corrupted!]
      return s_stringMgr.getString("DataSetUpdateableTableModelImpl.error.unknownerrorupdate");

    // insert succeeded
    try {
      IObjectTreeAPI api = _session.getObjectTreeAPIOfActiveSessionWindow();
      api.refreshSelectedTab();
    } catch (Exception e) {
      e.printStackTrace();
    }

    return null;
  }