Search in sources :

Example 76 with Command

use of org.h2.command.Command in project h2database by h2database.

the class Parser method parseBegin.

private TransactionCommand parseBegin() {
    TransactionCommand command;
    if (!readIf("WORK")) {
        readIf("TRANSACTION");
    }
    command = new TransactionCommand(session, CommandInterface.BEGIN);
    return command;
}
Also used : TransactionCommand(org.h2.command.dml.TransactionCommand)

Example 77 with Command

use of org.h2.command.Command in project h2database by h2database.

the class Parser method parseTruncate.

private Prepared parseTruncate() {
    read("TABLE");
    Table table = readTableOrView();
    TruncateTable command = new TruncateTable(session);
    command.setTable(table);
    return command;
}
Also used : RangeTable(org.h2.table.RangeTable) TruncateTable(org.h2.command.ddl.TruncateTable) CreateTable(org.h2.command.ddl.CreateTable) FunctionTable(org.h2.table.FunctionTable) CreateLinkedTable(org.h2.command.ddl.CreateLinkedTable) Table(org.h2.table.Table) DropTable(org.h2.command.ddl.DropTable) TruncateTable(org.h2.command.ddl.TruncateTable)

Example 78 with Command

use of org.h2.command.Command in project h2database by h2database.

the class Parser method parseComment.

private Prepared parseComment() {
    int type = 0;
    read("ON");
    boolean column = false;
    if (readIf("TABLE") || readIf("VIEW")) {
        type = DbObject.TABLE_OR_VIEW;
    } else if (readIf("COLUMN")) {
        column = true;
        type = DbObject.TABLE_OR_VIEW;
    } else if (readIf("CONSTANT")) {
        type = DbObject.CONSTANT;
    } else if (readIf("CONSTRAINT")) {
        type = DbObject.CONSTRAINT;
    } else if (readIf("ALIAS")) {
        type = DbObject.FUNCTION_ALIAS;
    } else if (readIf("INDEX")) {
        type = DbObject.INDEX;
    } else if (readIf("ROLE")) {
        type = DbObject.ROLE;
    } else if (readIf("SCHEMA")) {
        type = DbObject.SCHEMA;
    } else if (readIf("SEQUENCE")) {
        type = DbObject.SEQUENCE;
    } else if (readIf("TRIGGER")) {
        type = DbObject.TRIGGER;
    } else if (readIf("USER")) {
        type = DbObject.USER;
    } else if (readIf("DOMAIN")) {
        type = DbObject.USER_DATATYPE;
    } else {
        throw getSyntaxError();
    }
    SetComment command = new SetComment(session);
    String objectName;
    if (column) {
        // can't use readIdentifierWithSchema() because
        // it would not read schema.table.column correctly
        // if the db name is equal to the schema name
        ArrayList<String> list = New.arrayList();
        do {
            list.add(readUniqueIdentifier());
        } while (readIf("."));
        schemaName = session.getCurrentSchemaName();
        if (list.size() == 4) {
            if (!equalsToken(database.getShortName(), list.remove(0))) {
                throw DbException.getSyntaxError(sqlCommand, parseIndex, "database name");
            }
        }
        if (list.size() == 3) {
            schemaName = list.remove(0);
        }
        if (list.size() != 2) {
            throw DbException.getSyntaxError(sqlCommand, parseIndex, "table.column");
        }
        objectName = list.get(0);
        command.setColumn(true);
        command.setColumnName(list.get(1));
    } else {
        objectName = readIdentifierWithSchema();
    }
    command.setSchemaName(schemaName);
    command.setObjectName(objectName);
    command.setObjectType(type);
    read("IS");
    command.setCommentExpression(readExpression());
    return command;
}
Also used : SetComment(org.h2.command.ddl.SetComment) ValueString(org.h2.value.ValueString) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint)

Example 79 with Command

use of org.h2.command.Command in project h2database by h2database.

the class Parser method parseCreateSchema.

private CreateSchema parseCreateSchema() {
    CreateSchema command = new CreateSchema(session);
    command.setIfNotExists(readIfNotExists());
    command.setSchemaName(readUniqueIdentifier());
    if (readIf("AUTHORIZATION")) {
        command.setAuthorization(readUniqueIdentifier());
    } else {
        command.setAuthorization(session.getUser().getName());
    }
    if (readIf("WITH")) {
        command.setTableEngineParams(readTableEngineParams());
    }
    return command;
}
Also used : CreateSchema(org.h2.command.ddl.CreateSchema)

Example 80 with Command

use of org.h2.command.Command in project h2database by h2database.

the class Parser method initialize.

