Search in sources :

Example 1 with TableNode

use of com.wplatform.ddal.dispatch.rule.TableNode in project jdbc-shards by wplatform.

the class XmlRuleConfigParser method parsePartition.

private void parsePartition(TableRouter tableRouter, List<XNode> list) {
    List<TableNode> tableNodes = New.arrayList();
    for (XNode xNode : list) {
        String shard = xNode.getStringAttribute("shard");
        String suffix = xNode.getStringAttribute("suffix");
        shard = shard == null ? null : shard.trim();
        suffix = suffix == null ? null : suffix.trim();
        if (StringUtils.isNullOrEmpty(shard)) {
            throw new ParsingException("Error parsing ddal-rule XML. Cause: " + "the shard attribute of 'table' element is required.");
        }
        List<String> shards = collectItems(shard);
        List<String> suffixes = collectItems(suffix);
        if (suffixes.isEmpty()) {
            for (String shardItem : shards) {
                TableNode node = new TableNode(shardItem, null, null);
                if (tableNodes.contains(node)) {
                    throw new ParsingException("Duplicate " + node + " defined in " + tableRouter.getId() + "'s partition");
                }
                tableNodes.add(node);
            }
        } else {
            for (String shardItem : shards) {
                for (String suffixItem : suffixes) {
                    TableNode node = new TableNode(shardItem, null, suffixItem);
                    if (tableNodes.contains(node)) {
                        throw new ParsingException("Duplicate " + node + " defined in " + tableRouter.getId() + "'s partition");
                    }
                    tableNodes.add(node);
                }
            }
        }
    }
    tableRouter.setPartition(tableNodes);
}
Also used : TableNode(com.wplatform.ddal.dispatch.rule.TableNode)

Example 2 with TableNode

use of com.wplatform.ddal.dispatch.rule.TableNode in project jdbc-shards by wplatform.

the class DefineCommandExecutor method getSymmetryRelation.

protected static Map<TableNode, TableNode> getSymmetryRelation(TableNode[] n1, TableNode[] n2) {
    if (n1.length != n2.length) {
        return null;
    }
    Map<TableNode, TableNode> tableNode = New.hashMap();
    for (TableNode tn1 : n1) {
        String sName = tn1.getShardName();
        String suffix = tn1.getSuffix();
        TableNode matched = null;
        for (TableNode tn2 : n2) {
            if (!sName.equals(tn2.getShardName())) {
                continue;
            }
            if (suffix != null && !suffix.equals(tn2.getSuffix())) {
                continue;
            }
            matched = tn2;
        }
        if (matched == null) {
            return null;
        }
        tableNode.put(tn1, matched);
    }
    if (tableNode.size() != n1.length) {
        return null;
    }
    return tableNode;
}
Also used : TableNode(com.wplatform.ddal.dispatch.rule.TableNode)

Example 3 with TableNode

use of com.wplatform.ddal.dispatch.rule.TableNode in project jdbc-shards by wplatform.

the class AlterTableAddConstraintExecutor method executeUpdate.

@Override
public int executeUpdate() {
    String tableName = prepared.getTableName();
    TableMate table = getTableMate(tableName);
    session.getUser().checkRight(table, Right.ALL);
    TableNode[] tableNodes = table.getPartitionNode();
    int type = prepared.getType();
    switch(type) {
        case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_REFERENTIAL:
            {
                String refTableName = prepared.getRefTableName();
                TableMate refTable = getTableMate(refTableName);
                TableNode[] refTableNode = table.getPartitionNode();
                Map<TableNode, TableNode> symmetryRelation = getSymmetryRelation(tableNodes, refTableNode);
                if (symmetryRelation == null) {
                    throw DbException.get(ErrorCode.CHECK_CONSTRAINT_INVALID, "The original table and reference table should be symmetrical.");
                }
                session.getUser().checkRight(refTable, Right.ALL);
            }
        case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY:
        case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_UNIQUE:
        case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_CHECK:
            {
                execute(tableNodes);
                break;
            }
        default:
            throw DbException.throwInternalError("type=" + type);
    }
    return 0;
}
Also used : TableNode(com.wplatform.ddal.dispatch.rule.TableNode) TableMate(com.wplatform.ddal.dbobject.table.TableMate) Map(java.util.Map) AlterTableAddConstraint(com.wplatform.ddal.command.ddl.AlterTableAddConstraint)

Example 4 with TableNode

use of com.wplatform.ddal.dispatch.rule.TableNode 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 5 with TableNode

use of com.wplatform.ddal.dispatch.rule.TableNode in project jdbc-shards by wplatform.

the class TableMate method isNodeMatch.

private static boolean isNodeMatch(TableNode[] nodes1, TableNode[] nodes2) {
    TableNode[] group1 = RoutingResult.fixedResult(nodes1).group();
    TableNode[] group2 = RoutingResult.fixedResult(nodes2).group();
    for (TableNode tableNode1 : group1) {
        boolean isMatched = false;
        for (TableNode tableNode2 : group2) {
            if (tableNode1.getShardName().equals(tableNode2.getShardName())) {
                isMatched = true;
                break;
            }
        }
        if (!isMatched) {
            return false;
        }
    }
    return true;
}
Also used : TableNode(com.wplatform.ddal.dispatch.rule.TableNode)

Aggregations

TableNode (com.wplatform.ddal.dispatch.rule.TableNode)15 TableMate (com.wplatform.ddal.dbobject.table.TableMate)7 Map (java.util.Map)4 AlterTableAddConstraint (com.wplatform.ddal.command.ddl.AlterTableAddConstraint)3 Column (com.wplatform.ddal.dbobject.table.Column)3 JdbcWorker (com.wplatform.ddal.excutor.JdbcWorker)3 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)3 Value (com.wplatform.ddal.value.Value)3 DefineCommand (com.wplatform.ddal.command.ddl.DefineCommand)2 IndexColumn (com.wplatform.ddal.dbobject.table.IndexColumn)2 DbException (com.wplatform.ddal.message.DbException)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2 AlterTableAlterColumn (com.wplatform.ddal.command.ddl.AlterTableAlterColumn)1 CreateIndex (com.wplatform.ddal.command.ddl.CreateIndex)1 Insert (com.wplatform.ddal.command.dml.Insert)1 Query (com.wplatform.ddal.command.dml.Query)1 Parameter (com.wplatform.ddal.command.expression.Parameter)1 TableConfig (com.wplatform.ddal.config.TableConfig)1 IndexMate (com.wplatform.ddal.dbobject.index.IndexMate)1