Search in sources :

Example 36 with Value

use of org.h2.value.Value in project h2database by h2database.

the class Parser method readInt.

private int readInt() {
    boolean minus = false;
    if (currentTokenType == MINUS) {
        minus = true;
        read();
    } else if (currentTokenType == PLUS) {
        read();
    }
    if (currentTokenType != VALUE) {
        throw DbException.getSyntaxError(sqlCommand, parseIndex, "integer");
    }
    if (minus) {
        // must do that now, otherwise Integer.MIN_VALUE would not work
        currentValue = currentValue.negate();
    }
    int i = currentValue.getInt();
    read();
    return i;
}
Also used : AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint)

Example 37 with Value

use of org.h2.value.Value in project h2database by h2database.

the class Parser method parseCreateTable.

private CreateTable parseCreateTable(boolean temp, boolean globalTemp, boolean persistIndexes) {
    boolean ifNotExists = readIfNotExists();
    String tableName = readIdentifierWithSchema();
    if (temp && globalTemp && equalsToken("SESSION", schemaName)) {
        // support weird syntax: declare global temporary table session.xy
        // (...) not logged
        schemaName = session.getCurrentSchemaName();
        globalTemp = false;
    }
    Schema schema = getSchema();
    CreateTable command = new CreateTable(session, schema);
    command.setPersistIndexes(persistIndexes);
    command.setTemporary(temp);
    command.setGlobalTemporary(globalTemp);
    command.setIfNotExists(ifNotExists);
    command.setTableName(tableName);
    command.setComment(readCommentIf());
    if (readIf("(")) {
        if (!readIf(")")) {
            do {
                parseTableColumnDefinition(command, schema, tableName);
            } while (readIfMore(false));
        }
    }
    // Allows "COMMENT='comment'" in DDL statements (MySQL syntax)
    if (readIf("COMMENT")) {
        if (readIf("=")) {
            // read the complete string comment, but nothing with it for now
            readString();
        }
    }
    if (readIf("ENGINE")) {
        if (readIf("=")) {
            // map MySQL engine types onto H2 behavior
            String tableEngine = readUniqueIdentifier();
            if ("InnoDb".equalsIgnoreCase(tableEngine)) {
            // ok
            } else if (!"MyISAM".equalsIgnoreCase(tableEngine)) {
                throw DbException.getUnsupportedException(tableEngine);
            }
        } else {
            command.setTableEngine(readUniqueIdentifier());
        }
    }
    if (readIf("WITH")) {
        command.setTableEngineParams(readTableEngineParams());
    }
    // MySQL compatibility
    if (readIf("AUTO_INCREMENT")) {
        read("=");
        if (currentTokenType != VALUE || currentValue.getType() != Value.INT) {
            throw DbException.getSyntaxError(sqlCommand, parseIndex, "integer");
        }
        read();
    }
    readIf("DEFAULT");
    if (readIf("CHARSET")) {
        read("=");
        if (!readIf("UTF8")) {
            read("UTF8MB4");
        }
    }
    if (temp) {
        if (readIf("ON")) {
            read("COMMIT");
            if (readIf("DROP")) {
                command.setOnCommitDrop();
            } else if (readIf("DELETE")) {
                read("ROWS");
                command.setOnCommitTruncate();
            }
        } else if (readIf("NOT")) {
            if (readIf("PERSISTENT")) {
                command.setPersistData(false);
            } else {
                read("LOGGED");
            }
        }
        if (readIf("TRANSACTIONAL")) {
            command.setTransactional(true);
        }
    } else if (!persistIndexes && readIf("NOT")) {
        read("PERSISTENT");
        command.setPersistData(false);
    }
    if (readIf("HIDDEN")) {
        command.setHidden(true);
    }
    if (readIf("AS")) {
        if (readIf("SORTED")) {
            command.setSortedInsertMode(true);
        }
        command.setQuery(parseSelect());
    }
    // for MySQL compatibility
    if (readIf("ROW_FORMAT")) {
        if (readIf("=")) {
            readColumnIdentifier();
        }
    }
    return command;
}
Also used : DropSchema(org.h2.command.ddl.DropSchema) CreateSchema(org.h2.command.ddl.CreateSchema) Schema(org.h2.schema.Schema) CreateTable(org.h2.command.ddl.CreateTable) ValueString(org.h2.value.ValueString)

