Search in sources :

Example 1 with Insert

use of com.wplatform.ddal.command.dml.Insert 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)

Aggregations

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 Sequence (com.wplatform.ddal.dbobject.schema.Sequence)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