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