Search in sources :

Example 86 with Db

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

the class MetaTable method checkIndex.

private boolean checkIndex(Session session, String value, Value indexFrom, Value indexTo) {
    if (value == null || (indexFrom == null && indexTo == null)) {
        return true;
    }
    Database db = session.getDatabase();
    Value v;
    if (database.getMode().lowerCaseIdentifiers) {
        v = ValueStringIgnoreCase.get(value);
    } else {
        v = ValueString.get(value);
    }
    if (indexFrom != null && db.compare(v, indexFrom) < 0) {
        return false;
    }
    if (indexTo != null && db.compare(v, indexTo) > 0) {
        return false;
    }
    return true;
}
Also used : Database(org.h2.engine.Database) Value(org.h2.value.Value)

Example 87 with Db

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

the class TableBase method getCreateSQL.

@Override
public String getCreateSQL() {
    Database db = getDatabase();
    if (db == null) {
        // closed
        return null;
    }
    StatementBuilder buff = new StatementBuilder("CREATE ");
    if (isTemporary()) {
        if (isGlobalTemporary()) {
            buff.append("GLOBAL ");
        } else {
            buff.append("LOCAL ");
        }
        buff.append("TEMPORARY ");
    } else if (isPersistIndexes()) {
        buff.append("CACHED ");
    } else {
        buff.append("MEMORY ");
    }
    buff.append("TABLE ");
    if (isHidden) {
        buff.append("IF NOT EXISTS ");
    }
    buff.append(getSQL());
    if (comment != null) {
        buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
    }
    buff.append("(\n    ");
    for (Column column : columns) {
        buff.appendExceptFirst(",\n    ");
        buff.append(column.getCreateSQL());
    }
    buff.append("\n)");
    if (tableEngine != null) {
        DbSettings s = db.getSettings();
        String d = s.defaultTableEngine;
        if (d == null && s.mvStore) {
            d = MVTableEngine.class.getName();
        }
        if (d == null || !tableEngine.endsWith(d)) {
            buff.append("\nENGINE ");
            buff.append(StringUtils.quoteIdentifier(tableEngine));
        }
    }
    if (!tableEngineParams.isEmpty()) {
        buff.append("\nWITH ");
        buff.resetCount();
        for (String parameter : tableEngineParams) {
            buff.appendExceptFirst(", ");
            buff.append(StringUtils.quoteIdentifier(parameter));
        }
    }
    if (!isPersistIndexes() && !isPersistData()) {
        buff.append("\nNOT PERSISTENT");
    }
    if (isHidden) {
        buff.append("\nHIDDEN");
    }
    return buff.toString();
}
Also used : DbSettings(org.h2.engine.DbSettings) StatementBuilder(org.h2.util.StatementBuilder) Database(org.h2.engine.Database) MVTableEngine(org.h2.mvstore.db.MVTableEngine)

Example 88 with Db

use of org.h2.jaqu.Db 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;
}
Also used : SetComment(org.h2.command.ddl.SetComment) ValueString(org.h2.value.ValueString) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint)

Example 89 with Db

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

the class Parser method createCTEView.

private TableView createCTEView(String cteViewName, String querySQL, List<Column> columnTemplateList, boolean allowRecursiveQueryDetection, boolean addViewToSession, boolean isPersistent, Session targetSession) {
    Database db = targetSession.getDatabase();
    Schema schema = getSchemaWithDefault();
    int id = db.allocateObjectId();
    Column[] columnTemplateArray = columnTemplateList.toArray(new Column[0]);
    // No easy way to determine if this is a recursive query up front, so we just compile
    // it twice - once without the flag set, and if we didn't see a recursive term,
    // then we just compile it again.
    TableView view;
    synchronized (targetSession) {
        view = new TableView(schema, id, cteViewName, querySQL, parameters, columnTemplateArray, targetSession, allowRecursiveQueryDetection, false, /* literalsChecked */
        true, /* isTableExpression */
        isPersistent);
        if (!view.isRecursiveQueryDetected() && allowRecursiveQueryDetection) {
            if (isPersistent) {
                db.addSchemaObject(targetSession, view);
                view.lock(targetSession, true, true);
                targetSession.getDatabase().removeSchemaObject(targetSession, view);
            } else {
                session.removeLocalTempTable(view);
            }
            view = new TableView(schema, id, cteViewName, querySQL, parameters, columnTemplateArray, targetSession, false, /* assume recursive */
            false, /* literalsChecked */
            true, /* isTableExpression */
            isPersistent);
        }
        // both removeSchemaObject and removeLocalTempTable hold meta locks
        targetSession.getDatabase().unlockMeta(targetSession);
    }
    view.setTableExpression(true);
    view.setTemporary(!isPersistent);
    view.setHidden(true);
    view.setOnCommitDrop(false);
    if (addViewToSession) {
        if (isPersistent) {
            db.addSchemaObject(targetSession, view);
            view.unlock(targetSession);
            db.unlockMeta(targetSession);
        } else {
            targetSession.addLocalTempTable(view);
        }
    }
    return view;
}
Also used : AlterTableRenameColumn(org.h2.command.ddl.AlterTableRenameColumn) AlterTableAlterColumn(org.h2.command.ddl.AlterTableAlterColumn) Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) IndexColumn(org.h2.table.IndexColumn) DropSchema(org.h2.command.ddl.DropSchema) CreateSchema(org.h2.command.ddl.CreateSchema) Schema(org.h2.schema.Schema) Database(org.h2.engine.Database) DropDatabase(org.h2.command.ddl.DropDatabase) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint) TableView(org.h2.table.TableView)

Example 90 with Db

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

the class AlterIndexRename method update.

@Override
public int update() {
    session.commit(true);
    Database db = session.getDatabase();
    oldIndex = oldSchema.findIndex(session, oldIndexName);
    if (oldIndex == null) {
        if (!ifExists) {
            throw DbException.get(ErrorCode.INDEX_NOT_FOUND_1, newIndexName);
        }
        return 0;
    }
    if (oldSchema.findIndex(session, newIndexName) != null || newIndexName.equals(oldIndexName)) {
        throw DbException.get(ErrorCode.INDEX_ALREADY_EXISTS_1, newIndexName);
    }
    session.getUser().checkRight(oldIndex.getTable(), Right.ALL);
    db.renameSchemaObject(session, oldIndex, newIndexName);
    return 0;
}
Also used : Database(org.h2.engine.Database)

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