use of io.shardingjdbc.core.parsing.SQLParsingEngine in project sharding-jdbc by shardingjdbc.
the class ShowCreateTableMergedResult method init.
private Iterator<MemoryQueryResultRow> init(final List<QueryResult> queryResults) throws SQLException {
List<MemoryQueryResultRow> result = new LinkedList<>();
for (QueryResult each : queryResults) {
while (each.next()) {
MemoryQueryResultRow memoryResultSetRow = new MemoryQueryResultRow(each);
String tableName = memoryResultSetRow.getCell(1).toString();
Optional<TableRule> tableRule = shardingRule.tryFindTableRuleByActualTable(tableName);
if (tableRule.isPresent()) {
String logicTableName = tableRule.get().getLogicTable();
memoryResultSetRow.setCell(1, logicTableName);
String createTableDDL = memoryResultSetRow.getCell(2).toString();
SQLParsingEngine sqlParsingEngine = new SQLParsingEngine(DatabaseType.MySQL, createTableDDL, shardingRule);
String actualTableName = sqlParsingEngine.parse().getTables().getSingleTableName();
if (actualTableName.startsWith("`")) {
logicTableName = "`" + logicTableName + "`";
}
memoryResultSetRow.setCell(2, createTableDDL.replaceFirst(actualTableName, logicTableName));
result.add(memoryResultSetRow);
}
}
}
if (!result.isEmpty()) {
setCurrentResultSetRow(result.get(0));
}
return result.iterator();
}
use of io.shardingjdbc.core.parsing.SQLParsingEngine in project sharding-jdbc by shardingjdbc.
the class ParsingSQLRouter method parse.
@Override
public SQLStatement parse(final String logicSQL) {
SQLParsingEngine parsingEngine = new SQLParsingEngine(databaseType, logicSQL, shardingRule);
SQLStatement result = parsingEngine.parse();
if (result instanceof InsertStatement) {
((InsertStatement) result).appendGenerateKeyToken(shardingRule);
}
return result;
}
use of io.shardingjdbc.core.parsing.SQLParsingEngine in project sharding-jdbc by shardingjdbc.
the class ComStmtPreparePacket method execute.
@Override
public List<DatabaseProtocolPacket> execute() {
log.debug("COM_STMT_PREPARE received for Sharding-Proxy: {}", sql);
List<DatabaseProtocolPacket> result = new LinkedList<>();
int currentSequenceId = 0;
SQLStatement sqlStatement = new SQLParsingEngine(DatabaseType.MySQL, sql, ShardingRuleRegistry.getInstance().getShardingRule()).parse();
result.add(new ComStmtPrepareOKPacket(++currentSequenceId, PreparedStatementRegistry.getInstance().register(sql), getNumColumns(sqlStatement), sqlStatement.getParametersIndex(), 0));
for (int i = 0; i < sqlStatement.getParametersIndex(); i++) {
// TODO add column name
result.add(new ColumnDefinition41Packet(++currentSequenceId, ShardingConstant.LOGIC_SCHEMA_NAME, sqlStatement.getTables().isSingleTable() ? sqlStatement.getTables().getSingleTableName() : "", "", "", "", 100, ColumnType.MYSQL_TYPE_VARCHAR, 0));
}
if (sqlStatement.getParametersIndex() > 0) {
result.add(new EofPacket(++currentSequenceId, 0, StatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue()));
}
// TODO add If numColumns > 0
return result;
}
use of io.shardingjdbc.core.parsing.SQLParsingEngine in project sharding-jdbc by shardingjdbc.
the class UpdateStatementParserTest method parseWithOr.
@Test(expected = SQLParsingUnsupportedException.class)
public void parseWithOr() {
ShardingRule shardingRule = createShardingRule();
new SQLParsingEngine(DatabaseType.Oracle, "UPDATE TABLE_XXX SET field1=1 WHERE field1<1 AND (field1 >2 OR field2 =1)", shardingRule).parse();
}
use of io.shardingjdbc.core.parsing.SQLParsingEngine in project sharding-jdbc by shardingjdbc.
the class UpdateStatementParserTest method parseWithSpecialSyntax.
private void parseWithSpecialSyntax(final DatabaseType dbType, final String actualSQL) {
ShardingRule shardingRule = createShardingRule();
DMLStatement updateStatement = (DMLStatement) new SQLParsingEngine(dbType, actualSQL, shardingRule).parse();
assertThat(updateStatement.getTables().find("TABLE_XXX").get().getName(), is("TABLE_XXX"));
assertFalse(updateStatement.getTables().find("TABLE_XXX").get().getAlias().isPresent());
Condition condition = updateStatement.getConditions().find(new Column("field1", "TABLE_XXX")).get();
assertThat(condition.getOperator(), is(ShardingOperator.EQUAL));
assertThat(((ListShardingValue<? extends Comparable>) condition.getShardingValue(Collections.emptyList())).getValues().iterator().next(), is((Object) 1));
}
Aggregations