Skip to content

Commit 6134ae9

Browse files
authored
Merge pull request #3 from codingapi/dev
Dev
2 parents 74890da + 64af8c8 commit 6134ae9

31 files changed

+515
-275
lines changed

pom.xml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.codingapi.dbstream</groupId>
66
<artifactId>dbstream-driver</artifactId>
7-
<version>1.0.8</version>
7+
<version>1.0.9</version>
88

99
<url>https://github.com/codingapi/dbstream-driver</url>
1010
<name>dbstream-driver</name>
@@ -34,8 +34,7 @@
3434
<lombok.version>1.18.42</lombok.version>
3535

3636
<!-- test dependencies properties-->
37-
<springboot.version>2.7.18</springboot.version>
38-
<h2.version>2.2.222</h2.version>
37+
<springboot.version>3.5.7</springboot.version>
3938
</properties>
4039

4140
<dependencies>
@@ -55,7 +54,14 @@
5554
<dependency>
5655
<groupId>com.h2database</groupId>
5756
<artifactId>h2</artifactId>
58-
<version>${h2.version}</version>
57+
<version>2.2.222</version>
58+
<scope>test</scope>
59+
</dependency>
60+
61+
<dependency>
62+
<groupId>org.postgresql</groupId>
63+
<artifactId>postgresql</artifactId>
64+
<version>42.7.3</version>
5965
<scope>test</scope>
6066
</dependency>
6167

src/main/java/com/codingapi/dbstream/driver/DBStreamProxyDriver.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.codingapi.dbstream.driver;
22

33
import com.codingapi.dbstream.interceptor.SQLRunningContext;
4-
import com.codingapi.dbstream.listener.SQLDeleteExecuteListener;
5-
import com.codingapi.dbstream.listener.SQLInsertExecuteListener;
6-
import com.codingapi.dbstream.listener.SQLUpdateExecuteListener;
4+
import com.codingapi.dbstream.listener.stream.SQLDeleteExecuteListener;
5+
import com.codingapi.dbstream.listener.stream.SQLInsertExecuteListener;
6+
import com.codingapi.dbstream.listener.stream.SQLUpdateExecuteListener;
77
import com.codingapi.dbstream.proxy.ConnectionProxy;
88
import com.codingapi.dbstream.scanner.DBMetaContext;
99
import com.codingapi.dbstream.scanner.DBMetaData;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.codingapi.dbstream.interceptor;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
import java.util.*;
7+
8+
/**
9+
* SQL执行参数信息
10+
*/
11+
public class SQLExecuteParam {
12+
13+
14+
/**
15+
* SQL参数,integer index模式
16+
*/
17+
@Getter
18+
private final Map<Integer,Object> indexParams;
19+
/**
20+
* SQL参数,string key 模型
21+
*/
22+
@Getter
23+
private final Map<String, Object> mapParams;
24+
25+
/**
26+
* 执行的sql
27+
*/
28+
@Getter
29+
@Setter
30+
private String sql;
31+
32+
public SQLExecuteParam() {
33+
this.indexParams = new HashMap<>();
34+
this.mapParams = new HashMap<>();
35+
}
36+
37+
38+
/**
39+
* 更新sql参数
40+
*
41+
* @param key 参数key
42+
* @param value 参数值
43+
*/
44+
public void setParam(String key, Object value) {
45+
mapParams.put(key, value);
46+
}
47+
48+
/**
49+
* 更新sql参数
50+
*
51+
* @param index 参数索引
52+
* @param value 参数值
53+
*/
54+
public void setParam(int index, Object value) {
55+
indexParams.put(index, value);
56+
}
57+
58+
/**
59+
* 清理参数
60+
*/
61+
public void cleanParams(){
62+
this.indexParams.clear();
63+
this.mapParams.clear();
64+
}
65+
66+
/**
67+
* 获取参数列表
68+
* @return List
69+
*/
70+
public List<Object> getListParams(){
71+
List<Object> list = new ArrayList<>();
72+
if (indexParams.isEmpty()) {
73+
return list;
74+
}
75+
List<Integer> keys = new ArrayList<>(indexParams.keySet());
76+
Collections.sort(keys);
77+
for(Integer key: keys){
78+
list.add(indexParams.get(key));
79+
}
80+
return list;
81+
}
82+
}

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

