|
@@ -6,14 +6,16 @@ import com.rthoni.intellij.codefromds.dbo.options.ColumnSelection;
|
6
|
6
|
import com.rthoni.intellij.codefromds.dbo.options.GenerateOptions;
|
7
|
7
|
import com.rthoni.intellij.codefromds.dbo.options.TableSelection;
|
8
|
8
|
import com.rthoni.intellij.codefromds.dbo.options.TypesCastOptions;
|
9
|
|
-import com.rthoni.intellij.codefromds.dbo.template.ColumnDataSourceDbo;
|
10
|
|
-import com.rthoni.intellij.codefromds.dbo.template.DataSourceDbo;
|
11
|
|
-import com.rthoni.intellij.codefromds.dbo.template.SqlTypeDbo;
|
12
|
|
-import com.rthoni.intellij.codefromds.dbo.template.TableDataSourceDbo;
|
|
9
|
+import com.rthoni.intellij.codefromds.dbo.template.*;
|
|
10
|
+import groovy.json.StringEscapeUtils;
|
13
|
11
|
import org.json.JSONArray;
|
14
|
12
|
import org.json.JSONObject;
|
15
|
13
|
import org.jtwig.JtwigModel;
|
16
|
14
|
import org.jtwig.JtwigTemplate;
|
|
15
|
+import org.jtwig.environment.EnvironmentConfiguration;
|
|
16
|
+import org.jtwig.environment.EnvironmentConfigurationBuilder;
|
|
17
|
+import org.jtwig.functions.FunctionRequest;
|
|
18
|
+import org.jtwig.functions.SimpleJtwigFunction;
|
17
|
19
|
|
18
|
20
|
import java.io.File;
|
19
|
21
|
import java.io.FileOutputStream;
|
|
@@ -21,6 +23,7 @@ import java.io.IOException;
|
21
|
23
|
import java.util.HashMap;
|
22
|
24
|
import java.util.List;
|
23
|
25
|
import java.util.Vector;
|
|
26
|
+import java.util.function.Consumer;
|
24
|
27
|
|
25
|
28
|
/**
|
26
|
29
|
* Created by robin on 11/15/16.
|
|
@@ -54,11 +57,27 @@ public abstract class Generator {
|
54
|
57
|
return options;
|
55
|
58
|
}
|
56
|
59
|
|
57
|
|
- public static String convertSqlType(SqlTypeDbo type, TypesCastOptions options)
|
|
60
|
+ public static String escapeReservedWords(String id, TypesCastOptions options)
|
58
|
61
|
{
|
|
62
|
+ if (options.getReservedWords().contains(id)) {
|
|
63
|
+ return "_" + id;
|
|
64
|
+ }
|
|
65
|
+ return id;
|
|
66
|
+ }
|
|
67
|
+
|
|
68
|
+ public static String escapeString(String str)
|
|
69
|
+ {
|
|
70
|
+ return StringEscapeUtils.escapeJava(str);
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+ public static void convertSqlType(SqlTypeDbo type, TypesCastOptions options)
|
|
74
|
+ {
|
|
75
|
+ if (type == null) {
|
|
76
|
+ return;
|
|
77
|
+ }
|
59
|
78
|
boolean isArray = type.getType().endsWith("[]");
|
60
|
79
|
String sqlTypeName = isArray ? type.getType().substring(0, type.getType().length() - 2) : type.getType();
|
61
|
|
- String typeName = type.getType();
|
|
80
|
+ String typeName = null;
|
62
|
81
|
HashMap<String, HashMap<String, String>> types = options.getTypes();
|
63
|
82
|
|
64
|
83
|
if (types.containsKey(sqlTypeName)) {
|
|
@@ -70,12 +89,16 @@ public abstract class Generator {
|
70
|
89
|
typeName = subtype.get("*");
|
71
|
90
|
}
|
72
|
91
|
}
|
|
92
|
+ if (typeName == null) {
|
|
93
|
+ typeName = types.get("*").get("*");
|
|
94
|
+ }
|
73
|
95
|
if (isArray) {
|
74
|
96
|
typeName = options.getArrayTemplate().replace("%t", typeName);
|
75
|
97
|
}
|
76
|
|
- return typeName;
|
|
98
|
+ type.setLanguageType(typeName);
|
|
99
|
+ type.setLanguageTypeNotNull(options.getNonNullableTypes().contains(typeName));
|
77
|
100
|
}
|
78
|
|
-//
|
|
101
|
+
|
79
|
102
|
// public static boolean isUnionSame(List<ColumnDataSourceDbo> part1, List<ColumnDataSourceDbo> part2, List<ColumnDataSourceDbo> list2)
|
80
|
103
|
// {
|
81
|
104
|
// List<ColumnDataSourceDbo> list1 = new Vector<>(part1);
|
|
@@ -107,12 +130,19 @@ public abstract class Generator {
|
107
|
130
|
ColumnSelection columnSelection = tableSelection.getColumns().stream()
|
108
|
131
|
.filter(c -> c.getColumn().getName().equals(column.getName()))
|
109
|
132
|
.findFirst().orElse(null);
|
110
|
|
- column.setType(convertSqlType(column.getSqlType(), types));
|
111
|
|
- column.setTypeNotNull(types.getNonNullableTypes().contains(column.getType()));
|
|
133
|
+ convertSqlType(column.getSqlType(), types);
|
112
|
134
|
column.setSelected(columnSelection.isSelected());
|
113
|
135
|
}
|
114
|
136
|
}
|
115
|
137
|
|
|
138
|
+ for (StoredProcedureDbo spDbo : dataSourceDbo.getStoredProcedures())
|
|
139
|
+ {
|
|
140
|
+ convertSqlType(spDbo.getSqlType(), types);
|
|
141
|
+ for (StoredProcedureArgDbo arg : spDbo.getArguments()) {
|
|
142
|
+ convertSqlType(arg.getSqlType(), types);
|
|
143
|
+ }
|
|
144
|
+ }
|
|
145
|
+
|
116
|
146
|
return dataSourceDbo;
|
117
|
147
|
}
|
118
|
148
|
|
|
@@ -148,19 +178,70 @@ public abstract class Generator {
|
148
|
178
|
}
|
149
|
179
|
dbo.setNonNullableTypes(nonNullableTypes);
|
150
|
180
|
|
|
181
|
+ List<String> reservedWords = new Vector<>();
|
|
182
|
+ array = obj.getJSONArray("reserved-words");
|
|
183
|
+ for (int i = 0; i < array.length(); ++i) {
|
|
184
|
+ reservedWords.add(array.getString(i));
|
|
185
|
+ }
|
|
186
|
+ dbo.setReservedWords(reservedWords);
|
|
187
|
+
|
151
|
188
|
dbo.setArrayTemplate(obj.getString("arrayTemplate"));
|
152
|
189
|
|
153
|
190
|
return dbo;
|
154
|
191
|
}
|
155
|
192
|
|
156
|
|
- public static void generateFile(String templatePath, String outputFile, DataSourceDbo dataSource, TableDataSourceDbo table) throws IOException
|
|
193
|
+ public static void generateFile(String templatePath, String outputFile, DataSourceDbo dataSource,
|
|
194
|
+ TableDataSourceDbo table, TypesCastOptions options) throws IOException
|
157
|
195
|
{
|
158
|
196
|
String data = Helper.readFile(templatePath);
|
159
|
197
|
|
160
|
198
|
FileOutputStream file = new FileOutputStream(outputFile);
|
161
|
199
|
|
162
|
|
- JtwigTemplate template = JtwigTemplate.inlineTemplate(data);
|
163
|
|
- JtwigModel model = JtwigModel.newModel().with("dataSource", dataSource).with("table", table);
|
|
200
|
+ final SimpleJtwigFunction escapeReservedWords = new SimpleJtwigFunction() {
|
|
201
|
+ @Override
|
|
202
|
+ public String name() {
|
|
203
|
+ return "escapeReservedWords";
|
|
204
|
+ }
|
|
205
|
+
|
|
206
|
+ @Override
|
|
207
|
+ public Object execute(FunctionRequest functionRequest) {
|
|
208
|
+ String arg = (String) functionRequest.get(0);
|
|
209
|
+ String v = escapeReservedWords(arg, options);
|
|
210
|
+ return v;
|
|
211
|
+ }
|
|
212
|
+ };
|
|
213
|
+
|
|
214
|
+ final SimpleJtwigFunction escapeString = new SimpleJtwigFunction() {
|
|
215
|
+ @Override
|
|
216
|
+ public String name() {
|
|
217
|
+ return "escapeString";
|
|
218
|
+ }
|
|
219
|
+
|
|
220
|
+ @Override
|
|
221
|
+ public Object execute(FunctionRequest functionRequest) {
|
|
222
|
+ Object obj = functionRequest.get(0);
|
|
223
|
+ if (obj == null)
|
|
224
|
+ {
|
|
225
|
+ return null;
|
|
226
|
+ }
|
|
227
|
+ String arg = (String) obj;
|
|
228
|
+ String v = escapeString(arg);
|
|
229
|
+ return v;
|
|
230
|
+ }
|
|
231
|
+ };
|
|
232
|
+
|
|
233
|
+ final EnvironmentConfiguration configuration = EnvironmentConfigurationBuilder
|
|
234
|
+ .configuration()
|
|
235
|
+ .functions()
|
|
236
|
+ .add(escapeReservedWords)
|
|
237
|
+ .add(escapeString)
|
|
238
|
+ .and()
|
|
239
|
+ .build();
|
|
240
|
+
|
|
241
|
+ JtwigTemplate template = JtwigTemplate.inlineTemplate(data, configuration);
|
|
242
|
+ JtwigModel model = JtwigModel.newModel()
|
|
243
|
+ .with("dataSource", dataSource)
|
|
244
|
+ .with("table", table);
|
164
|
245
|
|
165
|
246
|
template.render(model, file);
|
166
|
247
|
|
|
@@ -180,11 +261,11 @@ public abstract class Generator {
|
180
|
261
|
|
181
|
262
|
DataSourceDbo dbo = convertOptions(options, types);
|
182
|
263
|
|
183
|
|
- generateFile(dataSourceTemplateAbsolutePath, dataSourceAbsolutePath, dbo, null);
|
|
264
|
+ generateFile(dataSourceTemplateAbsolutePath, dataSourceAbsolutePath, dbo, null, types);
|
184
|
265
|
for (TableDataSourceDbo table : dbo.getTables()) {
|
185
|
266
|
if (table.hasAny()) {
|
186
|
267
|
String filename = modelsAbsolutePath + File.separator + table.getName() + "." + options.getFilesExtension();
|
187
|
|
- generateFile(modelsTemplateAbsolutePath, filename, dbo, table);
|
|
268
|
+ generateFile(modelsTemplateAbsolutePath, filename, dbo, table, types);
|
188
|
269
|
}
|
189
|
270
|
}
|
190
|
271
|
}
|