Search in sources :

Example 1 with CreateIndex

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

Aggregations

AlterTableAddConstraint (com.wplatform.ddal.command.ddl.AlterTableAddConstraint)1 CreateIndex (com.wplatform.ddal.command.ddl.CreateIndex)1 DefineCommand (com.wplatform.ddal.command.ddl.DefineCommand)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 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)1 Map (java.util.Map)1