Example 38 with Value

use of org.h2.value.Value in project h2database by h2database.

the class Parser method parsePrepared.

private Prepared parsePrepared() {
    int start = lastParseIndex;
    Prepared c = null;
    String token = currentToken;
    if (token.length() == 0) {
        c = new NoOperation(session);
    } else {
        char first = token.charAt(0);
        switch(first) {
            case '?':
                // read the ? as a parameter
                readTerm();
                // this is an 'out' parameter - set a dummy value
                parameters.get(0).setValue(ValueNull.INSTANCE);
                read("=");
                read("CALL");
                c = parseCall();
                break;
            case '(':
                c = parseSelect();
                break;
            case 'a':
            case 'A':
                if (readIf("ALTER")) {
                    c = parseAlter();
                } else if (readIf("ANALYZE")) {
                    c = parseAnalyze();
                }
                break;
            case 'b':
            case 'B':
                if (readIf("BACKUP")) {
                    c = parseBackup();
                } else if (readIf("BEGIN")) {
                    c = parseBegin();
                }
                break;
            case 'c':
            case 'C':
                if (readIf("COMMIT")) {
                    c = parseCommit();
                } else if (readIf("CREATE")) {
                    c = parseCreate();
                } else if (readIf("CALL")) {
                    c = parseCall();
                } else if (readIf("CHECKPOINT")) {
                    c = parseCheckpoint();
                } else if (readIf("COMMENT")) {
                    c = parseComment();
                }
                break;
            case 'd':
            case 'D':
                if (readIf("DELETE")) {
                    c = parseDelete();
                } else if (readIf("DROP")) {
                    c = parseDrop();
                } else if (readIf("DECLARE")) {
                    // support for DECLARE GLOBAL TEMPORARY TABLE...
                    c = parseCreate();
                } else if (readIf("DEALLOCATE")) {
                    c = parseDeallocate();
                }
                break;
            case 'e':
            case 'E':
                if (readIf("EXPLAIN")) {
                    c = parseExplain();
                } else if (readIf("EXECUTE")) {
                    c = parseExecute();
                }
                break;
            case 'f':
            case 'F':
                if (isToken("FROM")) {
                    c = parseSelect();
                }
                break;
            case 'g':
            case 'G':
                if (readIf("GRANT")) {
                    c = parseGrantRevoke(CommandInterface.GRANT);
                }
                break;
            case 'h':
            case 'H':
                if (readIf("HELP")) {
                    c = parseHelp();
                }
                break;
            case 'i':
            case 'I':
                if (readIf("INSERT")) {
                    c = parseInsert();
                }
                break;
            case 'm':
            case 'M':
                if (readIf("MERGE")) {
                    c = parseMerge();
                }
                break;
            case 'p':
            case 'P':
                if (readIf("PREPARE")) {
                    c = parsePrepare();
                }
                break;
            case 'r':
            case 'R':
                if (readIf("ROLLBACK")) {
                    c = parseRollback();
                } else if (readIf("REVOKE")) {
                    c = parseGrantRevoke(CommandInterface.REVOKE);
                } else if (readIf("RUNSCRIPT")) {
                    c = parseRunScript();
                } else if (readIf("RELEASE")) {
                    c = parseReleaseSavepoint();
                } else if (readIf("REPLACE")) {
                    c = parseReplace();
                }
                break;
            case 's':
            case 'S':
                if (isToken("SELECT")) {
                    c = parseSelect();
                } else if (readIf("SET")) {
                    c = parseSet();
                } else if (readIf("SAVEPOINT")) {
                    c = parseSavepoint();
                } else if (readIf("SCRIPT")) {
                    c = parseScript();
                } else if (readIf("SHUTDOWN")) {
                    c = parseShutdown();
                } else if (readIf("SHOW")) {
                    c = parseShow();
                }
                break;
            case 't':
            case 'T':
                if (readIf("TRUNCATE")) {
                    c = parseTruncate();
                }
                break;
            case 'u':
            case 'U':
                if (readIf("UPDATE")) {
                    c = parseUpdate();
                } else if (readIf("USE")) {
                    c = parseUse();
                }
                break;
            case 'v':
            case 'V':
                if (readIf("VALUES")) {
                    c = parseValues();
                }
                break;
            case 'w':
            case 'W':
                if (readIf("WITH")) {
                    c = parseWithStatementOrQuery();
                }
                break;
            case ';':
                c = new NoOperation(session);
                break;
            default:
                throw getSyntaxError();
        }
        if (indexedParameterList != null) {
            for (int i = 0, size = indexedParameterList.size(); i < size; i++) {
                if (indexedParameterList.get(i) == null) {
                    indexedParameterList.set(i, new Parameter(i));
                }
            }
            parameters = indexedParameterList;
        }
        if (readIf("{")) {
            do {
                int index = (int) readLong() - 1;
                if (index < 0 || index >= parameters.size()) {
                    throw getSyntaxError();
                }
                Parameter p = parameters.get(index);
                if (p == null) {
                    throw getSyntaxError();
                }
                read(":");
                Expression expr = readExpression();
                expr = expr.optimize(session);
                p.setValue(expr.getValue(session));
            } while (readIf(","));
            read("}");
            for (Parameter p : parameters) {
                p.checkSet();
            }
            parameters.clear();
        }
    }
    if (c == null) {
        throw getSyntaxError();
    }
    setSQL(c, null, start);
    return c;
}
Also used : NoOperation(org.h2.command.dml.NoOperation) Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) Parameter(org.h2.expression.Parameter) ConditionInParameter(org.h2.expression.ConditionInParameter) ValueString(org.h2.value.ValueString) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint)

