Search in sources :

Example 31 with Db

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

the class SetComment method update.

@Override
public int update() {
    session.commit(true);
    Database db = session.getDatabase();
    session.getUser().checkAdmin();
    DbObject object = null;
    int errorCode = ErrorCode.GENERAL_ERROR_1;
    if (schemaName == null) {
        schemaName = session.getCurrentSchemaName();
    }
    switch(objectType) {
        case DbObject.CONSTANT:
            object = db.getSchema(schemaName).getConstant(objectName);
            break;
        case DbObject.CONSTRAINT:
            object = db.getSchema(schemaName).getConstraint(objectName);
            break;
        case DbObject.FUNCTION_ALIAS:
            object = db.getSchema(schemaName).findFunction(objectName);
            errorCode = ErrorCode.FUNCTION_ALIAS_NOT_FOUND_1;
            break;
        case DbObject.INDEX:
            object = db.getSchema(schemaName).getIndex(objectName);
            break;
        case DbObject.ROLE:
            schemaName = null;
            object = db.findRole(objectName);
            errorCode = ErrorCode.ROLE_NOT_FOUND_1;
            break;
        case DbObject.SCHEMA:
            schemaName = null;
            object = db.findSchema(objectName);
            errorCode = ErrorCode.SCHEMA_NOT_FOUND_1;
            break;
        case DbObject.SEQUENCE:
            object = db.getSchema(schemaName).getSequence(objectName);
            break;
        case DbObject.TABLE_OR_VIEW:
            object = db.getSchema(schemaName).getTableOrView(session, objectName);
            break;
        case DbObject.TRIGGER:
            object = db.getSchema(schemaName).findTrigger(objectName);
            errorCode = ErrorCode.TRIGGER_NOT_FOUND_1;
            break;
        case DbObject.USER:
            schemaName = null;
            object = db.getUser(objectName);
            break;
        case DbObject.USER_DATATYPE:
            schemaName = null;
            object = db.findUserDataType(objectName);
            errorCode = ErrorCode.USER_DATA_TYPE_ALREADY_EXISTS_1;
            break;
        default:
    }
    if (object == null) {
        throw DbException.get(errorCode, objectName);
    }
    String text = expr.optimize(session).getValue(session).getString();
    if (column) {
        Table table = (Table) object;
        table.getColumn(columnName).setComment(text);
    } else {
        object.setComment(text);
    }
    if (column || objectType == DbObject.TABLE_OR_VIEW || objectType == DbObject.USER || objectType == DbObject.INDEX || objectType == DbObject.CONSTRAINT) {
        db.updateMeta(session, object);
    } else {
        Comment comment = db.findComment(object);
        if (comment == null) {
            if (text == null) {
            // reset a non-existing comment - nothing to do
            } else {
                int id = getObjectId();
                comment = new Comment(db, id, object);
                comment.setCommentText(text);
                db.addDatabaseObject(session, comment);
            }
        } else {
            if (text == null) {
                db.removeDatabaseObject(session, comment);
            } else {
                comment.setCommentText(text);
                db.updateMeta(session, comment);
            }
        }
    }
    return 0;
}
Also used : Comment(org.h2.engine.Comment) Table(org.h2.table.Table) DbObject(org.h2.engine.DbObject) Database(org.h2.engine.Database)

Example 32 with Db

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

the class GeneratedKeys method getKeys.

/**
 * Returns generated keys.
 *
 * @param session
 *            session
 * @return local result with generated keys
 */
