Search in sources :

Example 11 with TableNode

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

the class TruncateTableExecutor method executeUpdate.

@Override
public int executeUpdate() {
    TableMate table = castTableMate(prepared.getTable());
    session.commit(true);
    session.getUser().checkRight(table, Right.DELETE);
    TableNode[] nodes = table.getPartitionNode();
    execute(nodes);
    return 0;
}
Also used : TableNode(com.wplatform.ddal.dispatch.rule.TableNode) TableMate(com.wplatform.ddal.dbobject.table.TableMate)

Example 12 with TableNode

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

the class AlterTableAddConstraintExecutor method doTranslate.

@Override
protected String doTranslate(TableNode tableNode) {
    String tableName = prepared.getTableName();
    TableMate table = getTableMate(tableName);
    String forTable = tableNode.getCompositeObjectName();
    IndexColumn.mapColumns(prepared.getIndexColumns(), table);
    switch(prepared.getType()) {
        case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY:
            {
                return doBuildUnique(forTable, AlterTableAddConstraint.PRIMARY_KEY);
            }
        case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_UNIQUE:
            {
                String uniqueType = AlterTableAddConstraint.UNIQUE + " KEY";
                return doBuildUnique(forTable, uniqueType);
            }
        case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_CHECK:
            {
                /*
             * MySQL. The CHECK clause is parsed but ignored by all storage
             * engines.
             */
                return doBuildCheck(forTable);
            }
        case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_REFERENTIAL:
            {
                /*
             * MySQL. The FOREIGN KEY and REFERENCES clauses are supported by
             * the InnoDB and NDB storage engines
             */
                String refTableName = prepared.getRefTableName();
                TableMate refTable = getTableMate(refTableName);
                Map<TableNode, TableNode> symmetryRelation = getSymmetryRelation(table.getPartitionNode(), refTable.getPartitionNode());
                TableNode relation = symmetryRelation.get(tableNode);
                if (relation == null) {
                    throw DbException.get(ErrorCode.CHECK_CONSTRAINT_INVALID, "The original table and reference table should be symmetrical.");
                }
                return doBuildReferences(forTable, relation.getCompositeObjectName());
            }
        default:
            throw DbException.throwInternalError("type=" + prepared.getType());
    }
}
Also used : TableNode(com.wplatform.ddal.dispatch.rule.TableNode) TableMate(com.wplatform.ddal.dbobject.table.TableMate) Map(java.util.Map)

Example 13 with TableNode

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

the class AlterTableAlterColumnExecutor method executeUpdate.

@Override
public int executeUpdate() {
    Table parseTable = prepared.getTable();
    if (!(parseTable instanceof TableMate)) {
        DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, parseTable.getSQL());
    }
    TableMate table = (TableMate) parseTable;
    session.getUser().checkRight(table, Right.ALL);
    TableNode[] tableNodes = table.getPartitionNode();
    Column oldColumn = prepared.getOldColumn();
    int type = prepared.getType();
    switch(type) {
        case CommandInterface.ALTER_TABLE_ALTER_COLUMN_NOT_NULL:
            {
                if (!oldColumn.isNullable()) {
                    break;
                }
            }
        case CommandInterface.ALTER_TABLE_ALTER_COLUMN_NULL:
            {
                if (oldColumn.isNullable()) {
                    break;
                }
            }
        case CommandInterface.ALTER_TABLE_ALTER_COLUMN_DEFAULT:
        case CommandInterface.ALTER_TABLE_ALTER_COLUMN_CHANGE_TYPE:
        case CommandInterface.ALTER_TABLE_ADD_COLUMN:
        case CommandInterface.ALTER_TABLE_DROP_COLUMN:
            {
                execute(tableNodes);
                table.loadMataData(session);
                break;
            }
        case CommandInterface.ALTER_TABLE_ALTER_COLUMN_SELECTIVITY:
            {
                // not supported.
                return 0;
            }
        default:
            throw DbException.throwInternalError("type=" + type);
    }
    return 0;
}
Also used : Table(com.wplatform.ddal.dbobject.table.Table) AlterTableAlterColumn(com.wplatform.ddal.command.ddl.AlterTableAlterColumn) Column(com.wplatform.ddal.dbobject.table.Column) TableNode(com.wplatform.ddal.dispatch.rule.TableNode) TableMate(com.wplatform.ddal.dbobject.table.TableMate)