Example 39 with Value

use of org.h2.value.Value in project h2database by h2database.

the class AlterTableAlterColumn method update.

@Override
public int update() {
    session.commit(true);
    Database db = session.getDatabase();
    Table table = getSchema().resolveTableOrView(session, tableName);
    if (table == null) {
        if (ifTableExists) {
            return 0;
        }
        throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableName);
    }
    session.getUser().checkRight(table, Right.ALL);
    table.checkSupportAlter();
    table.lock(session, true, true);
    if (newColumn != null) {
        checkDefaultReferencesTable(table, newColumn.getDefaultExpression());
        checkClustering(newColumn);
    }
    if (columnsToAdd != null) {
        for (Column column : columnsToAdd) {
            checkDefaultReferencesTable(table, column.getDefaultExpression());
            checkClustering(column);
        }
    }
    switch(type) {
        case CommandInterface.ALTER_TABLE_ALTER_COLUMN_NOT_NULL:
            {
                if (!oldColumn.isNullable()) {
                    // no change
                    break;
                }
                checkNoNullValues(table);
                oldColumn.setNullable(false);
                db.updateMeta(session, table);
                break;
            }
        case CommandInterface.ALTER_TABLE_ALTER_COLUMN_NULL:
            {
                if (oldColumn.isNullable()) {
                    // no change
                    break;
                }
                checkNullable(table);
                oldColumn.setNullable(true);
                db.updateMeta(session, table);
                break;
            }
        case CommandInterface.ALTER_TABLE_ALTER_COLUMN_DEFAULT:
            {
                Sequence sequence = oldColumn == null ? null : oldColumn.getSequence();
                checkDefaultReferencesTable(table, defaultExpression);
                oldColumn.setSequence(null);
                oldColumn.setDefaultExpression(session, defaultExpression);
                removeSequence(table, sequence);
                db.updateMeta(session, table);
                break;
            }
        case CommandInterface.ALTER_TABLE_ALTER_COLUMN_ON_UPDATE:
            {
                checkDefaultReferencesTable(table, defaultExpression);
                oldColumn.setOnUpdateExpression(session, defaultExpression);
                db.updateMeta(session, table);
                break;
            }
        case CommandInterface.ALTER_TABLE_ALTER_COLUMN_CHANGE_TYPE:
            {
                // and does not affect the storage structure.
                if (oldColumn.isWideningConversion(newColumn)) {
                    convertAutoIncrementColumn(table, newColumn);
                    oldColumn.copy(newColumn);
                    db.updateMeta(session, table);
                } else {
                    oldColumn.setSequence(null);
                    oldColumn.setDefaultExpression(session, null);
                    oldColumn.setConvertNullToDefault(false);
                    if (oldColumn.isNullable() && !newColumn.isNullable()) {
                        checkNoNullValues(table);
                    } else if (!oldColumn.isNullable() && newColumn.isNullable()) {
                        checkNullable(table);
                    }
                    if (oldColumn.getVisible() ^ newColumn.getVisible()) {
                        oldColumn.setVisible(newColumn.getVisible());
                    }
                    convertAutoIncrementColumn(table, newColumn);
                    copyData(table);
                }
                table.setModified();
                break;
            }
        case CommandInterface.ALTER_TABLE_ADD_COLUMN:
            {
                // ifNotExists only supported for single column add
                if (ifNotExists && columnsToAdd != null && columnsToAdd.size() == 1 && table.doesColumnExist(columnsToAdd.get(0).getName())) {
                    break;
                }
                ArrayList<Sequence> sequences = generateSequences(columnsToAdd, false);
                if (columnsToAdd != null) {
                    changePrimaryKeysToNotNull(columnsToAdd);
                }
                copyData(table, sequences, true);
                break;
            }
        case CommandInterface.ALTER_TABLE_DROP_COLUMN:
            {
                if (table.getColumns().length - columnsToRemove.size() < 1) {
                    throw DbException.get(ErrorCode.CANNOT_DROP_LAST_COLUMN, columnsToRemove.get(0).getSQL());
                }
                table.dropMultipleColumnsConstraintsAndIndexes(session, columnsToRemove);
                copyData(table);
                break;
            }
        case CommandInterface.ALTER_TABLE_ALTER_COLUMN_SELECTIVITY:
            {
                int value = newSelectivity.optimize(session).getValue(session).getInt();
                oldColumn.setSelectivity(value);
                db.updateMeta(session, table);
                break;
            }
        case CommandInterface.ALTER_TABLE_ALTER_COLUMN_VISIBILITY:
            {
                oldColumn.setVisible(newVisibility);
                table.setModified();
                db.updateMeta(session, table);
                break;
            }
        default:
            DbException.throwInternalError("type=" + type);
    }
    return 0;
}
Also used : Table(org.h2.table.Table) Column(org.h2.table.Column) Database(org.h2.engine.Database) ArrayList(java.util.ArrayList) Sequence(org.h2.schema.Sequence)

