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;
}
Aggregations