1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.scripting.thymeleaf;
17
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.io.InputStreamReader;
21 import java.lang.reflect.InvocationTargetException;
22 import java.nio.charset.Charset;
23 import java.nio.charset.StandardCharsets;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Optional;
29 import java.util.Properties;
30 import java.util.function.Consumer;
31 import java.util.function.Function;
32 import java.util.stream.Stream;
33
34 import org.mybatis.scripting.thymeleaf.PropertyAccessor.BuiltIn.StandardPropertyAccessor;
35 import org.mybatis.scripting.thymeleaf.processor.BindVariableRender;
36 import org.thymeleaf.util.ClassLoaderUtils;
37 import org.thymeleaf.util.StringUtils;
38
39
40
41
42
43
44
45
46 public class SqlGeneratorConfig {
47
48 private static class PropertyKeys {
49 private static final String CONFIG_FILE = "mybatis-thymeleaf.config.file";
50 private static final String CONFIG_ENCODING = "mybatis-thymeleaf.config.encoding";
51 }
52
53 private static class Defaults {
54 private static final String PROPERTIES_FILE = "mybatis-thymeleaf.properties";
55 }
56
57 private static final Map<Class<?>, Function<String, Object>> TYPE_CONVERTERS;
58
59 static {
60 Map<Class<?>, Function<String, Object>> converters = new HashMap<>();
61 converters.put(boolean.class, v -> Boolean.valueOf(v.trim()));
62 converters.put(String.class, String::trim);
63 converters.put(Character[].class, v -> Stream.of(v.split(",")).map(String::trim).filter(e -> e.length() == 1)
64 .map(e -> e.charAt(0)).toArray(Character[]::new));
65 converters.put(Character.class, v -> v.trim().charAt(0));
66 converters.put(Charset.class, v -> Charset.forName(v.trim()));
67 converters.put(Long.class, v -> Long.valueOf(v.trim()));
68 converters.put(String[].class, v -> Stream.of(v.split(",")).map(String::trim).toArray(String[]::new));
69 converters.put(Class.class, SqlGeneratorConfig::toClassForName);
70 TYPE_CONVERTERS = Collections.unmodifiableMap(converters);
71 }
72
73
74
75
76 private boolean use2way = true;
77
78
79
80
81 private TemplateEngineCustomizer customizer;
82
83
84
85
86 private final TemplateFileConfig templateFile = new TemplateFileConfig();
87
88
89
90
91 private final DialectConfig dialect = new DialectConfig();
92
93
94
95
96
97
98
99
100
101 public boolean isUse2way() {
102 return use2way;
103 }
104
105
106
107
108
109
110
111 public void setUse2way(boolean use2way) {
112 this.use2way = use2way;
113 }
114
115
116
117
118
119
120
121
122
123
124
125 @Deprecated
126 public Class<? extends TemplateEngineCustomizer> getCustomizer() {
127 return customizer == null ? null : customizer.getClass();
128 }
129
130
131
132
133
134
135
136 @Deprecated
137 public void setCustomizer(Class<? extends TemplateEngineCustomizer> customizer) {
138 this.customizer = newInstanceForType(customizer);
139 }
140
141 public TemplateEngineCustomizer getCustomizerInstance() {
142 return customizer;
143 }
144
145 public void setCustomizerInstance(TemplateEngineCustomizer customizer) {
146 this.customizer = customizer;
147 }
148
149
150
151
152
153
154 public TemplateFileConfig getTemplateFile() {
155 return templateFile;
156 }
157
158
159
160
161
162
163 public DialectConfig getDialect() {
164 return dialect;
165 }
166
167
168
169
170
171
172 public static class TemplateFileConfig {
173
174
175
176
177 private Charset encoding = StandardCharsets.UTF_8;
178
179
180
181
182 private String baseDir = "";
183
184
185
186
187
188 private String[] patterns = { "*.sql" };
189
190
191
192
193 private boolean cacheEnabled = true;
194
195
196
197
198 private Long cacheTtl;
199
200
201
202
203
204
205
206
207
208 public Charset getEncoding() {
209 return encoding;
210 }
211
212
213
214
215
216
217
218 public void setEncoding(Charset encoding) {
219 this.encoding = encoding;
220 }
221
222
223
224
225
226
227
228
229
230 public String getBaseDir() {
231 return baseDir;
232 }
233
234
235
236
237
238
239
240 public void setBaseDir(String baseDir) {
241 this.baseDir = baseDir;
242 }
243
244
245
246
247
248
249
250
251
252 public String[] getPatterns() {
253 return patterns;
254 }
255
256
257
258
259
260
261
262 public void setPatterns(String... patterns) {
263 this.patterns = patterns;
264 }
265
266
267
268
269
270
271
272
273
274 public boolean isCacheEnabled() {
275 return cacheEnabled;
276 }
277
278
279
280
281
282
283
284 public void setCacheEnabled(boolean cacheEnabled) {
285 this.cacheEnabled = cacheEnabled;
286 }
287
288
289
290
291
292
293
294
295
296 public Long getCacheTtl() {
297 return cacheTtl;
298 }
299
300
301
302
303
304
305
306 public void setCacheTtl(Long cacheTtl) {
307 this.cacheTtl = cacheTtl;
308 }
309
310 }
311
312
313
314
315
316
317 public static class DialectConfig {
318
319
320
321
322 private String prefix = "mb";
323
324
325
326
327 private Character likeEscapeChar = '\\';
328
329
330
331
332 private String likeEscapeClauseFormat = "ESCAPE '%s'";
333
334
335
336
337
338 private Character[] likeAdditionalEscapeTargetChars;
339
340
341
342
343 private BindVariableRender bindVariableRender;
344
345
346
347
348
349
350
351
352
353 public String getPrefix() {
354 return prefix;
355 }
356
357
358
359
360
361
362
363 public void setPrefix(String prefix) {
364 this.prefix = prefix;
365 }
366
367
368
369
370
371
372
373
374
375 public Character getLikeEscapeChar() {
376 return likeEscapeChar;
377 }
378
379
380
381
382
383
384
385 public void setLikeEscapeChar(Character likeEscapeChar) {
386 this.likeEscapeChar = likeEscapeChar;
387 }
388
389
390
391
392
393
394
395
396
397 public String getLikeEscapeClauseFormat() {
398 return likeEscapeClauseFormat;
399 }
400
401
402
403
404
405
406
407 public void setLikeEscapeClauseFormat(String likeEscapeClauseFormat) {
408 this.likeEscapeClauseFormat = likeEscapeClauseFormat;
409 }
410
411
412
413
414
415
416
417
418
419 public Character[] getLikeAdditionalEscapeTargetChars() {
420 return likeAdditionalEscapeTargetChars;
421 }
422
423
424
425
426
427
428
429 public void setLikeAdditionalEscapeTargetChars(Character... likeAdditionalEscapeTargetChars) {
430 this.likeAdditionalEscapeTargetChars = likeAdditionalEscapeTargetChars;
431 }
432
433
434
435
436
437
438
439
440
441
442
443 @Deprecated
444 public Class<? extends BindVariableRender> getBindVariableRender() {
445 return bindVariableRender == null ? null : bindVariableRender.getClass();
446 }
447
448
449
450
451
452
453
454
455 @Deprecated
456 public void setBindVariableRender(Class<? extends BindVariableRender> bindVariableRender) {
457 this.bindVariableRender = newInstanceForType(bindVariableRender);
458 }
459
460 public BindVariableRender getBindVariableRenderInstance() {
461 return bindVariableRender;
462 }
463
464 public void setBindVariableRenderInstance(BindVariableRender bindVariableRender) {
465 this.bindVariableRender = bindVariableRender;
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 public static SqlGeneratorConfig newInstance() {
552 SqlGeneratorConfig config = new SqlGeneratorConfig();
553 applyDefaultProperties(config);
554 return config;
555 }
556
557
558
559
560
561
562
563
564
565
566
567
568
569 public static SqlGeneratorConfig newInstanceWithResourcePath(String resourcePath) {
570 SqlGeneratorConfig config = new SqlGeneratorConfig();
571 applyResourcePath(config, resourcePath);
572 return config;
573 }
574
575
576
577
578
579
580
581
582
583
584
585 public static SqlGeneratorConfig newInstanceWithProperties(Properties customProperties) {
586 SqlGeneratorConfig config = new SqlGeneratorConfig();
587 applyProperties(config, customProperties);
588 return config;
589 }
590
591
592
593
594
595
596
597
598
599
600
601 public static SqlGeneratorConfig newInstanceWithCustomizer(Consumer<SqlGeneratorConfig> customizer) {
602 SqlGeneratorConfig config = new SqlGeneratorConfig();
603 customizer.accept(config);
604 applyDefaultProperties(config);
605 return config;
606 }
607
608
609
610
611
612
613
614 static <T extends SqlGeneratorConfig> void applyDefaultProperties(T config) {
615 applyProperties(config, loadDefaultProperties());
616 }
617
618
619
620
621
622
623
624
625
626 static <T extends SqlGeneratorConfig> void applyResourcePath(T config, String resourcePath) {
627 Properties properties = loadDefaultProperties();
628 properties.putAll(loadProperties(resourcePath));
629 applyProperties(config, properties);
630 }
631
632
633
634
635
636
637
638
639
640 static <T extends SqlGeneratorConfig> void applyProperties(T config, Properties customProperties) {
641 Properties properties = loadDefaultProperties();
642 Optional.ofNullable(customProperties).ifPresent(properties::putAll);
643 override(config, properties);
644 }
645
646
647
648
649
650
651
652
653
654
655
656 static <T> T newInstanceForType(Class<T> type) {
657 try {
658 return type.getConstructor().newInstance();
659 } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
660 throw new IllegalStateException("Cannot create an instance for class: " + type, e);
661 }
662 }
663
664 private static void override(SqlGeneratorConfig config, Properties properties) {
665 PropertyAccessor standardPropertyAccessor = PropertyAccessor.BuiltIn.STANDARD;
666 try {
667 properties.forEach((key, value) -> {
668 String propertyPath = StringUtils.unCapitalize(StringUtils.capitalizeWords(key, "-").replaceAll("-", ""));
669 try {
670 Object target = config;
671 String propertyName;
672 if (propertyPath.indexOf('.') != -1) {
673 String[] propertyPaths = StringUtils.split(propertyPath, ".");
674 propertyName = propertyPaths[propertyPaths.length - 1];
675 for (String path : Arrays.copyOf(propertyPaths, propertyPaths.length - 1)) {
676 target = standardPropertyAccessor.getPropertyValue(target, path);
677 }
678 } else {
679 propertyName = propertyPath;
680 }
681 Object convertedValue = TYPE_CONVERTERS
682 .getOrDefault(standardPropertyAccessor.getPropertyType(target.getClass(), propertyName), v -> v)
683 .apply(value.toString());
684 standardPropertyAccessor.setPropertyValue(target, propertyName, convertedValue);
685 } catch (IllegalArgumentException e) {
686 throw new IllegalArgumentException(
687 String.format("Detected an invalid property. key='%s' value='%s'", key, value), e);
688 }
689 });
690 } finally {
691 StandardPropertyAccessor.clearCache();
692 }
693 }
694
695 private static Properties loadDefaultProperties() {
696 return loadProperties(System.getProperty(PropertyKeys.CONFIG_FILE, Defaults.PROPERTIES_FILE));
697 }
698
699 private static Properties loadProperties(String resourcePath) {
700 Properties properties = new Properties();
701 Optional.ofNullable(ClassLoaderUtils.findResourceAsStream(resourcePath)).ifPresent(in -> {
702 Charset encoding = Optional.ofNullable(System.getProperty(PropertyKeys.CONFIG_ENCODING)).map(Charset::forName)
703 .orElse(StandardCharsets.UTF_8);
704 try (InputStreamReader inReader = new InputStreamReader(in, encoding);
705 BufferedReader bufReader = new BufferedReader(inReader)) {
706 properties.load(bufReader);
707 } catch (IOException e) {
708 throw new IllegalStateException(e);
709 }
710 });
711 return properties;
712 }
713
714 private static Class<?> toClassForName(String value) {
715 try {
716 return ClassLoaderUtils.loadClass(value.trim());
717 } catch (ClassNotFoundException e) {
718 throw new IllegalStateException(e);
719 }
720 }
721
722 }