Search in sources :

Example 6 with StatementBuilder

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();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder)

Example 7 with StatementBuilder

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();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) IndexColumn(org.h2.table.IndexColumn)

Example 8 with StatementBuilder

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;
}
Also used : SchemaObject(org.h2.schema.SchemaObject) Schema(org.h2.schema.Schema) StatementBuilder(org.h2.util.StatementBuilder) Database(org.h2.engine.Database)

Example 9 with StatementBuilder

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();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder)

Example 10 with StatementBuilder

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();
}
Also used : Column(org.h2.table.Column) StatementBuilder(org.h2.util.StatementBuilder) ValueString(org.h2.value.ValueString)

Aggregations

StatementBuilder (org.h2.util.StatementBuilder)82 Value (org.h2.value.Value)16 Column (org.h2.table.Column)15 IndexColumn (org.h2.table.IndexColumn)11 PreparedStatement (java.sql.PreparedStatement)9 Expression (org.h2.expression.Expression)9 ValueString (org.h2.value.ValueString)7 Index (org.h2.index.Index)6 DbException (org.h2.message.DbException)6 ResultSet (java.sql.ResultSet)5 Row (org.h2.result.Row)4 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 Constraint (org.h2.constraint.Constraint)3 Database (org.h2.engine.Database)3 ExpressionColumn (org.h2.expression.ExpressionColumn)3 HashMap (java.util.HashMap)2 SelectOrderBy (org.h2.command.dml.SelectOrderBy)2 ResultInterface (org.h2.result.ResultInterface)2 SearchRow (org.h2.result.SearchRow)2