use of com.wplatform.ddal.command.ddl.AlterTableAddConstraint 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;
}
use of com.wplatform.ddal.command.ddl.AlterTableAddConstraint 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();
}
Aggregations