示例#1
0
  @Override
  public void add(Session session, Row row) {
    StatementBuilder sql = new StatementBuilder("insert into ");
    sql.append(getName()).append("(_gui_row_id_");

    for (Column c : getColumns()) {
      sql.append(",");
      sql.append(c.getName());
    }

    sql.append(") values (");
    sql.append(row.getKey());

    for (Column c : getColumns()) {
      sql.append(",");
      Value v = row.getValue(c.getColumnId());
      if (v == null) {
        sql.append("DEFAULT");
      } else {
        sql.append(v.getSQL());
      }
    }
    sql.append(")");

    Prepared prepared = session.prepare(sql.toString(), true);
    prepared.setLocal(false);
    prepared.update();
  }
示例#2
0
 /**
  * Update a list of rows in this table.
  *
  * @param prepared the prepared statement
  * @param session the session
  * @param rows a list of row pairs of the form old row, new row, old row, new row,...
  */
 public void updateRows(Prepared prepared, Session session, RowList rows) {
   // in case we need to undo the update
   int rollback = session.getUndoLogPos();
   // remove the old rows
   int rowScanCount = 0;
   for (rows.reset(); rows.hasNext(); ) {
     if ((++rowScanCount & 127) == 0) {
       prepared.checkCanceled();
     }
     Row o = rows.next();
     rows.next();
     removeRow(session, o);
     session.log(this, UndoLogRecord.DELETE, o);
   }
   // add the new rows
   for (rows.reset(); rows.hasNext(); ) {
     if ((++rowScanCount & 127) == 0) {
       prepared.checkCanceled();
     }
     rows.next();
     Row n = rows.next();
     try {
       addRow(session, n);
     } catch (DbException e) {
       if (e.getErrorCode() == ErrorCode.CONCURRENT_UPDATE_1) {
         session.rollbackTo(rollback, false);
       }
       throw e;
     }
     session.log(this, UndoLogRecord.INSERT, n);
   }
 }
示例#3
0
  public GlobalUniqueIndex(
      Session session,
      MVTable table,
      int id,
      String indexName,
      IndexColumn[] columns,
      IndexType indexType) {
    initIndexBase(table, id, indexName, columns, indexType);
    if (!database.isStarting()) {
      checkIndexColumnTypes(columns);
    }

    StatementBuilder sql = new StatementBuilder("create ");
    if (table.isGlobalTemporary()) sql.append("global temporary ");
    else if (table.isTemporary()) sql.append("local temporary ");
    sql.append("table if not exists ").append(getName()).append("(_gui_row_id_ long,");

    for (Column c : getColumns()) {
      sql.append(c.getCreateSQL()).append(",");
    }

    sql.append("primary key(");
    for (Column c : getColumns()) {
      sql.appendExceptFirst(",");
      sql.append(c.getName());
    }
    sql.append("))");

    Prepared prepared = session.prepare(sql.toString(), true);
    prepared.setLocal(true);
    prepared.update();
  }
示例#4
0
  @Override
  public void rename(String newName) {
    StatementBuilder sql = new StatementBuilder("alter table ");
    sql.append(getName()).append(" rename to").append(newName);

    Prepared prepared = getDatabase().getSystemSession().prepare(sql.toString(), true);
    prepared.setLocal(true);
    prepared.update();
  }
示例#5
0
 private void merge(Row row) {
   ArrayList<Parameter> k = update.getParameters();
   for (int i = 0; i < columns.length; i++) {
     Column col = columns[i];
     Value v = row.getValue(col.getColumnId());
     Parameter p = k.get(i);
     p.setValue(v);
   }
   for (int i = 0; i < keys.length; i++) {
     Column col = keys[i];
     Value v = row.getValue(col.getColumnId());
     if (v == null) {
       throw DbException.get(ErrorCode.COLUMN_CONTAINS_NULL_VALUES_1, col.getSQL());
     }
     Parameter p = k.get(columns.length + i);
     p.setValue(v);
   }
   int count = update.update();
   if (count == 0) {
     try {
       table.validateConvertUpdateSequence(session, row);
       boolean done = table.fireBeforeRow(session, null, row);
       if (!done) {
         table.lock(session, true, false);
         table.addRow(session, row);
         session.log(table, UndoLogRecord.INSERT, row);
         table.fireAfterRow(session, null, row, false);
       }
     } catch (DbException e) {
       if (e.getErrorCode() == ErrorCode.DUPLICATE_KEY_1) {
         // possibly a concurrent merge or insert
         Index index = (Index) e.getSource();
         if (index != null) {
           // verify the index columns match the key
           Column[] indexColumns = index.getColumns();
           boolean indexMatchesKeys = false;
           if (indexColumns.length <= keys.length) {
             for (int i = 0; i < indexColumns.length; i++) {
               if (indexColumns[i] != keys[i]) {
                 indexMatchesKeys = false;
                 break;
               }
             }
           }
           if (indexMatchesKeys) {
             throw DbException.get(ErrorCode.CONCURRENT_UPDATE_1, table.getName());
           }
         }
       }
       throw e;
     }
   } else if (count != 1) {
     throw DbException.get(ErrorCode.DUPLICATE_KEY_1, table.getSQL());
   }
 }
