Search in sources :

Example 1 with Sequence

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

the class Table method removeChildrenAndResources.

@Override
public void removeChildrenAndResources(Session session) {
    for (Right right : database.getAllRights()) {
        if (right.getGrantedTable() == this) {
            database.removeDatabaseObject(session, right);
        }
    }
    // before removing the table object)
    while (sequences != null && sequences.size() > 0) {
        Sequence sequence = sequences.get(0);
        sequences.remove(0);
        if (!isTemporary()) {
            // this is possible when calling ALTER TABLE ALTER COLUMN
            if (database.getDependentTable(sequence, this) == null) {
                database.removeSchemaObject(session, sequence);
            }
        }
    }
}
Also used : Right(com.wplatform.ddal.dbobject.Right) Sequence(com.wplatform.ddal.dbobject.schema.Sequence)

Example 2 with Sequence

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

the class CreateTableExecutor method executeUpdate.

@Override
public int executeUpdate() {
    String tableName = prepared.getTableName();
    TableMate tableMate = getTableMate(tableName);
    if (tableMate == null) {
        throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableName);
    }
    if (!tableMate.isInited()) {
        if (prepared.isIfNotExists()) {
            return 0;
        }
        throw DbException.get(ErrorCode.TABLE_OR_VIEW_ALREADY_EXISTS_1, tableName);
    }
    Query query = prepared.getQuery();
    if (query != null) {
        query.prepare();
        if (prepared.getColumnCount() == 0) {
            generateColumnsFromQuery();
        } else if (prepared.getColumnCount() != query.getColumnCount()) {
            throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
        }
    }
    ArrayList<Sequence> sequences = New.arrayList();
    for (Column c : tableMate.getColumns()) {
        if (c.isAutoIncrement()) {
            c.convertAutoIncrementToSequence(session, database.getSchema(session.getCurrentSchemaName()), tableMate.getId(), prepared.isTemporary());
        }
        Sequence seq = c.getSequence();
        if (seq != null) {
            sequences.add(seq);
        }
    }
    try {
        for (Column c : tableMate.getColumns()) {
            c.prepareExpression(session);
        }
        for (Sequence sequence : sequences) {
            tableMate.addSequence(sequence);
        }
        for (DefineCommand command : prepared.getConstraintCommands()) {
            if (command.getType() == CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_REFERENTIAL) {
                AlterTableAddConstraint stmt = (AlterTableAddConstraint) command;
                String refTableName = stmt.getRefTableName();
                TableMate refTable = getTableMate(refTableName);
                if (refTable != null && refTable.getPartitionNode().length > 1) {
                    TableNode[] tableNodes = tableMate.getPartitionNode();
                    TableNode[] refTableNodes = refTable.getPartitionNode();
                    Map<TableNode, TableNode> symmetryRelation = getSymmetryRelation(tableNodes, refTableNodes);
                    if (symmetryRelation == null) {
                        throw DbException.get(ErrorCode.CHECK_CONSTRAINT_INVALID, "Create foreign key for table,the original table and the reference table should be symmetrical.");
                    }
                }
            }
        }
        TableNode[] nodes = tableMate.getPartitionNode();
        execute(nodes);
        if (query != null) {
            Insert insert = new Insert(session);
            insert.setSortedInsertMode(prepared.isSortedInsertMode());
            insert.setQuery(query);
            insert.setTable(tableMate);
            insert.setInsertFromSelect(true);
            insert.prepare();
            insert.update();
        }
    } catch (DbException e) {
        throw e;
    }
    tableMate.loadMataData(session);
    return 0;
}
Also used : AlterTableAddConstraint(com.wplatform.ddal.command.ddl.AlterTableAddConstraint) Query(com.wplatform.ddal.command.dml.Query) Sequence(com.wplatform.ddal.dbobject.schema.Sequence) Insert(com.wplatform.ddal.command.dml.Insert) DefineCommand(com.wplatform.ddal.command.ddl.DefineCommand) DbException(com.wplatform.ddal.message.DbException) IndexColumn(com.wplatform.ddal.dbobject.table.IndexColumn) Column(com.wplatform.ddal.dbobject.table.Column) TableNode(com.wplatform.ddal.dispatch.rule.TableNode) TableMate(com.wplatform.ddal.dbobject.table.TableMate)

