use of org.h2.util.StatementBuilder in project h2database by h2database.
the class ConstraintReferential method buildUpdateSQL.
private void buildUpdateSQL() {
if (updateAction == ConstraintActionType.RESTRICT) {
return;
}
StatementBuilder buff = new StatementBuilder();
appendUpdate(buff);
appendWhere(buff);
updateSQL = buff.toString();
}
use of org.h2.util.StatementBuilder in project h2database by h2database.
the class ConstraintReferential method getCreateSQLForCopy.
/**
* Create the SQL statement of this object so a copy of the table can be
* made.
*
* @param forTable the table to create the object for
* @param forRefTable the referenced table
* @param quotedName the name of this object (quoted if necessary)
* @param internalIndex add the index name to the statement
* @return the SQL statement
*/
public String getCreateSQLForCopy(Table forTable, Table forRefTable, String quotedName, boolean internalIndex) {
StatementBuilder buff = new StatementBuilder("ALTER TABLE ");
String mainTable = forTable.getSQL();
buff.append(mainTable).append(" ADD CONSTRAINT ");
if (forTable.isHidden()) {
buff.append("IF NOT EXISTS ");
}
buff.append(quotedName);
if (comment != null) {
buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
}
IndexColumn[] cols = columns;
IndexColumn[] refCols = refColumns;
buff.append(" FOREIGN KEY(");
for (IndexColumn c : cols) {
buff.appendExceptFirst(", ");
buff.append(c.getSQL());
}
buff.append(')');
if (internalIndex && indexOwner && forTable == this.table) {
buff.append(" INDEX ").append(index.getSQL());
}
buff.append(" REFERENCES ");
String quotedRefTable;
if (this.table == this.refTable) {
// self-referencing constraints: need to use new table
quotedRefTable = forTable.getSQL();
} else {
quotedRefTable = forRefTable.getSQL();
}
buff.append(quotedRefTable).append('(');
buff.resetCount();
for (IndexColumn r : refCols) {
buff.appendExceptFirst(", ");
buff.append(r.getSQL());
}
buff.append(')');
if (internalIndex && refIndexOwner && forTable == this.table) {
buff.append(" INDEX ").append(refIndex.getSQL());
}
if (deleteAction != ConstraintActionType.RESTRICT) {
buff.append(" ON DELETE ").append(deleteAction.getSqlName());
}
if (updateAction != ConstraintActionType.RESTRICT) {
buff.append(" ON UPDATE ").append(updateAction.getSqlName());
}
return buff.append(" NOCHECK").toString();
}
use of org.h2.util.StatementBuilder in project h2database by h2database.
the class DropSchema method update.
@Override
public int update() {
session.getUser().checkSchemaAdmin();
session.commit(true);
Database db = session.getDatabase();
Schema schema = db.findSchema(schemaName);
if (schema == null) {
if (!ifExists) {
throw DbException.get(ErrorCode.SCHEMA_NOT_FOUND_1, schemaName);
}
} else {
if (!schema.canDrop()) {
throw DbException.get(ErrorCode.SCHEMA_CAN_NOT_BE_DROPPED_1, schemaName);
}
if (dropAction == ConstraintActionType.RESTRICT && !schema.isEmpty()) {
StatementBuilder buff = new StatementBuilder();
for (SchemaObject object : schema.getAll()) {
buff.appendExceptFirst(", ");
buff.append(object.getName());
}
if (buff.length() > 0) {
throw DbException.get(ErrorCode.CANNOT_DROP_2, schemaName, buff.toString());
}
}
db.removeDatabaseObject(session, schema);
}
return 0;
}
use of org.h2.util.StatementBuilder in project h2database by h2database.
the class ExpressionList method getSQL.
@Override
public String getSQL() {
StatementBuilder buff = new StatementBuilder("(");
for (Expression e : list) {
buff.appendExceptFirst(", ");
buff.append(e.getSQL());
}
if (list.length == 1) {
buff.append(',');
}
return buff.append(')').toString();
}
use of org.h2.util.StatementBuilder in project h2database by h2database.
the class Function method getSQL.
@Override
public String getSQL() {
StatementBuilder buff = new StatementBuilder(info.name);
if (info.type == CASE) {
if (args[0] != null) {
buff.append(" ").append(args[0].getSQL());
}
for (int i = 1, len = args.length - 1; i < len; i += 2) {
buff.append(" WHEN ").append(args[i].getSQL());
buff.append(" THEN ").append(args[i + 1].getSQL());
}
if (args.length % 2 == 0) {
buff.append(" ELSE ").append(args[args.length - 1].getSQL());
}
return buff.append(" END").toString();
}
buff.append('(');
switch(info.type) {
case CAST:
{
buff.append(args[0].getSQL()).append(" AS ").append(new Column(null, dataType, precision, scale, displaySize).getCreateSQL());
break;
}
case CONVERT:
{
if (database.getMode().swapConvertFunctionParameters) {
buff.append(new Column(null, dataType, precision, scale, displaySize).getCreateSQL()).append(',').append(args[0].getSQL());
} else {
buff.append(args[0].getSQL()).append(',').append(new Column(null, dataType, precision, scale, displaySize).getCreateSQL());
}
break;
}
case EXTRACT:
{
ValueString v = (ValueString) ((ValueExpression) args[0]).getValue(null);
buff.append(v.getString()).append(" FROM ").append(args[1].getSQL());
break;
}
default:
{
for (Expression e : args) {
buff.appendExceptFirst(", ");
buff.append(e.getSQL());
}
}
}
return buff.append(')').toString();
}
Aggregations