1717public 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