Example 3 with Sequence

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

the class Function method getValueWithArgs.

private Value getValueWithArgs(Session session, Expression[] args) {
    Value[] values = new Value[args.length];
    if (info.nullIfParameterIsNull) {
        for (int i = 0; i < args.length; i++) {
            Expression e = args[i];
            Value v = e.getValue(session);
            if (v == ValueNull.INSTANCE) {
                return ValueNull.INSTANCE;
            }
            values[i] = v;
        }
    }
    Value v0 = getNullOrValue(session, args, values, 0);
    Value resultSimple = getSimpleValue(session, v0, args, values);
    if (resultSimple != null) {
        return resultSimple;
    }
    Value v1 = getNullOrValue(session, args, values, 1);
    Value v2 = getNullOrValue(session, args, values, 2);
    Value v3 = getNullOrValue(session, args, values, 3);
    Value v4 = getNullOrValue(session, args, values, 4);
    Value v5 = getNullOrValue(session, args, values, 5);
    Value result;
    switch(info.type) {
        case ATAN2:
            result = ValueDouble.get(Math.atan2(v0.getDouble(), v1.getDouble()));
            break;
        case BITAND:
            result = ValueLong.get(v0.getLong() & v1.getLong());
            break;
        case BITOR:
            result = ValueLong.get(v0.getLong() | v1.getLong());
            break;
        case BITXOR:
            result = ValueLong.get(v0.getLong() ^ v1.getLong());
            break;
        case MOD:
            {
                long x = v1.getLong();
                if (x == 0) {
                    throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL());
                }
                result = ValueLong.get(v0.getLong() % x);
                break;
            }
        case POWER:
            result = ValueDouble.get(Math.pow(v0.getDouble(), v1.getDouble()));
            break;
        case ROUND:
            {
                double f = v1 == null ? 1. : Math.pow(10., v1.getDouble());
                result = ValueDouble.get(Math.round(v0.getDouble() * f) / f);
                break;
            }
        case TRUNCATE:
            {
                if (v0.getType() == Value.TIMESTAMP) {
                    java.sql.Timestamp d = v0.getTimestamp();
                    Calendar c = Calendar.getInstance();
                    c.setTime(d);
                    c.set(Calendar.HOUR_OF_DAY, 0);
                    c.set(Calendar.MINUTE, 0);
                    c.set(Calendar.SECOND, 0);
                    c.set(Calendar.MILLISECOND, 0);
                    result = ValueTimestamp.fromMillis(c.getTimeInMillis());
                } else {
                    double d = v0.getDouble();
                    int p = v1 == null ? 0 : v1.getInt();
                    double f = Math.pow(10., p);
                    double g = d * f;
                    result = ValueDouble.get(((d < 0) ? Math.ceil(g) : Math.floor(g)) / f);
                }
                break;
            }
        case DIFFERENCE:
            result = ValueInt.get(getDifference(v0.getString(), v1.getString()));
            break;
        case INSERT:
            {
                if (v1 == ValueNull.INSTANCE || v2 == ValueNull.INSTANCE) {
                    result = v1;
                } else {
                    result = ValueString.get(insert(v0.getString(), v1.getInt(), v2.getInt(), v3.getString()), database.getMode().treatEmptyStringsAsNull);
                }
                break;
            }
        case LEFT:
            result = ValueString.get(left(v0.getString(), v1.getInt()), database.getMode().treatEmptyStringsAsNull);
            break;
        case LOCATE:
            {
                int start = v2 == null ? 0 : v2.getInt();
                result = ValueInt.get(locate(v0.getString(), v1.getString(), start));
                break;
            }
        case INSTR:
            {
                int start = v2 == null ? 0 : v2.getInt();
                result = ValueInt.get(locate(v1.getString(), v0.getString(), start));
                break;
            }
        case REPEAT:
            {
                int count = Math.max(0, v1.getInt());
                result = ValueString.get(repeat(v0.getString(), count), database.getMode().treatEmptyStringsAsNull);
                break;
            }
        case REPLACE:
            {
                String s0 = v0.getString();
                String s1 = v1.getString();
                String s2 = (v2 == null) ? "" : v2.getString();
                result = ValueString.get(replace(s0, s1, s2), database.getMode().treatEmptyStringsAsNull);
                break;
            }
        case RIGHT:
            result = ValueString.get(right(v0.getString(), v1.getInt()), database.getMode().treatEmptyStringsAsNull);
            break;
        case LTRIM:
            result = ValueString.get(StringUtils.trim(v0.getString(), true, false, v1 == null ? " " : v1.getString()), database.getMode().treatEmptyStringsAsNull);
            break;
        case TRIM:
            result = ValueString.get(StringUtils.trim(v0.getString(), true, true, v1 == null ? " " : v1.getString()), database.getMode().treatEmptyStringsAsNull);
            break;
        case RTRIM:
            result = ValueString.get(StringUtils.trim(v0.getString(), false, true, v1 == null ? " " : v1.getString()), database.getMode().treatEmptyStringsAsNull);
            break;
        case SUBSTR:
        case SUBSTRING:
            {
                String s = v0.getString();
                int offset = v1.getInt();
                if (offset < 0) {
                    offset = s.length() + offset + 1;
                }
                int length = v2 == null ? s.length() : v2.getInt();
                result = ValueString.get(substring(s, offset, length), database.getMode().treatEmptyStringsAsNull);
                break;
            }
        case POSITION:
            result = ValueInt.get(locate(v0.getString(), v1.getString(), 0));
            break;
        case XMLATTR:
            result = ValueString.get(StringUtils.xmlAttr(v0.getString(), v1.getString()), database.getMode().treatEmptyStringsAsNull);
            break;
        case XMLNODE:
            {
                String attr = v1 == null ? null : v1 == ValueNull.INSTANCE ? null : v1.getString();
                String content = v2 == null ? null : v2 == ValueNull.INSTANCE ? null : v2.getString();
                boolean indent = v3 == null ? true : v3.getBoolean();
                result = ValueString.get(StringUtils.xmlNode(v0.getString(), attr, content, indent), database.getMode().treatEmptyStringsAsNull);
                break;
            }
        case REGEXP_REPLACE:
            {
                String regexp = v1.getString();
                String replacement = v2.getString();
                try {
                    result = ValueString.get(v0.getString().replaceAll(regexp, replacement), database.getMode().treatEmptyStringsAsNull);
                } catch (StringIndexOutOfBoundsException e) {
                    throw DbException.get(ErrorCode.LIKE_ESCAPE_ERROR_1, e, replacement);
                } catch (PatternSyntaxException e) {
                    throw DbException.get(ErrorCode.LIKE_ESCAPE_ERROR_1, e, regexp);
                }
                break;
            }
        case RPAD:
            result = ValueString.get(StringUtils.pad(v0.getString(), v1.getInt(), v2 == null ? null : v2.getString(), true), database.getMode().treatEmptyStringsAsNull);
            break;
        case LPAD:
            result = ValueString.get(StringUtils.pad(v0.getString(), v1.getInt(), v2 == null ? null : v2.getString(), false), database.getMode().treatEmptyStringsAsNull);
            break;
        case TO_CHAR:
            switch(v0.getType()) {
                case Value.TIME:
                case Value.DATE:
                case Value.TIMESTAMP:
                    result = ValueString.get(toChar(v0.getTimestamp(), v1 == null ? null : v1.getString(), v2 == null ? null : v2.getString()), database.getMode().treatEmptyStringsAsNull);
                    break;
                case Value.SHORT:
                case Value.INT:
                case Value.LONG:
                case Value.DECIMAL:
                case Value.DOUBLE:
                case Value.FLOAT:
                    result = ValueString.get(toChar(v0.getBigDecimal(), v1 == null ? null : v1.getString(), v2 == null ? null : v2.getString()), database.getMode().treatEmptyStringsAsNull);
                    break;
                default:
                    result = ValueString.get(v0.getString(), database.getMode().treatEmptyStringsAsNull);
            }
            break;
        case TRANSLATE:
            {
                String matching = v1.getString();
                String replacement = v2.getString();
                result = ValueString.get(translate(v0.getString(), matching, replacement), database.getMode().treatEmptyStringsAsNull);
                break;
            }
        case H2VERSION:
            result = ValueString.get(Constants.getVersion(), database.getMode().treatEmptyStringsAsNull);
            break;
        case DATE_ADD:
            result = ValueTimestamp.get(dateadd(v0.getString(), v1.getInt(), v2.getTimestamp()));
            break;
        case DATE_DIFF:
            result = ValueLong.get(datediff(v0.getString(), v1.getTimestamp(), v2.getTimestamp()));
            break;
        case EXTRACT:
            {
                int field = getDatePart(v0.getString());
                result = ValueInt.get(DateTimeUtils.getDatePart(v1.getTimestamp(), field));
                break;
            }
        case FORMATDATETIME:
            {
                if (v0 == ValueNull.INSTANCE || v1 == ValueNull.INSTANCE) {
                    result = ValueNull.INSTANCE;
                } else {
                    String locale = v2 == null ? null : v2 == ValueNull.INSTANCE ? null : v2.getString();
                    String tz = v3 == null ? null : v3 == ValueNull.INSTANCE ? null : v3.getString();
                    result = ValueString.get(DateTimeUtils.formatDateTime(v0.getTimestamp(), v1.getString(), locale, tz), database.getMode().treatEmptyStringsAsNull);
                }
                break;
            }
        case PARSEDATETIME:
            {
                if (v0 == ValueNull.INSTANCE || v1 == ValueNull.INSTANCE) {
                    result = ValueNull.INSTANCE;
                } else {
                    String locale = v2 == null ? null : v2 == ValueNull.INSTANCE ? null : v2.getString();
                    String tz = v3 == null ? null : v3 == ValueNull.INSTANCE ? null : v3.getString();
                    java.util.Date d = DateTimeUtils.parseDateTime(v0.getString(), v1.getString(), locale, tz);
                    result = ValueTimestamp.fromMillis(d.getTime());
                }
                break;
            }
        case NULLIF:
            result = database.areEqual(v0, v1) ? ValueNull.INSTANCE : v0;
            break;
        // system
        case NEXTVAL:
            {
                Sequence sequence = getSequence(session, v0, v1);
                SequenceValue value = new SequenceValue(sequence);
                result = value.getValue(session);
                break;
            }
        case CURRVAL:
            {
                Sequence sequence = getSequence(session, v0, v1);
                result = ValueLong.get(sequence.getCurrentValue());
                break;
            }
        case CSVREAD:
            {
                String fileName = v0.getString();
                String columnList = v1 == null ? null : v1.getString();
                Csv csv = new Csv();
                String options = v2 == null ? null : v2.getString();
                String charset = null;
                if (options != null && options.indexOf('=') >= 0) {
                    charset = csv.setOptions(options);
                } else {
                    charset = options;
                    String fieldSeparatorRead = v3 == null ? null : v3.getString();
                    String fieldDelimiter = v4 == null ? null : v4.getString();
                    String escapeCharacter = v5 == null ? null : v5.getString();
                    Value v6 = getNullOrValue(session, args, values, 6);
                    String nullString = v6 == null ? null : v6.getString();
                    setCsvDelimiterEscape(csv, fieldSeparatorRead, fieldDelimiter, escapeCharacter);
                    csv.setNullString(nullString);
                }
                char fieldSeparator = csv.getFieldSeparatorRead();
                String[] columns = StringUtils.arraySplit(columnList, fieldSeparator, true);
                try {
                    ValueResultSet vr = ValueResultSet.get(csv.read(fileName, columns, charset));
                    result = vr;
                } catch (SQLException e) {
                    throw DbException.convert(e);
                }
                break;
            }
        case CSVWRITE:
            {
                session.getUser().checkAdmin();
                Connection conn = session.createConnection(false);
                Csv csv = new Csv();
                String options = v2 == null ? null : v2.getString();
                String charset = null;
                if (options != null && options.indexOf('=') >= 0) {
                    charset = csv.setOptions(options);
                } else {
                    charset = options;
                    String fieldSeparatorWrite = v3 == null ? null : v3.getString();
                    String fieldDelimiter = v4 == null ? null : v4.getString();
                    String escapeCharacter = v5 == null ? null : v5.getString();
                    Value v6 = getNullOrValue(session, args, values, 6);
                    String nullString = v6 == null ? null : v6.getString();
                    Value v7 = getNullOrValue(session, args, values, 7);
                    String lineSeparator = v7 == null ? null : v7.getString();
                    setCsvDelimiterEscape(csv, fieldSeparatorWrite, fieldDelimiter, escapeCharacter);
                    csv.setNullString(nullString);
                    if (lineSeparator != null) {
                        csv.setLineSeparator(lineSeparator);
                    }
                }
                try {
                    int rows = csv.write(conn, v0.getString(), v1.getString(), charset);
                    result = ValueInt.get(rows);
                } catch (SQLException e) {
                    throw DbException.convert(e);
                }
                break;
            }
        case SET:
            {
                Variable var = (Variable) args[0];
                session.setVariable(var.getName(), v1);
                result = v1;
                break;
            }
        case TRUNCATE_VALUE:
            {
                result = v0.convertPrecision(v1.getLong(), v2.getBoolean());
                break;
            }
        case XMLTEXT:
            if (v1 == null) {
                result = ValueString.get(StringUtils.xmlText(v0.getString()), database.getMode().treatEmptyStringsAsNull);
            } else {
                result = ValueString.get(StringUtils.xmlText(v0.getString(), v1.getBoolean()), database.getMode().treatEmptyStringsAsNull);
            }
            break;
        case VALUES:
            result = session.getVariable(args[0].getSchemaName() + "." + args[0].getTableName() + "." + args[0].getColumnName());
            break;
        case EXPAND:
        case DATABASE:
        case DATABASE_PATH:
        case HASH:
        case ENCRYPT:
        case DECRYPT:
        case COMPRESS:
        case FILE_READ:
        case LINK_SCHEMA:
            throw DbException.throwInternalError("function unimplemented. tyle=" + info.type);
        default:
            throw DbException.throwInternalError("type=" + info.type);
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) Csv(com.wplatform.ddal.result.Csv) Connection(java.sql.Connection) Sequence(com.wplatform.ddal.dbobject.schema.Sequence) Timestamp(java.sql.Timestamp) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 4 with Sequence

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