public LocalResult getKeys(Session session) {
    Database db = session == null ? null : session.getDatabase();
    if (Boolean.FALSE.equals(generatedKeysRequest)) {
        clear(null);
        return new LocalResult();
    }
    ArrayList<ExpressionColumn> expressionColumns;
    if (Boolean.TRUE.equals(generatedKeysRequest)) {
        expressionColumns = new ArrayList<>(allColumns.size());
        for (Column column : allColumns) {
            expressionColumns.add(new ExpressionColumn(db, column));
        }
    } else if (generatedKeysRequest instanceof int[]) {
        if (table != null) {
            int[] indices = (int[]) generatedKeysRequest;
            Column[] columns = table.getColumns();
            int cnt = columns.length;
            allColumns.clear();
            expressionColumns = new ArrayList<>(indices.length);
            for (int idx : indices) {
                if (idx >= 1 && idx <= cnt) {
                    Column column = columns[idx - 1];
                    expressionColumns.add(new ExpressionColumn(db, column));
                    allColumns.add(column);
                }
            }
        } else {
            clear(null);
            return new LocalResult();
        }
    } else if (generatedKeysRequest instanceof String[]) {
        if (table != null) {
            String[] names = (String[]) generatedKeysRequest;
            allColumns.clear();
            expressionColumns = new ArrayList<>(names.length);
            for (String name : names) {
                Column column;
                search: if (table.doesColumnExist(name)) {
                    column = table.getColumn(name);
                } else {
                    name = StringUtils.toUpperEnglish(name);
                    if (table.doesColumnExist(name)) {
                        column = table.getColumn(name);
                    } else {
                        for (Column c : table.getColumns()) {
                            if (c.getName().equalsIgnoreCase(name)) {
                                column = c;
                                break search;
                            }
                        }
                        continue;
                    }
                }
                expressionColumns.add(new ExpressionColumn(db, column));
                allColumns.add(column);
            }
        } else {
            clear(null);
            return new LocalResult();
        }
    } else {
        clear(null);
        return new LocalResult();
    }
    int columnCount = expressionColumns.size();
    if (columnCount == 0) {
        clear(null);
        return new LocalResult();
    }
    LocalResult result = new LocalResult(session, expressionColumns.toArray(new Expression[0]), columnCount);
    for (Map<Column, Value> map : data) {
        Value[] row = new Value[columnCount];
        for (Map.Entry<Column, Value> entry : map.entrySet()) {
            int idx = allColumns.indexOf(entry.getKey());
            if (idx >= 0) {
                row[idx] = entry.getValue();
            }
        }
        for (int i = 0; i < columnCount; i++) {
            if (row[i] == null) {
                row[i] = ValueNull.INSTANCE;
            }
        }
        result.addRow(row);
    }
    clear(null);
    return result;
}
Also used : ArrayList(java.util.ArrayList) ExpressionColumn(org.h2.expression.ExpressionColumn) LocalResult(org.h2.result.LocalResult) ExpressionColumn(org.h2.expression.ExpressionColumn) Column(org.h2.table.Column) Expression(org.h2.expression.Expression) Value(org.h2.value.Value) HashMap(java.util.HashMap) Map(java.util.Map)

Example 33 with Db

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

the class CreateConstant method update.

@Override
public int update() {
    session.commit(true);
    session.getUser().checkAdmin();
    Database db = session.getDatabase();
    if (getSchema().findConstant(constantName) != null) {
        if (ifNotExists) {
            return 0;
        }
        throw DbException.get(ErrorCode.CONSTANT_ALREADY_EXISTS_1, constantName);
    }
    int id = getObjectId();
    Constant constant = new Constant(getSchema(), id, constantName);
    expression = expression.optimize(session);
    Value value = expression.getValue(session);
    constant.setValue(value);
    db.addSchemaObject(session, constant);
    return 0;
}
Also used : Constant(org.h2.schema.Constant) Database(org.h2.engine.Database) Value(org.h2.value.Value)

Example 34 with Db

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

the class CreateSequence method update.

@Override
public int update() {
    session.commit(true);
    Database db = session.getDatabase();
    if (getSchema().findSequence(sequenceName) != null) {
        if (ifNotExists) {
            return 0;
        }
        throw DbException.get(ErrorCode.SEQUENCE_ALREADY_EXISTS_1, sequenceName);
    }
    int id = getObjectId();
    Long startValue = getLong(start);
    Long inc = getLong(increment);
    Long cache = getLong(cacheSize);
    Long min = getLong(minValue);
    Long max = getLong(maxValue);
    Sequence sequence = new Sequence(getSchema(), id, sequenceName, startValue, inc, cache, min, max, cycle, belongsToTable);
    db.addSchemaObject(session, sequence);
    return 0;
}
Also used : Database(org.h2.engine.Database) Sequence(org.h2.schema.Sequence)

Example 35 with Db

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

the class CreateSynonym method createTableSynonym.

private int createTableSynonym(Database db) {
    TableSynonym old = getSchema().getSynonym(data.synonymName);
    if (old != null) {
        if (orReplace) {
        // ok, we replacing the existing synonym
        } else if (ifNotExists) {
            return 0;
        } else {
            throw DbException.get(ErrorCode.TABLE_OR_VIEW_ALREADY_EXISTS_1, data.synonymName);
        }
    }
    TableSynonym table;
    if (old != null) {
        table = old;
        data.schema = table.getSchema();
        table.updateData(data);
        table.setComment(comment);
        table.setModified();
        db.updateMeta(session, table);
    } else {
        data.id = getObjectId();
        table = getSchema().createSynonym(data);
        table.setComment(comment);
        db.addSchemaObject(session, table);
    }
    table.updateSynonymFor();
    return 0;
}
Also used : TableSynonym(org.h2.table.TableSynonym)

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