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()); } }
@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(); }
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 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); }
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; }
@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(); }