Search in sources :

Example 11 with Mode

use of org.h2.engine.Mode 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 12 with Mode

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

the class Engine method createSessionAndValidate.

private Session createSessionAndValidate(ConnectionInfo ci) {
    try {
        ConnectionInfo backup = null;
        String lockMethodName = ci.getProperty("FILE_LOCK", null);
        FileLockMethod fileLockMethod = FileLock.getFileLockMethod(lockMethodName);
        if (fileLockMethod == FileLockMethod.SERIALIZED) {
            // In serialized mode, database instance sharing is not possible
            ci.setProperty("OPEN_NEW", "TRUE");
            try {
                backup = ci.clone();
            } catch (CloneNotSupportedException e) {
                throw DbException.convert(e);
            }
        }
        Session session = openSession(ci);
        validateUserAndPassword(true);
        if (backup != null) {
            session.setConnectionInfo(backup);
        }
        return session;
    } catch (DbException e) {
        if (e.getErrorCode() == ErrorCode.WRONG_USER_OR_PASSWORD) {
            validateUserAndPassword(false);
        }
        throw e;
    }
}
Also used : FileLockMethod(org.h2.store.FileLockMethod) DbException(org.h2.message.DbException)

Example 13 with Mode

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

the class Function method getSimpleValue.

private Value getSimpleValue(Session session, Value v0, Expression[] args, Value[] values) {
    Value result;
    switch(info.type) {
        case ABS:
            result = v0.getSignum() >= 0 ? v0 : v0.negate();
            break;
        case ACOS:
            result = ValueDouble.get(Math.acos(v0.getDouble()));
            break;
        case ASIN:
            result = ValueDouble.get(Math.asin(v0.getDouble()));
            break;
        case ATAN:
            result = ValueDouble.get(Math.atan(v0.getDouble()));
            break;
        case CEILING:
            result = ValueDouble.get(Math.ceil(v0.getDouble()));
            break;
        case COS:
            result = ValueDouble.get(Math.cos(v0.getDouble()));
            break;
        case COSH:
            result = ValueDouble.get(Math.cosh(v0.getDouble()));
            break;
        case COT:
            {
                double d = Math.tan(v0.getDouble());
                if (d == 0.0) {
                    throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL());
                }
                result = ValueDouble.get(1. / d);
                break;
            }
        case DEGREES:
            result = ValueDouble.get(Math.toDegrees(v0.getDouble()));
            break;
        case EXP:
            result = ValueDouble.get(Math.exp(v0.getDouble()));
            break;
        case FLOOR:
            result = ValueDouble.get(Math.floor(v0.getDouble()));
            break;
        case LN:
            result = ValueDouble.get(Math.log(v0.getDouble()));
            break;
        case LOG:
            if (database.getMode().logIsLogBase10) {
                result = ValueDouble.get(Math.log10(v0.getDouble()));
            } else {
                result = ValueDouble.get(Math.log(v0.getDouble()));
            }
            break;
        case LOG10:
            result = ValueDouble.get(Math.log10(v0.getDouble()));
            break;
        case PI:
            result = ValueDouble.get(Math.PI);
            break;
        case RADIANS:
            result = ValueDouble.get(Math.toRadians(v0.getDouble()));
            break;
        case RAND:
            {
                if (v0 != null) {
                    session.getRandom().setSeed(v0.getInt());
                }
                result = ValueDouble.get(session.getRandom().nextDouble());
                break;
            }
        case ROUNDMAGIC:
            result = ValueDouble.get(roundMagic(v0.getDouble()));
            break;
        case SIGN:
            result = ValueInt.get(v0.getSignum());
            break;
        case SIN:
            result = ValueDouble.get(Math.sin(v0.getDouble()));
            break;
        case SINH:
            result = ValueDouble.get(Math.sinh(v0.getDouble()));
            break;
        case SQRT:
            result = ValueDouble.get(Math.sqrt(v0.getDouble()));
            break;
        case TAN:
            result = ValueDouble.get(Math.tan(v0.getDouble()));
            break;
        case TANH:
            result = ValueDouble.get(Math.tanh(v0.getDouble()));
            break;
        case SECURE_RAND:
            result = ValueBytes.getNoCopy(MathUtils.secureRandomBytes(v0.getInt()));
            break;
        case EXPAND:
            result = ValueBytes.getNoCopy(CompressTool.getInstance().expand(v0.getBytesNoCopy()));
            break;
        case ZERO:
            result = ValueInt.get(0);
            break;
        case RANDOM_UUID:
            result = ValueUuid.getNewRandom();
            break;
        // string
        case ASCII:
            {
                String s = v0.getString();
                if (s.length() == 0) {
                    result = ValueNull.INSTANCE;
                } else {
                    result = ValueInt.get(s.charAt(0));
                }
                break;
            }
        case BIT_LENGTH:
            result = ValueLong.get(16 * length(v0));
            break;
        case CHAR:
            result = ValueString.get(String.valueOf((char) v0.getInt()), database.getMode().treatEmptyStringsAsNull);
            break;
        case CHAR_LENGTH:
        case LENGTH:
            result = ValueLong.get(length(v0));
            break;
        case OCTET_LENGTH:
            result = ValueLong.get(2 * length(v0));
            break;
        case CONCAT_WS:
        case CONCAT:
            {
                result = ValueNull.INSTANCE;
                int start = 0;
                String separator = "";
                if (info.type == CONCAT_WS) {
                    start = 1;
                    separator = getNullOrValue(session, args, values, 0).getString();
                }
                for (int i = start; i < args.length; i++) {
                    Value v = getNullOrValue(session, args, values, i);
                    if (v == ValueNull.INSTANCE) {
                        continue;
                    }
                    if (result == ValueNull.INSTANCE) {
                        result = v;
                    } else {
                        String tmp = v.getString();
                        if (!StringUtils.isNullOrEmpty(separator) && !StringUtils.isNullOrEmpty(tmp)) {
                            tmp = separator + tmp;
                        }
                        result = ValueString.get(result.getString() + tmp, database.getMode().treatEmptyStringsAsNull);
                    }
                }
                if (info.type == CONCAT_WS) {
                    if (separator != null && result == ValueNull.INSTANCE) {
                        result = ValueString.get("", database.getMode().treatEmptyStringsAsNull);
                    }
                }
                break;
            }
        case HEXTORAW:
            result = ValueString.get(hexToRaw(v0.getString()), database.getMode().treatEmptyStringsAsNull);
            break;
        case LOWER:
        case LCASE:
            // TODO this is locale specific, need to document or provide a way
            // to set the locale
            result = ValueString.get(v0.getString().toLowerCase(), database.getMode().treatEmptyStringsAsNull);
            break;
        case RAWTOHEX:
            result = ValueString.get(rawToHex(v0.getString()), database.getMode().treatEmptyStringsAsNull);
            break;
        case SOUNDEX:
            result = ValueString.get(getSoundex(v0.getString()), database.getMode().treatEmptyStringsAsNull);
            break;
        case SPACE:
            {
                int len = Math.max(0, v0.getInt());
                char[] chars = new char[len];
                for (int i = len - 1; i >= 0; i--) {
                    chars[i] = ' ';
                }
                result = ValueString.get(new String(chars), database.getMode().treatEmptyStringsAsNull);
                break;
            }
        case UPPER:
        case UCASE:
            // TODO this is locale specific, need to document or provide a way
            // to set the locale
            result = ValueString.get(v0.getString().toUpperCase(), database.getMode().treatEmptyStringsAsNull);
            break;
        case STRINGENCODE:
            result = ValueString.get(StringUtils.javaEncode(v0.getString()), database.getMode().treatEmptyStringsAsNull);
            break;
        case STRINGDECODE:
            result = ValueString.get(StringUtils.javaDecode(v0.getString()), database.getMode().treatEmptyStringsAsNull);
            break;
        case STRINGTOUTF8:
            result = ValueBytes.getNoCopy(v0.getString().getBytes(StandardCharsets.UTF_8));
            break;
        case UTF8TOSTRING:
            result = ValueString.get(new String(v0.getBytesNoCopy(), StandardCharsets.UTF_8), database.getMode().treatEmptyStringsAsNull);
            break;
        case XMLCOMMENT:
            result = ValueString.get(StringUtils.xmlComment(v0.getString()), database.getMode().treatEmptyStringsAsNull);
            break;
        case XMLCDATA:
            result = ValueString.get(StringUtils.xmlCData(v0.getString()), database.getMode().treatEmptyStringsAsNull);
            break;
        case XMLSTARTDOC:
            result = ValueString.get(StringUtils.xmlStartDoc(), database.getMode().treatEmptyStringsAsNull);
            break;
        case DAY_NAME:
            {
                int dayOfWeek = DateTimeUtils.getSundayDayOfWeek(DateTimeUtils.dateAndTimeFromValue(v0)[0]);
                result = ValueString.get(DateTimeFunctions.getMonthsAndWeeks(1)[dayOfWeek], database.getMode().treatEmptyStringsAsNull);
                break;
            }
        case DAY_OF_MONTH:
        case DAY_OF_WEEK:
        case DAY_OF_YEAR:
        case HOUR:
        case MINUTE:
        case MONTH:
        case QUARTER:
        case ISO_YEAR:
        case ISO_WEEK:
        case ISO_DAY_OF_WEEK:
        case SECOND:
        case WEEK:
        case YEAR:
            result = ValueInt.get(DateTimeFunctions.getIntDatePart(v0, info.type));
            break;
        case MONTH_NAME:
            {
                int month = DateTimeUtils.monthFromDateValue(DateTimeUtils.dateAndTimeFromValue(v0)[0]);
                result = ValueString.get(DateTimeFunctions.getMonthsAndWeeks(0)[month - 1], database.getMode().treatEmptyStringsAsNull);
                break;
            }
        case CURDATE:
        case CURRENT_DATE:
            {
                long now = session.getTransactionStart();
                // need to normalize
                result = ValueDate.fromMillis(now);
                break;
            }
        case CURTIME:
        case CURRENT_TIME:
            {
                long now = session.getTransactionStart();
                // need to normalize
                result = ValueTime.fromMillis(now);
                break;
            }
        case NOW:
        case CURRENT_TIMESTAMP:
            {
                long now = session.getTransactionStart();
                ValueTimestamp vt = ValueTimestamp.fromMillis(now);
                if (v0 != null) {
                    Mode mode = database.getMode();
                    vt = (ValueTimestamp) vt.convertScale(mode.convertOnlyToSmallerScale, v0.getInt());
                }
                result = vt;
                break;
            }
        case DATABASE:
            result = ValueString.get(database.getShortName(), database.getMode().treatEmptyStringsAsNull);
            break;
        case USER:
        case CURRENT_USER:
            result = ValueString.get(session.getUser().getName(), database.getMode().treatEmptyStringsAsNull);
            break;
        case IDENTITY:
            result = session.getLastIdentity();
            break;
        case SCOPE_IDENTITY:
            result = session.getLastScopeIdentity();
            break;
        case AUTOCOMMIT:
            result = ValueBoolean.get(session.getAutoCommit());
            break;
        case READONLY:
            result = ValueBoolean.get(database.isReadOnly());
            break;
        case DATABASE_PATH:
            {
                String path = database.getDatabasePath();
                result = path == null ? (Value) ValueNull.INSTANCE : ValueString.get(path, database.getMode().treatEmptyStringsAsNull);
                break;
            }
        case LOCK_TIMEOUT:
            result = ValueInt.get(session.getLockTimeout());
            break;
        case DISK_SPACE_USED:
            result = ValueLong.get(getDiskSpaceUsed(session, v0));
            break;
        case CAST:
        case CONVERT:
            {
                v0 = v0.convertTo(dataType);
                Mode mode = database.getMode();
                v0 = v0.convertScale(mode.convertOnlyToSmallerScale, scale);
                v0 = v0.convertPrecision(getPrecision(), false);
                result = v0;
                break;
            }
        case MEMORY_FREE:
            session.getUser().checkAdmin();
            result = ValueInt.get(Utils.getMemoryFree());
            break;
        case MEMORY_USED:
            session.getUser().checkAdmin();
            result = ValueInt.get(Utils.getMemoryUsed());
            break;
        case LOCK_MODE:
            result = ValueInt.get(database.getLockMode());
            break;
        case SCHEMA:
            result = ValueString.get(session.getCurrentSchemaName(), database.getMode().treatEmptyStringsAsNull);
            break;
        case SESSION_ID:
            result = ValueInt.get(session.getId());
            break;
        case IFNULL:
            {
                result = v0;
                if (v0 == ValueNull.INSTANCE) {
                    result = getNullOrValue(session, args, values, 1);
                }
                result = convertResult(result);
                break;
            }
        case CASEWHEN:
            {
                Value v;
                if (!v0.getBoolean()) {
                    v = getNullOrValue(session, args, values, 2);
                } else {
                    v = getNullOrValue(session, args, values, 1);
                }
                result = v.convertTo(dataType);
                break;
            }
        case DECODE:
            {
                int index = -1;
                for (int i = 1, len = args.length - 1; i < len; i += 2) {
                    if (database.areEqual(v0, getNullOrValue(session, args, values, i))) {
                        index = i + 1;
                        break;
                    }
                }
                if (index < 0 && args.length % 2 == 0) {
                    index = args.length - 1;
                }
                Value v = index < 0 ? ValueNull.INSTANCE : getNullOrValue(session, args, values, index);
                result = v.convertTo(dataType);
                break;
            }
        case NVL2:
            {
                Value v;
                if (v0 == ValueNull.INSTANCE) {
                    v = getNullOrValue(session, args, values, 2);
                } else {
                    v = getNullOrValue(session, args, values, 1);
                }
                result = v.convertTo(dataType);
                break;
            }
        case COALESCE:
            {
                result = v0;
                for (int i = 0; i < args.length; i++) {
                    Value v = getNullOrValue(session, args, values, i);
                    if (v != ValueNull.INSTANCE) {
                        result = v.convertTo(dataType);
                        break;
                    }
                }
                break;
            }
        case GREATEST:
        case LEAST:
            {
                result = ValueNull.INSTANCE;
                for (int i = 0; i < args.length; i++) {
                    Value v = getNullOrValue(session, args, values, i);
                    if (v != ValueNull.INSTANCE) {
                        v = v.convertTo(dataType);
                        if (result == ValueNull.INSTANCE) {
                            result = v;
                        } else {
                            int comp = database.compareTypeSafe(result, v);
                            if (info.type == GREATEST && comp < 0) {
                                result = v;
                            } else if (info.type == LEAST && comp > 0) {
                                result = v;
                            }
                        }
                    }
                }
                break;
            }
        case CASE:
            {
                Expression then = null;
                if (v0 == null) {
                    // (null, when, then, when, then, else)
                    for (int i = 1, len = args.length - 1; i < len; i += 2) {
                        Value when = args[i].getValue(session);
                        if (when.getBoolean()) {
                            then = args[i + 1];
                            break;
                        }
                    }
                } else {
                    // (expr, when, then, when, then, else)
                    if (v0 != ValueNull.INSTANCE) {
                        for (int i = 1, len = args.length - 1; i < len; i += 2) {
                            Value when = args[i].getValue(session);
                            if (database.areEqual(v0, when)) {
                                then = args[i + 1];
                                break;
                            }
                        }
                    }
                }
                if (then == null && args.length % 2 == 0) {
                    // then = elsePart
                    then = args[args.length - 1];
                }
                Value v = then == null ? ValueNull.INSTANCE : then.getValue(session);
                result = v.convertTo(dataType);
                break;
            }
        case ARRAY_GET:
            {
                if (v0.getType() == Value.ARRAY) {
                    Value v1 = getNullOrValue(session, args, values, 1);
                    int element = v1.getInt();
                    Value[] list = ((ValueArray) v0).getList();
                    if (element < 1 || element > list.length) {
                        result = ValueNull.INSTANCE;
                    } else {
                        result = list[element - 1];
                    }
                } else {
                    result = ValueNull.INSTANCE;
                }
                break;
            }
        case ARRAY_LENGTH:
            {
                if (v0.getType() == Value.ARRAY) {
                    Value[] list = ((ValueArray) v0).getList();
                    result = ValueInt.get(list.length);
                } else {
                    result = ValueNull.INSTANCE;
                }
                break;
            }
        case ARRAY_CONTAINS:
            {
                result = ValueBoolean.FALSE;
                if (v0.getType() == Value.ARRAY) {
                    Value v1 = getNullOrValue(session, args, values, 1);
                    Value[] list = ((ValueArray) v0).getList();
                    for (Value v : list) {
                        if (v.equals(v1)) {
                            result = ValueBoolean.TRUE;
                            break;
                        }
                    }
                }
                break;
            }
        case CANCEL_SESSION:
            {
                result = ValueBoolean.get(cancelStatement(session, v0.getInt()));
                break;
            }
        case TRANSACTION_ID:
            {
                result = session.getTransactionId();
                break;
            }
        default:
            result = null;
    }
    return result;
}
Also used : ValueTimestamp(org.h2.value.ValueTimestamp) Mode(org.h2.engine.Mode) Value(org.h2.value.Value) ValueString(org.h2.value.ValueString)

