Search in sources :

Example 6 with Sequence

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

the class Table method addDependencies.

/**
 * Add all objects that this table depends on to the hash set.
 *
 * @param dependencies the current set of dependencies
 */
public void addDependencies(HashSet<DbObject> dependencies) {
    if (dependencies.contains(this)) {
        // avoid endless recursion
        return;
    }
    if (sequences != null) {
        for (Sequence s : sequences) {
            dependencies.add(s);
        }
    }
    ExpressionVisitor visitor = ExpressionVisitor.getDependenciesVisitor(dependencies);
    for (Column col : columns) {
        col.isEverything(visitor);
    }
    dependencies.add(this);
}
Also used : Sequence(com.wplatform.ddal.dbobject.schema.Sequence) ExpressionVisitor(com.wplatform.ddal.command.expression.ExpressionVisitor)

Example 7 with Sequence

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

the class Parser method parseColumnForTable.

private Column parseColumnForTable(String columnName, boolean defaultNullable) {
    Column column;
    boolean isIdentity = false;
    if (readIf("IDENTITY") || readIf("BIGSERIAL")) {
        column = new Column(columnName, Value.LONG);
        column.setOriginalSQL("IDENTITY");
        parseAutoIncrement(column);
        // PostgreSQL compatibility
        if (!database.getMode().serialColumnIsNotPK) {
            column.setPrimaryKey(true);
        }
    } else if (readIf("SERIAL")) {
        column = new Column(columnName, Value.INT);
        column.setOriginalSQL("SERIAL");
        parseAutoIncrement(column);
        // PostgreSQL compatibility
        if (!database.getMode().serialColumnIsNotPK) {
            column.setPrimaryKey(true);
        }
    } else {
        column = parseColumnWithType(columnName);
    }
    if (readIf("NOT")) {
        read("NULL");
        column.setNullable(false);
    } else if (readIf("NULL")) {
        column.setNullable(true);
    } else {
        // domains may be defined as not nullable
        column.setNullable(defaultNullable & column.isNullable());
    }
    if (readIf("AS")) {
        if (isIdentity) {
            getSyntaxError();
        }
        Expression expr = readExpression();
        column.setComputedExpression(expr);
    } else if (readIf("DEFAULT")) {
        Expression defaultExpression = readExpression();
        column.setDefaultExpression(session, defaultExpression);
    } else if (readIf("GENERATED")) {
        if (!readIf("ALWAYS")) {
            read("BY");
            read("DEFAULT");
        }
        read("AS");
        read("IDENTITY");
        long start = 1, increment = 1;
        if (readIf("(")) {
            read("START");
            readIf("WITH");
            start = readLong();
            readIf(",");
            if (readIf("INCREMENT")) {
                readIf("BY");
                increment = readLong();
            }
            read(")");
        }
        column.setPrimaryKey(true);
        column.setAutoIncrement(true, start, increment);
    }
    if (readIf("NOT")) {
        read("NULL");
        column.setNullable(false);
    } else {
        readIf("NULL");
    }
    if (readIf("AUTO_INCREMENT") || readIf("BIGSERIAL") || readIf("SERIAL")) {
        parseAutoIncrement(column);
        if (readIf("NOT")) {
            read("NULL");
        }
    } else if (readIf("IDENTITY")) {
        parseAutoIncrement(column);
        column.setPrimaryKey(true);
        if (readIf("NOT")) {
            read("NULL");
        }
    }
    if (readIf("NULL_TO_DEFAULT")) {
        column.setConvertNullToDefault(true);
    }
    if (readIf("SEQUENCE")) {
        Sequence sequence = readSequence();
        column.setSequence(sequence);
    }
    if (readIf("SELECTIVITY")) {
        int value = readPositiveInt();
        column.setSelectivity(value);
    }
    String comment = readCommentIf();
    if (comment != null) {
        column.setComment(comment);
    }
    return column;
}
Also used : Sequence(com.wplatform.ddal.dbobject.schema.Sequence)