Example 14 with TableNode

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

the class CreateTableExecutor method doTranslate.

@Override
protected String doTranslate(TableNode tableNode) {
    StatementBuilder buff = new StatementBuilder("CREATE ");
    if (prepared.isTemporary()) {
        buff.append("TEMPORARY ");
    }
    buff.append("TABLE ");
    if (prepared.isIfNotExists()) {
        buff.append("IF NOT EXISTS ");
    }
    buff.append(identifier(tableNode.getCompositeObjectName()));
    if (prepared.getComment() != null) {
    // buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(prepared.getComment()));
    }
    buff.append("(");
    for (Column column : prepared.getColumns()) {
        buff.appendExceptFirst(", ");
        buff.append(column.getCreateSQL());
    }
    for (DefineCommand command : prepared.getConstraintCommands()) {
        buff.appendExceptFirst(", ");
        int type = command.getType();
        switch(type) {
            case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY:
                {
                    AlterTableAddConstraint stmt = (AlterTableAddConstraint) command;
                    buff.append(" CONSTRAINT PRIMARY KEY");
                    if (stmt.isPrimaryKeyHash()) {
                        buff.append(" USING HASH");
                    }
                    buff.resetCount();
                    buff.append("(");
                    for (IndexColumn c : stmt.getIndexColumns()) {
                        buff.appendExceptFirst(", ");
                        buff.append(identifier(c.columnName));
                    }
                    buff.append(")");
                    break;
                }
            case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_UNIQUE:
                {
                    AlterTableAddConstraint stmt = (AlterTableAddConstraint) command;
                    buff.append(" CONSTRAINT UNIQUE KEY");
                    buff.resetCount();
                    buff.append("(");
                    for (IndexColumn c : stmt.getIndexColumns()) {
                        buff.appendExceptFirst(", ");
                        buff.append(identifier(c.columnName));
                    }
                    buff.append(")");
                    break;
                }
            case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_CHECK:
                {
                    AlterTableAddConstraint stmt = (AlterTableAddConstraint) command;
                    String enclose = StringUtils.enclose(stmt.getCheckExpression().getSQL());
                    buff.append(" CHECK").append(enclose);
                    break;
                }
            case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_REFERENTIAL:
                {
                    AlterTableAddConstraint stmt = (AlterTableAddConstraint) command;
                    String refTableName = stmt.getRefTableName();
                    TableMate table = getTableMate(stmt.getTableName());
                    TableMate refTable = getTableMate(refTableName);
                    if (refTable != null) {
                        TableNode[] partitionNode = refTable.getPartitionNode();
                        if (partitionNode.length > 1) {
                            TableNode[] tableNodes = table.getPartitionNode();
                            TableNode[] refTableNodes = partitionNode;
                            Map<TableNode, TableNode> symmetryRelation = getSymmetryRelation(tableNodes, refTableNodes);
                            TableNode relation = symmetryRelation.get(tableNode);
                            if (relation == null) {
                                throw DbException.get(ErrorCode.CHECK_CONSTRAINT_INVALID, "The original table and reference table should be symmetrical.");
                            }
                            refTableName = relation.getCompositeObjectName();
                        } else if (partitionNode.length == 1) {
                            refTableName = partitionNode[0].getCompositeObjectName();
                        }
                    }
                    IndexColumn[] cols = stmt.getIndexColumns();
                    IndexColumn[] refCols = stmt.getRefIndexColumns();
                    buff.resetCount();
                    buff.append(" CONSTRAINT FOREIGN KEY(");
                    for (IndexColumn c : cols) {
                        buff.appendExceptFirst(", ");
                        buff.append(c.columnName);
                    }
                    buff.append(")");
                    buff.append(" REFERENCES ");
                    buff.append(identifier(refTableName)).append("(");
                    buff.resetCount();
                    for (IndexColumn r : refCols) {
                        buff.appendExceptFirst(", ");
                        buff.append(r.columnName);
                    }
                    buff.append(")");
                    break;
                }
            case CommandInterface.CREATE_INDEX:
                {
                    CreateIndex stmt = (CreateIndex) command;
                    if (stmt.isSpatial()) {
                        buff.append(" SPATIAL INDEX");
                    } else {
                        buff.append(" INDEX");
                        if (stmt.isHash()) {
                            buff.append(" USING HASH");
                        }
                    }
                    buff.resetCount();
                    buff.append("(");
                    for (IndexColumn c : stmt.getIndexColumns()) {
                        buff.appendExceptFirst(", ");
                        buff.append(identifier(c.columnName));
                    }
                    buff.append(")");
                    break;
                }
            default:
                throw DbException.throwInternalError("type=" + type);
        }
    }
    buff.append(")");
    if (prepared.getTableEngine() != null) {
        buff.append(" ENGINE = ");
        buff.append(prepared.getTableEngine());
    }
    ArrayList<String> tableEngineParams = prepared.getTableEngineParams();
    if (tableEngineParams != null && tableEngineParams.isEmpty()) {
        buff.append("WITH ");
        buff.resetCount();
        for (String parameter : tableEngineParams) {
            buff.appendExceptFirst(", ");
            buff.append(StringUtils.quoteIdentifier(parameter));
        }
    }
    if (prepared.getCharset() != null) {
        buff.append(" DEFAULT CHARACTER SET = ");
        buff.append(prepared.getCharset());
    }
    return buff.toString();
}
Also used : AlterTableAddConstraint(com.wplatform.ddal.command.ddl.AlterTableAddConstraint) CreateIndex(com.wplatform.ddal.command.ddl.CreateIndex) IndexColumn(com.wplatform.ddal.dbobject.table.IndexColumn) Column(com.wplatform.ddal.dbobject.table.Column) StatementBuilder(com.wplatform.ddal.util.StatementBuilder) 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) DefineCommand(com.wplatform.ddal.command.ddl.DefineCommand) IndexColumn(com.wplatform.ddal.dbobject.table.IndexColumn)

