use of com.wplatform.ddal.dispatch.rule.TableNode in project jdbc-shards by wplatform.
the class XmlRuleConfigParser method parsePartition.
private void parsePartition(TableRouter tableRouter, List<XNode> list) {
List<TableNode> tableNodes = New.arrayList();
for (XNode xNode : list) {
String shard = xNode.getStringAttribute("shard");
String suffix = xNode.getStringAttribute("suffix");
shard = shard == null ? null : shard.trim();
suffix = suffix == null ? null : suffix.trim();
if (StringUtils.isNullOrEmpty(shard)) {
throw new ParsingException("Error parsing ddal-rule XML. Cause: " + "the shard attribute of 'table' element is required.");
}
List<String> shards = collectItems(shard);
List<String> suffixes = collectItems(suffix);
if (suffixes.isEmpty()) {
for (String shardItem : shards) {
TableNode node = new TableNode(shardItem, null, null);
if (tableNodes.contains(node)) {
throw new ParsingException("Duplicate " + node + " defined in " + tableRouter.getId() + "'s partition");
}
tableNodes.add(node);
}
} else {
for (String shardItem : shards) {
for (String suffixItem : suffixes) {
TableNode node = new TableNode(shardItem, null, suffixItem);
if (tableNodes.contains(node)) {
throw new ParsingException("Duplicate " + node + " defined in " + tableRouter.getId() + "'s partition");
}
tableNodes.add(node);
}
}
}
}
tableRouter.setPartition(tableNodes);
}
use of com.wplatform.ddal.dispatch.rule.TableNode in project jdbc-shards by wplatform.
the class DefineCommandExecutor method getSymmetryRelation.
protected static Map<TableNode, TableNode> getSymmetryRelation(TableNode[] n1, TableNode[] n2) {
if (n1.length != n2.length) {
return null;
}
Map<TableNode, TableNode> tableNode = New.hashMap();
for (TableNode tn1 : n1) {
String sName = tn1.getShardName();
String suffix = tn1.getSuffix();
TableNode matched = null;
for (TableNode tn2 : n2) {
if (!sName.equals(tn2.getShardName())) {
continue;
}
if (suffix != null && !suffix.equals(tn2.getSuffix())) {
continue;
}
matched = tn2;
}
if (matched == null) {
return null;
}
tableNode.put(tn1, matched);
}
if (tableNode.size() != n1.length) {
return null;
}
return tableNode;
}
use of com.wplatform.ddal.dispatch.rule.TableNode 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;
}
use of com.wplatform.ddal.dispatch.rule.TableNode 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.dispatch.rule.TableNode in project jdbc-shards by wplatform.
the class TableMate method isNodeMatch.
private static boolean isNodeMatch(TableNode[] nodes1, TableNode[] nodes2) {
TableNode[] group1 = RoutingResult.fixedResult(nodes1).group();
TableNode[] group2 = RoutingResult.fixedResult(nodes2).group();
for (TableNode tableNode1 : group1) {
boolean isMatched = false;
for (TableNode tableNode2 : group2) {
if (tableNode1.getShardName().equals(tableNode2.getShardName())) {
isMatched = true;
break;
}
}
if (!isMatched) {
return false;
}
}
return true;
}
Aggregations