Search in sources :

Example 11 with Schema

use of com.wplatform.ddal.dbobject.schema.Schema in project jdbc-shards by wplatform.

the class CommonPreparedExecutor method findTableMate.

/**
 * @param tableName
 */
public TableMate findTableMate(String tableName) {
    Table table = database.getSchema(session.getCurrentSchemaName()).findTableOrView(session, tableName);
    if (table == null) {
        String[] schemaNames = session.getSchemaSearchPath();
        if (schemaNames != null) {
            for (String name : schemaNames) {
                Schema s = database.getSchema(name);
                table = s.findTableOrView(session, tableName);
                if (table != null) {
                    break;
                }
            }
        }
    }
    if (table != null && table instanceof TableMate) {
        return (TableMate) table;
    }
    return null;
}
Also used : Table(com.wplatform.ddal.dbobject.table.Table) Schema(com.wplatform.ddal.dbobject.schema.Schema) TableMate(com.wplatform.ddal.dbobject.table.TableMate)

Example 12 with Schema

use of com.wplatform.ddal.dbobject.schema.Schema in project jdbc-shards by wplatform.

the class Parser method parseCreateTable.

private CreateTable parseCreateTable(boolean temp, boolean globalTemp) {
    boolean ifNotExists = readIfNoExists();
    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.setTemporary(temp);
    command.setGlobalTemporary(globalTemp);
    command.setIfNotExists(ifNotExists);
    command.setTableName(tableName);
    command.setComment(readCommentIf());
    if (readIf("(")) {
        if (!readIf(")")) {
            do {
                DefineCommand c = parseAlterTableAddConstraintIf(tableName, schema);
                if (c != null) {
                    command.addConstraintCommand(c);
                } else {
                    String columnName = readColumnIdentifier();
                    Column column = parseColumnForTable(columnName, true);
                    if (column.isAutoIncrement() && column.isPrimaryKey()) {
                        column.setPrimaryKey(false);
                        IndexColumn[] cols = { new IndexColumn() };
                        cols[0].columnName = column.getName();
                        AlterTableAddConstraint pk = new AlterTableAddConstraint(session, schema, false);
                        pk.setType(CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY);
                        pk.setTableName(tableName);
                        pk.setIndexColumns(cols);
                        command.addConstraintCommand(pk);
                    }
                    command.addColumn(column);
                    String constraintName = null;
                    if (readIf("CONSTRAINT")) {
                        constraintName = readColumnIdentifier();
                    }
                    if (readIf("PRIMARY")) {
                        read("KEY");
                        boolean hash = readIf("HASH");
                        IndexColumn[] cols = { new IndexColumn() };
                        cols[0].columnName = column.getName();
                        AlterTableAddConstraint pk = new AlterTableAddConstraint(session, schema, false);
                        pk.setPrimaryKeyHash(hash);
                        pk.setType(CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY);
                        pk.setTableName(tableName);
                        pk.setIndexColumns(cols);
                        command.addConstraintCommand(pk);
                        if (readIf("AUTO_INCREMENT")) {
                            parseAutoIncrement(column);
                        }
                    } else if (readIf("UNIQUE")) {
                        AlterTableAddConstraint unique = new AlterTableAddConstraint(session, schema, false);
                        unique.setConstraintName(constraintName);
                        unique.setType(CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_UNIQUE);
                        IndexColumn[] cols = { new IndexColumn() };
                        cols[0].columnName = columnName;
                        unique.setIndexColumns(cols);
                        unique.setTableName(tableName);
                        command.addConstraintCommand(unique);
                    }
                    if (readIf("NOT")) {
                        read("NULL");
                        column.setNullable(false);
                    } else {
                        readIf("NULL");
                    }
                    if (readIf("CHECK")) {
                        Expression expr = readExpression();
                        column.addCheckConstraint(session, expr);
                    }
                    if (readIf("REFERENCES")) {
                        AlterTableAddConstraint ref = new AlterTableAddConstraint(session, schema, false);
                        ref.setConstraintName(constraintName);
                        ref.setType(CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_REFERENTIAL);
                        IndexColumn[] cols = { new IndexColumn() };
                        cols[0].columnName = columnName;
                        ref.setIndexColumns(cols);
                        ref.setTableName(tableName);
                        parseReferences(ref, schema, tableName);
                        command.addConstraintCommand(ref);
                    }
                }
            } while (readIfMore());
        }
    }
    // 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)) {
                command.setTableEngine("InnoDb");
            } else if ("MyISAM".equalsIgnoreCase(tableEngine)) {
                command.setTableEngine("MyISAM");
            } else {
                throw DbException.getUnsupportedException(tableEngine);
            }
        } else {
            command.setTableEngine(readUniqueIdentifier());
            if (readIf("WITH")) {
                ArrayList<String> tableEngineParams = New.arrayList();
                do {
                    tableEngineParams.add(readUniqueIdentifier());
                } while (readIf(","));
                command.setTableEngineParams(tableEngineParams);
            }
        }
    } else if (database.getSettings().defaultTableEngine != null) {
        command.setTableEngine(database.getSettings().defaultTableEngine);
    }
    // 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("=");
        // read("UTF8");
        String charset = StringUtils.toLowerEnglish(readUniqueIdentifier());
        command.setCharset(charset);
    }
    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")) {
                throw getSyntaxError();
            } else {
                read("LOGGED");
            }
        }
        if (readIf("TRANSACTIONAL")) {
            command.setTransactional(true);
        }
    } else if (readIf("NOT")) {
        read("PERSISTENT");
        throw getSyntaxError();
    }
    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 : Schema(com.wplatform.ddal.dbobject.schema.Schema)

