Search in sources :

Example 1 with TableMate

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

the class SelectExecutor method scanLevelValidation.

private void scanLevelValidation(TableFilter filter) {
    Table test = filter.getTable();
    if (!(test instanceof Table)) {
        return;
    }
    TableMate table = castTableMate(test);
    table.check();
    Index index = filter.getIndex();
    int scanLevel = table.getScanLevel();
    switch(scanLevel) {
        case TableConfig.SCANLEVEL_UNLIMITED:
            break;
        case TableConfig.SCANLEVEL_FILTER:
            if (filter.getFilterCondition() == null) {
                throw DbException.get(ErrorCode.NOT_ALLOWED_TO_SCAN_TABLE, table.getSQL(), "filter", "filter");
            }
            break;
        case TableConfig.SCANLEVEL_ANYINDEX:
            if (index.getIndexType().isScan()) {
                throw DbException.get(ErrorCode.NOT_ALLOWED_TO_SCAN_TABLE, table.getSQL(), "anyIndex", "index");
            }
            break;
        case TableConfig.SCANLEVEL_UNIQUEINDEX:
            if (!index.getIndexType().isUnique()) {
                throw DbException.get(ErrorCode.NOT_ALLOWED_TO_SCAN_TABLE, table.getSQL(), "uniqueIndex", "unique index");
            }
            break;
        case TableConfig.SCANLEVEL_SHARDINGKEY:
            if (!index.getIndexType().isShardingKey()) {
                throw DbException.get(ErrorCode.NOT_ALLOWED_TO_SCAN_TABLE, table.getSQL(), "shardingKey", "sharding key");
            }
            break;
        default:
            throw DbException.throwInternalError("error table scan level " + scanLevel);
    }
}
Also used : Table(com.wplatform.ddal.dbobject.table.Table) Index(com.wplatform.ddal.dbobject.index.Index) TableMate(com.wplatform.ddal.dbobject.table.TableMate)

Example 2 with TableMate

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

the class DeleteExecutor method executeUpdate.

@Override
public int executeUpdate() {
    TableFilter tableFilter = prepared.getTableFilter();
    TableMate table = castTableMate(tableFilter.getTable());
    table.check();
    session.getUser().checkRight(table, Right.DELETE);
    return updateRow(table, null, tableFilter.getIndexConditions());
}
Also used : TableFilter(com.wplatform.ddal.dbobject.table.TableFilter) TableMate(com.wplatform.ddal.dbobject.table.TableMate)

Example 3 with TableMate

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

the class MergeExecutor method merge.

private void merge(Row row) {
    TableMate table = castTableMate(prepared.getTable());
    Prepared update = prepared.getUpdate();
    Column[] columns = prepared.getColumns();
    Column[] keys = prepared.getKeys();
    ArrayList<Parameter> k = update.getParameters();
    for (int i = 0; i < columns.length; i++) {
        Column col = columns[i];
        Value v = row.getValue(col.getColumnId());
        Parameter p = k.get(i);
        p.setValue(v);
    }
    for (int i = 0; i < keys.length; i++) {
        Column col = keys[i];
        Value v = row.getValue(col.getColumnId());
        if (v == null) {
            throw DbException.get(ErrorCode.COLUMN_CONTAINS_NULL_VALUES_1, col.getSQL());
        }
        Parameter p = k.get(columns.length + i);
        p.setValue(v);
    }
    int count = update.update();
    if (count == 0) {
        try {
            table.validateConvertUpdateSequence(session, row);
            updateRow(table, row);
        } catch (DbException e) {
            throw e;
        }
    } else if (count != 1) {
        throw DbException.get(ErrorCode.DUPLICATE_KEY_1, table.getSQL());
    }
}
Also used : Column(com.wplatform.ddal.dbobject.table.Column) Prepared(com.wplatform.ddal.command.Prepared) Value(com.wplatform.ddal.value.Value) Parameter(com.wplatform.ddal.command.expression.Parameter) TableMate(com.wplatform.ddal.dbobject.table.TableMate) DbException(com.wplatform.ddal.message.DbException)

Example 4 with TableMate

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

Example 5 with TableMate

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

the class DropTableExecutor method prepareDrop.

private void prepareDrop(DropTable next) {
    String tableName = next.getTableName();
    TableMate table = getTableMate(tableName);
    if (table == null) {
        if (!next.isIfExists()) {
            throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableName);
        }
    }
    session.getUser().checkRight(table, Right.ALL);
    next = prepared.getNext();
    if (next != null) {
        prepareDrop(next);
    }
}
Also used : 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