Search in sources :

Example 1 with Setting

use of org.h2.engine.Setting in project siena by mandubian.

the class FullText method search.

/**
     * Do the search.
     *
     * @param conn the database connection
     * @param text the query
     * @param limit the limit
     * @param offset the offset
     * @param data whether the raw data should be returned
     * @return the result set
     */
protected static ResultSet search(Connection conn, String text, int limit, int offset, boolean data) throws SQLException {
    SimpleResultSet result = createResultSet(data);
    if (conn.getMetaData().getURL().startsWith("jdbc:columnlist:")) {
        // this is just to query the result set columns
        return result;
    }
    if (text == null || text.trim().length() == 0) {
        return result;
    }
    FullTextSettings setting = FullTextSettings.getInstance(conn);
    if (!setting.isInitialized()) {
        init(conn);
    }
    HashSet<String> words = New.hashSet();
    addWords(setting, words, text);
    HashSet<Integer> rIds = null, lastRowIds = null;
    HashMap<String, Integer> allWords = setting.getWordList();
    PreparedStatement prepSelectMapByWordId = setting.prepare(conn, SELECT_MAP_BY_WORD_ID);
    for (String word : words) {
        lastRowIds = rIds;
        rIds = New.hashSet();
        Integer wId = allWords.get(word);
        if (wId == null) {
            continue;
        }
        prepSelectMapByWordId.setInt(1, wId.intValue());
        ResultSet rs = prepSelectMapByWordId.executeQuery();
        while (rs.next()) {
            Integer rId = rs.getInt(1);
            if (lastRowIds == null || lastRowIds.contains(rId)) {
                rIds.add(rId);
            }
        }
    }
    if (rIds == null || rIds.size() == 0) {
        return result;
    }
    PreparedStatement prepSelectRowById = setting.prepare(conn, SELECT_ROW_BY_ID);
    int rowCount = 0;
    for (int rowId : rIds) {
        prepSelectRowById.setInt(1, rowId);
        ResultSet rs = prepSelectRowById.executeQuery();
        if (!rs.next()) {
            continue;
        }
        if (offset > 0) {
            offset--;
        } else {
            String key = rs.getString(1);
            int indexId = rs.getInt(2);
            IndexInfo index = setting.getIndexInfo(indexId);
            if (data) {
                /*Object[][] columnData = parseKey(conn, key);
                	result.addRow(
                            index.schema,
                            index.table,
                            columnData[0],
                            columnData[1],
                            1.0);*/
                String[] splits = key.split("=");
                String[] col0 = new String[1];
                col0[0] = splits[0];
                String[] col1 = new String[1];
                col1[0] = splits[1];
                result.addRow(index.schema, index.table, col0, col1, 1.0);
            } else {
                String query = StringUtils.quoteIdentifier(index.schema) + "." + StringUtils.quoteIdentifier(index.table) + " WHERE " + key;
                result.addRow(query, 1.0);
            }
            rowCount++;
            if (limit > 0 && rowCount >= limit) {
                break;
            }
        }
    }
    return result;
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 2 with Setting

use of org.h2.engine.Setting in project siena by mandubian.

the class FullText method reindex.

/**
     * Re-creates the full text index for this database. Calling this method is
     * usually not needed, as the index is kept up-to-date automatically.
     *
     * @param conn the connection
     */
public static void reindex(Connection conn) throws SQLException {
    init(conn);
    removeAllTriggers(conn, TRIGGER_PREFIX);
    FullTextSettings setting = FullTextSettings.getInstance(conn);
    setting.getWordList().clear();
    Statement stat = conn.createStatement();
    stat.execute("TRUNCATE TABLE " + SCHEMA + ".WORDS");
    stat.execute("TRUNCATE TABLE " + SCHEMA + ".ROWS");
    stat.execute("TRUNCATE TABLE " + SCHEMA + ".MAP");
    ResultSet rs = stat.executeQuery("SELECT * FROM " + SCHEMA + ".FT_INDEXES");
    while (rs.next()) {
        String schema = rs.getString("SCHEMA");
        String table = rs.getString("TABLE");
        createTrigger(conn, schema, table);
        indexExistingRows(conn, schema, table);
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Example 3 with Setting

use of org.h2.engine.Setting in project h2database by h2database.

the class ScriptCommand method query.

@Override
public ResultInterface query(int maxrows) {
    session.getUser().checkAdmin();
    reset();
    Database db = session.getDatabase();
    if (schemaNames != null) {
        for (String schemaName : schemaNames) {
            Schema schema = db.findSchema(schemaName);
            if (schema == null) {
                throw DbException.get(ErrorCode.SCHEMA_NOT_FOUND_1, schemaName);
            }
        }
    }
    try {
        result = createResult();
        deleteStore();
        openOutput();
        if (out != null) {
            buffer = new byte[Constants.IO_BUFFER_SIZE];
        }
        if (settings) {
            for (Setting setting : db.getAllSettings()) {
                if (setting.getName().equals(SetTypes.getTypeName(SetTypes.CREATE_BUILD))) {
                    // (it is only set when creating the database)
                    continue;
                }
                add(setting.getCreateSQL(), false);
            }
        }
        if (out != null) {
            add("", true);
        }
        for (User user : db.getAllUsers()) {
            add(user.getCreateSQL(passwords), false);
        }
        for (Role role : db.getAllRoles()) {
            add(role.getCreateSQL(true), false);
        }
        for (Schema schema : db.getAllSchemas()) {
            if (excludeSchema(schema)) {
                continue;
            }
            add(schema.getCreateSQL(), false);
        }
        for (UserDataType datatype : db.getAllUserDataTypes()) {
            if (drop) {
                add(datatype.getDropSQL(), false);
            }
            add(datatype.getCreateSQL(), false);
        }
        for (SchemaObject obj : db.getAllSchemaObjects(DbObject.CONSTANT)) {
            if (excludeSchema(obj.getSchema())) {
                continue;
            }
            Constant constant = (Constant) obj;
            add(constant.getCreateSQL(), false);
        }
        final ArrayList<Table> tables = db.getAllTablesAndViews(false);
        // sort by id, so that views are after tables and views on views
        // after the base views
        Collections.sort(tables, new Comparator<Table>() {

            @Override
            public int compare(Table t1, Table t2) {
                return t1.getId() - t2.getId();
            }
        });
        // Generate the DROP XXX  ... IF EXISTS
        for (Table table : tables) {
            if (excludeSchema(table.getSchema())) {
                continue;
            }
            if (excludeTable(table)) {
                continue;
            }
            if (table.isHidden()) {
                continue;
            }
            table.lock(session, false, false);
            String sql = table.getCreateSQL();
            if (sql == null) {
                // null for metadata tables
                continue;
            }
            if (drop) {
                add(table.getDropSQL(), false);
            }
        }
        for (SchemaObject obj : db.getAllSchemaObjects(DbObject.FUNCTION_ALIAS)) {
            if (excludeSchema(obj.getSchema())) {
                continue;
            }
            if (drop) {
                add(obj.getDropSQL(), false);
            }
            add(obj.getCreateSQL(), false);
        }
        for (UserAggregate agg : db.getAllAggregates()) {
            if (drop) {
                add(agg.getDropSQL(), false);
            }
            add(agg.getCreateSQL(), false);
        }
        for (SchemaObject obj : db.getAllSchemaObjects(DbObject.SEQUENCE)) {
            if (excludeSchema(obj.getSchema())) {
                continue;
            }
            Sequence sequence = (Sequence) obj;
            if (drop) {
                add(sequence.getDropSQL(), false);
            }
            add(sequence.getCreateSQL(), false);
        }
        // Generate CREATE TABLE and INSERT...VALUES
        int count = 0;
        for (Table table : tables) {
            if (excludeSchema(table.getSchema())) {
                continue;
            }
            if (excludeTable(table)) {
                continue;
            }
            if (table.isHidden()) {
                continue;
            }
            table.lock(session, false, false);
            String createTableSql = table.getCreateSQL();
            if (createTableSql == null) {
                // null for metadata tables
                continue;
            }
            final TableType tableType = table.getTableType();
            add(createTableSql, false);
            final ArrayList<Constraint> constraints = table.getConstraints();
            if (constraints != null) {
                for (Constraint constraint : constraints) {
                    if (Constraint.Type.PRIMARY_KEY == constraint.getConstraintType()) {
                        add(constraint.getCreateSQLWithoutIndexes(), false);
                    }
                }
            }
            if (TableType.TABLE == tableType) {
                if (table.canGetRowCount()) {
                    String rowcount = "-- " + table.getRowCountApproximation() + " +/- SELECT COUNT(*) FROM " + table.getSQL();
                    add(rowcount, false);
                }
                if (data) {
                    count = generateInsertValues(count, table);
                }
            }
            final ArrayList<Index> indexes = table.getIndexes();
            for (int j = 0; indexes != null && j < indexes.size(); j++) {
                Index index = indexes.get(j);
                if (!index.getIndexType().getBelongsToConstraint()) {
                    add(index.getCreateSQL(), false);
                }
            }
        }
        if (tempLobTableCreated) {
            add("DROP TABLE IF EXISTS SYSTEM_LOB_STREAM", true);
            add("CALL SYSTEM_COMBINE_BLOB(-1)", true);
            add("DROP ALIAS IF EXISTS SYSTEM_COMBINE_CLOB", true);
            add("DROP ALIAS IF EXISTS SYSTEM_COMBINE_BLOB", true);
            tempLobTableCreated = false;
        }
        // Generate CREATE CONSTRAINT ...
        final ArrayList<SchemaObject> constraints = db.getAllSchemaObjects(DbObject.CONSTRAINT);
        Collections.sort(constraints, new Comparator<SchemaObject>() {

            @Override
            public int compare(SchemaObject c1, SchemaObject c2) {
                return ((Constraint) c1).compareTo((Constraint) c2);
            }
        });
        for (SchemaObject obj : constraints) {
            if (excludeSchema(obj.getSchema())) {
                continue;
            }
            Constraint constraint = (Constraint) obj;
            if (excludeTable(constraint.getTable())) {
                continue;
            }
            if (constraint.getTable().isHidden()) {
                continue;
            }
            if (Constraint.Type.PRIMARY_KEY != constraint.getConstraintType()) {
                add(constraint.getCreateSQLWithoutIndexes(), false);
            }
        }
        // Generate CREATE TRIGGER ...
        for (SchemaObject obj : db.getAllSchemaObjects(DbObject.TRIGGER)) {
            if (excludeSchema(obj.getSchema())) {
                continue;
            }
            TriggerObject trigger = (TriggerObject) obj;
            if (excludeTable(trigger.getTable())) {
                continue;
            }
            add(trigger.getCreateSQL(), false);
        }
        // Generate GRANT ...
        for (Right right : db.getAllRights()) {
            DbObject object = right.getGrantedObject();
            if (object != null) {
                if (object instanceof Schema) {
                    if (excludeSchema((Schema) object)) {
                        continue;
                    }
                } else if (object instanceof Table) {
                    Table table = (Table) object;
                    if (excludeSchema(table.getSchema())) {
                        continue;
                    }
                    if (excludeTable(table)) {
                        continue;
                    }
                }
            }
            add(right.getCreateSQL(), false);
        }
        // Generate COMMENT ON ...
        for (Comment comment : db.getAllComments()) {
            add(comment.getCreateSQL(), false);
        }
        if (out != null) {
            out.close();
        }
    } catch (IOException e) {
        throw DbException.convertIOException(e, getFileName());
    } finally {
        closeIO();
    }
    result.done();
    LocalResult r = result;
    reset();
    return r;
}
Also used : SchemaObject(org.h2.schema.SchemaObject) User(org.h2.engine.User) Constraint(org.h2.constraint.Constraint) Constant(org.h2.schema.Constant) Schema(org.h2.schema.Schema) Right(org.h2.engine.Right) TriggerObject(org.h2.schema.TriggerObject) Index(org.h2.index.Index) ValueString(org.h2.value.ValueString) LocalResult(org.h2.result.LocalResult) Database(org.h2.engine.Database) Comment(org.h2.engine.Comment) Table(org.h2.table.Table) TableType(org.h2.table.TableType) DbObject(org.h2.engine.DbObject) Setting(org.h2.engine.Setting) UserDataType(org.h2.engine.UserDataType) Sequence(org.h2.schema.Sequence) IOException(java.io.IOException) Constraint(org.h2.constraint.Constraint) Role(org.h2.engine.Role) UserAggregate(org.h2.engine.UserAggregate)

Example 4 with Setting

use of org.h2.engine.Setting in project h2database by h2database.

the class Set method update.

@Override
public int update() {
    Database database = session.getDatabase();
    String name = SetTypes.getTypeName(type);
    switch(type) {
        case SetTypes.ALLOW_LITERALS:
            {
                session.getUser().checkAdmin();
                int value = getIntValue();
                if (value < 0 || value > 2) {
                    throw DbException.getInvalidValueException("ALLOW_LITERALS", getIntValue());
                }
                database.setAllowLiterals(value);
                addOrUpdateSetting(name, null, value);
                break;
            }
        case SetTypes.CACHE_SIZE:
            if (getIntValue() < 0) {
                throw DbException.getInvalidValueException("CACHE_SIZE", getIntValue());
            }
            session.getUser().checkAdmin();
            database.setCacheSize(getIntValue());
            addOrUpdateSetting(name, null, getIntValue());
            break;
        case SetTypes.CLUSTER:
            {
                if (Constants.CLUSTERING_ENABLED.equals(stringValue)) {
                    // ignore, as the cluster setting is checked later
                    break;
                }
                String value = StringUtils.quoteStringSQL(stringValue);
                if (!value.equals(database.getCluster())) {
                    if (!value.equals(Constants.CLUSTERING_DISABLED)) {
                        // anybody can disable the cluster
                        // (if he can't access a cluster node)
                        session.getUser().checkAdmin();
                    }
                    database.setCluster(value);
                    // use the system session so that the current transaction
                    // (if any) is not committed
                    Session sysSession = database.getSystemSession();
                    synchronized (sysSession) {
                        synchronized (database) {
                            addOrUpdateSetting(sysSession, name, value, 0);
                            sysSession.commit(true);
                        }
                    }
                }
                break;
            }
        case SetTypes.COLLATION:
            {
                session.getUser().checkAdmin();
                final boolean binaryUnsigned = database.getCompareMode().isBinaryUnsigned();
                CompareMode compareMode;
                StringBuilder buff = new StringBuilder(stringValue);
                if (stringValue.equals(CompareMode.OFF)) {
                    compareMode = CompareMode.getInstance(null, 0, binaryUnsigned);
                } else {
                    int strength = getIntValue();
                    buff.append(" STRENGTH ");
                    if (strength == Collator.IDENTICAL) {
                        buff.append("IDENTICAL");
                    } else if (strength == Collator.PRIMARY) {
                        buff.append("PRIMARY");
                    } else if (strength == Collator.SECONDARY) {
                        buff.append("SECONDARY");
                    } else if (strength == Collator.TERTIARY) {
                        buff.append("TERTIARY");
                    }
                    compareMode = CompareMode.getInstance(stringValue, strength, binaryUnsigned);
                }
                CompareMode old = database.getCompareMode();
                if (old.equals(compareMode)) {
                    break;
                }
                Table table = database.getFirstUserTable();
                if (table != null) {
                    throw DbException.get(ErrorCode.COLLATION_CHANGE_WITH_DATA_TABLE_1, table.getSQL());
                }
                addOrUpdateSetting(name, buff.toString(), 0);
                database.setCompareMode(compareMode);
                break;
            }
        case SetTypes.BINARY_COLLATION:
            {
                session.getUser().checkAdmin();
                Table table = database.getFirstUserTable();
                if (table != null) {
                    throw DbException.get(ErrorCode.COLLATION_CHANGE_WITH_DATA_TABLE_1, table.getSQL());
                }
                CompareMode currentMode = database.getCompareMode();
                CompareMode newMode;
                if (stringValue.equals(CompareMode.SIGNED)) {
                    newMode = CompareMode.getInstance(currentMode.getName(), currentMode.getStrength(), false);
                } else if (stringValue.equals(CompareMode.UNSIGNED)) {
                    newMode = CompareMode.getInstance(currentMode.getName(), currentMode.getStrength(), true);
                } else {
                    throw DbException.getInvalidValueException("BINARY_COLLATION", stringValue);
                }
                addOrUpdateSetting(name, stringValue, 0);
                database.setCompareMode(newMode);
                break;
            }
        case SetTypes.COMPRESS_LOB:
            {
                session.getUser().checkAdmin();
                int algo = CompressTool.getCompressAlgorithm(stringValue);
                database.setLobCompressionAlgorithm(algo == Compressor.NO ? null : stringValue);
                addOrUpdateSetting(name, stringValue, 0);
                break;
            }
        case SetTypes.CREATE_BUILD:
            {
                session.getUser().checkAdmin();
                if (database.isStarting()) {
                    // just ignore the command if not starting
                    // this avoids problems when running recovery scripts
                    int value = getIntValue();
                    addOrUpdateSetting(name, null, value);
                }
                break;
            }
        case SetTypes.DATABASE_EVENT_LISTENER:
            {
                session.getUser().checkAdmin();
                database.setEventListenerClass(stringValue);
                break;
            }
        case SetTypes.DB_CLOSE_DELAY:
            {
                int x = getIntValue();
                if (x == -1) {
                // -1 is a special value for in-memory databases,
                // which means "keep the DB alive and use the same
                // DB for all connections"
                } else if (x < 0) {
                    throw DbException.getInvalidValueException("DB_CLOSE_DELAY", x);
                }
                session.getUser().checkAdmin();
                database.setCloseDelay(getIntValue());
                addOrUpdateSetting(name, null, getIntValue());
                break;
            }
        case SetTypes.DEFAULT_LOCK_TIMEOUT:
            if (getIntValue() < 0) {
                throw DbException.getInvalidValueException("DEFAULT_LOCK_TIMEOUT", getIntValue());
            }
            session.getUser().checkAdmin();
            addOrUpdateSetting(name, null, getIntValue());
            break;
        case SetTypes.DEFAULT_TABLE_TYPE:
            session.getUser().checkAdmin();
            database.setDefaultTableType(getIntValue());
            addOrUpdateSetting(name, null, getIntValue());
            break;
        case SetTypes.EXCLUSIVE:
            {
                session.getUser().checkAdmin();
                int value = getIntValue();
                switch(value) {
                    case 0:
                        database.setExclusiveSession(null, false);
                        break;
                    case 1:
                        database.setExclusiveSession(session, false);
                        break;
                    case 2:
                        database.setExclusiveSession(session, true);
                        break;
                    default:
                        throw DbException.getInvalidValueException("EXCLUSIVE", value);
                }
                break;
            }
        case SetTypes.JAVA_OBJECT_SERIALIZER:
            {
                session.getUser().checkAdmin();
                Table table = database.getFirstUserTable();
                if (table != null) {
                    throw DbException.get(ErrorCode.JAVA_OBJECT_SERIALIZER_CHANGE_WITH_DATA_TABLE, table.getSQL());
                }
                database.setJavaObjectSerializerName(stringValue);
                addOrUpdateSetting(name, stringValue, 0);
                break;
            }
        case SetTypes.IGNORECASE:
            session.getUser().checkAdmin();
            database.setIgnoreCase(getIntValue() == 1);
            addOrUpdateSetting(name, null, getIntValue());
            break;
        case SetTypes.LOCK_MODE:
            session.getUser().checkAdmin();
            database.setLockMode(getIntValue());
            addOrUpdateSetting(name, null, getIntValue());
            break;
        case SetTypes.LOCK_TIMEOUT:
            if (getIntValue() < 0) {
                throw DbException.getInvalidValueException("LOCK_TIMEOUT", getIntValue());
            }
            session.setLockTimeout(getIntValue());
            break;
        case SetTypes.LOG:
            {
                int value = getIntValue();
                if (database.isPersistent() && value != database.getLogMode()) {
                    session.getUser().checkAdmin();
                    database.setLogMode(value);
                }
                break;
            }
        case SetTypes.MAX_LENGTH_INPLACE_LOB:
            {
                if (getIntValue() < 0) {
                    throw DbException.getInvalidValueException("MAX_LENGTH_INPLACE_LOB", getIntValue());
                }
                session.getUser().checkAdmin();
                database.setMaxLengthInplaceLob(getIntValue());
                addOrUpdateSetting(name, null, getIntValue());
                break;
            }
        case SetTypes.MAX_LOG_SIZE:
            if (getIntValue() < 0) {
                throw DbException.getInvalidValueException("MAX_LOG_SIZE", getIntValue());
            }
            session.getUser().checkAdmin();
            database.setMaxLogSize((long) getIntValue() * 1024 * 1024);
            addOrUpdateSetting(name, null, getIntValue());
            break;
        case SetTypes.MAX_MEMORY_ROWS:
            {
                if (getIntValue() < 0) {
                    throw DbException.getInvalidValueException("MAX_MEMORY_ROWS", getIntValue());
                }
                session.getUser().checkAdmin();
                database.setMaxMemoryRows(getIntValue());
                addOrUpdateSetting(name, null, getIntValue());
                break;
            }
        case SetTypes.MAX_MEMORY_UNDO:
            {
                if (getIntValue() < 0) {
                    throw DbException.getInvalidValueException("MAX_MEMORY_UNDO", getIntValue());
                }
                session.getUser().checkAdmin();
                database.setMaxMemoryUndo(getIntValue());
                addOrUpdateSetting(name, null, getIntValue());
                break;
            }
        case SetTypes.MAX_OPERATION_MEMORY:
            {
                if (getIntValue() < 0) {
                    throw DbException.getInvalidValueException("MAX_OPERATION_MEMORY", getIntValue());
                }
                session.getUser().checkAdmin();
                int value = getIntValue();
                database.setMaxOperationMemory(value);
                break;
            }
        case SetTypes.MODE:
            Mode mode = Mode.getInstance(stringValue);
            if (mode == null) {
                throw DbException.get(ErrorCode.UNKNOWN_MODE_1, stringValue);
            }
            if (database.getMode() != mode) {
                session.getUser().checkAdmin();
                database.setMode(mode);
                session.getColumnNamerConfiguration().configure(mode.getEnum());
            }
            break;
        case SetTypes.MULTI_THREADED:
            {
                session.getUser().checkAdmin();
                database.setMultiThreaded(getIntValue() == 1);
                break;
            }
        case SetTypes.MVCC:
            {
                if (database.isMultiVersion() != (getIntValue() == 1)) {
                    throw DbException.get(ErrorCode.CANNOT_CHANGE_SETTING_WHEN_OPEN_1, "MVCC");
                }
                break;
            }
        case SetTypes.OPTIMIZE_REUSE_RESULTS:
            {
                session.getUser().checkAdmin();
                database.setOptimizeReuseResults(getIntValue() != 0);
                break;
            }
        case SetTypes.QUERY_TIMEOUT:
            {
                if (getIntValue() < 0) {
                    throw DbException.getInvalidValueException("QUERY_TIMEOUT", getIntValue());
                }
                int value = getIntValue();
                session.setQueryTimeout(value);
                break;
            }
        case SetTypes.REDO_LOG_BINARY:
            {
                int value = getIntValue();
                session.setRedoLogBinary(value == 1);
                break;
            }
        case SetTypes.REFERENTIAL_INTEGRITY:
            {
                session.getUser().checkAdmin();
                int value = getIntValue();
                if (value < 0 || value > 1) {
                    throw DbException.getInvalidValueException("REFERENTIAL_INTEGRITY", getIntValue());
                }
                database.setReferentialIntegrity(value == 1);
                break;
            }
        case SetTypes.QUERY_STATISTICS:
            {
                session.getUser().checkAdmin();
                int value = getIntValue();
                if (value < 0 || value > 1) {
                    throw DbException.getInvalidValueException("QUERY_STATISTICS", getIntValue());
                }
                database.setQueryStatistics(value == 1);
                break;
            }
        case SetTypes.QUERY_STATISTICS_MAX_ENTRIES:
            {
                session.getUser().checkAdmin();
                int value = getIntValue();
                if (value < 1) {
                    throw DbException.getInvalidValueException("QUERY_STATISTICS_MAX_ENTRIES", getIntValue());
                }
                database.setQueryStatisticsMaxEntries(value);
                break;
            }
        case SetTypes.SCHEMA:
            {
                Schema schema = database.getSchema(stringValue);
                session.setCurrentSchema(schema);
                break;
            }
        case SetTypes.SCHEMA_SEARCH_PATH:
            {
                session.setSchemaSearchPath(stringValueList);
                break;
            }
        case SetTypes.TRACE_LEVEL_FILE:
            session.getUser().checkAdmin();
            if (getCurrentObjectId() == 0) {
                // don't set the property when opening the database
                // this is for compatibility with older versions, because
                // this setting was persistent
                database.getTraceSystem().setLevelFile(getIntValue());
            }
            break;
        case SetTypes.TRACE_LEVEL_SYSTEM_OUT:
            session.getUser().checkAdmin();
            if (getCurrentObjectId() == 0) {
                // don't set the property when opening the database
                // this is for compatibility with older versions, because
                // this setting was persistent
                database.getTraceSystem().setLevelSystemOut(getIntValue());
            }
            break;
        case SetTypes.TRACE_MAX_FILE_SIZE:
            {
                if (getIntValue() < 0) {
                    throw DbException.getInvalidValueException("TRACE_MAX_FILE_SIZE", getIntValue());
                }
                session.getUser().checkAdmin();
                int size = getIntValue() * 1024 * 1024;
                database.getTraceSystem().setMaxFileSize(size);
                addOrUpdateSetting(name, null, getIntValue());
                break;
            }
        case SetTypes.THROTTLE:
            {
                if (getIntValue() < 0) {
                    throw DbException.getInvalidValueException("THROTTLE", getIntValue());
                }
                session.setThrottle(getIntValue());
                break;
            }
        case SetTypes.UNDO_LOG:
            {
                int value = getIntValue();
                if (value < 0 || value > 1) {
                    throw DbException.getInvalidValueException("UNDO_LOG", getIntValue());
                }
                session.setUndoLogEnabled(value == 1);
                break;
            }
        case SetTypes.VARIABLE:
            {
                Expression expr = expression.optimize(session);
                session.setVariable(stringValue, expr.getValue(session));
                break;
            }
        case SetTypes.WRITE_DELAY:
            {
                if (getIntValue() < 0) {
                    throw DbException.getInvalidValueException("WRITE_DELAY", getIntValue());
                }
                session.getUser().checkAdmin();
                database.setWriteDelay(getIntValue());
                addOrUpdateSetting(name, null, getIntValue());
                break;
            }
        case SetTypes.RETENTION_TIME:
            {
                if (getIntValue() < 0) {
                    throw DbException.getInvalidValueException("RETENTION_TIME", getIntValue());
                }
                session.getUser().checkAdmin();
                database.setRetentionTime(getIntValue());
                addOrUpdateSetting(name, null, getIntValue());
                break;
            }
        case SetTypes.ROW_FACTORY:
            {
                session.getUser().checkAdmin();
                String rowFactoryName = expression.getColumnName();
                Class<RowFactory> rowFactoryClass = JdbcUtils.loadUserClass(rowFactoryName);
                RowFactory rowFactory;
                try {
                    rowFactory = rowFactoryClass.newInstance();
                } catch (Exception e) {
                    throw DbException.convert(e);
                }
                database.setRowFactory(rowFactory);
                break;
            }
        case SetTypes.BATCH_JOINS:
            {
                int value = getIntValue();
                if (value != 0 && value != 1) {
                    throw DbException.getInvalidValueException("BATCH_JOINS", getIntValue());
                }
                session.setJoinBatchEnabled(value == 1);
                break;
            }
        case SetTypes.FORCE_JOIN_ORDER:
            {
                int value = getIntValue();
                if (value != 0 && value != 1) {
                    throw DbException.getInvalidValueException("FORCE_JOIN_ORDER", value);
                }
                session.setForceJoinOrder(value == 1);
                break;
            }
        case SetTypes.LAZY_QUERY_EXECUTION:
            {
                int value = getIntValue();
                if (value != 0 && value != 1) {
                    throw DbException.getInvalidValueException("LAZY_QUERY_EXECUTION", value);
                }
                session.setLazyQueryExecution(value == 1);
                break;
            }
        case SetTypes.BUILTIN_ALIAS_OVERRIDE:
            {
                session.getUser().checkAdmin();
                int value = getIntValue();
                if (value != 0 && value != 1) {
                    throw DbException.getInvalidValueException("BUILTIN_ALIAS_OVERRIDE", value);
                }
                database.setAllowBuiltinAliasOverride(value == 1);
                break;
            }
        case SetTypes.COLUMN_NAME_RULES:
            {
                session.getUser().checkAdmin();
                session.getColumnNamerConfiguration().configure(expression.getColumnName());
                break;
            }
        default:
            DbException.throwInternalError("type=" + type);
    }
    // the meta data information has changed
    database.getNextModificationDataId();
    // query caches might be affected as well, for example
    // when changing the compatibility mode
    database.getNextModificationMetaId();
    return 0;
}
Also used : RowFactory(org.h2.result.RowFactory) Table(org.h2.table.Table) ValueExpression(org.h2.expression.ValueExpression) Expression(org.h2.expression.Expression) CompareMode(org.h2.value.CompareMode) Mode(org.h2.engine.Mode) Schema(org.h2.schema.Schema) Database(org.h2.engine.Database) CompareMode(org.h2.value.CompareMode) DbException(org.h2.message.DbException) Session(org.h2.engine.Session)

Example 5 with Setting

use of org.h2.engine.Setting in project h2database by h2database.

the class Set method addOrUpdateSetting.

private void addOrUpdateSetting(Session session, String name, String s, int v) {
    Database database = session.getDatabase();
    if (database.isReadOnly()) {
        return;
    }
    Setting setting = database.findSetting(name);
    boolean addNew = false;
    if (setting == null) {
        addNew = true;
        int id = getObjectId();
        setting = new Setting(database, id, name);
    }
    if (s != null) {
        if (!addNew && setting.getStringValue().equals(s)) {
            return;
        }
        setting.setStringValue(s);
    } else {
        if (!addNew && setting.getIntValue() == v) {
            return;
        }
        setting.setIntValue(v);
    }
    if (addNew) {
        database.addDatabaseObject(session, setting);
    } else {
        database.updateMeta(session, setting);
    }
}
Also used : Setting(org.h2.engine.Setting) Database(org.h2.engine.Database)

Aggregations

PreparedStatement (java.sql.PreparedStatement)10 ResultSet (java.sql.ResultSet)8 DbException (org.h2.message.DbException)8 Statement (java.sql.Statement)7 SimpleResultSet (org.h2.tools.SimpleResultSet)6 Schema (org.h2.schema.Schema)4 ArrayList (java.util.ArrayList)3 Constraint (org.h2.constraint.Constraint)3 Database (org.h2.engine.Database)3 Setting (org.h2.engine.Setting)3 ValueString (org.h2.value.ValueString)3 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 HashSet (java.util.HashSet)2 DbObject (org.h2.engine.DbObject)2 Right (org.h2.engine.Right)2 Role (org.h2.engine.Role)2 Session (org.h2.engine.Session)2 User (org.h2.engine.User)2 UserAggregate (org.h2.engine.UserAggregate)2