Example 13 with Schema

use of com.wplatform.ddal.dbobject.schema.Schema in project jdbc-shards by wplatform.

the class Parser method parseCreateConstant.

private CreateConstant parseCreateConstant() {
    boolean ifNotExists = readIfNoExists();
    String constantName = readIdentifierWithSchema();
    Schema schema = getSchema();
    if (isKeyword(constantName)) {
        throw DbException.get(ErrorCode.CONSTANT_ALREADY_EXISTS_1, constantName);
    }
    read("VALUE");
    Expression expr = readExpression();
    CreateConstant command = new CreateConstant(session, schema);
    command.setConstantName(constantName);
    command.setExpression(expr);
    command.setIfNotExists(ifNotExists);
    return command;
}
Also used : Schema(com.wplatform.ddal.dbobject.schema.Schema)

Example 14 with Schema

use of com.wplatform.ddal.dbobject.schema.Schema in project jdbc-shards by wplatform.

the class Parser method parseAlterTableAddColumn.

private AlterTableAlterColumn parseAlterTableAddColumn(Table table) {
    readIf("COLUMN");
    Schema schema = table.getSchema();
    AlterTableAlterColumn command = new AlterTableAlterColumn(session, schema);
    command.setType(CommandInterface.ALTER_TABLE_ADD_COLUMN);
    command.setTable(table);
    ArrayList<Column> columnsToAdd = New.arrayList();
    if (readIf("(")) {
        command.setIfNotExists(false);
        do {
            String columnName = readColumnIdentifier();
            Column column = parseColumnForTable(columnName, true);
            columnsToAdd.add(column);
        } while (readIf(","));
        read(")");
        command.setNewColumns(columnsToAdd);
    } else {
        boolean ifNotExists = readIfNoExists();
        command.setIfNotExists(ifNotExists);
        String columnName = readColumnIdentifier();
        Column column = parseColumnForTable(columnName, true);
        columnsToAdd.add(column);
        if (readIf("BEFORE")) {
            command.setAddBefore(readColumnIdentifier());
        } else if (readIf("AFTER")) {
            command.setAddAfter(readColumnIdentifier());
        }
    }
    command.setNewColumns(columnsToAdd);
    return command;
}
Also used : Schema(com.wplatform.ddal.dbobject.schema.Schema)

