diff --git a/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/exec/BaseTransactionalExecutor.java b/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/exec/BaseTransactionalExecutor.java index 8e1e33721d0..56cd8c0216d 100644 --- a/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/exec/BaseTransactionalExecutor.java +++ b/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/exec/BaseTransactionalExecutor.java @@ -16,10 +16,6 @@ package com.alibaba.fescar.rm.datasource.exec; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.List; - import com.alibaba.fescar.core.context.RootContext; import com.alibaba.fescar.rm.datasource.StatementProxy; import com.alibaba.fescar.rm.datasource.sql.SQLRecognizer; @@ -27,6 +23,10 @@ import com.alibaba.fescar.rm.datasource.sql.struct.TableMeta; import com.alibaba.fescar.rm.datasource.sql.struct.TableMetaCache; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.List; + public abstract class BaseTransactionalExecutor implements Executor { protected StatementProxy statementProxy; @@ -56,7 +56,7 @@ protected String buildWhereConditionByPKs(List pkRows) throws SQLExceptio StringBuffer whereConditionAppender = new StringBuffer(); for (int i = 0; i < pkRows.size(); i++) { Field field = pkRows.get(i); - whereConditionAppender.append(field.getName() + " = ?"); + whereConditionAppender.append(getColumnNameInSQL(field.getName()) + " = ?"); if (i < (pkRows.size() - 1)) { whereConditionAppender.append(" OR "); } @@ -64,6 +64,25 @@ protected String buildWhereConditionByPKs(List pkRows) throws SQLExceptio return whereConditionAppender.toString(); } + protected String getColumnNameInSQL(String columnName) { + String tableAlias = sqlRecognizer.getTableAlias(); + if (tableAlias == null) { + return columnName; + } else { + return tableAlias + "." + columnName; + } + } + + protected String getFromTableInSQL() { + String tableName = sqlRecognizer.getTableName(); + String tableAlias = sqlRecognizer.getTableAlias(); + if (tableAlias == null) { + return tableName; + } else { + return tableName + " " + tableAlias; + } + } + protected TableMeta getTableMeta() { return getTableMeta(sqlRecognizer.getTableName()); } diff --git a/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/exec/DeleteExecutor.java b/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/exec/DeleteExecutor.java index 222afd34938..d5c997ca232 100644 --- a/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/exec/DeleteExecutor.java +++ b/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/exec/DeleteExecutor.java @@ -16,13 +16,6 @@ package com.alibaba.fescar.rm.datasource.exec; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.List; - import com.alibaba.fescar.rm.datasource.ParametersHolder; import com.alibaba.fescar.rm.datasource.StatementProxy; import com.alibaba.fescar.rm.datasource.sql.SQLDeleteRecognizer; @@ -30,6 +23,13 @@ import com.alibaba.fescar.rm.datasource.sql.struct.TableMeta; import com.alibaba.fescar.rm.datasource.sql.struct.TableRecords; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; + public class DeleteExecutor extends AbstractDMLBaseExecutor { public DeleteExecutor(StatementProxy statementProxy, StatementCallback statementCallback, SQLRecognizer sqlRecognizer) { @@ -39,6 +39,7 @@ public DeleteExecutor(StatementProxy statementProxy, StatementCallback statement @Override protected TableRecords beforeImage() throws SQLException { SQLDeleteRecognizer visitor = (SQLDeleteRecognizer) sqlRecognizer; + TableMeta tmeta = getTableMeta(visitor.getTableName()); List columns = new ArrayList<>(); for (String column : tmeta.getAllColumns().keySet()) { @@ -48,7 +49,7 @@ protected TableRecords beforeImage() throws SQLException { StringBuffer selectSQLAppender = new StringBuffer("SELECT "); for (int i = 0; i < columns.size(); i++) { - selectSQLAppender.append(columns.get(i)); + selectSQLAppender.append(getColumnNameInSQL(columns.get(i))); if (i < (columns.size() - 1)) { selectSQLAppender.append(", "); } @@ -60,7 +61,7 @@ protected TableRecords beforeImage() throws SQLException { } else { whereCondition = visitor.getWhereCondition(); } - selectSQLAppender.append(" FROM " + tmeta.getTableName() + " WHERE " + whereCondition + " FOR UPDATE"); + selectSQLAppender.append(" FROM " + getFromTableInSQL() + " WHERE " + whereCondition + " FOR UPDATE"); String selectSQL = selectSQLAppender.toString(); TableRecords beforeImage = null; diff --git a/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/exec/InsertExecutor.java b/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/exec/InsertExecutor.java index 4e3e1b1ecf6..7701497e711 100644 --- a/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/exec/InsertExecutor.java +++ b/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/exec/InsertExecutor.java @@ -47,8 +47,8 @@ protected TableRecords beforeImage() throws SQLException { @Override protected TableRecords afterImage(TableRecords beforeImage) throws SQLException { - SQLInsertRecognizer visitor = (SQLInsertRecognizer) sqlRecognizer; - List insertColumns = visitor.getInsertColumns(); + SQLInsertRecognizer recogizier = (SQLInsertRecognizer) sqlRecognizer; + List insertColumns = recogizier.getInsertColumns(); TableMeta tmeta = getTableMeta(); TableRecords afterImage = null; if (tmeta.containsPK(insertColumns)) { @@ -60,7 +60,7 @@ protected TableRecords afterImage(TableRecords beforeImage) throws SQLException if (statementProxy instanceof PreparedStatementProxy) { pkValues = ((PreparedStatementProxy) statementProxy).getParamsByIndex(paramIdx); } else { - List> insertRows = visitor.getInsertRows(); + List> insertRows = recogizier.getInsertRows(); pkValues = new ArrayList<>(insertRows.size()); for (List row : insertRows) { pkValues.add(row.get(paramIdx)); diff --git a/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/exec/SelectForUpdateExecutor.java b/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/exec/SelectForUpdateExecutor.java index 2cd9b66ae2a..8d72ab540dd 100644 --- a/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/exec/SelectForUpdateExecutor.java +++ b/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/exec/SelectForUpdateExecutor.java @@ -47,8 +47,8 @@ public Object doExecute(Object... args) throws Throwable { boolean originalAutoCommit = conn.getAutoCommit(); StringBuffer selectSQLAppender = new StringBuffer("SELECT "); - selectSQLAppender.append(getTableMeta().getPkName()); - selectSQLAppender.append(" FROM " + getTableMeta().getTableName()); + selectSQLAppender.append(getColumnNameInSQL(getTableMeta().getPkName())); + selectSQLAppender.append(" FROM " + getFromTableInSQL()); String whereCondition = null; ArrayList paramAppender = new ArrayList<>(); if (statementProxy instanceof ParametersHolder) { diff --git a/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/exec/UpdateExecutor.java b/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/exec/UpdateExecutor.java index 623c207a46d..93f22f64f34 100644 --- a/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/exec/UpdateExecutor.java +++ b/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/exec/UpdateExecutor.java @@ -39,14 +39,15 @@ public UpdateExecutor(StatementProxy statementProxy, StatementCallback statement @Override protected TableRecords beforeImage() throws SQLException { - SQLUpdateRecognizer visitor = (SQLUpdateRecognizer) sqlRecognizer; + SQLUpdateRecognizer recognizer = (SQLUpdateRecognizer) sqlRecognizer; + TableMeta tmeta = getTableMeta(); - List updateColumns = visitor.getUpdateColumns(); + List updateColumns = recognizer.getUpdateColumns(); StringBuffer selectSQLAppender = new StringBuffer("SELECT "); if (!tmeta.containsPK(updateColumns)) { // PK should be included. - selectSQLAppender.append(tmeta.getPkName() + ", "); + selectSQLAppender.append(getColumnNameInSQL(tmeta.getPkName()) + ", "); } for (int i = 0; i < updateColumns.size(); i++) { selectSQLAppender.append(updateColumns.get(i)); @@ -57,11 +58,11 @@ protected TableRecords beforeImage() throws SQLException { String whereCondition = null; ArrayList paramAppender = new ArrayList<>(); if (statementProxy instanceof ParametersHolder) { - whereCondition = visitor.getWhereCondition((ParametersHolder) statementProxy, paramAppender); + whereCondition = recognizer.getWhereCondition((ParametersHolder) statementProxy, paramAppender); } else { - whereCondition = visitor.getWhereCondition(); + whereCondition = recognizer.getWhereCondition(); } - selectSQLAppender.append(" FROM " + tmeta.getTableName() + " WHERE " + whereCondition + " FOR UPDATE"); + selectSQLAppender.append(" FROM " + getFromTableInSQL() + " WHERE " + whereCondition + " FOR UPDATE"); String selectSQL = selectSQLAppender.toString(); TableRecords beforeImage = null; @@ -97,17 +98,18 @@ protected TableRecords beforeImage() throws SQLException { @Override protected TableRecords afterImage(TableRecords beforeImage) throws SQLException { - SQLUpdateRecognizer visitor = (SQLUpdateRecognizer) sqlRecognizer; + SQLUpdateRecognizer recognizer = (SQLUpdateRecognizer) sqlRecognizer; + TableMeta tmeta = getTableMeta(); if (beforeImage == null || beforeImage.size() == 0) { return TableRecords.empty(getTableMeta()); } - List updateColumns = visitor.getUpdateColumns(); + List updateColumns = recognizer.getUpdateColumns(); StringBuffer selectSQLAppender = new StringBuffer("SELECT "); if (!tmeta.containsPK(updateColumns)) { // PK should be included. - selectSQLAppender.append(tmeta.getPkName() + ", "); + selectSQLAppender.append(getColumnNameInSQL(tmeta.getPkName()) + ", "); } for (int i = 0; i < updateColumns.size(); i++) { selectSQLAppender.append(updateColumns.get(i)); @@ -116,7 +118,7 @@ protected TableRecords afterImage(TableRecords beforeImage) throws SQLException } } List pkRows = beforeImage.pkRows(); - selectSQLAppender.append(" FROM " + tmeta.getTableName() + " WHERE " + buildWhereConditionByPKs(pkRows) + " FOR UPDATE"); + selectSQLAppender.append(" FROM " + getFromTableInSQL() + " WHERE " + buildWhereConditionByPKs(pkRows) + " FOR UPDATE"); String selectSQL = selectSQLAppender.toString(); TableRecords afterImage = null; diff --git a/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/sql/SQLRecognizer.java b/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/sql/SQLRecognizer.java index f481d5c08a9..36a64d67633 100644 --- a/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/sql/SQLRecognizer.java +++ b/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/sql/SQLRecognizer.java @@ -28,18 +28,18 @@ public interface SQLRecognizer { /** * TableRecords source related in the SQL, including alias if any. * SELECT id, name FROM user u WHERE ... - * TableRecords source should be 'user u' for this SQL. + * Alias should be 'u' for this SQL. * * @return table source. */ - String getTableSource(); + String getTableAlias(); /** * TableRecords name related in the SQL. * SELECT id, name FROM user u WHERE ... * TableRecords name should be 'user' for this SQL, without alias 'u'. * - * @see #getTableSource() + * @see #getTableAlias() * @return table name. */ String getTableName(); diff --git a/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/sql/druid/MySQLDeleteRecognizer.java b/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/sql/druid/MySQLDeleteRecognizer.java index 10771bd4e13..7f3cf5fa39e 100644 --- a/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/sql/druid/MySQLDeleteRecognizer.java +++ b/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/sql/druid/MySQLDeleteRecognizer.java @@ -44,11 +44,8 @@ public SQLType getSQLType() { } @Override - public String getTableSource() { - StringBuffer sb = new StringBuffer(); - MySqlOutputVisitor visitor = new MySqlOutputVisitor(sb); - visitor.visit((SQLExprTableSource)ast.getTableSource()); - return sb.toString(); + public String getTableAlias() { + return ast.getTableSource().getAlias(); } @Override diff --git a/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/sql/druid/MySQLInsertRecognizer.java b/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/sql/druid/MySQLInsertRecognizer.java index d9a8d880204..40754caf594 100644 --- a/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/sql/druid/MySQLInsertRecognizer.java +++ b/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/sql/druid/MySQLInsertRecognizer.java @@ -46,11 +46,8 @@ public SQLType getSQLType() { } @Override - public String getTableSource() { - StringBuffer sb = new StringBuffer(); - MySqlOutputVisitor visitor = new MySqlOutputVisitor(sb); - visitor.visit(ast.getTableSource()); - return sb.toString(); + public String getTableAlias() { + return ast.getTableSource().getAlias(); } @Override diff --git a/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/sql/druid/MySQLSelectForUpdateRecognizer.java b/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/sql/druid/MySQLSelectForUpdateRecognizer.java index 05b0be04b0d..3c41defc2ab 100644 --- a/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/sql/druid/MySQLSelectForUpdateRecognizer.java +++ b/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/sql/druid/MySQLSelectForUpdateRecognizer.java @@ -16,23 +16,19 @@ package com.alibaba.fescar.rm.datasource.sql.druid; -import java.util.ArrayList; - import com.alibaba.druid.sql.ast.SQLExpr; import com.alibaba.druid.sql.ast.SQLStatement; import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr; import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr; -import com.alibaba.druid.sql.ast.statement.SQLExprTableSource; -import com.alibaba.druid.sql.ast.statement.SQLSelect; -import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock; -import com.alibaba.druid.sql.ast.statement.SQLSelectStatement; -import com.alibaba.druid.sql.ast.statement.SQLTableSource; +import com.alibaba.druid.sql.ast.statement.*; import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor; import com.alibaba.fescar.rm.datasource.ParametersHolder; import com.alibaba.fescar.rm.datasource.sql.SQLParsingException; import com.alibaba.fescar.rm.datasource.sql.SQLSelectRecognizer; import com.alibaba.fescar.rm.datasource.sql.SQLType; +import java.util.ArrayList; + public class MySQLSelectForUpdateRecognizer extends BaseRecognizer implements SQLSelectRecognizer { private final SQLSelectStatement ast; @@ -96,13 +92,10 @@ private SQLSelectQueryBlock getSelect() { } @Override - public String getTableSource() { + public String getTableAlias() { SQLSelectQueryBlock selectQueryBlock = getSelect(); SQLTableSource tableSource = selectQueryBlock.getFrom(); - StringBuffer sb = new StringBuffer(); - MySqlOutputVisitor visitor = new MySqlOutputVisitor(sb); - visitor.visit((SQLExprTableSource)tableSource); - return sb.toString(); + return tableSource.getAlias(); } @Override diff --git a/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/sql/druid/MySQLUpdateRecognizer.java b/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/sql/druid/MySQLUpdateRecognizer.java index 801a283c060..34c8fa3fadc 100644 --- a/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/sql/druid/MySQLUpdateRecognizer.java +++ b/rm-datasource/src/main/java/com/alibaba/fescar/rm/datasource/sql/druid/MySQLUpdateRecognizer.java @@ -30,6 +30,7 @@ import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement; import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; import com.alibaba.fescar.rm.datasource.ParametersHolder; import com.alibaba.fescar.rm.datasource.sql.SQLParsingException; import com.alibaba.fescar.rm.datasource.sql.SQLType; @@ -122,12 +123,8 @@ public String getWhereCondition() { } @Override - public String getTableSource() { - StringBuffer sb = new StringBuffer(); - MySqlOutputVisitor visitor = new MySqlOutputVisitor(sb); - SQLExprTableSource tableSource = (SQLExprTableSource) ast.getTableSource(); - visitor.visit(tableSource); - return sb.toString(); + public String getTableAlias() { + return ast.getTableSource().getAlias(); } @Override diff --git a/test/src/main/java/com/alibaba/fescar/test/DataSourceBasicTest.java b/test/src/main/java/com/alibaba/fescar/test/DataSourceBasicTest.java index 190539339bc..81b932fa280 100644 --- a/test/src/main/java/com/alibaba/fescar/test/DataSourceBasicTest.java +++ b/test/src/main/java/com/alibaba/fescar/test/DataSourceBasicTest.java @@ -26,12 +26,16 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.JdbcTemplate; +import java.util.Date; + public class DataSourceBasicTest { public void runBusiness(JdbcTemplate jdbcTemplate) { -// jdbcTemplate.update("insert into user0 (id, name, gmt) values (?, ?, ?)", -// new Object[] { 1, "xxx", new Date() }); - jdbcTemplate.update("update user0 set name = 'yyyy' where id = ?", new Object[] {1}); -// jdbcTemplate.update("delete from user0 where id = ?", new Object[] {1}); + //jdbcTemplate.update("insert into user0 (id, name, gmt) values (?, ?, ?)", + // new Object[] { 2, "xxx", new Date() }); +// jdbcTemplate.update("update user0 a set a.name = 'yyyy' where a.id = ?", new Object[] {1}); +// jdbcTemplate.update("update user0 a set a.name = 'yyyy' where a.name = ?", new Object[] {"yyyy"}); +// jdbcTemplate.update("delete from user0 where id = ?", new Object[] {2}); + jdbcTemplate.queryForRowSet("select a.name from user0 a where a.id = ? for update", new Object[] {1}); }