@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; }
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(); }
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()); } }
private ValueResultSet getTable( Session session, Expression[] argList, boolean onlyColumnList, boolean distinctRows) { int len = columnList.length; Expression[] header = new Expression[len]; Database db = session.getDatabase(); for (int i = 0; i < len; i++) { Column c = columnList[i]; ExpressionColumn col = new ExpressionColumn(db, c); header[i] = col; } LocalResult result = new LocalResult(session, header, len); if (distinctRows) { result.setDistinct(); } if (!onlyColumnList) { Value[][] list = new Value[len][]; int rows = 0; for (int i = 0; i < len; i++) { Value v = argList[i].getValue(session); if (v == ValueNull.INSTANCE) { list[i] = new Value[0]; } else { ValueArray array = (ValueArray) v.convertTo(Value.ARRAY); Value[] l = array.getList(); list[i] = l; rows = Math.max(rows, l.length); } } for (int row = 0; row < rows; row++) { Value[] r = new Value[len]; for (int j = 0; j < len; j++) { Value[] l = list[j]; Value v; if (l.length <= row) { v = ValueNull.INSTANCE; } else { Column c = columnList[j]; v = l[row]; v = c.convert(v); v = v.convertPrecision(c.getPrecision(), false); v = v.convertScale(true, c.getScale()); } r[j] = v; } result.addRow(r); } } result.done(); ValueResultSet vr = ValueResultSet.get(getSimpleResultSet(result, Integer.MAX_VALUE)); return vr; }
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); }
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; }
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(); }
@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(); }
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); }