Search in sources :

Example 16 with Column

use of com.wplatform.ddal.dbobject.table.Column 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 17 with Column

use of com.wplatform.ddal.dbobject.table.Column in project jdbc-shards by wplatform.

the class CreateTableExecutor method generateColumnsFromQuery.

private void generateColumnsFromQuery() {
    int columnCount = prepared.getQuery().getColumnCount();
    ArrayList<Expression> expressions = prepared.getQuery().getExpressions();
    for (int i = 0; i < columnCount; i++) {
        Expression expr = expressions.get(i);
        int type = expr.getType();
        String name = expr.getAlias();
        long precision = expr.getPrecision();
        int displaySize = expr.getDisplaySize();
        DataType dt = DataType.getDataType(type);
        if (precision > 0 && (dt.defaultPrecision == 0 || (dt.defaultPrecision > precision && dt.defaultPrecision < Byte.MAX_VALUE))) {
            // dont' set precision to MAX_VALUE if this is the default
            precision = dt.defaultPrecision;
        }
        int scale = expr.getScale();
        if (scale > 0 && (dt.defaultScale == 0 || (dt.defaultScale > scale && dt.defaultScale < precision))) {
            scale = dt.defaultScale;
        }
        if (scale > precision) {
            precision = scale;
        }
        Column col = new Column(name, type, precision, scale, displaySize);
        prepared.addColumn(col);
    }
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression) IndexColumn(com.wplatform.ddal.dbobject.table.IndexColumn) Column(com.wplatform.ddal.dbobject.table.Column) DataType(com.wplatform.ddal.value.DataType) AlterTableAddConstraint(com.wplatform.ddal.command.ddl.AlterTableAddConstraint)

Example 18 with Column

use of com.wplatform.ddal.dbobject.table.Column in project jdbc-shards by wplatform.

the class RoutingHandlerImpl method getRuleColumnArgs.

private Map<String, List<Value>> getRuleColumnArgs(TableMate table, SearchRow row) {
    Map<String, List<Value>> args = New.hashMap();
    TableRouter tableRouter = table.getTableRouter();
    for (RuleColumn ruleCol : tableRouter.getRuleColumns()) {
        Column[] columns = table.getColumns();
        Column matched = null;
        for (Column column : columns) {
            String colName = column.getName();
            if (colName.equalsIgnoreCase(ruleCol.getName())) {
                matched = column;
                break;
            }
        }
        if (matched == null) {
            throw DbException.getInvalidValueException("RuleColumn", ruleCol);
        }
        Value value = row.getValue(matched.getColumnId());
        if (value != null && value != ValueNull.INSTANCE) {
            List<Value> values = New.arrayList(1);
            values.add(value);
            args.put(ruleCol.getName(), values);
        }
    }
    return args;
}
Also used : Column(com.wplatform.ddal.dbobject.table.Column) Value(com.wplatform.ddal.value.Value) List(java.util.List)

Example 19 with Column

use of com.wplatform.ddal.dbobject.table.Column in project jdbc-shards by wplatform.

the class RoutingHandlerImpl method exportRangeArg.

/**
     * @param table
     * @param first
     * @param last
     * @param routingArgs
     */
private void exportRangeArg(TableMate table, SearchRow first, SearchRow last, Map<String, List<Value>> routingArgs) {
    TableRouter tr = table.getTableRouter();
    List<RuleColumn> ruleCols = tr.getRuleColumns();
    if (first != null && last != null) {
        for (int i = 0; first != null && i < first.getColumnCount(); i++) {
            Value firstV = first.getValue(i);
            Value listV = last.getValue(i);
            if (firstV == null || listV == null || firstV == ValueNull.INSTANCE || listV == ValueNull.INSTANCE) {
                continue;
            }
            Column col = table.getColumn(i);
            String colName = col.getName();
            RuleColumn matched = null;
            for (RuleColumn ruleColumn : ruleCols) {
                if (colName.equalsIgnoreCase(ruleColumn.getName())) {
                    matched = ruleColumn;
                }
            }
            if (matched == null) {
                continue;
            }
            List<Value> values = routingArgs.get(matched.getName());
            if (values == null) {
                values = New.arrayList();
                routingArgs.put(matched.getName(), values);
            }
            int compare = database.compare(firstV, listV);
            if (compare == 0) {
                values.add(firstV);
            } else if (compare < 0) {
                List<Value> enumValue = enumRange(firstV, listV);
                if (enumValue != null) {
                    values.addAll(enumValue);
                }
            } else {
                throw new TableRoutingException(table.getName() + " routing error. The conidition " + matched.getName() + " is alwarys false.");
            }
        }
    }
}
Also used : Column(com.wplatform.ddal.dbobject.table.Column) Value(com.wplatform.ddal.value.Value) List(java.util.List)

