Browse Source

sql type to language type

tags/v1.1.0
Robin Thoni 8 years ago
parent
commit
dd9b4771ac

+ 64
- 7
src/com/rthoni/intellij/codefromds/business/Generator.java View File

@@ -1,6 +1,7 @@
1 1
 package com.rthoni.intellij.codefromds.business;
2 2
 
3 3
 import com.intellij.database.model.DasForeignKey;
4
+import com.intellij.database.model.DataType;
4 5
 import com.intellij.database.model.MultiRef;
5 6
 import com.intellij.database.psi.DbDataSource;
6 7
 import com.intellij.database.util.DasUtil;
@@ -8,10 +9,12 @@ import com.intellij.openapi.project.Project;
8 9
 import com.rthoni.intellij.codefromds.dbo.options.ColumnSelection;
9 10
 import com.rthoni.intellij.codefromds.dbo.options.GenerateOptions;
10 11
 import com.rthoni.intellij.codefromds.dbo.options.TableSelection;
12
+import com.rthoni.intellij.codefromds.dbo.options.TypesCastOptions;
11 13
 import com.rthoni.intellij.codefromds.dbo.template.ColumnDataSourceDbo;
12 14
 import com.rthoni.intellij.codefromds.dbo.template.DataSourceDbo;
13 15
 import com.rthoni.intellij.codefromds.dbo.template.ForeignKeyDbo;
14 16
 import com.rthoni.intellij.codefromds.dbo.template.TableDataSourceDbo;
17
+import org.json.JSONArray;
15 18
 import org.json.JSONObject;
16 19
 import org.jtwig.JtwigModel;
17 20
 import org.jtwig.JtwigTemplate;
