Search in sources :

Example 36 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class CreateTable method update.

@Override
public int update() {
    if (!transactional) {
        session.commit(true);
    }
    Database db = session.getDatabase();
    if (!db.isPersistent()) {
        data.persistIndexes = false;
    }
    boolean isSessionTemporary = data.temporary && !data.globalTemporary;
    if (!isSessionTemporary) {
        db.lockMeta(session);
    }
    if (getSchema().resolveTableOrView(session, data.tableName) != null) {
        if (ifNotExists) {
            return 0;
        }
        throw DbException.get(ErrorCode.TABLE_OR_VIEW_ALREADY_EXISTS_1, data.tableName);
    }
    if (asQuery != null) {
        asQuery.prepare();
        if (data.columns.isEmpty()) {
            generateColumnsFromQuery();
        } else if (data.columns.size() != asQuery.getColumnCount()) {
            throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
        }
    }
    changePrimaryKeysToNotNull(data.columns);
    data.id = getObjectId();
    data.create = create;
    data.session = session;
    Table table = getSchema().createTable(data);
    ArrayList<Sequence> sequences = generateSequences(data.columns, data.temporary);
    table.setComment(comment);
    if (isSessionTemporary) {
        if (onCommitDrop) {
            table.setOnCommitDrop(true);
        }
        if (onCommitTruncate) {
            table.setOnCommitTruncate(true);
        }
        session.addLocalTempTable(table);
    } else {
        db.lockMeta(session);
        db.addSchemaObject(session, table);
    }
    try {
        for (Column c : data.columns) {
            c.prepareExpression(session);
        }
        for (Sequence sequence : sequences) {
            table.addSequence(sequence);
        }
        createConstraints();
        if (asQuery != null) {
            boolean old = session.isUndoLogEnabled();
            try {
                session.setUndoLogEnabled(false);
                session.startStatementWithinTransaction();
                Insert insert = new Insert(session);
                insert.setSortedInsertMode(sortedInsertMode);
                insert.setQuery(asQuery);
                insert.setTable(table);
                insert.setInsertFromSelect(true);
                insert.prepare();
                insert.update();
            } finally {
                session.setUndoLogEnabled(old);
            }
        }
        HashSet<DbObject> set = new HashSet<>();
        set.clear();
        table.addDependencies(set);
        for (DbObject obj : set) {
            if (obj == table) {
                continue;
            }
            if (obj.getType() == DbObject.TABLE_OR_VIEW) {
                if (obj instanceof Table) {
                    Table t = (Table) obj;
                    if (t.getId() > table.getId()) {
                        throw DbException.get(ErrorCode.FEATURE_NOT_SUPPORTED_1, "Table depends on another table " + "with a higher ID: " + t + ", this is currently not supported, " + "as it would prevent the database from " + "being re-opened");
                    }
                }
            }
        }
    } catch (DbException e) {
        db.checkPowerOff();
        db.removeSchemaObject(session, table);
        if (!transactional) {
            session.commit(true);
        }
        throw e;
    }
    return 0;
}
Also used : Table(org.h2.table.Table) ExpressionColumn(org.h2.expression.ExpressionColumn) Column(org.h2.table.Column) DbObject(org.h2.engine.DbObject) Database(org.h2.engine.Database) Sequence(org.h2.schema.Sequence) Insert(org.h2.command.dml.Insert) HashSet(java.util.HashSet) DbException(org.h2.message.DbException)

Example 37 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class DropAggregate method update.

@Override
public int update() {
    session.getUser().checkAdmin();
    session.commit(true);
    Database db = session.getDatabase();
    UserAggregate aggregate = db.findAggregate(name);
    if (aggregate == null) {
        if (!ifExists) {
            throw DbException.get(ErrorCode.AGGREGATE_NOT_FOUND_1, name);
        }
    } else {
        db.removeDatabaseObject(session, aggregate);
    }
    return 0;
}
Also used : UserAggregate(org.h2.engine.UserAggregate) Database(org.h2.engine.Database)

Example 38 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class CreateUser method update.

@Override
public int update() {
    session.getUser().checkAdmin();
    session.commit(true);
    Database db = session.getDatabase();
    if (db.findRole(userName) != null) {
        throw DbException.get(ErrorCode.ROLE_ALREADY_EXISTS_1, userName);
    }
    if (db.findUser(userName) != null) {
        if (ifNotExists) {
            return 0;
        }
        throw DbException.get(ErrorCode.USER_ALREADY_EXISTS_1, userName);
    }
    int id = getObjectId();
    User user = new User(db, id, userName, false);
    user.setAdmin(admin);
    user.setComment(comment);
    if (hash != null && salt != null) {
        setSaltAndHash(user, session, salt, hash);
    } else if (password != null) {
        setPassword(user, session, password);
    } else {
        throw DbException.throwInternalError();
    }
    db.addDatabaseObject(session, user);
    return 0;
}
Also used : User(org.h2.engine.User) Database(org.h2.engine.Database)

Example 39 with Db

use of org.h2.jaqu.Db 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 40 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class TableFunction method getTable.

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();
    return ValueResultSet.get(getSimpleResultSet(result, Integer.MAX_VALUE));
}
Also used : LocalResult(org.h2.result.LocalResult) Column(org.h2.table.Column) Database(org.h2.engine.Database) Value(org.h2.value.Value) ValueArray(org.h2.value.ValueArray)

Aggregations

Database (org.h2.engine.Database)70 Connection (java.sql.Connection)31 Statement (java.sql.Statement)20 Table (org.h2.table.Table)19 PreparedStatement (java.sql.PreparedStatement)18 ResultSet (java.sql.ResultSet)13 SQLException (java.sql.SQLException)13 Column (org.h2.table.Column)12 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)9 StatementBuilder (org.h2.util.StatementBuilder)9 DbObject (org.h2.engine.DbObject)8 File (java.io.File)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 DbException (org.h2.message.DbException)7 Schema (org.h2.schema.Schema)7 Before (org.junit.Before)7 InputStream (java.io.InputStream)6 ExpressionColumn (org.h2.expression.ExpressionColumn)6 JdbcConnection (org.h2.jdbc.JdbcConnection)6