-
Notifications
You must be signed in to change notification settings - Fork 8
/
SqlContextImpl.java
1258 lines (1119 loc) · 32.3 KB
/
SqlContextImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2017-present, Future Corporation
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package jp.co.future.uroborosql.context;
import java.io.InputStream;
import java.io.Reader;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLType;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jp.co.future.uroborosql.enums.SqlKind;
import jp.co.future.uroborosql.exception.ParameterNotFoundRuntimeException;
import jp.co.future.uroborosql.filter.SqlFilterManager;
import jp.co.future.uroborosql.filter.SqlFilterManagerImpl;
import jp.co.future.uroborosql.parameter.InOutParameter;
import jp.co.future.uroborosql.parameter.OutParameter;
import jp.co.future.uroborosql.parameter.Parameter;
import jp.co.future.uroborosql.parameter.ReaderParameter;
import jp.co.future.uroborosql.parameter.StreamParameter;
import jp.co.future.uroborosql.parameter.mapper.BindParameterMapperManager;
import jp.co.future.uroborosql.parser.TransformContext;
import jp.co.future.uroborosql.utils.BeanAccessor;
import jp.co.future.uroborosql.utils.StringUtils;
/**
* SQLコンテキスト実装クラス
*
* @author H.Sugimoto
*/
public class SqlContextImpl implements SqlContext {
/**
* @see #getParameterNames()
*/
private class ParameterNames extends AbstractSet<String> {
@Override
public Iterator<String> iterator() {
return parameterMap.keySet().iterator();
}
@Override
public int size() {
return parameterMap.size();
}
@Override
public boolean contains(final Object o) {
return parameterMap.containsKey(o);
}
}
/** where句の直後にくるANDやORを除外するための正規表現 */
protected static final Pattern WHERE_CLAUSE_PATTERN = Pattern
.compile("(?i)(?<clause>(^|\\s+)(WHERE\\s+(--.*|/\\*[^(/\\*|\\*/)]+?\\*/\\s*)*\\s*))(AND\\s+|OR\\s+)");
/** 各句の最初に現れるカンマを除去するための正規表現 */
protected static final Pattern REMOVE_FIRST_COMMA_PATTERN = Pattern
.compile(
"(?i)(?<keyword>((^|\\s+)(SELECT|ORDER\\s+BY|GROUP\\s+BY|SET)\\s+|\\(\\s*)(--.*|/\\*[^(/\\*|\\*/)]+?\\*/\\s*)*\\s*)(,)");
/** 不要な空白、改行を除去するための正規表現 */
protected static final Pattern CLEAR_BLANK_PATTERN = Pattern.compile("(?m)^\\s*(\\r\\n|\\r|\\n)");
/** ロガー */
private static final Logger LOG = LoggerFactory.getLogger(SqlContextImpl.class);
/** SQL名 */
private String sqlName;
/** 変換前のSQL文 */
private String originalSql;
/** 変換後のSQL文 */
private final StringBuilder executableSql = new StringBuilder();
/** 変換後のSQL文字列をキャッシュしたもの。 */
private String executableSqlCache = "";
/** SQL文の識別子 */
private String sqlId;
/** SQLを実行するスキーマ */
private String schema;
/** SQL実行の最大リトライ数 */
private int maxRetryCount = 0;
/** リトライを行う場合の待機時間(ms) */
private int retryWaitTime = 0;
/** パラメータ保持用マップ */
private Map<String, Parameter> parameterMap = new HashMap<>();
/** 定数パラメータ保持用マップ */
private Map<String, Parameter> constParameterMap = null;
/** SqlFilter管理クラス */
private SqlFilterManager sqlFilterManager = new SqlFilterManagerImpl();
/** バインド対象パラメータ名リスト */
private final List<String> bindNames = new ArrayList<>();
/** バインド変数リスト */
private final List<Object> bindVariables = new ArrayList<>();
/** 有効フラグ(BEGIN句で使用) */
private boolean enabled = true;
/** バッチ処理用パラメータ保持用マップリスト */
private final List<Map<String, Parameter>> batchParameters = new ArrayList<>();
/** 列の型の再定義保持用マップ */
private final Map<Integer, Integer> defineColumnTypeMap = new HashMap<>();
/** カーソルのタイプ(デフォルト値:カーソルは最初から最後まで順方向にしか移動できません。) */
private int resultSetType = ResultSet.TYPE_FORWARD_ONLY;
/** 変更可能性(デフォルト値:カーソルはデータの読み出ししかサポートしません。) */
private int resultSetConcurrency = ResultSet.CONCUR_READ_ONLY;
/** 実行するSQLの種別 */
private SqlKind sqlKind = SqlKind.NONE;
/** 自動採番するキーカラム名の配列 */
private String[] generatedKeyColumns;
/** 自動採番するキーカラム値の配列 */
private Object[] generatedKeyValues;
/** コンテキスト属性情報 */
private final Map<String, Object> contextAttributes = new HashMap<>();
/** 自動パラメータバインド関数(query用) */
private Consumer<SqlContext> queryAutoParameterBinder = null;
/** 自動パラメータバインド関数(update/batch/proc用) */
private Consumer<SqlContext> updateAutoParameterBinder = null;
/** パラメータ変換マネージャ */
private BindParameterMapperManager parameterMapperManager;
/** パラメータ名Set */
private ParameterNames parameterNames;
/** 更新処理実行時に通常の更新SQL発行の代わりに疑似的に実行する処理. */
private Function<SqlContext, Integer> updateMockAction;
/**
* コンストラクタ。
*/
SqlContextImpl() {
}
/**
* コピーコンストラクタ
*
* @param parent コピー元のSQLコンテキスト
*/
private SqlContextImpl(final SqlContextImpl parent) {
enabled = false;
sqlId = parent.sqlId;
sqlName = parent.sqlName;
maxRetryCount = parent.maxRetryCount;
retryWaitTime = parent.retryWaitTime;
parameterMap = parent.parameterMap;
constParameterMap = parent.constParameterMap;
sqlFilterManager = parent.sqlFilterManager;
batchParameters.addAll(parent.batchParameters);
defineColumnTypeMap.putAll(parent.defineColumnTypeMap);
resultSetType = parent.resultSetType;
resultSetConcurrency = parent.resultSetConcurrency;
sqlKind = parent.sqlKind;
contextAttributes.putAll(parent.contextAttributes);
queryAutoParameterBinder = parent.queryAutoParameterBinder;
parameterMapperManager = parent.parameterMapperManager;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.parser.TransformContext#copyTransformContext()
*/
@Override
public TransformContext copyTransformContext() {
return new SqlContextImpl(this);
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.parser.TransformContext#getExecutableSql()
*/
@Override
public String getExecutableSql() {
if (StringUtils.isEmpty(executableSqlCache)) {
if (executableSql.length() > 0) {
executableSqlCache = executableSql.toString();
if (executableSqlCache.toUpperCase().contains("WHERE")) {
// where句の直後に来るANDやORの除去
StringBuffer buff = new StringBuffer();
Matcher matcher = WHERE_CLAUSE_PATTERN.matcher(executableSqlCache);
while (matcher.find()) {
String whereClause = matcher.group("clause");
matcher.appendReplacement(buff, whereClause);
}
matcher.appendTail(buff);
executableSqlCache = buff.toString();
}
// 各句の直後に現れる不要なカンマの除去
StringBuffer buff = new StringBuffer();
Matcher removeCommaMatcher = REMOVE_FIRST_COMMA_PATTERN.matcher(executableSqlCache);
while (removeCommaMatcher.find()) {
String clauseWords = removeCommaMatcher.group("keyword");
removeCommaMatcher.appendReplacement(buff, clauseWords);
}
removeCommaMatcher.appendTail(buff);
executableSqlCache = buff.toString();
// 空行の除去
executableSqlCache = CLEAR_BLANK_PATTERN.matcher(executableSqlCache).replaceAll("");
}
}
return executableSqlCache;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.context.SqlContext#getSql()
*/
@Override
public String getSql() {
return originalSql;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.context.SqlContext#setSql(java.lang.String)
*/
@Override
public SqlContext setSql(final String originalSql) {
this.originalSql = originalSql;
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.context.SqlContext#getSqlName()
*/
@Override
public String getSqlName() {
return sqlName;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.context.SqlContext#setSqlName(java.lang.String)
*/
@Override
public SqlContext setSqlName(final String sqlName) {
this.sqlName = sqlName;
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.context.SqlContext#getSqlId()
*/
@Override
public String getSqlId() {
return sqlId;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.context.SqlContext#setSqlId(java.lang.String)
*/
@Override
public SqlContext setSqlId(final String sqlId) {
this.sqlId = sqlId;
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.context.SqlContext#getSchema()
*/
@Override
public String getSchema() {
return this.schema;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.context.SqlContext#setSchema(java.lang.String)
*/
@Override
public SqlContext setSchema(String schema) {
this.schema = schema;
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.context.SqlContext#getMaxRetryCount()
*/
@Override
public int getMaxRetryCount() {
return maxRetryCount;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.context.SqlContext#setMaxRetryCount(int)
*/
@Override
public SqlContext setMaxRetryCount(final int maxRetryCount) {
this.maxRetryCount = maxRetryCount;
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.context.SqlContext#getRetryWaitTime()
*/
@Override
public int getRetryWaitTime() {
return retryWaitTime;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.context.SqlContext#setRetryWaitTime(int)
*/
@Override
public SqlContext setRetryWaitTime(final int retryWaitTime) {
this.retryWaitTime = retryWaitTime;
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.parser.TransformContext#getParam(java.lang.String)
*/
@Override
public Parameter getParam(final String paramName) {
Parameter param = getBindParameter(paramName);
if (param == null) {
Map<String, Parameter> constParams = getConstParameterMap();
if (constParams != null) {
param = constParams.get(paramName.toUpperCase());
}
}
return param;
}
/**
* parameterMapから指定されたキーのパラメータを取得する
*
* @param paramName パラメータ名
* @return パラメータ
*/
private Parameter getBindParameter(final String paramName) {
// メソッド呼び出しかどうかで処理を振り分け
if (paramName.contains(".") && paramName.contains("(") && paramName.contains(")")) {
// メソッド呼び出しの場合は、SqlParserで値を評価するタイミングでparameterをaddしているので、そのまま返却する
return parameterMap.get(paramName);
} else {
String[] keys = paramName.split("\\.");
String baseName = keys[0];
Parameter parameter = parameterMap.get(baseName);
if (parameter == null) {
return null;
}
if (keys.length > 1) {
String propertyName = keys[1];
return parameter.createSubParameter(propertyName);
}
return parameter;
}
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#context()
*/
@Override
public SqlContext context() {
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#hasParam(java.lang.String)
*/
@Override
public boolean hasParam(final String paramName) {
return parameterMap.containsKey(paramName);
}
/**
* パラメータの追加
*
* @param parameter パラメータ
*
* @return SqlContext
*/
private SqlContext param(final Parameter parameter) {
parameterMap.put(parameter.getParameterName(), parameter);
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.parser.TransformContext#param(java.lang.String, java.lang.Object)
*/
@Override
public <V> SqlContext param(final String parameterName, final V value) {
return param(new Parameter(parameterName, value));
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#param(String, Supplier)
*/
@Override
public <V> SqlContext param(final String paramName, final Supplier<V> supplier) {
return this.param(paramName, supplier != null ? supplier.get() : null);
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#paramIfAbsent(java.lang.String, java.lang.Object)
*/
@Override
public <V> SqlContext paramIfAbsent(final String parameterName, final V value) {
if (!hasParam(parameterName)) {
param(parameterName, value);
}
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#paramList(String, Object...)
*/
@Override
@Deprecated
public <V> SqlContext paramList(final String parameterName, @SuppressWarnings("unchecked") final V... value) {
return param(parameterName, Arrays.asList(value));
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#paramList(java.lang.String, java.util.function.Supplier)
*/
@Override
@Deprecated
public <V> SqlContext paramList(final String parameterName, final Supplier<Iterable<V>> supplier) {
return param(parameterName, supplier);
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#paramListIfAbsent(String, Object...)
*/
@Override
@Deprecated
public <V> SqlContext paramListIfAbsent(final String parameterName,
@SuppressWarnings("unchecked") final V... value) {
return paramIfAbsent(parameterName, Arrays.asList(value));
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#paramMap(java.util.Map)
*/
@Override
public SqlContext paramMap(final Map<String, Object> paramMap) {
if (paramMap != null) {
paramMap.forEach((k, v) -> param(k, v));
}
return this;
}
@Override
public <V> SqlContext paramBean(final V bean) {
if (bean != null) {
BeanAccessor.fields(bean.getClass()).stream()
.forEach(f -> param(f.getName(), BeanAccessor.value(f, bean)));
}
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#param(java.lang.String, java.lang.Object, java.sql.SQLType)
*/
@Override
public <V> SqlContext param(final String parameterName, final V value, final SQLType sqlType) {
return param(new Parameter(parameterName, value, sqlType));
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#paramIfAbsent(java.lang.String, java.lang.Object, java.sql.SQLType)
*/
@Override
public <V> SqlContext paramIfAbsent(final String parameterName, final V value, final SQLType sqlType) {
if (!hasParam(parameterName)) {
param(parameterName, value, sqlType);
}
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#param(java.lang.String, java.lang.Object, int)
*/
@Override
public <V> SqlContext param(final String parameterName, final V value, final int sqlType) {
return param(new Parameter(parameterName, value, sqlType));
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#paramIfAbsent(java.lang.String, java.lang.Object, int)
*/
@Override
public <V> SqlContext paramIfAbsent(final String parameterName, final V value, final int sqlType) {
if (!hasParam(parameterName)) {
param(parameterName, value, sqlType);
}
return this;
}
/**
* パラメータ名のリストを取得する
*
* @return パラメータ名のリスト
*/
public Set<String> getParameterNames() {
return parameterNames != null ? parameterNames : (parameterNames = new ParameterNames());
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.ProcedureFluent#outParam(java.lang.String, java.sql.SQLType)
*/
@Override
public SqlContext outParam(final String parameterName, final SQLType sqlType) {
return param(new OutParameter(parameterName, sqlType));
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.ProcedureFluent#outParam(java.lang.String, int)
*/
@Override
public SqlContext outParam(final String parameterName, final int sqlType) {
return param(new OutParameter(parameterName, sqlType));
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.ProcedureFluent#inOutParam(java.lang.String, java.lang.Object, java.sql.SQLType)
*/
@Override
public <V> SqlContext inOutParam(final String parameterName, final V value, final SQLType sqlType) {
return param(new InOutParameter(parameterName, value, sqlType));
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.ProcedureFluent#inOutParamIfAbsent(java.lang.String, java.lang.Object, java.sql.SQLType)
*/
@Override
public <V> SqlContext inOutParamIfAbsent(final String parameterName, final V value, final SQLType sqlType) {
if (!hasParam(parameterName)) {
inOutParam(parameterName, value, sqlType);
}
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.ProcedureFluent#inOutParam(java.lang.String, java.lang.Object, int)
*/
@Override
public <V> SqlContext inOutParam(final String parameterName, final V value, final int sqlType) {
return param(new InOutParameter(parameterName, value, sqlType));
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.ProcedureFluent#inOutParamIfAbsent(java.lang.String, java.lang.Object, int)
*/
@Override
public <V> SqlContext inOutParamIfAbsent(final String parameterName, final V value, final int sqlType) {
if (!hasParam(parameterName)) {
inOutParam(parameterName, value, sqlType);
}
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#blobParam(java.lang.String, java.io.InputStream)
*/
@Override
public SqlContext blobParam(final String parameterName, final InputStream value) {
return param(new StreamParameter(parameterName, value));
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#blobParamIfAbsent(java.lang.String, java.io.InputStream)
*/
@Override
public SqlContext blobParamIfAbsent(final String parameterName, final InputStream value) {
if (!hasParam(parameterName)) {
blobParam(parameterName, value);
}
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#blobParam(java.lang.String, java.io.InputStream, int)
*/
@Override
public SqlContext blobParam(final String parameterName, final InputStream value, final int len) {
return param(new StreamParameter(parameterName, value, len));
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#blobParamIfAbsent(java.lang.String, java.io.InputStream, int)
*/
@Override
public SqlContext blobParamIfAbsent(final String parameterName, final InputStream value, final int len) {
if (!hasParam(parameterName)) {
blobParam(parameterName, value, len);
}
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#clobParam(java.lang.String, java.io.Reader)
*/
@Override
public SqlContext clobParam(final String paramName, final Reader value) {
return param(new ReaderParameter(paramName, value));
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#clobParamIfAbsent(java.lang.String, java.io.Reader)
*/
@Override
public SqlContext clobParamIfAbsent(final String paramName, final Reader value) {
if (!hasParam(paramName)) {
clobParam(paramName, value);
}
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#clobParam(java.lang.String, java.io.Reader, int)
*/
@Override
public SqlContext clobParam(final String paramName, final Reader value, final int len) {
return param(new ReaderParameter(paramName, value, len));
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#clobParamIfAbsent(java.lang.String, java.io.Reader, int)
*/
@Override
public SqlContext clobParamIfAbsent(final String paramName, final Reader value, final int len) {
if (!hasParam(paramName)) {
clobParam(paramName, value, len);
}
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#retry(int)
*/
@Override
public SqlContext retry(final int count) {
return retry(count, 0);
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#retry(int, int)
*/
@Override
public SqlContext retry(final int count, final int waitTime) {
return this.setMaxRetryCount(count).setRetryWaitTime(waitTime);
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.fluent.SqlFluent#sqlId(String)
*/
@Override
public SqlContext sqlId(final String sqlId) {
this.setSqlId(sqlId);
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.parser.TransformContext#addSqlPart(java.lang.String)
*/
@Override
public TransformContext addSqlPart(final String sqlPart) {
executableSql.append(sqlPart);
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.parser.TransformContext#isEnabled()
*/
@Override
public boolean isEnabled() {
return enabled;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.parser.TransformContext#setEnabled(boolean)
*/
@Override
public void setEnabled(final boolean enabled) {
this.enabled = enabled;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.parser.TransformContext#addBindName(java.lang.String)
*/
@Override
public TransformContext addBindName(final String bindName) {
bindNames.add(bindName);
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.parser.TransformContext#addBindNames(java.util.List)
*/
@Override
public TransformContext addBindNames(final List<String> bindNames) {
this.bindNames.addAll(bindNames);
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.parser.TransformContext#getBindNames()
*/
@Override
public List<String> getBindNames() {
return bindNames;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.parser.TransformContext#addBindVariable(java.lang.Object)
*/
@Override
public TransformContext addBindVariable(final Object bindVariable) {
bindVariables.add(bindVariable);
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.parser.TransformContext#addBindVariables(java.lang.Object[])
*/
@Override
public TransformContext addBindVariables(final Object[] bindVariables) {
for (Object bindVariable : bindVariables) {
this.bindVariables.add(bindVariable);
}
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.parser.TransformContext#getBindVariables()
*/
@Override
public Object[] getBindVariables() {
return bindVariables.toArray();
}
/**
* バインドパラメータ配列取得
*
* @return バインドパラメータ配列
*/
public Parameter[] getBindParameters() {
return bindNames.stream().map(this::getParam).filter(Objects::nonNull).toArray(Parameter[]::new);
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.context.SqlContext#bindParams(java.sql.PreparedStatement)
*/
@Override
public void bindParams(final PreparedStatement preparedStatement) throws SQLException {
Parameter[] bindParameters = getBindParameters();
Set<String> matchParams = new HashSet<>();
int parameterIndex = 1;
for (Parameter bindParameter : bindParameters) {
Parameter parameter = getSqlFilterManager().doParameter(bindParameter);
parameterIndex = parameter.setParameter(preparedStatement, parameterIndex, parameterMapperManager);
matchParams.add(parameter.getParameterName());
}
// SQL上のバインドパラメータ群(bindNames)に対応する値がすべて設定されているかどうかをチェックする
if (!matchParams.containsAll(bindNames)) {
Set<String> missMatchParams = new LinkedHashSet<>(bindNames);
missMatchParams.removeAll(matchParams);
throw new ParameterNotFoundRuntimeException("Parameter " + missMatchParams.toString() + " is not found.");
}
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.context.SqlContext#bindBatchParams(java.sql.PreparedStatement)
*/
@Override
public void bindBatchParams(final PreparedStatement preparedStatement) throws SQLException {
for (Map<String, Parameter> paramMap : batchParameters) {
parameterMap = paramMap;
bindParams(preparedStatement);
preparedStatement.addBatch();
}
LOG.debug("{} items Added for batch process.", batchParameters.size());
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.context.SqlContext#getOutParams(java.sql.CallableStatement)
*/
@Override
public Map<String, Object> getOutParams(final CallableStatement callableStatement) throws SQLException {
Map<String, Object> out = new HashMap<>();
Parameter[] bindParameters = getBindParameters();
int parameterIndex = 1;
for (Parameter parameter : bindParameters) {
if (parameter instanceof OutParameter) {
String key = parameter.getParameterName();
out.put(key, getSqlFilterManager().doOutParameter(key, callableStatement.getObject(parameterIndex)));
}
parameterIndex++;
}
LOG.debug("Stored procedure out parameter[{}]", out);
return out;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.context.SqlContext#addBatch()
*/
@Override
public SqlContext addBatch() {
acceptUpdateAutoParameterBinder();
batchParameters.add(parameterMap);
parameterMap = new HashMap<>();
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.context.SqlContext#clearBatch()
*/
@Override
public SqlContext clearBatch() {
batchParameters.clear();
return this;
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.context.SqlContext#batchCount()
*/
@Override
public int batchCount() {
return batchParameters.size();
}
/**
* {@inheritDoc}
*
* @see jp.co.future.uroborosql.context.SqlContext#acceptQueryAutoParameterBinder()
*/
@Override
public void acceptQueryAutoParameterBinder() {
if (queryAutoParameterBinder != null) {
queryAutoParameterBinder.accept(this);
}
}