use of org.h2.schema.Sequence in project h2database by h2database.
the class Merge method prepare.
@Override
public void prepare() {
if (columns == null) {
if (!valuesExpressionList.isEmpty() && valuesExpressionList.get(0).length == 0) {
// special case where table is used as a sequence
columns = new Column[0];
} else {
columns = targetTable.getColumns();
}
}
if (!valuesExpressionList.isEmpty()) {
for (Expression[] expr : valuesExpressionList) {
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 = targetTable.getPrimaryKey();
if (idx == null) {
throw DbException.get(ErrorCode.CONSTRAINT_NOT_FOUND_1, "PRIMARY KEY");
}
keys = idx.getColumns();
}
StatementBuilder buff = new StatementBuilder("UPDATE ");
buff.append(targetTable.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);
}
use of org.h2.schema.Sequence in project h2database by h2database.
the class Table method removeChildrenAndResources.
@Override
public void removeChildrenAndResources(Session session) {
while (!dependentViews.isEmpty()) {
TableView view = dependentViews.get(0);
dependentViews.remove(0);
database.removeSchemaObject(session, view);
}
while (synonyms != null && !synonyms.isEmpty()) {
TableSynonym synonym = synonyms.remove(0);
database.removeSchemaObject(session, synonym);
}
while (triggers != null && !triggers.isEmpty()) {
TriggerObject trigger = triggers.remove(0);
database.removeSchemaObject(session, trigger);
}
while (constraints != null && !constraints.isEmpty()) {
Constraint constraint = constraints.remove(0);
database.removeSchemaObject(session, constraint);
}
for (Right right : database.getAllRights()) {
if (right.getGrantedObject() == this) {
database.removeDatabaseObject(session, right);
}
}
database.removeMeta(session, getId());
// before removing the table object)
while (sequences != null && !sequences.isEmpty()) {
Sequence sequence = sequences.remove(0);
// this is possible when calling ALTER TABLE ALTER COLUMN
if (database.getDependentTable(sequence, this) == null) {
database.removeSchemaObject(session, sequence);
}
}
}
use of org.h2.schema.Sequence in project h2database by h2database.
the class Column method getCreateSQL.
private String getCreateSQL(boolean includeName) {
StringBuilder buff = new StringBuilder();
if (includeName && name != null) {
buff.append(Parser.quoteIdentifier(name)).append(' ');
}
if (originalSQL != null) {
buff.append(originalSQL);
} else {
buff.append(DataType.getDataType(type).name);
switch(type) {
case Value.DECIMAL:
buff.append('(').append(precision).append(", ").append(scale).append(')');
break;
case Value.ENUM:
buff.append('(');
for (int i = 0; i < enumerators.length; i++) {
buff.append('\'').append(enumerators[i]).append('\'');
if (i < enumerators.length - 1) {
buff.append(',');
}
}
buff.append(')');
break;
case Value.BYTES:
case Value.STRING:
case Value.STRING_IGNORECASE:
case Value.STRING_FIXED:
if (precision < Integer.MAX_VALUE) {
buff.append('(').append(precision).append(')');
}
break;
default:
}
}
if (!visible) {
buff.append(" INVISIBLE ");
}
if (defaultExpression != null) {
String sql = defaultExpression.getSQL();
if (sql != null) {
if (isComputed) {
buff.append(" AS ").append(sql);
} else if (defaultExpression != null) {
buff.append(" DEFAULT ").append(sql);
}
}
}
if (onUpdateExpression != null) {
String sql = onUpdateExpression.getSQL();
if (sql != null) {
buff.append(" ON UPDATE ").append(sql);
}
}
if (!nullable) {
buff.append(" NOT NULL");
}
if (convertNullToDefault) {
buff.append(" NULL_TO_DEFAULT");
}
if (sequence != null) {
buff.append(" SEQUENCE ").append(sequence.getSQL());
}
if (selectivity != 0) {
buff.append(" SELECTIVITY ").append(selectivity);
}
if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
}
if (checkConstraint != null) {
buff.append(" CHECK ").append(checkConstraintSQL);
}
return buff.toString();
}
use of org.h2.schema.Sequence in project h2database by h2database.
the class Column method convertAutoIncrementToSequence.
/**
* Convert the auto-increment flag to a sequence that is linked with this
* table.
*
* @param session the session
* @param schema the schema where the sequence should be generated
* @param id the object id
* @param temporary true if the sequence is temporary and does not need to
* be stored
*/
public void convertAutoIncrementToSequence(Session session, Schema schema, int id, boolean temporary) {
if (!autoIncrement) {
DbException.throwInternalError();
}
if ("IDENTITY".equals(originalSQL)) {
originalSQL = "BIGINT";
} else if ("SERIAL".equals(originalSQL)) {
originalSQL = "INT";
}
String sequenceName;
do {
ValueUuid uuid = ValueUuid.getNewRandom();
String s = uuid.getString();
s = StringUtils.toUpperEnglish(s.replace('-', '_'));
sequenceName = "SYSTEM_SEQUENCE_" + s;
} while (schema.findSequence(sequenceName) != null);
Sequence seq = new Sequence(schema, id, sequenceName, start, increment);
seq.setTemporary(temporary);
session.getDatabase().addSchemaObject(session, seq);
setAutoIncrement(false, 0, 0);
SequenceValue seqValue = new SequenceValue(seq);
setDefaultExpression(session, seqValue);
setSequence(seq);
}
use of org.h2.schema.Sequence in project h2database by h2database.
the class Parser method parseComment.
private Prepared parseComment() {
int type = 0;
read("ON");
boolean column = false;
if (readIf("TABLE") || readIf("VIEW")) {
type = DbObject.TABLE_OR_VIEW;
} else if (readIf("COLUMN")) {
column = true;
type = DbObject.TABLE_OR_VIEW;
} else if (readIf("CONSTANT")) {
type = DbObject.CONSTANT;
} else if (readIf("CONSTRAINT")) {
type = DbObject.CONSTRAINT;
} else if (readIf("ALIAS")) {
type = DbObject.FUNCTION_ALIAS;
} else if (readIf("INDEX")) {
type = DbObject.INDEX;
} else if (readIf("ROLE")) {
type = DbObject.ROLE;
} else if (readIf("SCHEMA")) {
type = DbObject.SCHEMA;
} else if (readIf("SEQUENCE")) {
type = DbObject.SEQUENCE;
} else if (readIf("TRIGGER")) {
type = DbObject.TRIGGER;
} else if (readIf("USER")) {
type = DbObject.USER;
} else if (readIf("DOMAIN")) {
type = DbObject.USER_DATATYPE;
} else {
throw getSyntaxError();
}
SetComment command = new SetComment(session);
String objectName;
if (column) {
// can't use readIdentifierWithSchema() because
// it would not read schema.table.column correctly
// if the db name is equal to the schema name
ArrayList<String> list = New.arrayList();
do {
list.add(readUniqueIdentifier());
} while (readIf("."));
schemaName = session.getCurrentSchemaName();
if (list.size() == 4) {
if (!equalsToken(database.getShortName(), list.remove(0))) {
throw DbException.getSyntaxError(sqlCommand, parseIndex, "database name");
}
}
if (list.size() == 3) {
schemaName = list.remove(0);
}
if (list.size() != 2) {
throw DbException.getSyntaxError(sqlCommand, parseIndex, "table.column");
}
objectName = list.get(0);
command.setColumn(true);
command.setColumnName(list.get(1));
} else {
objectName = readIdentifierWithSchema();
}
command.setSchemaName(schemaName);
command.setObjectName(objectName);
command.setObjectType(type);
read("IS");
command.setCommentExpression(readExpression());
return command;
}
Aggregations