Example 8 with Sequence

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

the class Parser method readSequence.

private Sequence readSequence() {
    // same algorithm as readTableOrView
    String sequenceName = readIdentifierWithSchema(null);
    if (schemaName != null) {
        return getSchema().getSequence(sequenceName);
    }
    Sequence sequence = findSequence(session.getCurrentSchemaName(), sequenceName);
    if (sequence != null) {
        return sequence;
    }
    throw DbException.get(ErrorCode.SEQUENCE_NOT_FOUND_1, sequenceName);
}
Also used : Sequence(com.wplatform.ddal.dbobject.schema.Sequence)

Example 9 with Sequence

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

the class Parser method findSequence.

private Sequence findSequence(String schema, String sequenceName) {
    Sequence sequence = database.getSchema(schema).findSequence(sequenceName);
    if (sequence != null) {
        return sequence;
    }
    String[] schemaNames = session.getSchemaSearchPath();
    if (schemaNames != null) {
        for (String n : schemaNames) {
            sequence = database.getSchema(n).findSequence(sequenceName);
            if (sequence != null) {
                return sequence;
            }
        }
    }
    return null;
}
Also used : Sequence(com.wplatform.ddal.dbobject.schema.Sequence)

Example 10 with Sequence

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

the class Function method getSequence.

private Sequence getSequence(Session session, Value v0, Value v1) {
    String schemaName, sequenceName;
    if (v1 == null) {
        Parser p = new Parser(session);
        String sql = v0.getString();
        Expression expr = p.parseExpression(sql);
        if (expr instanceof ExpressionColumn) {
            ExpressionColumn seq = (ExpressionColumn) expr;
            schemaName = seq.getOriginalTableAliasName();
            if (schemaName == null) {
                schemaName = session.getCurrentSchemaName();
                sequenceName = sql;
            } else {
                sequenceName = seq.getColumnName();
            }
        } else {
            throw DbException.getSyntaxError(sql, 1);
        }
    } else {
        schemaName = v0.getString();
        sequenceName = v1.getString();
    }
    Schema s = database.findSchema(schemaName);
    if (s == null) {
        schemaName = StringUtils.toUpperEnglish(schemaName);
        s = database.getSchema(schemaName);
    }
    Sequence seq = s.findSequence(sequenceName);
    if (seq == null) {
        sequenceName = StringUtils.toUpperEnglish(sequenceName);
        seq = s.getSequence(sequenceName);
    }
    return seq;
}
Also used : Schema(com.wplatform.ddal.dbobject.schema.Schema) Sequence(com.wplatform.ddal.dbobject.schema.Sequence) Parser(com.wplatform.ddal.command.Parser)

Aggregations

Sequence (com.wplatform.ddal.dbobject.schema.Sequence)11 Parser (com.wplatform.ddal.command.Parser)1 AlterTableAddConstraint (com.wplatform.ddal.command.ddl.AlterTableAddConstraint)1 DefineCommand (com.wplatform.ddal.command.ddl.DefineCommand)1 Insert (com.wplatform.ddal.command.dml.Insert)1 Query (com.wplatform.ddal.command.dml.Query)1 ExpressionVisitor (com.wplatform.ddal.command.expression.ExpressionVisitor)1 FunctionAlias (com.wplatform.ddal.dbobject.FunctionAlias)1 Right (com.wplatform.ddal.dbobject.Right)1 Schema (com.wplatform.ddal.dbobject.schema.Schema)1 Column (com.wplatform.ddal.dbobject.table.Column)1 IndexColumn (com.wplatform.ddal.dbobject.table.IndexColumn)1 TableMate (com.wplatform.ddal.dbobject.table.TableMate)1 TableNode (com.wplatform.ddal.dispatch.rule.TableNode)1 DbException (com.wplatform.ddal.message.DbException)1 Csv (com.wplatform.ddal.result.Csv)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Timestamp (java.sql.Timestamp)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1