Exemple #1
0
 public void prepare() {
   if (columns == null) {
     if (list.size() > 0 && list.get(0).length == 0) {
       // special case where table is used as a sequence
       columns = new Column[0];
     } else {
       columns = table.getColumns();
     }
   }
   if (list.size() > 0) {
     for (Expression[] expr : list) {
       if (expr.length != columns.length) {
         throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
       }
       for (int i = 0; i < expr.length; i++) {
         Expression e = expr[i];
         if (e != null) {
           expr[i] = e.optimize(session);
         }
       }
     }
   } else {
     query.prepare();
     if (query.getColumnCount() != columns.length) {
       throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
     }
   }
   if (keys == null) {
     Index idx = table.getPrimaryKey();
     if (idx == null) {
       throw DbException.get(ErrorCode.CONSTRAINT_NOT_FOUND_1, "PRIMARY KEY");
     }
     keys = idx.getColumns();
   }
   StatementBuilder buff = new StatementBuilder("UPDATE ");
   buff.append(table.getSQL()).append(" SET ");
   for (Column c : columns) {
     buff.appendExceptFirst(", ");
     buff.append(c.getSQL()).append("=?");
   }
   buff.append(" WHERE ");
   buff.resetCount();
   for (Column c : keys) {
     buff.appendExceptFirst(" AND ");
     buff.append(c.getSQL()).append("=?");
   }
   String sql = buff.toString();
   update = session.prepare(sql);
 }
Exemple #2
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());
   }
 }
Exemple #3
0
 public String getPlanSQL() {
   StatementBuilder buff = new StatementBuilder("MERGE INTO ");
   buff.append(table.getSQL()).append('(');
   for (Column c : columns) {
     buff.appendExceptFirst(", ");
     buff.append(c.getSQL());
   }
   buff.append(')');
   if (keys != null) {
     buff.append(" KEY(");
     buff.resetCount();
     for (Column c : keys) {
       buff.appendExceptFirst(", ");
       buff.append(c.getSQL());
     }
     buff.append(')');
   }
   buff.append('\n');
   if (list.size() > 0) {
     buff.append("VALUES ");
     int row = 0;
     for (Expression[] expr : list) {
       if (row++ > 0) {
         buff.append(", ");
       }
       buff.append('(');
       buff.resetCount();
       for (Expression e : expr) {
         buff.appendExceptFirst(", ");
         if (e == null) {
           buff.append("DEFAULT");
         } else {
           buff.append(e.getSQL());
         }
       }
       buff.append(')');
     }
   } else {
     buff.append(query.getPlanSQL());
   }
   return buff.toString();
 }