the class Parser method readTerm.

private Expression readTerm() {
    Expression r;
    switch(currentTokenType) {
        case AT:
            read();
            r = new Variable(session, readAliasIdentifier());
            if (readIf(":=")) {
                Expression value = readExpression();
                Function function = Function.getFunction(database, "SET");
                function.setParameter(0, r);
                function.setParameter(1, value);
                r = function;
            }
            break;
        case PARAMETER:
            // there must be no space between ? and the number
            boolean indexed = Character.isDigit(sqlCommandChars[parseIndex]);
            read();
            Parameter p;
            if (indexed && currentTokenType == VALUE && currentValue.getType() == Value.INT) {
                if (indexedParameterList == null) {
                    if (parameters == null) {
                        // example check constraints)
                        throw getSyntaxError();
                    } else if (parameters.size() > 0) {
                        throw DbException.get(ErrorCode.CANNOT_MIX_INDEXED_AND_UNINDEXED_PARAMS);
                    }
                    indexedParameterList = New.arrayList();
                }
                int index = currentValue.getInt() - 1;
                if (index < 0 || index >= Constants.MAX_PARAMETER_INDEX) {
                    throw DbException.getInvalidValueException("parameter index", index);
                }
                if (indexedParameterList.size() <= index) {
                    indexedParameterList.ensureCapacity(index + 1);
                    while (indexedParameterList.size() <= index) {
                        indexedParameterList.add(null);
                    }
                }
                p = indexedParameterList.get(index);
                if (p == null) {
                    p = new Parameter(index);
                    indexedParameterList.set(index, p);
                }
                read();
            } else {
                if (indexedParameterList != null) {
                    throw DbException.get(ErrorCode.CANNOT_MIX_INDEXED_AND_UNINDEXED_PARAMS);
                }
                p = new Parameter(parameters.size());
            }
            parameters.add(p);
            r = p;
            break;
        case KEYWORD:
            if (isToken("SELECT") || isToken("FROM")) {
                Query query = parseSelect();
                r = new Subquery(query);
            } else {
                throw getSyntaxError();
            }
            break;
        case IDENTIFIER:
            String name = currentToken;
            if (currentTokenQuoted) {
                read();
                if (readIf("(")) {
                    r = readFunction(null, name);
                } else if (readIf(".")) {
                    r = readTermObjectDot(name);
                } else {
                    r = new ExpressionColumn(database, null, null, name);
                }
            } else {
                read();
                if (readIf(".")) {
                    r = readTermObjectDot(name);
                } else if (equalsToken("CASE", name)) {
                    // CASE must be processed before (,
                    // otherwise CASE(3) would be a function call, which it is
                    // not
                    r = readCase();
                } else if (readIf("(")) {
                    r = readFunction(null, name);
                } else if (equalsToken("CURRENT_USER", name)) {
                    r = readFunctionWithoutParameters("USER");
                } else if (equalsToken("CURRENT", name)) {
                    if (readIf("TIMESTAMP")) {
                        r = readFunctionWithoutParameters("CURRENT_TIMESTAMP");
                    } else if (readIf("TIME")) {
                        r = readFunctionWithoutParameters("CURRENT_TIME");
                    } else if (readIf("DATE")) {
                        r = readFunctionWithoutParameters("CURRENT_DATE");
                    } else {
                        r = new ExpressionColumn(database, null, null, name);
                    }
                } else if (equalsToken("NEXT", name) && readIf("VALUE")) {
                    read("FOR");
                    Sequence sequence = readSequence();
                    r = new SequenceValue(sequence);
                } else if (currentTokenType == VALUE && currentValue.getType() == Value.STRING) {
                    if (equalsToken("DATE", name) || equalsToken("D", name)) {
                        String date = currentValue.getString();
                        read();
                        r = ValueExpression.get(ValueDate.parse(date));
                    } else if (equalsToken("TIME", name) || equalsToken("T", name)) {
                        String time = currentValue.getString();
                        read();
                        r = ValueExpression.get(ValueTime.parse(time));
                    } else if (equalsToken("TIMESTAMP", name) || equalsToken("TS", name)) {
                        String timestamp = currentValue.getString();
                        read();
                        r = ValueExpression.get(ValueTimestamp.parse(timestamp));
                    } else if (equalsToken("X", name)) {
                        read();
                        byte[] buffer = StringUtils.convertHexToBytes(currentValue.getString());
                        r = ValueExpression.get(ValueBytes.getNoCopy(buffer));
                    } else if (equalsToken("E", name)) {
                        String text = currentValue.getString();
                        // the PostgreSQL ODBC driver uses
                        // LIKE E'PROJECT\\_DATA' instead of LIKE
                        // 'PROJECT\_DATA'
                        // N: SQL-92 "National Language" strings
                        text = StringUtils.replaceAll(text, "\\\\", "\\");
                        read();
                        r = ValueExpression.get(ValueString.get(text));
                    } else if (equalsToken("N", name)) {
                        // SQL-92 "National Language" strings
                        String text = currentValue.getString();
                        read();
                        r = ValueExpression.get(ValueString.get(text));
                    } else {
                        r = new ExpressionColumn(database, null, null, name);
                    }
                } else {
                    r = new ExpressionColumn(database, null, null, name);
                }
            }
            break;
        case MINUS:
            read();
            if (currentTokenType == VALUE) {
                r = ValueExpression.get(currentValue.negate());
                if (r.getType() == Value.LONG && r.getValue(session).getLong() == Integer.MIN_VALUE) {
                    // convert Integer.MIN_VALUE to type 'int'
                    // (Integer.MAX_VALUE+1 is of type 'long')
                    r = ValueExpression.get(ValueInt.get(Integer.MIN_VALUE));
                } else if (r.getType() == Value.DECIMAL && r.getValue(session).getBigDecimal().compareTo(ValueLong.MIN_BD) == 0) {
                    // convert Long.MIN_VALUE to type 'long'
                    // (Long.MAX_VALUE+1 is of type 'decimal')
                    r = ValueExpression.get(ValueLong.get(Long.MIN_VALUE));
                }
                read();
            } else {
                r = new Operation(Operation.NEGATE, readTerm(), null);
            }
            break;
        case PLUS:
            read();
            r = readTerm();
            break;
        case OPEN:
            read();
            if (readIf(")")) {
                r = new ExpressionList(new Expression[0]);
            } else {
                r = readExpression();
                if (readIf(",")) {
                    ArrayList<Expression> list = New.arrayList();
                    list.add(r);
                    while (!readIf(")")) {
                        r = readExpression();
                        list.add(r);
                        if (!readIf(",")) {
                            read(")");
                            break;
                        }
                    }
                    Expression[] array = new Expression[list.size()];
                    list.toArray(array);
                    r = new ExpressionList(array);
                } else {
                    read(")");
                }
            }
            break;
        case TRUE:
            read();
            r = ValueExpression.get(ValueBoolean.get(true));
            break;
        case FALSE:
            read();
            r = ValueExpression.get(ValueBoolean.get(false));
            break;
        case CURRENT_TIME:
            read();
            r = readFunctionWithoutParameters("CURRENT_TIME");
            break;
        case CURRENT_DATE:
            read();
            r = readFunctionWithoutParameters("CURRENT_DATE");
            break;
        case CURRENT_TIMESTAMP:
            {
                Function function = Function.getFunction(database, "CURRENT_TIMESTAMP");
                read();
                if (readIf("(")) {
                    if (!readIf(")")) {
                        function.setParameter(0, readExpression());
                        read(")");
                    }
                }
                function.doneWithParameters();
                r = function;
                break;
            }
        case ROWNUM:
            read();
            if (readIf("(")) {
                read(")");
            }
            r = new Rownum(currentSelect == null ? currentPrepared : currentSelect);
            break;
        case NULL:
            read();
            r = ValueExpression.getNull();
            break;
        case VALUE:
            r = ValueExpression.get(currentValue);
            read();
            break;
        default:
            throw getSyntaxError();
    }
    if (readIf("[")) {
        Function function = Function.getFunction(database, "ARRAY_GET");
        function.setParameter(0, r);
        r = readExpression();
        r = new Operation(Operation.PLUS, r, ValueExpression.get(ValueInt.get(1)));
        function.setParameter(1, r);
        r = function;
        read("]");
    }
    if (readIf("::")) {
        // PostgreSQL compatibility
        if (isToken("PG_CATALOG")) {
            read("PG_CATALOG");
            read(".");
        }
        if (readIf("REGCLASS")) {
            FunctionAlias f = findFunctionAlias(Constants.SCHEMA_MAIN, "PG_GET_OID");
            if (f == null) {
                throw getSyntaxError();
            }
            Expression[] args = { r };
            JavaFunction func = new JavaFunction(f, args);
            r = func;
        } else {
            Column col = parseColumnWithType(null);
            Function function = Function.getFunction(database, "CAST");
            function.setDataType(col);
            function.setParameter(0, r);
            r = function;
        }
    }
    return r;
}
Also used : FunctionAlias(com.wplatform.ddal.dbobject.FunctionAlias) Sequence(com.wplatform.ddal.dbobject.schema.Sequence)

