forked from querydsl/querydsl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AntMetaDataExporter.java
750 lines (606 loc) · 19.8 KB
/
AntMetaDataExporter.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
/*
* Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.querydsl.sql.codegen.ant;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Comparator;
import java.util.List;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import com.google.common.collect.Lists;
import com.mysema.codegen.model.SimpleType;
import com.querydsl.codegen.BeanSerializer;
import com.querydsl.sql.Configuration;
import com.querydsl.sql.SQLTemplates;
import com.querydsl.sql.codegen.DefaultNamingStrategy;
import com.querydsl.sql.codegen.MetaDataExporter;
import com.querydsl.sql.codegen.NamingStrategy;
import com.querydsl.sql.codegen.support.NumericMapping;
import com.querydsl.sql.codegen.support.RenameMapping;
import com.querydsl.sql.codegen.support.TypeMapping;
import com.querydsl.sql.types.Type;
/**
* {@code AntMetaDataExporter} exports JDBC metadata to Querydsl query types
*
* @author tiwe
*
*/
public class AntMetaDataExporter extends Task {
/**
* JDBC driver class name
*/
private String jdbcDriver;
/**
* JDBC connection url
*/
private String jdbcUrl;
/**
* JDBC connection username
*/
private String jdbcUser;
/**
* JDBC connection password
*/
private String jdbcPassword;
/**
* name prefix for querydsl-types (default: "Q")
*/
private String namePrefix;
/**
* name suffix for querydsl-types (default: "")
*/
private String nameSuffix;
/**
* name prefix for bean types (default: "")
*/
private String beanPrefix;
/**
* name suffix for bean types (default: "")
*/
private String beanSuffix;
/**
* package name for sources
*/
private String packageName;
/**
* package name for bean sources (default: packageName)
*/
private String beanPackageName;
/**
* schemaPattern a schema name pattern; must match the schema name
* as it is stored in the database; "" retrieves those without a schema;
* {@code null} means that the schema name should not be used to narrow
* the search (default: null)
*/
private String schemaPattern;
/**
* tableNamePattern a table name pattern; must match the
* table name as it is stored in the database (default: null)
*/
private String tableNamePattern;
/**
* target source folder to create the sources into (e.g. target/generated-sources/java)
*/
private String targetFolder;
/**
* target source folder to create the bean sources into
*/
private String beansTargetFolder;
/**
* naming strategy class to override (default: DefaultNamingStrategy)
*/
private String namingStrategyClass = DefaultNamingStrategy.class.getName();
/**
* bean serializer class (default: BeanSerializer)
*/
private String beanSerializerClass = BeanSerializer.class.getName();
/**
* serializer class to override
*/
private String serializerClass;
/**
* serialize beans as well
*/
private boolean exportBeans;
/**
* additional bean interfaces
*/
private String[] beanInterfaces;
/**
* toString() method addition to beans
*/
private boolean beanAddToString;
/**
* full constructor addition to beans
*/
private boolean beanAddFullConstructor;
/**
* use supertype in beans
*/
private boolean beanPrintSupertype;
/**
* wrap key properties into inner classes (default: false)
*/
private boolean innerClassesForKeys;
/**
* export validation annotations (default: false)
*/
private boolean validationAnnotations;
/**
* export column annotations (default: false)
*/
private boolean columnAnnotations;
/**
* custom types to use
*/
private String[] customTypes;
/**
* scala generation mode
*/
private boolean createScalaSources;
/**
* append schema name to package
*/
private boolean schemaToPackage;
/**
* lower case normalization of names
*/
private boolean lowerCase;
/**
* export tables
*/
private boolean exportTables = true;
/**
* export views
*/
private boolean exportViews = true;
/**
* export all
*/
private boolean exportAll;
/**
* export primary keys
*/
private boolean exportPrimaryKeys = true;
/**
* export foreign keys
*/
private boolean exportForeignKeys = true;
/**
* export direct foreign keys
*/
private boolean exportDirectForeignKeys = true;
/**
* export inverse foreign keys
*/
private boolean exportInverseForeignKeys = true;
/**
* override default column order (default: alphabetical)
*/
private String columnComparatorClass;
/**
* spatial type support
*/
private boolean spatial;
/**
* Comma-separated list of table types to export (allowable values will
* depend on JDBC driver). Allows for arbitrary set of types to be exported,
* e.g.: "TABLE, MATERIALIZED VIEW". The exportTables and exportViews
* parameters will be ignored if this parameter is set. (default: none)
*/
private String tableTypesToExport;
/**
* java import added to generated query classes:
* com.bar for package (without .* notation)
* com.bar.Foo for class
*/
private String[] imports;
// Ant only
private String sourceEncoding;
/**
* custom type mappings to use
*/
private List<TypeMapping> typeMappings = Lists.newArrayList();
/**
* custom numeric mappings
*/
private List<NumericMapping> numericMappings = Lists.newArrayList();
/**
* custom rename mappings
*/
private List<RenameMapping> renameMappings = Lists.newArrayList();
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void execute() {
if (targetFolder == null) {
throw new BuildException("targetFolder is a mandatory property");
}
Connection dbConn = null;
try {
Class.forName(jdbcDriver).newInstance();
dbConn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
Configuration configuration = new Configuration(SQLTemplates.DEFAULT);
MetaDataExporter exporter = new MetaDataExporter();
if (namePrefix != null) {
exporter.setNamePrefix(namePrefix);
}
if (nameSuffix != null) {
exporter.setNameSuffix(nameSuffix);
}
if (beanPrefix != null) {
exporter.setBeanPrefix(beanPrefix);
}
if (beanSuffix != null) {
exporter.setBeanSuffix(beanSuffix);
}
if (beansTargetFolder != null) {
exporter.setBeansTargetFolder(new File(beansTargetFolder));
}
exporter.setPackageName(packageName);
exporter.setBeanPackageName(beanPackageName);
exporter.setTargetFolder(new File(targetFolder));
exporter.setNamingStrategy((NamingStrategy) Class.forName(namingStrategyClass).newInstance());
exporter.setInnerClassesForKeys(innerClassesForKeys);
exporter.setSchemaPattern(schemaPattern);
exporter.setTableNamePattern(tableNamePattern);
exporter.setColumnAnnotations(columnAnnotations);
exporter.setValidationAnnotations(validationAnnotations);
exporter.setSchemaToPackage(schemaToPackage);
exporter.setLowerCase(lowerCase);
exporter.setExportTables(exportTables);
exporter.setExportViews(exportViews);
exporter.setExportAll(exportAll);
exporter.setTableTypesToExport(tableTypesToExport);
exporter.setExportPrimaryKeys(exportPrimaryKeys);
exporter.setExportForeignKeys(exportForeignKeys);
exporter.setExportDirectForeignKeys(exportDirectForeignKeys);
exporter.setExportInverseForeignKeys(exportInverseForeignKeys);
exporter.setSpatial(spatial);
if (imports != null && imports.length > 0) {
exporter.setImports(imports);
}
if (exportBeans) {
BeanSerializer serializer = (BeanSerializer) Class.forName(beanSerializerClass).newInstance();
if (beanInterfaces != null) {
for (String iface : beanInterfaces) {
int sepIndex = iface.lastIndexOf('.');
if (sepIndex < 0) {
serializer.addInterface(new SimpleType(iface));
} else {
String packageName = iface.substring(0, sepIndex);
String simpleName = iface.substring(sepIndex + 1);
serializer.addInterface(new SimpleType(iface, packageName, simpleName));
}
}
}
serializer.setAddFullConstructor(beanAddFullConstructor);
serializer.setAddToString(beanAddToString);
serializer.setPrintSupertype(beanPrintSupertype);
exporter.setBeanSerializer(serializer);
}
if (sourceEncoding != null) {
exporter.setSourceEncoding(sourceEncoding);
}
if (customTypes != null) {
for (String cl : customTypes) {
configuration.register((Type<?>) Class.forName(cl).newInstance());
}
}
if (typeMappings != null) {
for (TypeMapping mapping : typeMappings) {
mapping.apply(configuration);
}
}
if (numericMappings != null) {
for (NumericMapping mapping : numericMappings) {
mapping.apply(configuration);
}
}
if (renameMappings != null) {
for (RenameMapping mapping : renameMappings) {
mapping.apply(configuration);
}
}
if (columnComparatorClass != null) {
exporter.setColumnComparatorClass((Class) Class.forName(this.columnComparatorClass).asSubclass(Comparator.class));
}
exporter.setConfiguration(configuration);
exporter.export(dbConn.getMetaData());
} catch (RuntimeException e) {
throw new BuildException(e);
} catch (InstantiationException e) {
throw new BuildException(e);
} catch (IllegalAccessException e) {
throw new BuildException(e);
} catch (ClassNotFoundException e) {
throw new BuildException(e);
} catch (SQLException e) {
throw new BuildException(e);
} finally {
if (dbConn != null) {
try {
dbConn.close();
} catch (SQLException e2) {
throw new BuildException(e2);
}
}
}
}
public String getJdbcDriver() {
return jdbcDriver;
}
public void setJdbcDriver(String jdbcDriver) {
this.jdbcDriver = jdbcDriver;
}
public String getJdbcUrl() {
return jdbcUrl;
}
public void setJdbcUrl(String jdbcUrl) {
this.jdbcUrl = jdbcUrl;
}
public String getJdbcUser() {
return jdbcUser;
}
public void setJdbcUser(String jdbcUser) {
this.jdbcUser = jdbcUser;
}
public String getJdbcPassword() {
return jdbcPassword;
}
public void setJdbcPassword(String jdbcPassword) {
this.jdbcPassword = jdbcPassword;
}
public String getNamePrefix() {
return namePrefix;
}
public void setNamePrefix(String namePrefix) {
this.namePrefix = namePrefix;
}
public String getNameSuffix() {
return nameSuffix;
}
public void setNameSuffix(String nameSuffix) {
this.nameSuffix = nameSuffix;
}
public String getBeanPrefix() {
return beanPrefix;
}
public void setBeanPrefix(String beanPrefix) {
this.beanPrefix = beanPrefix;
}
public String getBeanSuffix() {
return beanSuffix;
}
public void setBeanSuffix(String beanSuffix) {
this.beanSuffix = beanSuffix;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public String getBeanPackageName() {
return beanPackageName;
}
public void setBeanPackageName(String beanPackageName) {
this.beanPackageName = beanPackageName;
}
public String getSchemaPattern() {
return schemaPattern;
}
public void setSchemaPattern(String schemaPattern) {
this.schemaPattern = schemaPattern;
}
public String getTableNamePattern() {
return tableNamePattern;
}
public void setTableNamePattern(String tableNamePattern) {
this.tableNamePattern = tableNamePattern;
}
public String getTargetFolder() {
return targetFolder;
}
public void setTargetFolder(String targetFolder) {
this.targetFolder = targetFolder;
}
public String getNamingStrategyClass() {
return namingStrategyClass;
}
public void setNamingStrategyClass(String namingStrategyClass) {
this.namingStrategyClass = namingStrategyClass;
}
public String getBeanSerializerClass() {
return beanSerializerClass;
}
public void setBeanSerializerClass(String beanSerializerClass) {
this.beanSerializerClass = beanSerializerClass;
}
public String getSerializerClass() {
return serializerClass;
}
public void setSerializerClass(String serializerClass) {
this.serializerClass = serializerClass;
}
public boolean isExportBeans() {
return exportBeans;
}
public void setExportBeans(boolean exportBeans) {
this.exportBeans = exportBeans;
}
public String[] getBeanInterfaces() {
return beanInterfaces;
}
public void setBeanInterfaces(String[] beanInterfaces) {
this.beanInterfaces = beanInterfaces;
}
public boolean isBeanAddToString() {
return beanAddToString;
}
public void setBeanAddToString(boolean beanAddToString) {
this.beanAddToString = beanAddToString;
}
public boolean isBeanAddFullConstructor() {
return beanAddFullConstructor;
}
public void setBeanAddFullConstructor(boolean beanAddFullConstructor) {
this.beanAddFullConstructor = beanAddFullConstructor;
}
public boolean isBeanPrintSupertype() {
return beanPrintSupertype;
}
public void setBeanPrintSupertype(boolean beanPrintSupertype) {
this.beanPrintSupertype = beanPrintSupertype;
}
public boolean isInnerClassesForKeys() {
return innerClassesForKeys;
}
public void setInnerClassesForKeys(boolean innerClassesForKeys) {
this.innerClassesForKeys = innerClassesForKeys;
}
public boolean isValidationAnnotations() {
return validationAnnotations;
}
public void setValidationAnnotations(boolean validationAnnotations) {
this.validationAnnotations = validationAnnotations;
}
public boolean isColumnAnnotations() {
return columnAnnotations;
}
public void setColumnAnnotations(boolean columnAnnotations) {
this.columnAnnotations = columnAnnotations;
}
public String[] getCustomTypes() {
return customTypes;
}
public void setCustomTypes(String[] customTypes) {
this.customTypes = customTypes;
}
public boolean isCreateScalaSources() {
return createScalaSources;
}
public void setCreateScalaSources(boolean createScalaSources) {
this.createScalaSources = createScalaSources;
}
public boolean isSchemaToPackage() {
return schemaToPackage;
}
public void setSchemaToPackage(boolean schemaToPackage) {
this.schemaToPackage = schemaToPackage;
}
public boolean isLowerCase() {
return lowerCase;
}
public void setLowerCase(boolean lowerCase) {
this.lowerCase = lowerCase;
}
public boolean isExportTables() {
return exportTables;
}
public void setExportTables(boolean exportTables) {
this.exportTables = exportTables;
}
public boolean isExportViews() {
return exportViews;
}
public void setExportViews(boolean exportViews) {
this.exportViews = exportViews;
}
public boolean isExportAll() {
return exportAll;
}
public void setExportAll(boolean exportAll) {
this.exportAll = exportAll;
}
public boolean isExportPrimaryKeys() {
return exportPrimaryKeys;
}
public void setExportPrimaryKeys(boolean exportPrimaryKeys) {
this.exportPrimaryKeys = exportPrimaryKeys;
}
public boolean isExportForeignKeys() {
return exportForeignKeys;
}
public boolean isExportDirectForeignKeys() {
return exportDirectForeignKeys;
}
public void setExportDirectForeignKeys(boolean exportDirectForeignKeys) {
this.exportDirectForeignKeys = exportDirectForeignKeys;
}
public boolean isExportInverseForeignKeys() {
return exportInverseForeignKeys;
}
public void setExportInverseForeignKeys(boolean exportInverseForeignKeys) {
this.exportInverseForeignKeys = exportInverseForeignKeys;
}
public void setExportForeignKeys(boolean exportForeignKeys) {
this.exportForeignKeys = exportForeignKeys;
}
public String getColumnComparatorClass() {
return columnComparatorClass;
}
public void setColumnComparatorClass(String columnComparatorClass) {
this.columnComparatorClass = columnComparatorClass;
}
public boolean isSpatial() {
return spatial;
}
public void setSpatial(boolean spatial) {
this.spatial = spatial;
}
public String getTableTypesToExport() {
return tableTypesToExport;
}
public void setTableTypesToExport(String tableTypesToExport) {
this.tableTypesToExport = tableTypesToExport;
}
public String[] getImports() {
return imports;
}
public void setImports(String[] imports) {
this.imports = imports;
}
public String getSourceEncoding() {
return sourceEncoding;
}
public void setSourceEncoding(String sourceEncoding) {
this.sourceEncoding = sourceEncoding;
}
public String getBeansTargetFolder() {
return beansTargetFolder;
}
public void setBeansTargetFolder(String beansTargetFolder) {
this.beansTargetFolder = beansTargetFolder;
}
/**
* Adds TypeMapping instance, called by Ant
*/
public void addTypeMapping(TypeMapping mapping) {
typeMappings.add(mapping);
}
/**
* Adds NumericMapping instance, called by Ant
*/
public void addNumericMapping(NumericMapping mapping) {
numericMappings.add(mapping);
}
/**
* Adds RenameMapping instance, called by Ant
*/
public void addRenameMapping(RenameMapping mapping) {
renameMappings.add(mapping);
}
}