Skip to content

Commit 5f0b7a6

Browse files
committed
fix SQLExecuteState indexParams
1 parent 0f02737 commit 5f0b7a6

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

src/main/java/com/codingapi/dbstream/interceptor/SQLExecuteState.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class SQLExecuteState {
2020
* SQL参数,integer index模式
2121
*/
2222
@Getter
23-
private final List<Object> listParams;
23+
private final Map<Integer,Object> indexParams;
2424
/**
2525
* SQL参数,string key 模型
2626
*/
@@ -51,7 +51,7 @@ public class SQLExecuteState {
5151
* 数据库的当前链接对象
5252
*/
5353
@Getter
54-
private final ConnectionProxy connection;
54+
private final ConnectionProxy connectionProxy;
5555

5656
/**
5757
* sql执行结果
@@ -73,13 +73,13 @@ public class SQLExecuteState {
7373
private long afterTimestamp;
7474

7575

76-
public SQLExecuteState(String sql, ConnectionProxy connection, Statement statement, DBMetaData metaData) {
76+
public SQLExecuteState(String sql, ConnectionProxy connectionProxy, Statement statement, DBMetaData metaData) {
7777
this.sql = sql;
78-
this.connection = connection;
78+
this.connectionProxy = connectionProxy;
7979
this.statement = statement;
8080
this.metaData = metaData;
8181

82-
this.listParams = new ArrayList<>();
82+
this.indexParams = new HashMap<>();
8383
this.mapParams = new HashMap<>();
8484
}
8585

@@ -124,7 +124,24 @@ public void setParam(String key, Object value) {
124124
* @param value 参数值
125125
*/
126126
public void setParam(int index, Object value) {
127-
listParams.add(index - 1, value);
127+
indexParams.put(index, value);
128+
}
129+
130+
/**
131+
* 获取参数列表
132+
* @return List
133+
*/
134+
public List<Object> getListParams(){
135+
List<Object> list = new ArrayList<>();
136+
if (indexParams.isEmpty()) {
137+
return list;
138+
}
139+
List<Integer> keys = new ArrayList<>(indexParams.keySet());
140+
Collections.sort(keys);
141+
for(Integer key: keys){
142+
list.add(indexParams.get(key));
143+
}
144+
return list;
128145
}
129146

130147

@@ -142,8 +159,8 @@ public DbTable getDbTable(String tableName) {
142159
* 获取事务标识信息
143160
*/
144161
public String getTransactionKey() {
145-
if (this.connection != null) {
146-
return this.connection.getTransactionKey();
162+
if (this.connectionProxy != null) {
163+
return this.connectionProxy.getTransactionKey();
147164
}
148165
return null;
149166
}
@@ -157,7 +174,7 @@ public String getTransactionKey() {
157174
* @throws SQLException 查询异常
158175
*/
159176
public List<Map<String, Object>> query(String sql, List<Object> params) throws SQLException {
160-
PreparedStatement preparedStatement = connection.prepareStatement(sql);
177+
PreparedStatement preparedStatement = connectionProxy.getConnection().prepareStatement(sql);
161178
for (int i = 0; i < params.size(); i++) {
162179
Object param = params.get(i);
163180
preparedStatement.setObject(i + 1, param);
@@ -235,7 +252,7 @@ public String getJdbcUrl() {
235252
public void updateMetaData(String tableName) throws SQLException {
236253
// 当前表需要更新时,将会连同所有带更新的表一次性全部更新
237254
if (this.metaData.isUpdateTableMeta(tableName)) {
238-
DBScanner dbScanner = new DBScanner(connection, getDriverProperties());
255+
DBScanner dbScanner = new DBScanner(connectionProxy.getConnection(), getDriverProperties());
239256
dbScanner.updateMetadata(this.metaData);
240257
}
241258
}

src/main/java/com/codingapi/dbstream/proxy/ConnectionProxy.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
public class ConnectionProxy implements Connection {
1515

16+
@Getter
1617
private final Connection connection;
1718
private final DBMetaData metaData;
1819

0 commit comments

Comments
 (0)