Example 14 with Mode

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

the class Session method unlockReadLocks.

/**
 * Unlock all read locks. This is done if the transaction isolation mode is
 * READ_COMMITTED.
 */
public void unlockReadLocks() {
    if (database.isMultiVersion()) {
        // MVCC: keep shared locks (insert / update / delete)
        return;
    }
    // locks is modified in the loop
    for (int i = 0; i < locks.size(); i++) {
        Table t = locks.get(i);
        if (!t.isLockedExclusively()) {
            synchronized (database) {
                t.unlock(this);
                locks.remove(i);
            }
            i--;
        }
    }
}
Also used : MVTable(org.h2.mvstore.db.MVTable) Table(org.h2.table.Table) Constraint(org.h2.constraint.Constraint)

Example 15 with Mode

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

the class SessionRemote method autoCommitIfCluster.

/**
 * Calls COMMIT if the session is in cluster mode.
 */
public void autoCommitIfCluster() {
    if (autoCommit && cluster) {
        // faster)
        for (int i = 0, count = 0; i < transferList.size(); i++) {
            Transfer transfer = transferList.get(i);
            try {
                traceOperation("COMMAND_COMMIT", 0);
                transfer.writeInt(SessionRemote.COMMAND_COMMIT);
                done(transfer);
            } catch (IOException e) {
                removeServer(e, i--, ++count);
            }
        }
    }
}
Also used : Transfer(org.h2.value.Transfer) IOException(java.io.IOException)

Aggregations

Connection (java.sql.Connection)12 Mode (org.h2.engine.Mode)12 ValueString (org.h2.value.ValueString)10 IOException (java.io.IOException)8 PreparedStatement (java.sql.PreparedStatement)8 Statement (java.sql.Statement)8 DbException (org.h2.message.DbException)8 CompareMode (org.h2.value.CompareMode)8 Value (org.h2.value.Value)8 ResultSet (java.sql.ResultSet)6 ArrayList (java.util.ArrayList)6 Column (org.h2.table.Column)6 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)5 ValueExpression (org.h2.expression.ValueExpression)5 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)5 SQLException (java.sql.SQLException)4 Expression (org.h2.expression.Expression)4 IndexColumn (org.h2.table.IndexColumn)4 Before (org.junit.Before)4 ExpressionColumn (org.h2.expression.ExpressionColumn)3