Browse Source

generate sp; added reserved words support; added escape functions

develop
Robin Thoni 7 years ago
parent
commit
de27b295c5

+ 35
- 12
src/com/rthoni/intellij/codefromds/DataAccess/DataSourcesDataAccess.java View File

55
                 columnDataSourceDbo.data.setSqlType(type);
55
                 columnDataSourceDbo.data.setSqlType(type);
56
                 columnDataSourceDbo.data.setDefaultValue(column.getDefault());
56
                 columnDataSourceDbo.data.setDefaultValue(column.getDefault());
57
                 columnDataSourceDbo.data.setPrimary(DasUtil.isPrimary(column));
57
                 columnDataSourceDbo.data.setPrimary(DasUtil.isPrimary(column));
58
-                columnDataSourceDbo.data.setSelected(false);
59
-                columnDataSourceDbo.data.setTypeNotNull(false);
60
-                columnDataSourceDbo.data.setType(null);
61
                 tableDataSourceDbo.data.addColumn(columnDataSourceDbo.data);
58
                 tableDataSourceDbo.data.addColumn(columnDataSourceDbo.data);
62
             }
59
             }
63
         });
60
         });
67
             if (dasObject instanceof DbTable) {
64
             if (dasObject instanceof DbTable) {
68
                 DbTable table = (DbTable) dasObject;
65
                 DbTable table = (DbTable) dasObject;
69
                 tableDataSourceDbo.data = dataSourceDbo.findTable(table.getName());
66
                 tableDataSourceDbo.data = dataSourceDbo.findTable(table.getName());
70
-                System.out.println("Table: " + tableDataSourceDbo.data.getName());
67
+//                System.out.println("Table: " + tableDataSourceDbo.data.getName());
71
                 DasUtil.getForeignKeys(table).forEach(fk ->
68
                 DasUtil.getForeignKeys(table).forEach(fk ->
72
                 {
69
                 {
73
                     if (fk.getRefTable() == null) {
70
                     if (fk.getRefTable() == null) {
90
                     srcTable.addSourceForeignKey(fkDbo);
87
                     srcTable.addSourceForeignKey(fkDbo);
91
                     targetTable.addTargetForeignKey(fkDbo);
88
                     targetTable.addTargetForeignKey(fkDbo);
92
 
89
 
93
-                    System.out.println("FK: " + fkDbo.getName());
90
+//                    System.out.println("FK: " + fkDbo.getName());
94
                 });
91
                 });
95
             }
92
             }
96
             else if (dasObject instanceof DbColumn) {
93
             else if (dasObject instanceof DbColumn) {
97
                 DbColumn column = (DbColumn) dasObject;
94
                 DbColumn column = (DbColumn) dasObject;
98
                 columnDataSourceDbo.data = tableDataSourceDbo.data.findColumn(column.getName());
95
                 columnDataSourceDbo.data = tableDataSourceDbo.data.findColumn(column.getName());
99
-                System.out.println("Column: " + columnDataSourceDbo.data.getName());
96
+//                System.out.println("Column: " + columnDataSourceDbo.data.getName());
100
             }
97
             }
101
             else if (dasObject instanceof DbIndex) {
98
             else if (dasObject instanceof DbIndex) {
102
                 DbIndex index = (DbIndex) dasObject;
99
                 DbIndex index = (DbIndex) dasObject;
103
-                System.out.println("Index: " + index.getName());
100
+//                System.out.println("Index: " + index.getName());
104
             }
101
             }
105
             else if (dasObject instanceof DbConstraint) {
102
             else if (dasObject instanceof DbConstraint) {
106
                 DbConstraint constraint = (DbConstraint) dasObject;
103
                 DbConstraint constraint = (DbConstraint) dasObject;
107
-                System.out.println("Constraint: " + constraint.getName());
104
+//                System.out.println("Constraint: " + constraint.getName());
108
             }
105
             }
109
             else if (dasObject instanceof DbRoutine) {
106
             else if (dasObject instanceof DbRoutine) {
110
                 DbRoutine sp = (DbRoutine) dasObject;
107
                 DbRoutine sp = (DbRoutine) dasObject;
111
-                System.out.println("SP: " + sp.getName());
112
-            }
113
-            else {
114
-                System.out.println(dasObject.getClass().getName());
108
+                StoredProcedureDbo spDbo = new StoredProcedureDbo();
109
+                spDbo.setSelected(true);
110
+                dataSourceDbo.addStoredProcedure(spDbo);
111
+                spDbo.setName(sp.getName());
112
+//                System.out.println("SP: " + spDbo.getName());
113
+                DataHolder<Integer> position = new DataHolder<>();
114
+                position.data = 0;
115
+                sp.getArguments().forEach(o ->
116
+                {
117
+                    SqlTypeDbo sqlTypeDbo = new SqlTypeDbo();
118
+                    sqlTypeDbo.setType(o.getDataType().typeName);
119
+                    sqlTypeDbo.setVagueArg(o.getDataType().vagueArg);
120
+
121
+                    if (o.getArgumentDirection().isReturnOrResult()) {
122
+                        spDbo.setSqlType(sqlTypeDbo);
123
+//                        System.out.println("SP return: " + sqlTypeDbo.getType());
124
+                    }
125
+                    else {
126
+                        StoredProcedureArgDbo argDbo = new StoredProcedureArgDbo();
127
+                        argDbo.setName(o.getName().isEmpty() ? "arg" + position.data : o.getName());
128
+                        argDbo.setSqlType(sqlTypeDbo);
129
+                        argDbo.setOut(o.getArgumentDirection().isOut());
130
+                        spDbo.addArgument(argDbo);
131
+                        ++position.data;
132
+//                        System.out.println("SP arg: " + o.getName());
133
+                    }
134
+                });
115
             }
135
             }
136
+//            else {
137
+//                System.out.println(dasObject.getClass().getName());
138
+//            }
116
         });