Example 40 with Value

use of org.h2.value.Value in project h2database by h2database.

the class CommandRemote method close.

@Override
public void close() {
    if (session == null || session.isClosed()) {
        return;
    }
    synchronized (session) {
        session.traceOperation("COMMAND_CLOSE", id);
        for (Transfer transfer : transferList) {
            try {
                transfer.writeInt(SessionRemote.COMMAND_CLOSE).writeInt(id);
            } catch (IOException e) {
                trace.error(e, "close");
            }
        }
    }
    session = null;
    try {
        for (ParameterInterface p : parameters) {
            Value v = p.getParamValue();
            if (v != null) {
                v.remove();
            }
        }
    } catch (DbException e) {
        trace.error(e, "close");
    }
    parameters.clear();
}
Also used : ParameterInterface(org.h2.expression.ParameterInterface) Transfer(org.h2.value.Transfer) Value(org.h2.value.Value) IOException(java.io.IOException) DbException(org.h2.message.DbException)

Aggregations

Value (org.h2.value.Value)291 SQLException (java.sql.SQLException)94 DbException (org.h2.message.DbException)91 ResultSet (java.sql.ResultSet)69 PreparedStatement (java.sql.PreparedStatement)61 Statement (java.sql.Statement)53 Column (org.h2.table.Column)44 ValueString (org.h2.value.ValueString)44 JdbcStatement (org.h2.jdbc.JdbcStatement)42 JdbcPreparedStatement (org.h2.jdbc.JdbcPreparedStatement)36 SimpleResultSet (org.h2.tools.SimpleResultSet)31 StatementBuilder (org.h2.util.StatementBuilder)28 IndexColumn (org.h2.table.IndexColumn)23 Expression (org.h2.expression.Expression)22 IOException (java.io.IOException)21 Row (org.h2.result.Row)19 SearchRow (org.h2.result.SearchRow)18 Connection (java.sql.Connection)17 ValueArray (org.h2.value.ValueArray)17 ValueTimestamp (org.h2.value.ValueTimestamp)17