Search in sources :

Example 11 with TableMate

use of com.wplatform.ddal.dbobject.table.TableMate in project jdbc-shards by wplatform.

the class AlterTableAddConstraintExecutor method executeUpdate.

@Override
public int executeUpdate() {
    String tableName = prepared.getTableName();
    TableMate table = getTableMate(tableName);
    session.getUser().checkRight(table, Right.ALL);
    TableNode[] tableNodes = table.getPartitionNode();
    int type = prepared.getType();
    switch(type) {
        case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_REFERENTIAL:
            {
                String refTableName = prepared.getRefTableName();
                TableMate refTable = getTableMate(refTableName);
                TableNode[] refTableNode = table.getPartitionNode();
                Map<TableNode, TableNode> symmetryRelation = getSymmetryRelation(tableNodes, refTableNode);
                if (symmetryRelation == null) {
                    throw DbException.get(ErrorCode.CHECK_CONSTRAINT_INVALID, "The original table and reference table should be symmetrical.");
                }
                session.getUser().checkRight(refTable, Right.ALL);
            }
        case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY:
        case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_UNIQUE:
        case CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_CHECK:
            {
                execute(tableNodes);
                break;
            }
        default:
            throw DbException.throwInternalError("type=" + type);
    }
    return 0;
}
Also used : 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)

Example 12 with TableMate

use of com.wplatform.ddal.dbobject.table.TableMate in project jdbc-shards by wplatform.

the class Database method openDatabase.

private synchronized void openDatabase() {
    User systemUser = new User(this, allocateObjectId(), SYSTEM_USER_NAME);
    systemUser.setAdmin(true);
    systemUser.setUserPasswordHash(new byte[0]);
    users.put(SYSTEM_USER_NAME, systemUser);
    Schema schema = new Schema(this, allocateObjectId(), Constants.SCHEMA_MAIN, systemUser, true);
    schemas.put(schema.getName(), schema);
    Role publicRole = new Role(this, 0, Constants.PUBLIC_ROLE_NAME, true);
    roles.put(Constants.PUBLIC_ROLE_NAME, publicRole);
    Session sysSession = createSession(systemUser);
    try {
        SchemaConfig sc = configuration.getSchemaConfig();
        List<TableConfig> ctList = sc.getTables();
        for (TableConfig tableConfig : ctList) {
            String identifier = tableConfig.getName();
            identifier = identifier(identifier);
            TableMate tableMate = new TableMate(schema, allocateObjectId(), identifier);
            tableMate.setTableRouter(tableConfig.getTableRouter());
            tableMate.setShards(tableConfig.getShards());
            tableMate.setScanLevel(tableConfig.getScanLevel());
            tableMate.loadMataData(sysSession);
            if (tableConfig.isValidation()) {
                tableMate.check();
            }
            this.addSchemaObject(tableMate);
        }
    } finally {
        sysSession.close();
    }
}
Also used : Role(com.wplatform.ddal.dbobject.Role) User(com.wplatform.ddal.dbobject.User) SchemaConfig(com.wplatform.ddal.config.SchemaConfig) Schema(com.wplatform.ddal.dbobject.schema.Schema) TableConfig(com.wplatform.ddal.config.TableConfig) TableMate(com.wplatform.ddal.dbobject.table.TableMate)

Example 13 with TableMate

use of com.wplatform.ddal.dbobject.table.TableMate in project jdbc-shards by wplatform.

the class CommonPreparedExecutor method findTableMate.

/**
 * @param tableName
 */
public TableMate findTableMate(String tableName) {
    Table table = database.getSchema(session.getCurrentSchemaName()).findTableOrView(session, tableName);
    if (table == null) {
        String[] schemaNames = session.getSchemaSearchPath();
        if (schemaNames != null) {
            for (String name : schemaNames) {
                Schema s = database.getSchema(name);
                table = s.findTableOrView(session, tableName);
                if (table != null) {
                    break;
                }
            }
        }
    }
    if (table != null && table instanceof TableMate) {
        return (TableMate) table;
    }
    return null;
}
Also used : Table(com.wplatform.ddal.dbobject.table.Table) Schema(com.wplatform.ddal.dbobject.schema.Schema) TableMate(com.wplatform.ddal.dbobject.table.TableMate)

Example 14 with TableMate

use of com.wplatform.ddal.dbobject.table.TableMate 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 TableMate

use of com.wplatform.ddal.dbobject.table.TableMate in project jdbc-shards by wplatform.

the class DropTableExecutor method executeDrop.

private void executeDrop(DropTable next) {
    String tableName = next.getTableName();
    TableMate table = findTableMate(tableName);
    if (table != null) {
        TableNode[] nodes = table.getPartitionNode();
        execute(nodes);
        table.markDeleted();
    }
    next = prepared.getNext();
    if (next != null) {
        executeDrop(next);
    }
}
Also used : TableNode(com.wplatform.ddal.dispatch.rule.TableNode) TableMate(com.wplatform.ddal.dbobject.table.TableMate)

Aggregations

TableMate (com.wplatform.ddal.dbobject.table.TableMate)22 Column (com.wplatform.ddal.dbobject.table.Column)12 DbException (com.wplatform.ddal.message.DbException)8 TableNode (com.wplatform.ddal.dispatch.rule.TableNode)7 Value (com.wplatform.ddal.value.Value)6 Row (com.wplatform.ddal.result.Row)5 SearchRow (com.wplatform.ddal.result.SearchRow)5 Query (com.wplatform.ddal.command.dml.Query)4 Expression (com.wplatform.ddal.command.expression.Expression)4 AlterTableAddConstraint (com.wplatform.ddal.command.ddl.AlterTableAddConstraint)3 Table (com.wplatform.ddal.dbobject.table.Table)3 ResultInterface (com.wplatform.ddal.result.ResultInterface)3 Map (java.util.Map)3 DefineCommand (com.wplatform.ddal.command.ddl.DefineCommand)2 Schema (com.wplatform.ddal.dbobject.schema.Schema)2 IndexColumn (com.wplatform.ddal.dbobject.table.IndexColumn)2 TableFilter (com.wplatform.ddal.dbobject.table.TableFilter)2 Prepared (com.wplatform.ddal.command.Prepared)1 AlterTableAlterColumn (com.wplatform.ddal.command.ddl.AlterTableAlterColumn)1 CreateIndex (com.wplatform.ddal.command.ddl.CreateIndex)1