139
         });
117
         return dataSourceDbo;
140
         return dataSourceDbo;
118
     }
141
     }

+ 96
- 15
src/com/rthoni/intellij/codefromds/business/Generator.java View File

6
 import com.rthoni.intellij.codefromds.dbo.options.GenerateOptions;
6
 import com.rthoni.intellij.codefromds.dbo.options.GenerateOptions;
7
 import com.rthoni.intellij.codefromds.dbo.options.TableSelection;
7
 import com.rthoni.intellij.codefromds.dbo.options.TableSelection;
8
 import com.rthoni.intellij.codefromds.dbo.options.TypesCastOptions;
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
 import org.json.JSONArray;
11
 import org.json.JSONArray;
14
 import org.json.JSONObject;
12
 import org.json.JSONObject;
15
 import org.jtwig.JtwigModel;
13
 import org.jtwig.JtwigModel;
16
 import org.jtwig.JtwigTemplate;
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
 import java.io.File;
20
 import java.io.File;
19
 import java.io.FileOutputStream;
21
 import java.io.FileOutputStream;
21
 import java.util.HashMap;
23
 import java.util.HashMap;
22
 import java.util.List;
24
 import java.util.List;
23
 import java.util.Vector;
25
 import java.util.Vector;
26
+import java.util.function.Consumer;
24
 
27
 
