use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.
the class DruidInsertParser method visitorParse.
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
MySqlInsertStatement insert = (MySqlInsertStatement) stmt;
String schemaName = schema == null ? null : schema.getName();
SQLExprTableSource tableSource = insert.getTableSource();
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, tableSource);
if (!ServerPrivileges.checkPrivilege(sc, schemaInfo.getSchema(), schemaInfo.getTable(), CheckType.INSERT)) {
String msg = "The statement DML privilege check is not passed, sql:" + stmt;
throw new SQLNonTransientException(msg);
}
schema = schemaInfo.getSchemaConfig();
String tableName = schemaInfo.getTable();
if (parserNoSharding(sc, schemaName, schemaInfo, rrs, insert)) {
return schema;
}
if (insert.getQuery() != null) {
// insert into .... select ....
String msg = "`INSERT ... SELECT Syntax` is not supported!";
LOGGER.info(msg);
throw new SQLNonTransientException(msg);
}
TableConfig tc = schema.getTables().get(tableName);
if (tc == null) {
String msg = "Table '" + schema.getName() + "." + tableName + "' doesn't exist";
throw new SQLException(msg, "42S02", ErrorCode.ER_NO_SUCH_TABLE);
}
if (tc.isGlobalTable()) {
String sql = rrs.getStatement();
if (tc.isAutoIncrement() || GlobalTableUtil.useGlobalTableCheck()) {
sql = convertInsertSQL(schemaInfo, insert, sql, tc, GlobalTableUtil.useGlobalTableCheck());
} else {
sql = RouterUtil.removeSchema(sql, schemaInfo.getSchema());
}
rrs.setStatement(sql);
RouterUtil.routeToMultiNode(false, rrs, tc.getDataNodes(), tc.isGlobalTable());
rrs.setFinishedRoute(true);
return schema;
}
if (tc.isAutoIncrement()) {
String sql = convertInsertSQL(schemaInfo, insert, rrs.getStatement(), tc, false);
rrs.setStatement(sql);
SQLStatementParser parser = new MySqlStatementParser(sql);
stmt = parser.parseStatement();
insert = (MySqlInsertStatement) stmt;
}
// insert childTable will finished router while parser
if (tc.getParentTC() != null) {
parserChildTable(schemaInfo, rrs, insert, sc);
return schema;
}
String partitionColumn = tc.getPartitionColumn();
if (partitionColumn != null) {
if (isMultiInsert(insert)) {
parserBatchInsert(schemaInfo, rrs, partitionColumn, insert);
} else {
parserSingleInsert(schemaInfo, rrs, partitionColumn, insert);
}
} else {
rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema()));
ctx.addTable(tableName);
}
return schema;
}
use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.
the class DruidReplaceParser method visitorParse.
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
// data & object prepare
MySqlReplaceStatement replace = (MySqlReplaceStatement) stmt;
String schemaName = schema == null ? null : schema.getName();
SQLExprTableSource tableSource = replace.getTableSource();
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, tableSource);
// privilege check
if (!ServerPrivileges.checkPrivilege(sc, schemaInfo.getSchema(), schemaInfo.getTable(), ServerPrivileges.CheckType.INSERT)) {
String msg = "The statement DML privilege check is not passed, sql:" + stmt;
throw new SQLNonTransientException(msg);
}
// No sharding table check
schema = schemaInfo.getSchemaConfig();
String tableName = schemaInfo.getTable();
if (parserNoSharding(sc, schemaName, schemaInfo, rrs, replace)) {
return schema;
}
if (replace.getQuery() != null) {
// replace into ...select with sharding not supported
String msg = "`INSERT ... SELECT Syntax` is not supported!";
LOGGER.info(msg);
throw new SQLNonTransientException(msg);
}
// check the config of target table
TableConfig tc = schema.getTables().get(tableName);
if (tc == null) {
String msg = "Table '" + schema.getName() + "." + tableName + "' doesn't exist";
throw new SQLException(msg, "42S02", ErrorCode.ER_NO_SUCH_TABLE);
}
// if the target table is global table than
if (tc.isGlobalTable()) {
String sql = rrs.getStatement();
if (tc.isAutoIncrement() || GlobalTableUtil.useGlobalTableCheck()) {
sql = convertReplaceSQL(schemaInfo, replace, sql, tc, GlobalTableUtil.useGlobalTableCheck(), sc);
} else {
sql = RouterUtil.removeSchema(sql, schemaInfo.getSchema());
}
rrs.setStatement(sql);
RouterUtil.routeToMultiNode(false, rrs, tc.getDataNodes(), tc.isGlobalTable());
rrs.setFinishedRoute(true);
return schema;
}
if (tc.isAutoIncrement()) {
String sql = convertReplaceSQL(schemaInfo, replace, rrs.getStatement(), tc, false, sc);
rrs.setStatement(sql);
SQLStatementParser parser = new MySqlStatementParser(sql);
stmt = parser.parseStatement();
replace = (MySqlReplaceStatement) stmt;
}
// childTable can be route in this part
if (tc.getParentTC() != null) {
parserChildTable(schemaInfo, rrs, replace, sc);
return schema;
}
String partitionColumn = tc.getPartitionColumn();
if (partitionColumn != null) {
if (isMultiReplace(replace)) {
parserBatchInsert(schemaInfo, rrs, partitionColumn, replace);
} else {
parserSingleInsert(schemaInfo, rrs, partitionColumn, replace);
}
} else {
rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema()));
ctx.addTable(tableName);
}
return schema;
}
use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.
the class DruidSelectParser method visitorParse.
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery();
String schemaName = schema == null ? null : schema.getName();
if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock) sqlSelectQuery;
if (mysqlSelectQuery.getInto() != null) {
throw new SQLNonTransientException("select ... into is not supported!");
}
SQLTableSource mysqlFrom = mysqlSelectQuery.getFrom();
if (mysqlFrom == null) {
List<SQLSelectItem> selectList = mysqlSelectQuery.getSelectList();
for (SQLSelectItem item : selectList) {
SQLExpr itemExpr = item.getExpr();
if (itemExpr instanceof SQLQueryExpr) {
rrs.setSqlStatement(selectStmt);
rrs.setNeedOptimizer(true);
return schema;
}
}
RouterUtil.routeNoNameTableToSingleNode(rrs, schema);
return schema;
}
SchemaInfo schemaInfo;
if (mysqlFrom instanceof SQLExprTableSource) {
SQLExprTableSource fromSource = (SQLExprTableSource) mysqlFrom;
schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, fromSource);
if (schemaInfo.isDual()) {
RouterUtil.routeNoNameTableToSingleNode(rrs, schema);
return schema;
}
if (matchSysTable(rrs, sc, schemaInfo)) {
return schema;
}
if (schemaInfo.getSchemaConfig() == null) {
String msg = "No Supported, sql:" + stmt;
throw new SQLNonTransientException(msg);
}
if (!ServerPrivileges.checkPrivilege(sc, schemaInfo.getSchema(), schemaInfo.getTable(), CheckType.SELECT)) {
String msg = "The statement DML privilege check is not passed, sql:" + stmt;
throw new SQLNonTransientException(msg);
}
rrs.setSchema(schemaInfo.getSchema());
rrs.setTable(schemaInfo.getTable());
rrs.setTableAlias(schemaInfo.getTableAlias());
rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema()));
schema = schemaInfo.getSchemaConfig();
if (DbleServer.getInstance().getTmManager().getSyncView(schema.getName(), schemaInfo.getTable()) != null) {
rrs.setNeedOptimizer(true);
rrs.setSqlStatement(selectStmt);
return schema;
}
if (RouterUtil.isNoSharding(schema, schemaInfo.getTable())) {
RouterUtil.routeToSingleNode(rrs, schema.getDataNode());
return schema;
}
TableConfig tc = schema.getTables().get(schemaInfo.getTable());
if (tc == null) {
String msg = "Table '" + schema.getName() + "." + schemaInfo.getTable() + "' doesn't exist";
throw new SQLException(msg, "42S02", ErrorCode.ER_NO_SUCH_TABLE);
}
super.visitorParse(schema, rrs, stmt, visitor, sc);
if (visitor.isHasSubQuery()) {
rrs.setSqlStatement(selectStmt);
rrs.setNeedOptimizer(true);
return schema;
}
parseOrderAggGroupMysql(schema, stmt, rrs, mysqlSelectQuery, tc);
// select ...for update /in shard mode /in transaction
if ((mysqlSelectQuery.isForUpdate() || mysqlSelectQuery.isLockInShareMode()) && !sc.isAutocommit()) {
rrs.setCanRunInReadDB(false);
}
} else if (mysqlFrom instanceof SQLSubqueryTableSource || mysqlFrom instanceof SQLJoinTableSource || mysqlFrom instanceof SQLUnionQueryTableSource) {
return executeComplexSQL(schemaName, schema, rrs, selectStmt, sc);
}
} else if (sqlSelectQuery instanceof MySqlUnionQuery) {
return executeComplexSQL(schemaName, schema, rrs, selectStmt, sc);
}
return schema;
}
use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.
the class DruidSingleUnitSelectParser method visitorParse.
@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery();
String schemaName = schema == null ? null : schema.getName();
if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock) selectStmt.getSelect().getQuery();
SQLTableSource mysqlFrom = mysqlSelectQuery.getFrom();
if (mysqlFrom == null) {
RouterUtil.routeNoNameTableToSingleNode(rrs, schema);
return schema;
}
if (mysqlFrom instanceof SQLSubqueryTableSource || mysqlFrom instanceof SQLJoinTableSource || mysqlFrom instanceof SQLUnionQueryTableSource) {
StringPtr sqlSchema = new StringPtr(null);
if (SchemaUtil.isNoSharding(sc, selectStmt.getSelect().getQuery(), selectStmt, schemaName, sqlSchema)) {
String realSchema = sqlSchema.get() == null ? schemaName : sqlSchema.get();
SchemaConfig schemaConfig = DbleServer.getInstance().getConfig().getSchemas().get(realSchema);
rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), realSchema));
RouterUtil.routeToSingleNode(rrs, schemaConfig.getDataNode());
return schemaConfig;
} else {
super.visitorParse(schema, rrs, stmt, visitor, sc);
return schema;
}
}
SQLExprTableSource fromSource = (SQLExprTableSource) mysqlFrom;
SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, fromSource);
if (schemaInfo.getSchemaConfig() == null) {
String msg = "No Supported, sql:" + stmt;
throw new SQLNonTransientException(msg);
}
if (!ServerPrivileges.checkPrivilege(sc, schemaInfo.getSchema(), schemaInfo.getTable(), CheckType.SELECT)) {
String msg = "The statement DML privilege check is not passed, sql:" + stmt;
throw new SQLNonTransientException(msg);
}
rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema()));
schema = schemaInfo.getSchemaConfig();
super.visitorParse(schema, rrs, stmt, visitor, sc);
if (visitor.isHasSubQuery()) {
this.getCtx().getRouteCalculateUnits().clear();
}
// change canRunInReadDB
if ((mysqlSelectQuery.isForUpdate() || mysqlSelectQuery.isLockInShareMode()) && !sc.isAutocommit()) {
rrs.setCanRunInReadDB(false);
}
} else if (sqlSelectQuery instanceof MySqlUnionQuery) {
StringPtr sqlSchema = new StringPtr(null);
if (SchemaUtil.isNoSharding(sc, selectStmt.getSelect().getQuery(), selectStmt, schemaName, sqlSchema)) {
String realSchema = sqlSchema.get() == null ? schemaName : sqlSchema.get();
SchemaConfig schemaConfig = DbleServer.getInstance().getConfig().getSchemas().get(realSchema);
rrs.setStatement(RouterUtil.removeSchema(rrs.getStatement(), realSchema));
RouterUtil.routeToSingleNode(rrs, schemaConfig.getDataNode());
return schemaConfig;
} else {
super.visitorParse(schema, rrs, stmt, visitor, sc);
}
}
return schema;
}
use of com.actiontech.dble.server.util.SchemaUtil.SchemaInfo in project dble by actiontech.
the class ShowColumns method response.
public static void response(ServerConnection c, String stmt) {
try {
SQLStatement statement = RouteStrategyFactory.getRouteStrategy().parserSQL(stmt);
MySqlShowColumnsStatement showColumnsStatement = (MySqlShowColumnsStatement) statement;
String table = StringUtil.removeBackQuote(showColumnsStatement.getTable().getSimpleName());
String schema = showColumnsStatement.getDatabase() == null ? c.getSchema() : showColumnsStatement.getDatabase().getSimpleName();
if (schema == null) {
c.writeErrMessage("3D000", "No database selected", ErrorCode.ER_NO_DB_ERROR);
return;
}
String sql = stmt;
if (showColumnsStatement.getDatabase() != null) {
showColumnsStatement.setDatabase(null);
sql = showColumnsStatement.toString();
}
if (DbleServer.getInstance().getSystemVariables().isLowerCaseTableNames()) {
schema = StringUtil.removeBackQuote(schema).toLowerCase();
table = table.toLowerCase();
}
SchemaInfo schemaInfo = new SchemaInfo(schema, table);
c.routeSystemInfoAndExecuteSQL(sql, schemaInfo, ServerParse.SHOW);
} catch (Exception e) {
c.writeErrMessage(ErrorCode.ER_PARSE_ERROR, e.toString());
}
}
Aggregations