Lines changed: 115 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,25 @@
1717
public class SQLExecuteState {
1818

1919
/**
20-
* SQL参数,integer index模式
20+
* 执行SQL队列
2121
*/
22-
@Getter
23-
private final Map<Integer,Object> indexParams;
22+
private final List<SQLExecuteParam> sqlExecuteParams;
23+
2424
/**
25-
* SQL参数,string key 模型
25+
* 当前执行对象
26+
*/
27+
private SQLExecuteParam currentExecute;
28+
29+
/**
30+
* 模式判断
2631
*/
2732
@Getter
28-
private final Map<String, Object> mapParams;
33+
private boolean batchMode = false;
2934

3035
/**
31-
* 执行的sql
36+
* 当前绑定sql
3237
*/
3338
@Getter
34-
@Setter
3539
private String sql;
3640

3741
/**
@@ -78,9 +82,56 @@ public SQLExecuteState(String sql, ConnectionProxy connectionProxy, Statement st
7882
this.connectionProxy = connectionProxy;
7983
this.statement = statement;
8084
this.metaData = metaData;
85+
this.sqlExecuteParams = new ArrayList<>();
86+
87+
this.currentExecute = new SQLExecuteParam();
88+
this.currentExecute.setSql(sql);
89+
this.sqlExecuteParams.add(currentExecute);
90+
}
8191

82-
this.indexParams = new HashMap<>();
83-
this.mapParams = new HashMap<>();
92+
public void setSql(String sql){
93+
this.sql = sql;
94+
if(this.currentExecute!=null) {
95+
this.currentExecute.setSql(sql);
96+
}
97+
}
98+
99+
/**
100+
* 添加任务队列
101+
*
102+
* @param sql 执行sql
103+
*/
104+
public void addBatch(String sql) {
105+
batchMode = true;
106+
SQLExecuteParam executeParam = new SQLExecuteParam();
107+
executeParam.setSql(sql);
108+
this.sqlExecuteParams.add(executeParam);
109+
this.currentExecute = executeParam;
110+
}
111+
112+
/**
113+
* 添加任务队列
114+
*
115+
*/
116+
public void addBatch() {
117+
this.addBatch(this.sql);
118+
}
119+
120+
/**
121+
* 清空队列
122+
*/
123+
public void clearBatch() {
124+
this.sqlExecuteParams.clear();
125+
this.currentExecute = null;
126+
}
127+
128+
/**
129+
* 清理参数设置
130+
*/
131+
public void cleanParams(){
132+
if(this.currentExecute!=null) {
133+
this.currentExecute.cleanParams();
134+
}
84135
}
85136

86137
/**
@@ -114,7 +165,9 @@ public long getExecuteTimestamp() {
114165
* @param value 参数值
115166
*/
116167
public void setParam(String key, Object value) {
117-
mapParams.put(key, value);
168+
if(this.currentExecute!=null) {
169+
currentExecute.setParam(key, value);
170+
}
118171
}
119172

120173
/**
@@ -124,24 +177,52 @@ public void setParam(String key, Object value) {
124177
* @param value 参数值
125178
*/
126179
public void setParam(int index, Object value) {
127-
indexParams.put(index, value);
180+
if(this.currentExecute!=null) {
181+
currentExecute.setParam(index, value);
182+
}
128183
}
129184

130185
/**
131186
* 获取参数列表
187+
*
132188
* @return List
133189
*/
134-
public List<Object> getListParams(){
135-
List<Object> list = new ArrayList<>();
136-
if (indexParams.isEmpty()) {
137-
return list;
190+
public List<Object> getListParams() {
191+
if(batchMode){
192+
if(this.sqlExecuteParams.isEmpty()){
193+
return new ArrayList<>();
194+
}
195+
int size = this.sqlExecuteParams.size();
196+
return this.sqlExecuteParams.get(size-2).getListParams();
138197
}
139-
List<Integer> keys = new ArrayList<>(indexParams.keySet());
140-
Collections.sort(keys);
141-
for(Integer key: keys){
142-
list.add(indexParams.get(key));
198+
if(this.currentExecute!=null) {
199+
return currentExecute.getListParams();
143200
}
144-
return list;
201+
return new ArrayList<>();
202+
}
203+
204+
205+
/**
206+
* 获取Batch的SQLExecuteState
207+
* @return List
208+
*/
209+
public List<SQLExecuteState> getBatchSQLExecuteStateList(){
210+
if(this.batchMode){
211+
if(this.sqlExecuteParams.isEmpty()){
212+
return new ArrayList<>();
213+
}
214+
int size = this.sqlExecuteParams.size();
215+
List<SQLExecuteState> list = new ArrayList<>();
216+
List<SQLExecuteParam> paramList = this.sqlExecuteParams.subList(0,size-1);
217+
for(SQLExecuteParam executeParam:paramList){
218+
SQLExecuteState executeState = new SQLExecuteState(executeParam.getSql(), connectionProxy,statement,metaData);
219+
executeState.currentExecute = executeParam;
220+
list.add(executeState);
221+
}
222+
return list;
223+
224+
}
225+
return new ArrayList<>();
145226
}
146227

147228

@@ -167,13 +248,15 @@ public String getTransactionKey() {
167248

168249
/**
169250
* 查询
251+
*
170252
* @param sql sql
171253
* @return 查询结果
172254
* @throws SQLException 查询异常
173255
*/
174256
public List<Map<String, Object>> query(String sql) throws SQLException {
175-
return this.query(sql,new ArrayList<>());
257+
return this.query(sql, new ArrayList<>());
176258
}
259+
177260
/**
178261
* 查询
179262
*
@@ -216,10 +299,12 @@ public List<Map<String, Object>> getStatementGenerateKeys(DbTable dbTable) {
216299
Map<String, Object> map = new HashMap<>();
217300
ResultSetMetaData resultSetMetaData = rs.getMetaData();
218301
int columnCount = resultSetMetaData.getColumnCount();
219-
List<DbColumn> primaryKeyColumns = dbTable.getPrimaryColumns();
220302
for (int i = 1; i <= columnCount; i++) {
221-
DbColumn dbColumn = primaryKeyColumns.get(i - 1);
222-
map.put(dbColumn.getName(), rs.getObject(i));
303+
String columName = resultSetMetaData.getColumnName(i);
304+
DbColumn dbColumn = dbTable.getColumnByName(columName);
305+
if (dbColumn != null) {
306+
map.put(dbColumn.getName(), rs.getObject(i));
307+
}
223308
}
224309
list.add(map);
225310
}
@@ -232,6 +317,7 @@ public List<Map<String, Object>> getStatementGenerateKeys(DbTable dbTable) {
232317

233318
/**
234319
* 获取驱动配置信息
320+
*
235321
* @return Properties
236322
*/
237323
public Properties getDriverProperties() {
@@ -244,6 +330,7 @@ public Properties getDriverProperties() {
244330

245331
/**
246332
* 获取数据库的jdbcUrl
333+
*
247334
* @return jdbcUrl
248335
*/
249336
public String getJdbcUrl() {
@@ -256,10 +343,11 @@ public String getJdbcUrl() {
256343

257344
/**
258345
* 获取数据库的jdbcKey
346+
*
259347
* @return jdbcKey
260348
*/
261-
public String getJdbcKey(){
262-
if(metaData==null){
349+
public String getJdbcKey() {
350+
if (metaData == null) {
263351
return null;
264352
}
265353
return metaData.getKeyJdbcKey();
@@ -268,6 +356,7 @@ public String getJdbcKey(){
268356

269357
/**
270358
* 更新数据库的元数据信息
359+
*
271360
* @param tableName 表名
272361
*/
273362
public void updateMetaData(String tableName) throws SQLException {

0 commit comments

Comments
 (0)