25
 /**
28
 /**
26
  * Created by robin on 11/15/16.
29
  * Created by robin on 11/15/16.
54
         return options;
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
         boolean isArray = type.getType().endsWith("[]");
78
         boolean isArray = type.getType().endsWith("[]");
60
         String sqlTypeName = isArray ? type.getType().substring(0, type.getType().length() - 2) : type.getType();
79
         String sqlTypeName = isArray ? type.getType().substring(0, type.getType().length() - 2) : type.getType();
61
-        String typeName = type.getType();
80
+        String typeName = null;
62
         HashMap<String, HashMap<String, String>> types = options.getTypes();
81
         HashMap<String, HashMap<String, String>> types = options.getTypes();
63
 
82
 
64
         if (types.containsKey(sqlTypeName)) {
83
         if (types.containsKey(sqlTypeName)) {
70
                 typeName = subtype.get("*");
89
                 typeName = subtype.get("*");
71
             }
90
             }
72
         }
91
         }
92
+        if (typeName == null) {
93
+            typeName = types.get("*").get("*");
94
+        }
73
         if (isArray) {
95
         if (isArray) {
74
             typeName = options.getArrayTemplate().replace("%t", typeName);
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
 //    public static boolean isUnionSame(List<ColumnDataSourceDbo> part1, List<ColumnDataSourceDbo> part2, List<ColumnDataSourceDbo> list2)
102
 //    public static boolean isUnionSame(List<ColumnDataSourceDbo> part1, List<ColumnDataSourceDbo> part2, List<ColumnDataSourceDbo> list2)
80
 //    {
103
 //    {
81
 //        List<ColumnDataSourceDbo> list1 = new Vector<>(part1);
104
 //        List<ColumnDataSourceDbo> list1 = new Vector<>(part1);
107
                 ColumnSelection columnSelection = tableSelection.getColumns().stream()
130
                 ColumnSelection columnSelection = tableSelection.getColumns().stream()
108
                         .filter(c -> c.getColumn().getName().equals(column.getName()))
131
                         .filter(c -> c.getColumn().getName().equals(column.getName()))
109
                         .findFirst().orElse(null);
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
                 column.setSelected(columnSelection.isSelected());
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
         return dataSourceDbo;
146
         return dataSourceDbo;
117
     }
147
     }
118
 
148
 
148
         }
178
         }
149
         dbo.setNonNullableTypes(nonNullableTypes);
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
         dbo.setArrayTemplate(obj.getString("arrayTemplate"));
188
         dbo.setArrayTemplate(obj.getString("arrayTemplate"));
152
 
189
 
153
         return dbo;
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
         String data = Helper.readFile(templatePath);
196
         String data = Helper.readFile(templatePath);
159
 
197
 
160
         FileOutputStream file = new FileOutputStream(outputFile);
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
         template.render(model, file);
246
         template.render(model, file);
166
 
247
 
180
 
261
 
181
         DataSourceDbo dbo = convertOptions(options, types);
262
         DataSourceDbo dbo = convertOptions(options, types);
182
 
263
 
183
-        generateFile(dataSourceTemplateAbsolutePath, dataSourceAbsolutePath, dbo, null);
264
+        generateFile(dataSourceTemplateAbsolutePath, dataSourceAbsolutePath, dbo, null, types);
184
         for (TableDataSourceDbo table : dbo.getTables()) {
265
         for (TableDataSourceDbo table : dbo.getTables()) {
185
             if (table.hasAny()) {
266
             if (table.hasAny()) {
186
                 String filename = modelsAbsolutePath + File.separator + table.getName() + "." + options.getFilesExtension();
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
     }

+ 10
- 0
src/com/rthoni/intellij/codefromds/dbo/options/TypesCastOptions.java View File

14
 
14
 
15
     private List<String> _nonNullableTypes;
15
     private List<String> _nonNullableTypes;
16
 
16
 
17
+    private List<String> _reservedWords;
18
+
17
     public String getArrayTemplate() {
19
     public String getArrayTemplate() {
18
         return _arrayTemplate;
20
         return _arrayTemplate;
19
     }
21
     }
37
     public void setNonNullableTypes(List<String> nonNullableTypes) {
39
     public void setNonNullableTypes(List<String> nonNullableTypes) {
38
         _nonNullableTypes = nonNullableTypes;
40
         _nonNullableTypes = nonNullableTypes;
39
     }
41
     }
42
+
43
+    public List<String> getReservedWords() {
44
+        return _reservedWords;
45
+    }
46
+
47
+    public void setReservedWords(List<String> reservedWords) {
48
+        _reservedWords = reservedWords;
49
+    }
40
 }
50
 }

+ 2
- 20
src/com/rthoni/intellij/codefromds/dbo/template/ColumnDataSourceDbo.java View File

1
 package com.rthoni.intellij.codefromds.dbo.template;
1
 package com.rthoni.intellij.codefromds.dbo.template;
2
 
2
 
3
-import org.apache.commons.lang.StringEscapeUtils;
4
-
5
 /**
3
 /**
6
  * Created by robin on 11/18/16.
4
  * Created by robin on 11/18/16.
7
  */
5
  */
13
 
11
 
14
     private boolean _isSelected;
12
     private boolean _isSelected;
15
 
13
 
16
-    private String _type;
17
-
18
     private SqlTypeDbo _sqlType;
14
     private SqlTypeDbo _sqlType;
19
 
15
 
20
     private boolean _notNull;
16
     private boolean _notNull;
21
 
17
 
22
-    private boolean _typeNotNull;
23
-
24
     private String _defaultValue;
18
     private String _defaultValue;
25
 
19
 
26
     public String getName() {
20
     public String getName() {
48
     }
42
     }
49
 
43
 
50
     public String getType() {
44
     public String getType() {
51
-        return _type;
52
-    }
53
-
54
-    public void setType(String type) {
55
-        _type = type;
45
+        return _sqlType != null ? _sqlType.getLanguageType() : null;
56
     }
46
     }
57
 
47
 
58
     public SqlTypeDbo getSqlType() {
48
     public SqlTypeDbo getSqlType() {
72
     }
62
     }
73
 
63
 
74
     public boolean isTypeNotNull() {
64
     public boolean isTypeNotNull() {
75
-        return _typeNotNull;
76
-    }
77
-
78
-    public void setTypeNotNull(boolean typeNotNull) {
79
-        _typeNotNull = typeNotNull;
65
+        return _sqlType != null && _sqlType.isLanguageTypeNotNull();
80
     }
66
     }
81
 
67
 
82
     public String getDefaultValue() {
68
     public String getDefaultValue() {
83
         return _defaultValue;
69
         return _defaultValue;
84
     }
70
     }
85
 
71
 
86
-    public String getDefaultValueEscaped() {
87
-        return StringEscapeUtils.escapeJava(_defaultValue);
88
-    }
89
-
90
     public boolean hasDefaultValue()
72
     public boolean hasDefaultValue()
