Search in sources :

Example 16 with Comment

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

the class Database method removeDatabaseObject.

/**
 * Remove the object from the database.
 *
 * @param session the session
 * @param obj the object to remove
 */
public synchronized void removeDatabaseObject(Session session, DbObject obj) {
    checkWritingAllowed();
    String objName = obj.getName();
    int type = obj.getType();
    HashMap<String, DbObject> map = getMap(type);
    if (SysProperties.CHECK && !map.containsKey(objName)) {
        DbException.throwInternalError("not found: " + objName);
    }
    Comment comment = findComment(obj);
    lockMeta(session);
    if (comment != null) {
        removeDatabaseObject(session, comment);
    }
    int id = obj.getId();
    obj.removeChildrenAndResources(session);
    map.remove(objName);
    removeMeta(session, id);
}
Also used : Constraint(org.h2.constraint.Constraint)

Example 17 with Comment

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

the class ConstraintUnique method getCreateSQLForCopy.

private String getCreateSQLForCopy(Table forTable, String quotedName, boolean internalIndex) {
    StatementBuilder buff = new StatementBuilder("ALTER TABLE ");
    buff.append(forTable.getSQL()).append(" ADD CONSTRAINT ");
    if (forTable.isHidden()) {
        buff.append("IF NOT EXISTS ");
    }
    buff.append(quotedName);
    if (comment != null) {
        buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
    }
    buff.append(' ').append(getConstraintType().getSqlName()).append('(');
    for (IndexColumn c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(Parser.quoteIdentifier(c.column.getName()));
    }
    buff.append(')');
    if (internalIndex && indexOwner && forTable == this.table) {
        buff.append(" INDEX ").append(index.getSQL());
    }
    return buff.toString();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) IndexColumn(org.h2.table.IndexColumn)

Example 18 with Comment

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

the class TableBase method getCreateSQL.

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

Example 19 with Comment

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

the class Column method getCreateSQL.

private String getCreateSQL(boolean includeName) {
    StringBuilder buff = new StringBuilder();
    if (includeName && name != null) {
        buff.append(Parser.quoteIdentifier(name)).append(' ');
    }
    if (originalSQL != null) {
        buff.append(originalSQL);
    } else {
        buff.append(DataType.getDataType(type).name);
        switch(type) {
            case Value.DECIMAL:
                buff.append('(').append(precision).append(", ").append(scale).append(')');
                break;
            case Value.ENUM:
                buff.append('(');
                for (int i = 0; i < enumerators.length; i++) {
                    buff.append('\'').append(enumerators[i]).append('\'');
                    if (i < enumerators.length - 1) {
                        buff.append(',');
                    }
                }
                buff.append(')');
                break;
            case Value.BYTES:
            case Value.STRING:
            case Value.STRING_IGNORECASE:
            case Value.STRING_FIXED:
                if (precision < Integer.MAX_VALUE) {
                    buff.append('(').append(precision).append(')');
                }
                break;
            default:
        }
    }
    if (!visible) {
        buff.append(" INVISIBLE ");
    }
    if (defaultExpression != null) {
        String sql = defaultExpression.getSQL();
        if (sql != null) {
            if (isComputed) {
                buff.append(" AS ").append(sql);
            } else if (defaultExpression != null) {
                buff.append(" DEFAULT ").append(sql);
            }
        }
    }
    if (onUpdateExpression != null) {
        String sql = onUpdateExpression.getSQL();
        if (sql != null) {
            buff.append(" ON UPDATE ").append(sql);
        }
    }
    if (!nullable) {
        buff.append(" NOT NULL");
    }
    if (convertNullToDefault) {
        buff.append(" NULL_TO_DEFAULT");
    }
    if (sequence != null) {
        buff.append(" SEQUENCE ").append(sequence.getSQL());
    }
    if (selectivity != 0) {
        buff.append(" SELECTIVITY ").append(selectivity);
    }
    if (comment != null) {
        buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
    }
    if (checkConstraint != null) {
        buff.append(" CHECK ").append(checkConstraintSQL);
    }
    return buff.toString();
}
Also used : ValueString(org.h2.value.ValueString)

Example 20 with Comment

use of org.h2.engine.Comment 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

Database (org.h2.engine.Database)9 ValueString (org.h2.value.ValueString)8 Table (org.h2.table.Table)7 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)5 StatementBuilder (org.h2.util.StatementBuilder)5 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)4 AlterTableRenameConstraint (org.h2.command.ddl.AlterTableRenameConstraint)4 Constraint (org.h2.constraint.Constraint)4 ExpressionColumn (org.h2.expression.ExpressionColumn)4 DbException (org.h2.message.DbException)4 Column (org.h2.table.Column)4 IndexColumn (org.h2.table.IndexColumn)4 PreparedStatement (java.sql.PreparedStatement)3 DbObject (org.h2.engine.DbObject)3 Expression (org.h2.expression.Expression)3 ValueExpression (org.h2.expression.ValueExpression)3 Index (org.h2.index.Index)3 Sequence (org.h2.schema.Sequence)3 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2