示例#6
0
  @Override
  public long getRowCount(Session session) {
    StatementBuilder sql = new StatementBuilder("select count(*) from ");
    sql.append(getName());

    if (session == null) session = getDatabase().getSystemSession();
    Prepared prepared = session.prepare(sql.toString(), true);
    prepared.setLocal(false);
    ResultInterface result = prepared.query(0);
    return result.getRowCount();
  }
示例#7
0
  private int execute(Prepared p) {
    beginTransaction(p);

    boolean isTopTransaction = false;
    boolean isNestedTransaction = false;
    Session session = p.getSession();

    try {
      if (!p.isLocal() && p.isBatch()) {
        if (session.isAutoCommit()) {
          session.setAutoCommit(false);
          isTopTransaction = true;
        } else {
          isNestedTransaction = true;
          session.addSavepoint(TransactionCommand.INTERNAL_SAVEPOINT);
        }
      }
      int updateCount = 0;
      switch (p.getType()) {
        case CommandInterface.INSERT:
          updateCount = nestedRouter.executeInsert((Insert) p);
          break;
        case CommandInterface.UPDATE:
          updateCount = nestedRouter.executeUpdate((Update) p);
          break;
        case CommandInterface.DELETE:
          updateCount = nestedRouter.executeDelete((Delete) p);
          break;
        case CommandInterface.MERGE:
          updateCount = nestedRouter.executeMerge((Merge) p);
          break;
      }
      if (isTopTransaction) session.commit(false);
      return updateCount;
    } catch (Exception e) {
      if (isTopTransaction) session.rollback();

      // 嵌套事务出错时提前rollback
      if (isNestedTransaction) session.rollbackToSavepoint(TransactionCommand.INTERNAL_SAVEPOINT);

      throw DbException.convert(e);
    } finally {
      if (isTopTransaction) session.setAutoCommit(true);
    }
  }
示例#8
0
  @Override
  public void remove(Session session, Row row) {
    StatementBuilder sql = new StatementBuilder("delete from ");
    sql.append(getName());
    if (row != null) {
      sql.append(" where ");

      for (Column c : getColumns()) {
        sql.appendExceptFirst(" and ");
        sql.append(c.getName()).append("=");
        Value v = row.getValue(c.getColumnId());
        if (v != null) {
          sql.append(v.getSQL());
        }
      }
    }

    Prepared prepared = session.prepare(sql.toString(), true);
    prepared.setLocal(false);
    prepared.update();
  }
示例#9
0
  private Cursor find(Session session, SearchRow first, boolean bigger, SearchRow last) {
    StatementBuilder sql = new StatementBuilder("select _gui_row_id_");
    for (Column c : getColumns()) {
      sql.append(",");
      sql.append(c.getName());
    }
    sql.append(" from ").append(getName());

    if (first != null || last != null) {
      sql.append(" where ");

      for (Column c : getColumns()) {
        sql.appendExceptFirst(" and ");
        if (first != null) {
          sql.append(c.getName()).append(">=");
          Value v = first.getValue(c.getColumnId());
          if (v != null) {
            sql.append(v.getSQL());
          }
        }
        if (last != null) {
          sql.append(c.getName()).append("<=");
          Value v = last.getValue(c.getColumnId());
          if (v != null) {
            sql.append(v.getSQL());
          }
        }
      }
    }

    Prepared prepared = session.prepare(sql.toString(), true);
    prepared.setLocal(false);
    ResultInterface result = prepared.query(0);
    if (bigger) result.next();
    return new GlobalUniqueIndexTableCursor(result);
  }
示例#10
0
 public void setCommand(Command command) {
   super.setCommand(command);
   if (query != null) {
     query.setCommand(command);
   }
 }
示例#11
0
 private void beginTransaction(Prepared p) {
   p.getSession().getTransaction(p);
 }
示例#12
0
 @Override
 public void truncate(Session session) {
   Prepared prepared = session.prepare("truncate table " + getName(), true);
   prepared.setLocal(true);
   prepared.update();
 }
示例#13
0
 @Override
 public void remove(Session session) {
   Prepared prepared = session.prepare("drop table if exists " + getName(), true);
   prepared.setLocal(true);
   prepared.update();
 }