91
     {
73
     {
92
         return _defaultValue != null && !_defaultValue.equals("");
74
         return _defaultValue != null && !_defaultValue.equals("");

+ 10
- 0
src/com/rthoni/intellij/codefromds/dbo/template/DataSourceDbo.java View File

12
 
12
 
13
     private List<TableDataSourceDbo> _tables = new Vector<>();
13
     private List<TableDataSourceDbo> _tables = new Vector<>();
14
 
14
 
15
+    private List<StoredProcedureDbo> _storedProcedures = new Vector<>();
16
+
17
+    public List<StoredProcedureDbo> getStoredProcedures() {
18
+        return _storedProcedures;
19
+    }
20
+
21
+    public void addStoredProcedure(StoredProcedureDbo storedProcedures) {
22
+        _storedProcedures.add(storedProcedures);
23
+    }
24
+
15
     public String getName() {
25
     public String getName() {
16
         return _name;
26
         return _name;
17
     }
27
     }

+ 20
- 0
src/com/rthoni/intellij/codefromds/dbo/template/SqlTypeDbo.java View File

9
 
9
 
10
     private String vagueArg;
10
     private String vagueArg;
11
 
11
 
12
+    private String _languageType;
13
+
14
+    private boolean _languageTypeNotNull;
15
+
12
     public String getType() {
16
     public String getType() {
13
         return _type;
17
         return _type;
14
     }
18
     }
24
     public void setVagueArg(String vagueArg) {
28
     public void setVagueArg(String vagueArg) {
25
         this.vagueArg = vagueArg;
29
         this.vagueArg = vagueArg;
26
     }
30
     }
31
+
32
+    public String getLanguageType() {
33
+        return _languageType;
34
+    }
35
+
36
+    public void setLanguageType(String languageType) {
37
+        _languageType = languageType;
38
+    }
39
+
40
+    public boolean isLanguageTypeNotNull() {
41
+        return _languageTypeNotNull;
42
+    }
43
+
44
+    public void setLanguageTypeNotNull(boolean languageTypeNotNull) {
45
+        _languageTypeNotNull = languageTypeNotNull;
46
+    }
27
 }
47
 }

+ 41
- 0
src/com/rthoni/intellij/codefromds/dbo/template/StoredProcedureArgDbo.java View File

1
+package com.rthoni.intellij.codefromds.dbo.template;
2
+
3
+/**
4
+ * Created by robin on 4/13/17.
5
+ */
6
+public class StoredProcedureArgDbo {
7
+
8
+    private String name;
9
+
10
+    private SqlTypeDbo _sqlType;
11
+
12
+    private boolean _isOut;
13
+
14
+    public String getName() {
15
+        return name;
16
+    }
17
+
18
+    public void setName(String name) {
19
+        this.name = name;
20
+    }
21
+
22
+    public String getType() {
23
+        return _sqlType != null ? _sqlType.getLanguageType() : null;
24
+    }
25
+
26
+    public SqlTypeDbo getSqlType() {
27
+        return _sqlType;
28
+    }
29
+
30
+    public void setSqlType(SqlTypeDbo sqlType) {
31
+        _sqlType = sqlType;
32
+    }
33
+
34
+    public boolean isOut() {
35
+        return _isOut;
36
+    }
37
+
38
+    public void setOut(boolean out) {
39
+        _isOut = out;
40
+    }
41
+}

+ 54
- 0
src/com/rthoni/intellij/codefromds/dbo/template/StoredProcedureDbo.java View File

1
+package com.rthoni.intellij.codefromds.dbo.template;
2
+
3
+import java.util.List;
4
+import java.util.Vector;
5
+
6
+/**
7
+ * Created by robin on 4/13/17.
8
+ */
9
+public class StoredProcedureDbo {
10
+
11
+    private String _name;
12
+
13
+    private List<StoredProcedureArgDbo> _arguments = new Vector<>();
14
+
15
+    private SqlTypeDbo _sqlType;
16
+
17
+    private boolean _isSelected;
18
+
19
+    public String getName() {
20
+        return _name;
21
+    }
22
+
23
+    public void setName(String name) {
24
+        _name = name;
25
+    }
26
+
27
+    public List<StoredProcedureArgDbo> getArguments() {
28
+        return _arguments;
29
+    }
30
+
31
+    public void addArgument(StoredProcedureArgDbo arguments) {
32
+        _arguments.add(arguments);
33
+    }
34
+
35
+    public String getType() {
36
+        return _sqlType != null ? _sqlType.getLanguageType() : null;
37
+    }
38
+
39
+    public SqlTypeDbo getSqlType() {
40
+        return _sqlType;
41
+    }
42
+
43
+    public void setSqlType(SqlTypeDbo sqlType) {
44
+        _sqlType = sqlType;
45
+    }
46
+
47
+    public boolean isSelected() {
48
+        return _isSelected;
49
+    }
50
+
51
+    public void setSelected(boolean selected) {
52
+        _isSelected = selected;
53
+    }
54
+}

Loading…
Cancel
Save