Example 15 with TableNode

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

the class PreparedRoutingExecutor method invokeUpdateRow.

/**
 * @param result
 * @param row
 * @return
 */
private int invokeUpdateRow(RoutingResult result, Row row) {
    List<JdbcWorker<Integer>> workers = New.arrayList(result.tableNodeCount());
    TableNode[] selectNodes = result.getSelectNodes();
    for (TableNode node : selectNodes) {
        StatementBuilder sqlBuff = new StatementBuilder();
        List<Value> params = doTranslate(node, row, sqlBuff);
        workers.add(createUpdateWorker(node.getShardName(), sqlBuff.toString(), params));
    }
    try {
        addRuningJdbcWorkers(workers);
        int affectRows = 0;
        if (workers.size() > 1) {
            // MILLISECONDS
            int queryTimeout = getQueryTimeout();
            List<Future<Integer>> invokeAll;
            if (queryTimeout > 0) {
                invokeAll = jdbcExecutor.invokeAll(workers, queryTimeout, TimeUnit.MILLISECONDS);
            } else {
                invokeAll = jdbcExecutor.invokeAll(workers);
            }
            for (Future<Integer> future : invokeAll) {
                affectRows += future.get();
            }
        } else if (workers.size() == 1) {
            affectRows = workers.get(0).doWork();
        }
        return affectRows;
    } catch (InterruptedException e) {
        throw DbException.convert(e);
    } catch (ExecutionException e) {
        throw DbException.convert(e.getCause());
    } finally {
        removeRuningJdbcWorkers(workers);
        for (JdbcWorker<Integer> jdbcWorker : workers) {
            jdbcWorker.closeResource();
        }
    }
}
Also used : JdbcWorker(com.wplatform.ddal.excutor.JdbcWorker) StatementBuilder(com.wplatform.ddal.util.StatementBuilder) TableNode(com.wplatform.ddal.dispatch.rule.TableNode) Value(com.wplatform.ddal.value.Value) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

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