Example 20 with Column

use of com.wplatform.ddal.dbobject.table.Column in project jdbc-shards by wplatform.

the class TableFunction method getTable.

private ValueResultSet getTable(Session session, Expression[] argList, boolean onlyColumnList, boolean distinctRows) {
    int len = columnList.length;
    Expression[] header = new Expression[len];
    Database db = session.getDatabase();
    for (int i = 0; i < len; i++) {
        Column c = columnList[i];
        ExpressionColumn col = new ExpressionColumn(db, c);
        header[i] = col;
    }
    LocalResult result = new LocalResult(session, header, len);
    if (distinctRows) {
        result.setDistinct();
    }
    if (!onlyColumnList) {
        Value[][] list = new Value[len][];
        int rows = 0;
        for (int i = 0; i < len; i++) {
            Value v = argList[i].getValue(session);
            if (v == ValueNull.INSTANCE) {
                list[i] = new Value[0];
            } else {
                ValueArray array = (ValueArray) v.convertTo(Value.ARRAY);
                Value[] l = array.getList();
                list[i] = l;
                rows = Math.max(rows, l.length);
            }
        }
        for (int row = 0; row < rows; row++) {
            Value[] r = new Value[len];
            for (int j = 0; j < len; j++) {
                Value[] l = list[j];
                Value v;
                if (l.length <= row) {
                    v = ValueNull.INSTANCE;
                } else {
                    Column c = columnList[j];
                    v = l[row];
                    v = c.convert(v);
                    v = v.convertPrecision(c.getPrecision(), false);
                    v = v.convertScale(true, c.getScale());
                }
                r[j] = v;
            }
            result.addRow(r);
        }
    }
    result.done();
    ValueResultSet vr = ValueResultSet.get(getSimpleResultSet(result, Integer.MAX_VALUE));
    return vr;
}
Also used : LocalResult(com.wplatform.ddal.result.LocalResult) Column(com.wplatform.ddal.dbobject.table.Column) Database(com.wplatform.ddal.engine.Database)

Aggregations

Column (com.wplatform.ddal.dbobject.table.Column)38 Expression (com.wplatform.ddal.command.expression.Expression)15 Value (com.wplatform.ddal.value.Value)14 TableMate (com.wplatform.ddal.dbobject.table.TableMate)12 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)8 DbException (com.wplatform.ddal.message.DbException)7 Row (com.wplatform.ddal.result.Row)6 SearchRow (com.wplatform.ddal.result.SearchRow)6 Query (com.wplatform.ddal.command.dml.Query)4 IndexColumn (com.wplatform.ddal.dbobject.table.IndexColumn)4 ResultInterface (com.wplatform.ddal.result.ResultInterface)4 AlterTableAddConstraint (com.wplatform.ddal.command.ddl.AlterTableAddConstraint)3 ValueExpression (com.wplatform.ddal.command.expression.ValueExpression)3 Index (com.wplatform.ddal.dbobject.index.Index)3 Table (com.wplatform.ddal.dbobject.table.Table)3 TableFilter (com.wplatform.ddal.dbobject.table.TableFilter)3 TableNode (com.wplatform.ddal.dispatch.rule.TableNode)3 Database (com.wplatform.ddal.engine.Database)3 List (java.util.List)3 Prepared (com.wplatform.ddal.command.Prepared)2