private void initialize(String sql) {
    if (sql == null) {
        sql = "";
    }
    originalSQL = sql;
    sqlCommand = sql;
    int len = sql.length() + 1;
    char[] command = new char[len];
    int[] types = new int[len];
    len--;
    sql.getChars(0, len, command, 0);
    boolean changed = false;
    command[len] = ' ';
    int startLoop = 0;
    int lastType = 0;
    for (int i = 0; i < len; i++) {
        char c = command[i];
        int type = 0;
        switch(c) {
            case '/':
                if (command[i + 1] == '*') {
                    // block comment
                    changed = true;
                    command[i] = ' ';
                    command[i + 1] = ' ';
                    startLoop = i;
                    i += 2;
                    checkRunOver(i, len, startLoop);
                    while (command[i] != '*' || command[i + 1] != '/') {
                        command[i++] = ' ';
                        checkRunOver(i, len, startLoop);
                    }
                    command[i] = ' ';
                    command[i + 1] = ' ';
                    i++;
                } else if (command[i + 1] == '/') {
                    // single line comment
                    changed = true;
                    startLoop = i;
                    while (true) {
                        c = command[i];
                        if (c == '\n' || c == '\r' || i >= len - 1) {
                            break;
                        }
                        command[i++] = ' ';
                        checkRunOver(i, len, startLoop);
                    }
                } else {
                    type = CHAR_SPECIAL_1;
                }
                break;
            case '-':
                if (command[i + 1] == '-') {
                    // single line comment
                    changed = true;
                    startLoop = i;
                    while (true) {
                        c = command[i];
                        if (c == '\n' || c == '\r' || i >= len - 1) {
                            break;
                        }
                        command[i++] = ' ';
                        checkRunOver(i, len, startLoop);
                    }
                } else {
                    type = CHAR_SPECIAL_1;
                }
                break;
            case '$':
                if (command[i + 1] == '$' && (i == 0 || command[i - 1] <= ' ')) {
                    // dollar quoted string
                    changed = true;
                    command[i] = ' ';
                    command[i + 1] = ' ';
                    startLoop = i;
                    i += 2;
                    checkRunOver(i, len, startLoop);
                    while (command[i] != '$' || command[i + 1] != '$') {
                        types[i++] = CHAR_DOLLAR_QUOTED_STRING;
                        checkRunOver(i, len, startLoop);
                    }
                    command[i] = ' ';
                    command[i + 1] = ' ';
                    i++;
                } else {
                    if (lastType == CHAR_NAME || lastType == CHAR_VALUE) {
                        // $ inside an identifier is supported
                        type = CHAR_NAME;
                    } else {
                        // but not at the start, to support PostgreSQL $1
                        type = CHAR_SPECIAL_1;
                    }
                }
                break;
            case '(':
            case ')':
            case '{':
            case '}':
            case '*':
            case ',':
            case ';':
            case '+':
            case '%':
            case '?':
            case '@':
            case ']':
                type = CHAR_SPECIAL_1;
                break;
            case '!':
            case '<':
            case '>':
            case '|':
            case '=':
            case ':':
            case '&':
            case '~':
                type = CHAR_SPECIAL_2;
                break;
            case '.':
                type = CHAR_DOT;
                break;
            case '\'':
                type = types[i] = CHAR_STRING;
                startLoop = i;
                while (command[++i] != '\'') {
                    checkRunOver(i, len, startLoop);
                }
                break;
            case '[':
                if (database.getMode().squareBracketQuotedNames) {
                    // SQL Server alias for "
                    command[i] = '"';
                    changed = true;
                    type = types[i] = CHAR_QUOTED;
                    startLoop = i;
                    while (command[++i] != ']') {
                        checkRunOver(i, len, startLoop);
                    }
                    command[i] = '"';
                } else {
                    type = CHAR_SPECIAL_1;
                }
                break;
            case '`':
                // MySQL alias for ", but not case sensitive
                command[i] = '"';
                changed = true;
                type = types[i] = CHAR_QUOTED;
                startLoop = i;
                while (command[++i] != '`') {
                    checkRunOver(i, len, startLoop);
                    c = command[i];
                    command[i] = Character.toUpperCase(c);
                }
                command[i] = '"';
                break;
            case '\"':
                type = types[i] = CHAR_QUOTED;
                startLoop = i;
                while (command[++i] != '\"') {
                    checkRunOver(i, len, startLoop);
                }
                break;
            case '_':
                type = CHAR_NAME;
                break;
            case '#':
                if (database.getMode().supportPoundSymbolForColumnNames) {
                    type = CHAR_NAME;
                } else {
                    type = CHAR_SPECIAL_1;
                }
                break;
            default:
                if (c >= 'a' && c <= 'z') {
                    if (identifiersToUpper) {
                        command[i] = (char) (c - ('a' - 'A'));
                        changed = true;
                    }
                    type = CHAR_NAME;
                } else if (c >= 'A' && c <= 'Z') {
                    type = CHAR_NAME;
                } else if (c >= '0' && c <= '9') {
                    type = CHAR_VALUE;
                } else {
                    if (c <= ' ' || Character.isSpaceChar(c)) {
                    // whitespace
                    } else if (Character.isJavaIdentifierPart(c)) {
                        type = CHAR_NAME;
                        if (identifiersToUpper) {
                            char u = Character.toUpperCase(c);
                            if (u != c) {
                                command[i] = u;
                                changed = true;
                            }
                        }
                    } else {
                        type = CHAR_SPECIAL_1;
                    }
                }
        }
        types[i] = type;
        lastType = type;
    }
    sqlCommandChars = command;
    types[len] = CHAR_END;
    characterTypes = types;
    if (changed) {
        sqlCommand = new String(command);
    }
    parseIndex = 0;
}
Also used : ValueString(org.h2.value.ValueString) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint)

Aggregations

ValueString (org.h2.value.ValueString)37 Expression (org.h2.expression.Expression)20 ValueExpression (org.h2.expression.ValueExpression)19 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)17 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)16 AlterTableRenameConstraint (org.h2.command.ddl.AlterTableRenameConstraint)16 Column (org.h2.table.Column)16 IndexColumn (org.h2.table.IndexColumn)13 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)12 ExpressionColumn (org.h2.expression.ExpressionColumn)12 Table (org.h2.table.Table)12 AlterTableRenameColumn (org.h2.command.ddl.AlterTableRenameColumn)11 Connection (java.sql.Connection)10 CreateLinkedTable (org.h2.command.ddl.CreateLinkedTable)10 CreateSchema (org.h2.command.ddl.CreateSchema)10 CreateTable (org.h2.command.ddl.CreateTable)10 DropSchema (org.h2.command.ddl.DropSchema)10 DropTable (org.h2.command.ddl.DropTable)10 Schema (org.h2.schema.Schema)10 TruncateTable (org.h2.command.ddl.TruncateTable)9