@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(); }
private int generateInsertValues(int count, Table table) throws IOException { PlanItem plan = table.getBestPlanItem(session, null, null); Index index = plan.getIndex(); Cursor cursor = index.find(session, null, null); Column[] columns = table.getColumns(); StatementBuilder buff = new StatementBuilder("INSERT INTO "); buff.append(table.getSQL()).append('('); for (Column col : columns) { buff.appendExceptFirst(", "); buff.append(Parser.quoteIdentifier(col.getName())); } buff.append(") VALUES"); if (!simple) { buff.append('\n'); } buff.append('('); String ins = buff.toString(); buff = null; while (cursor.next()) { Row row = cursor.get(); if (buff == null) { buff = new StatementBuilder(ins); } else { buff.append(",\n("); } for (int j = 0; j < row.getColumnCount(); j++) { if (j > 0) { buff.append(", "); } Value v = row.getValue(j); if (v.getPrecision() > lobBlockSize) { int id; if (v.getType() == Value.CLOB) { id = writeLobStream(v); buff.append("SYSTEM_COMBINE_CLOB(" + id + ")"); } else if (v.getType() == Value.BLOB) { id = writeLobStream(v); buff.append("SYSTEM_COMBINE_BLOB(" + id + ")"); } else { buff.append(v.getSQL()); } } else { buff.append(v.getSQL()); } } buff.append(')'); count++; if ((count & 127) == 0) { checkCanceled(); } if (simple || buff.length() > Constants.IO_BUFFER_SIZE) { add(buff.toString(), true); buff = null; } } if (buff != null) { add(buff.toString(), true); } return count; }
protected Row createRow(Value[] values) { Row newRow = table.getTemplateRow(); for (int j = 0; j < columns.length; j++) { Column c = columns[j]; int index = c.getColumnId(); Value v = c.convert(values[j]); newRow.setValue(index, v); } return newRow; }
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()); } }
/** * Validate all values in this row, convert the values if required, and update the sequence values * if required. This call will also set the default values if required and set the computed column * if there are any. * * @param session the session * @param row the row */ public void validateConvertUpdateSequence(Session session, Row row) { for (int i = 0; i < columns.length; i++) { Value value = row.getValue(i); Column column = columns[i]; Value v2; if (column.getComputed()) { // force updating the value value = null; v2 = column.computeValue(session, row); } v2 = column.validateConvertUpdateSequence(session, value); if (v2 != value) { row.setValue(i, v2); } } }
protected Row createRow(Expression[] expr, int rowId) { Row newRow = table.getTemplateRow(); for (int i = 0, len = columns.length; i < len; i++) { Column c = columns[i]; int index = c.getColumnId(); Expression e = expr[i]; if (e != null) { // e can be null (DEFAULT) try { Value v = c.convert(e.getValue(session)); newRow.setValue(index, v); } catch (DbException ex) { throw setRow(ex, rowId, getSQL(expr)); } } } return newRow; }
synchronized Row getNullRow() { if (nullRow == null) { nullRow = new Row(new Value[columns.length], 1); for (int i = 0; i < columns.length; i++) { nullRow.setValue(i, ValueNull.INSTANCE); } } return nullRow; }
@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(); }
@Override public Row get() { Row row = new Row(result.currentRow(), 0); row.setKey(row.getValue(0).getLong()); return row; }