Example 5 with Sequence

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

the class Parser method parseAlterSequence.

private AlterSequence parseAlterSequence() {
    String sequenceName = readIdentifierWithSchema();
    Sequence sequence = getSchema().getSequence(sequenceName);
    AlterSequence command = new AlterSequence(session, sequence.getSchema());
    command.setSequence(sequence);
    while (true) {
        if (readIf("RESTART")) {
            read("WITH");
            command.setStartWith(readExpression());
        } else if (readIf("INCREMENT")) {
            read("BY");
            command.setIncrement(readExpression());
        } else if (readIf("MINVALUE")) {
            command.setMinValue(readExpression());
        } else if (readIf("NOMINVALUE")) {
            command.setMinValue(null);
        } else if (readIf("MAXVALUE")) {
            command.setMaxValue(readExpression());
        } else if (readIf("NOMAXVALUE")) {
            command.setMaxValue(null);
        } else if (readIf("CYCLE")) {
            command.setCycle(true);
        } else if (readIf("NOCYCLE")) {
            command.setCycle(false);
        } else if (readIf("NO")) {
            if (readIf("MINVALUE")) {
                command.setMinValue(null);
            } else if (readIf("MAXVALUE")) {
                command.setMaxValue(null);
            } else if (readIf("CYCLE")) {
                command.setCycle(false);
            } else if (readIf("CACHE")) {
                command.setCacheSize(ValueExpression.get(ValueLong.get(1)));
            } else {
                break;
            }
        } else if (readIf("CACHE")) {
            command.setCacheSize(readExpression());
        } else if (readIf("NOCACHE")) {
            command.setCacheSize(ValueExpression.get(ValueLong.get(1)));
        } else {
            break;
        }
    }
    return command;
}
Also used : Sequence(com.wplatform.ddal.dbobject.schema.Sequence)

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