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