@@ -44,7 +47,7 @@ public abstract class Generator {
44 47
 
45 48
     public static GenerateOptions loadOptions(String configPath) throws Exception
46 49
     {
47
-        String data = Files.readAllLines(Paths.get(configPath), StandardCharsets.UTF_8).stream().reduce("", (s1, s2) -> s1 + s2 + "\n");
50
+        String data = Helper.readFile(configPath);
48 51
 
49 52
         JSONObject obj = new JSONObject(data);
50 53
         String src = obj.getJSONObject("selection").getString("source");
@@ -81,32 +84,53 @@ public abstract class Generator {
81 84
         return dbo;
82 85
     }
83 86
 
84
-    public static ColumnDataSourceDbo convertColumn(ColumnSelection columnSelection)
87
+    public static ColumnDataSourceDbo convertColumn(ColumnSelection columnSelection, TypesCastOptions options)
85 88
     {
86 89
         ColumnDataSourceDbo dbo = new ColumnDataSourceDbo();
87 90
         dbo.setName(columnSelection.getColumn().getName());
88 91
         dbo.setPrimary(DasUtil.isPrimary(columnSelection.getColumn()));
89 92
         dbo.setSelected(columnSelection.isSelected());
93
+
94
+        DataType type = columnSelection.getColumn().getDataType();
95
+        boolean isArray = type.typeName.endsWith("[]");
96
+        String sqlTypeName = isArray ? type.typeName.substring(0, type.typeName.length() - 2) : type.typeName;
97
+        String typeName = type.typeName;
98
+        HashMap<String, HashMap<String, String>> types = options.getTypes();
99
+
100
+        if (types.containsKey(sqlTypeName)) {
101
+            HashMap<String, String> subtype = types.get(sqlTypeName);
102
+            if (subtype.containsKey(type.vagueArg)) {
103
+                typeName = subtype.get(type.vagueArg);
104
+            }
105
+            else if (subtype.containsKey("*")) {
106
+                typeName = subtype.get("*");
107
+            }
108
+        }
109
+        if (isArray) {
110
+            typeName = options.getArrayTemplate().replace("%t", typeName);
111
+        }
112
+        dbo.setType(typeName);
113
+
90 114
         return dbo;
91 115
     }
92 116
 
93
-    public static TableDataSourceDbo convertTable(TableSelection tableSelection)
117
+    public static TableDataSourceDbo convertTable(TableSelection tableSelection, TypesCastOptions types)
94 118
     {
95 119
         TableDataSourceDbo dbo = new TableDataSourceDbo();
96 120
         dbo.setName(tableSelection.getTable().getName());
97
-        dbo.setColumns(tableSelection.getColumns().stream().map(Generator::convertColumn).collect(Collectors.toList()));
121
+        dbo.setColumns(tableSelection.getColumns().stream().map(c -> convertColumn(c, types)).collect(Collectors.toList()));
98 122
         dbo.setPrimaryKeys(dbo.getColumns().stream().filter(ColumnDataSourceDbo::isPrimary).collect(Collectors.toList()));
99 123
         dbo.setHasAny(!tableSelection.hasNone());
100 124
         dbo.setTargetForeignKeys(new Vector<>());
101 125
         return dbo;
102 126
     }
103 127
 
104
-    public static DataSourceDbo convertOptions(GenerateOptions options)
128
+    public static DataSourceDbo convertOptions(GenerateOptions options, TypesCastOptions types)
105 129
     {
106 130
         DataSourceDbo dbo = new DataSourceDbo();
107 131
 
108 132
         dbo.setName(options.getSelection().getSource().getName());
109
-        dbo.setTables(options.getSelection().getTables().stream().map(Generator::convertTable).collect(Collectors.toList()));
133
+        dbo.setTables(options.getSelection().getTables().stream().map(t -> convertTable(t, types)).collect(Collectors.toList()));
110 134
 
111 135
         List<TableDataSourceDbo> tables = dbo.getTables();
112 136
         List<TableSelection> tableSelections = options.getSelection().getTables();
@@ -119,6 +143,36 @@ public abstract class Generator {
119 143
         return dbo;
120 144
     }
121 145
 
146
+    public static TypesCastOptions loadTypesCast(String file) throws IOException
147
+    {
148
+        TypesCastOptions dbo = new TypesCastOptions();
149
+        String data = Helper.readFile(file);
150
+
151
+        JSONObject obj = new JSONObject(data);
152
+        JSONObject objTypes = obj.getJSONObject("types");
153
+        HashMap<String, HashMap<String, String>> map = new HashMap<>();
154
+
155
+        for (String key : objTypes.keySet()) {
156
+            HashMap<String, String> typeMap = new HashMap<>();
157
+            String type = objTypes.optString(key);
158
+            JSONObject typeObject = objTypes.optJSONObject(key);
159
+            if (type != null) {
160
+                typeMap.put("*", type);
161
+            }
162
+            else if (typeObject != null) {
163
+                for (String subtype : typeObject.keySet()) {
164
+                    typeMap.put(subtype, typeObject.getString(subtype));
165
+                }
166
+            }
167
+            map.put(key, typeMap);
168
+        }
169
+        dbo.setTypes(map);
170
+
171
+        dbo.setArrayTemplate(obj.getString("arrayTemplate"));
172
+
173
+        return dbo;
174
+    }
175
+
122 176
     public static void generateFile(String templatePath, String outputFile, DataSourceDbo dataSource, TableDataSourceDbo table) throws IOException
123 177
     {
124 178
         String data = Helper.readFile(templatePath);
@@ -138,8 +192,11 @@ public abstract class Generator {
138 192
         String modelsAbsolutePath = Helper.getAbsolutePath(project, options.getModelsRelativePath());
139 193
         String dataSourceTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getDataSourceTemplateRelativePath());
140 194
         String modelsTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getModelsTemplateRelativePath());
195
+        String typesCastAbsolutePath = Helper.getAbsolutePath(project, options.getCastFileRelativePath());
196
+
197
+        TypesCastOptions types = loadTypesCast(typesCastAbsolutePath);
141 198
 
142
-        DataSourceDbo dbo = convertOptions(options);
199
+        DataSourceDbo dbo = convertOptions(options, types);
143 200
 
144 201
         generateFile(dataSourceTemplateAbsolutePath, modelsAbsolutePath + File.separator +
145 202
                 "Database." + options.getFilesExtension(), dbo, null);

+ 5
- 1
src/com/rthoni/intellij/codefromds/business/Helper.java View File

@@ -61,8 +61,12 @@ public abstract class Helper {
61 61
     }
62 62
 
63 63
     public static String readFile(String path) throws IOException {
64
-        return Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8)
64
+        String data = Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8)
65 65
                 .stream().reduce("", (s1, s2) -> s1 + s2 + "\n");
66
+        if (data.startsWith("\uFEFF")) {
67
+            return data.substring(1);
68
+        }
69
+        return data;
66 70
     }
67 71
 
68 72
     public static String getJsonString(JSONObject obj, String key, String defaultValue)

+ 16
- 8
src/com/rthoni/intellij/codefromds/dbo/options/GenerateOptions.java View File

@@ -1,16 +1,11 @@
1 1
 package com.rthoni.intellij.codefromds.dbo.options;
2 2
 
3
-import com.intellij.database.model.DasTable;
4 3
 import com.intellij.database.psi.DbDataSource;
5
-import com.intellij.database.util.DasUtil;
6 4
 import com.rthoni.intellij.codefromds.business.Helper;
7 5
 import org.json.JSONObject;
8 6
 
9 7
 import java.io.File;
10 8
 import java.util.HashMap;
11
-import java.util.List;
12
-import java.util.Vector;
13
-import java.util.stream.Collectors;
14 9
 
15 10
 /**
16 11
  * Created by robin on 11/15/16.
@@ -29,12 +24,15 @@ public class GenerateOptions {
29 24
 
30 25
     private String _filesExtension;
31 26
 
27
+    private String _castFileRelativePath;
28
+
32 29
     public GenerateOptions(DbDataSource source) {
33 30
         _modelsRelativePath = "Models";
34
-        _dataSourceTemplateRelativePath = "Templates" + File.separator + "DataSource.twig";
35
-        _modelsTemplateRelativePath = "Templates" + File.separator + "Models.twig";
36
-        _configAbsolutePath = source.getProject().getBasePath() + File.separator + "code-from-ds.json";
31
+        _dataSourceTemplateRelativePath = "code-from-ds" + File.separator + "DataSource.twig";
32
+        _modelsTemplateRelativePath = "code-from-ds" + File.separator + "Models.twig";
33
+        _configAbsolutePath = source.getProject().getBasePath() + File.separator + "code-from-ds" + File.separator + "code-from-ds.json";
37 34
         _filesExtension = "cs";
35
+        _castFileRelativePath = "code-from-ds" + File.separator + "types-cast.json";
38 36
         _selection = new DataSourceSelection(source);
39 37
     }
40 38
 
@@ -46,6 +44,7 @@ public class GenerateOptions {
46 44
         map.put("dataSourceTemplateRelativePath", _dataSourceTemplateRelativePath);
47 45
         map.put("modelsTemplateRelativePath", _modelsTemplateRelativePath);
48 46
         map.put("filesExtension", _filesExtension);
47
+        map.put("castFileRelativePath", _castFileRelativePath);
49 48
         map.put("selection", _selection == null ? null : new JSONObject(_selection.toMap()));
50 49
 
51 50
         return map;
@@ -57,6 +56,7 @@ public class GenerateOptions {
57 56
         _dataSourceTemplateRelativePath = Helper.getJsonString(json, "dataSourceTemplateRelativePath");
58 57
         _modelsTemplateRelativePath = Helper.getJsonString(json, "modelsTemplateRelativePath");
59 58
         _filesExtension = Helper.getJsonString(json, "filesExtension");
59
+        _castFileRelativePath = Helper.getJsonString(json, "castFileRelativePath");
60 60
         _selection.fromJson(json.getJSONObject("selection"));
61 61
     }
62 62
 
@@ -107,4 +107,12 @@ public class GenerateOptions {
107 107
     public void setFilesExtension(String filesExtension) {
108 108
         _filesExtension = filesExtension;
109 109
     }
110
+
111
+    public String getCastFileRelativePath() {
112
+        return _castFileRelativePath;
113
+    }
114
+
115
+    public void setCastFileRelativePath(String castFileRelativePath) {
116
+        _castFileRelativePath = castFileRelativePath;
117
+    }
110 118
 }

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

@@ -0,0 +1,29 @@
1
+package com.rthoni.intellij.codefromds.dbo.options;
2
+
3
+import java.util.HashMap;
4
+
5
+/**
6
+ * Created by robin on 11/19/16.
7
+ */
8
+public class TypesCastOptions {
9
+
10
+    private String _arrayTemplate;
11
+
12
+    private HashMap<String, HashMap<String, String>> _types;
13
+
14
+    public String getArrayTemplate() {
15
+        return _arrayTemplate;
16
+    }
17
+
18
+    public void setArrayTemplate(String arrayTemplate) {
19
+        _arrayTemplate = arrayTemplate;
20
+    }
21
+
22
+    public HashMap<String, HashMap<String, String>> getTypes() {
23
+        return _types;
24
+    }
25
+
26
+    public void setTypes(HashMap<String, HashMap<String, String>> types) {
27
+        _types = types;
28
+    }
29
+}

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

@@ -11,6 +11,8 @@ public class ColumnDataSourceDbo {
11 11
 
12 12
     private boolean _isSelected;
13 13
 
14
+    private String _type;
15
+
14 16
     public String getName() {
15 17
         return _name;
16 18
     }
@@ -34,4 +36,12 @@ public class ColumnDataSourceDbo {
34 36
     public void setSelected(boolean selected) {
35 37
         _isSelected = selected;
36 38
     }
39
+
40
+    public String getType() {
41
+        return _type;
42
+    }
43
+
44
+    public void setType(String type) {
45
+        _type = type;
46
+    }
37 47
 }

+ 19
- 6
src/com/rthoni/intellij/codefromds/ui/dialogs/GenerateDialog.java View File

@@ -46,6 +46,8 @@ public class GenerateDialog extends DialogWrapper {
46 46
     private JLabel _lblDataSourceTemplatePath;
47 47
     private JLabel _lblModelsPath;
48 48
     private JLabel _lblModelsTemplatePath;
49
+    private TextFieldWithBrowseButton _textCastFile;
50
+    private JLabel _lblCastFile;
49 51
 
50 52
     private GenerateOptions _options;
51 53
 
@@ -71,6 +73,7 @@ public class GenerateDialog extends DialogWrapper {
71 73
             _textDataSourceTemplate.setText(Helper.getAbsolutePath(_project, _options.getDataSourceTemplateRelativePath()));
72 74
             _textModelsTemplate.setText(Helper.getAbsolutePath(_project, _options.getModelsTemplateRelativePath()));
73 75
             _textFilesExtension.setText(_options.getFilesExtension());
76
+            _textCastFile.setText(Helper.getAbsolutePath(_project, _options.getCastFileRelativePath()));
74 77
             _textConfigPath.setText(_options.getConfigAbsolutePath());
75 78
         }
76 79
         else {
@@ -79,6 +82,7 @@ public class GenerateDialog extends DialogWrapper {
79 82
             _textDataSourceTemplate.setText("");
80 83
             _textModelsTemplate.setText("");
81 84
             _textFilesExtension.setText("");
85
+            _textCastFile.setText("");
82 86
             _textConfigPath.setText("");
83 87
         }
84 88
     }
@@ -93,6 +97,7 @@ public class GenerateDialog extends DialogWrapper {
93 97
         File configPath = _options == null ? null : new File(_options.getConfigAbsolutePath());
94 98
         File configDir = _options == null ? null : new File(configPath.getParent());
95 99
         String extension = _options == null ? null : _options.getFilesExtension();
100
+        File castFilePath = _options == null ? null : new File(Helper.getAbsolutePath(_project, _options.getCastFileRelativePath()));
96 101
         if (_options == null) {
97 102
             info = new ValidationInfo("No Data Source Selected", _listDatasources);
98 103
         }
@@ -114,6 +119,9 @@ public class GenerateDialog extends DialogWrapper {
114 119
         else if (extension.startsWith(".")) {
115 120
             info = new ValidationInfo("Files extension must not include dot (.)", _textFilesExtension);
116 121
         }
122
+        else if (!castFilePath.exists() || !castFilePath.isFile()) {
123
+            info = new ValidationInfo("Cast file does not exists", _textCastFile.getTextField());
124
+        }
117 125
 
118 126
         return info;
119 127
     }
@@ -148,10 +156,11 @@ public class GenerateDialog extends DialogWrapper {
148 156
             }
149 157
         });
150 158
 
151
-        setupTextField(_textModels, null, true);
152
-        setupTextField(_textDataSourceTemplate, null, false);
153
-        setupTextField(_textModelsTemplate, null, false);
154
-        setupTextField(_textConfigPath, null, false);
159
+        setupTextField(_textModels, null, true, "Models");
160
+        setupTextField(_textDataSourceTemplate, null, false, "Data Source Template");
161
+        setupTextField(_textModelsTemplate, null, false, "Models Template");
162
+        setupTextField(_textConfigPath, null, false, "Configuration");
163
+        setupTextField(_textCastFile, null, false, "Types Cast");
155 164
 
156 165
         setupTextFieldListener(_textModels.getTextField(), s -> {
157 166
             _options.setModelsRelativePath(Helper.getRelativePath(_project, s));
@@ -167,6 +176,10 @@ public class GenerateDialog extends DialogWrapper {
167 176
         });
168 177
         setupTextFieldListener(_textFilesExtension, s -> _options.setFilesExtension(s));
169 178
         setupTextFieldListener(_textConfigPath.getTextField(), s -> _options.setConfigAbsolutePath(s));
179
+        setupTextFieldListener(_textCastFile.getTextField(), s -> {
180
+            _options.setCastFileRelativePath(Helper.getRelativePath(_project, s));
181
+            _lblCastFile.setText("$ProjectRoot/" + _options.getCastFileRelativePath());
182
+        });
170 183
 
171 184
         _listTables.setCellRenderer(new ColoredListCellRenderer() {
172 185
             @Override
@@ -216,12 +229,12 @@ public class GenerateDialog extends DialogWrapper {
216 229
         });
217 230
     }
218 231
 
219
-    private void setupTextField(TextFieldWithBrowseButton field, Project project, boolean dirsOnly)
232
+    private void setupTextField(TextFieldWithBrowseButton field, Project project, boolean dirsOnly, String title)
220 233
     {
221 234
         for (ActionListener l : field.getButton().getActionListeners()) {
222 235
             field.getButton().removeActionListener(l);
223 236
         }
224
-        field.addBrowseFolderListener("Choose Models Destination Folder", "Choose folder", project,
237
+        field.addBrowseFolderListener("Choose " + title + " " + (dirsOnly ? "Folder" : "File"), "Choose " + (dirsOnly ? "Folder" : "File"), project,
225 238
                 dirsOnly ? FileChooserDescriptorFactory.createSingleFolderDescriptor() : FileChooserDescriptorFactory.createSingleFileDescriptor());
226 239
     }
227 240
 

+ 27
- 5
src/com/rthoni/intellij/codefromds/ui/forms/GenerateForm.form View File

@@ -13,7 +13,7 @@
13 13
           <grid row="3" column="0" row-span="1" col-span="2" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
14 14
         </constraints>
15 15
       </vspacer>
16
-      <grid id="6946b" layout-manager="GridLayoutManager" row-count="8" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
16
+      <grid id="6946b" layout-manager="GridLayoutManager" row-count="10" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
17 17
         <margin top="0" left="0" bottom="0" right="0"/>
18 18
         <constraints>
19 19
           <grid row="2" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@@ -65,7 +65,7 @@
65 65
           </component>
66 66
           <component id="22d0d" class="javax.swing.JLabel">
67 67
             <constraints>
68
-              <grid row="7" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
68
+              <grid row="9" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
69 69
             </constraints>
70 70
             <properties>
71 71
               <text value="Configuration File:"/>
@@ -73,7 +73,7 @@
73 73
           </component>
74 74
           <component id="4c3cb" class="com.intellij.openapi.ui.TextFieldWithBrowseButton" binding="_textConfigPath">
75 75
             <constraints>
76
-              <grid row="7" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
76
+              <grid row="9" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
77 77
             </constraints>
78 78
             <properties/>
79 79
           </component>
@@ -103,7 +103,7 @@
103 103
           </component>
104 104
           <component id="c0c9e" class="javax.swing.JLabel">
105 105
             <constraints>
106
-              <grid row="6" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
106
+              <grid row="8" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
107 107
             </constraints>
108 108
             <properties>
109 109
               <text value="Files extension:"/>
@@ -111,12 +111,34 @@
111 111
           </component>
112 112
           <component id="880ce" class="javax.swing.JTextField" binding="_textFilesExtension">
113 113
             <constraints>
114
-              <grid row="6" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
114
+              <grid row="8" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
115 115
                 <preferred-size width="150" height="-1"/>
116 116
               </grid>
117 117
             </constraints>
118 118
             <properties/>
119 119
           </component>
120
+          <component id="3cbf6" class="javax.swing.JLabel">
121
+            <constraints>
122
+              <grid row="6" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
123
+            </constraints>
124
+            <properties>
125
+              <text value="Type Cast File:"/>
126
+            </properties>
127
+          </component>
128
+          <component id="853dd" class="com.intellij.openapi.ui.TextFieldWithBrowseButton" binding="_textCastFile">
129
+            <constraints>
130
+              <grid row="6" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
131
+            </constraints>
132
+            <properties/>
133
+          </component>
134
+          <component id="8682b" class="javax.swing.JLabel" binding="_lblCastFile">
135
+            <constraints>
136
+              <grid row="7" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
137
+            </constraints>
138
+            <properties>
139
+              <text value="$ProjectRoot/"/>
140
+            </properties>
141
+          </component>
120 142
         </children>
121 143
       </grid>
122 144
       <grid id="39adc" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">

Loading…
Cancel
Save