Example 15 with Schema

use of com.wplatform.ddal.dbobject.schema.Schema in project jdbc-shards by wplatform.

the class Parser method parseValuesTable.

private TableFilter parseValuesTable() {
    Schema mainSchema = database.getSchema(Constants.SCHEMA_MAIN);
    TableFunction tf = (TableFunction) Function.getFunction(database, "TABLE");
    ArrayList<Column> columns = New.arrayList();
    ArrayList<ArrayList<Expression>> rows = New.arrayList();
    do {
        int i = 0;
        ArrayList<Expression> row = New.arrayList();
        boolean multiColumn = readIf("(");
        do {
            Expression expr = readExpression();
            expr = expr.optimize(session);
            int type = expr.getType();
            long prec;
            int scale, displaySize;
            Column column;
            String columnName = "C" + (i + 1);
            if (rows.size() == 0) {
                if (type == Value.UNKNOWN) {
                    type = Value.STRING;
                }
                DataType dt = DataType.getDataType(type);
                prec = dt.defaultPrecision;
                scale = dt.defaultScale;
                displaySize = dt.defaultDisplaySize;
                column = new Column(columnName, type, prec, scale, displaySize);
                columns.add(column);
            }
            prec = expr.getPrecision();
            scale = expr.getScale();
            displaySize = expr.getDisplaySize();
            if (i >= columns.size()) {
                throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
            }
            Column c = columns.get(i);
            type = Value.getHigherOrder(c.getType(), type);
            prec = Math.max(c.getPrecision(), prec);
            scale = Math.max(c.getScale(), scale);
            displaySize = Math.max(c.getDisplaySize(), displaySize);
            column = new Column(columnName, type, prec, scale, displaySize);
            columns.set(i, column);
            row.add(expr);
            i++;
        } while (multiColumn && readIf(","));
        if (multiColumn) {
            read(")");
        }
        rows.add(row);
    } while (readIf(","));
    int columnCount = columns.size();
    int rowCount = rows.size();
    for (int i = 0; i < rowCount; i++) {
        if (rows.get(i).size() != columnCount) {
            throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
        }
    }
    for (int i = 0; i < columnCount; i++) {
        Column c = columns.get(i);
        if (c.getType() == Value.UNKNOWN) {
            c = new Column(c.getName(), Value.STRING, 0, 0, 0);
            columns.set(i, c);
        }
        Expression[] array = new Expression[rowCount];
        for (int j = 0; j < rowCount; j++) {
            array[j] = rows.get(j).get(i);
        }
        ExpressionList list = new ExpressionList(array);
        tf.setParameter(i, list);
    }
    tf.setColumns(columns);
    tf.doneWithParameters();
    Table table = new FunctionTable(mainSchema, session, tf, tf);
    TableFilter filter = new TableFilter(session, table, null, rightsChecked, currentSelect);
    return filter;
}
Also used : Schema(com.wplatform.ddal.dbobject.schema.Schema) ArrayList(java.util.ArrayList)

Aggregations

Schema (com.wplatform.ddal.dbobject.schema.Schema)16 TableMate (com.wplatform.ddal.dbobject.table.TableMate)2 Parser (com.wplatform.ddal.command.Parser)1 SchemaConfig (com.wplatform.ddal.config.SchemaConfig)1 TableConfig (com.wplatform.ddal.config.TableConfig)1 Role (com.wplatform.ddal.dbobject.Role)1 User (com.wplatform.ddal.dbobject.User)1 Constant (com.wplatform.ddal.dbobject.schema.Constant)1 Sequence (com.wplatform.ddal.dbobject.schema.Sequence)1 Table (com.wplatform.ddal.dbobject.table.Table)1 Database (com.wplatform.ddal.engine.Database)1 Mode (com.wplatform.ddal.engine.Mode)1 ArrayList (java.util.ArrayList)1