|
@@ -1,10 +1,6 @@
|
1
|
1
|
package com.rthoni.intellij.codefromds.business;
|
2
|
2
|
|
3
|
|
-import com.intellij.database.model.DasForeignKey;
|
4
|
|
-import com.intellij.database.model.DataType;
|
5
|
|
-import com.intellij.database.model.MultiRef;
|
6
|
3
|
import com.intellij.database.psi.DbDataSource;
|
7
|
|
-import com.intellij.database.util.DasUtil;
|
8
|
4
|
import com.intellij.openapi.project.Project;
|
9
|
5
|
import com.rthoni.intellij.codefromds.dbo.options.ColumnSelection;
|
10
|
6
|
import com.rthoni.intellij.codefromds.dbo.options.GenerateOptions;
|
|
@@ -12,7 +8,7 @@ import com.rthoni.intellij.codefromds.dbo.options.TableSelection;
|
12
|
8
|
import com.rthoni.intellij.codefromds.dbo.options.TypesCastOptions;
|
13
|
9
|
import com.rthoni.intellij.codefromds.dbo.template.ColumnDataSourceDbo;
|
14
|
10
|
import com.rthoni.intellij.codefromds.dbo.template.DataSourceDbo;
|
15
|
|
-import com.rthoni.intellij.codefromds.dbo.template.ForeignKeyDbo;
|
|
11
|
+import com.rthoni.intellij.codefromds.dbo.template.SqlTypeDbo;
|
16
|
12
|
import com.rthoni.intellij.codefromds.dbo.template.TableDataSourceDbo;
|
17
|
13
|
import org.json.JSONArray;
|
18
|
14
|
import org.json.JSONObject;
|
|
@@ -25,8 +21,6 @@ import java.io.IOException;
|
25
|
21
|
import java.util.HashMap;
|
26
|
22
|
import java.util.List;
|
27
|
23
|
import java.util.Vector;
|
28
|
|
-import java.util.stream.Collectors;
|
29
|
|
-import java.util.stream.StreamSupport;
|
30
|
24
|
|
31
|
25
|
/**
|
32
|
26
|
* Created by robin on 11/15/16.
|
|
@@ -60,38 +54,17 @@ public abstract class Generator {
|
60
|
54
|
return options;
|
61
|
55
|
}
|
62
|
56
|
|
63
|
|
- public static ForeignKeyDbo convertForeignKey(DasForeignKey fk, DataSourceDbo source)
|
|
57
|
+ public static String convertSqlType(SqlTypeDbo type, TypesCastOptions options)
|
64
|
58
|
{
|
65
|
|
- ForeignKeyDbo dbo = new ForeignKeyDbo();
|
66
|
|
-
|
67
|
|
- dbo.setName(fk.getName());
|
68
|
|
-
|
69
|
|
- dbo.setSourceForeignKeyName("fk_" + fk.getRefTable().getName());
|
70
|
|
- dbo.setSourceTable(source.getTables().stream().filter(t -> t.getName().equals(fk.getTable().getName())).findFirst().get());
|
71
|
|
- MultiRef columnsRef = fk.getColumnsRef();
|
72
|
|
- dbo.setSourceColumns(dbo.getSourceTable().getColumns().stream().filter(c -> StreamSupport.stream(columnsRef.names().spliterator(), false).anyMatch(cc -> cc.equals(c.getName()))).collect(Collectors.toList()));
|
73
|
|
-
|
74
|
|
- dbo.setTargetForeignKeyName(fk.getTable().getName() + "_fk");
|
75
|
|
- dbo.setTargetTable(source.getTables().stream().filter(t -> t.getName().equals(fk.getRefTable().getName())).findFirst().get());
|
76
|
|
- MultiRef refColumns = fk.getRefColumns();
|
77
|
|
- dbo.setTargetColumns(dbo.getTargetTable().getColumns().stream().filter(c -> StreamSupport.stream(refColumns.names().spliterator(), false).anyMatch(cc -> cc.equals(c.getName()))).collect(Collectors.toList()));
|
78
|
|
-
|
79
|
|
- dbo.getTargetTable().getTargetForeignKeys().add(dbo);
|
80
|
|
-
|
81
|
|
- return dbo;
|
82
|
|
- }
|
83
|
|
-
|
84
|
|
- public static String convertSqlType(DataType type, TypesCastOptions options)
|
85
|
|
- {
|
86
|
|
- boolean isArray = type.typeName.endsWith("[]");
|
87
|
|
- String sqlTypeName = isArray ? type.typeName.substring(0, type.typeName.length() - 2) : type.typeName;
|
88
|
|
- String typeName = type.typeName;
|
|
59
|
+ boolean isArray = type.getType().endsWith("[]");
|
|
60
|
+ String sqlTypeName = isArray ? type.getType().substring(0, type.getType().length() - 2) : type.getType();
|
|
61
|
+ String typeName = type.getType();
|
89
|
62
|
HashMap<String, HashMap<String, String>> types = options.getTypes();
|
90
|
63
|
|
91
|
64
|
if (types.containsKey(sqlTypeName)) {
|
92
|
65
|
HashMap<String, String> subtype = types.get(sqlTypeName);
|
93
|
|
- if (subtype.containsKey(type.vagueArg)) {
|
94
|
|
- typeName = subtype.get(type.vagueArg);
|
|
66
|
+ if (subtype.containsKey(type.getVagueArg())) {
|
|
67
|
+ typeName = subtype.get(type.getVagueArg());
|
95
|
68
|
}
|
96
|
69
|
else if (subtype.containsKey("*")) {
|
97
|
70
|
typeName = subtype.get("*");
|
|
@@ -102,48 +75,45 @@ public abstract class Generator {
|
102
|
75
|
}
|
103
|
76
|
return typeName;
|
104
|
77
|
}
|
105
|
|
-
|
106
|
|
- public static ColumnDataSourceDbo convertColumn(ColumnSelection columnSelection, TypesCastOptions options)
|
107
|
|
- {
|
108
|
|
- ColumnDataSourceDbo dbo = new ColumnDataSourceDbo();
|
109
|
|
- dbo.setName(columnSelection.getColumn().getName());
|
110
|
|
- dbo.setPrimary(DasUtil.isPrimary(columnSelection.getColumn()));
|
111
|
|
- dbo.setSelected(columnSelection.isSelected());
|
112
|
|
- dbo.setNotNull(columnSelection.getColumn().isNotNull());
|
113
|
|
- dbo.setType(convertSqlType(columnSelection.getColumn().getDataType(), options));
|
114
|
|
- dbo.setTypeNotNull(options.getNonNullableTypes().contains(dbo.getType()));
|
115
|
|
- dbo.setDefaultValue(columnSelection.getColumn().getDefault());
|
116
|
|
-
|
117
|
|
- return dbo;
|
118
|
|
- }
|
119
|
|
-
|
120
|
|
- public static TableDataSourceDbo convertTable(TableSelection tableSelection, TypesCastOptions types)
|
121
|
|
- {
|
122
|
|
- TableDataSourceDbo dbo = new TableDataSourceDbo();
|
123
|
|
- dbo.setName(tableSelection.getTable().getName());
|
124
|
|
- dbo.setColumns(tableSelection.getColumns().stream().map(c -> convertColumn(c, types)).collect(Collectors.toList()));
|
125
|
|
- dbo.setPrimaryKeys(dbo.getColumns().stream().filter(ColumnDataSourceDbo::isPrimary).collect(Collectors.toList()));
|
126
|
|
- dbo.setHasAny(!tableSelection.hasNone());
|
127
|
|
- dbo.setTargetForeignKeys(new Vector<>());
|
128
|
|
- return dbo;
|
129
|
|
- }
|
|
78
|
+//
|
|
79
|
+// public static boolean isUnionSame(List<ColumnDataSourceDbo> part1, List<ColumnDataSourceDbo> part2, List<ColumnDataSourceDbo> list2)
|
|
80
|
+// {
|
|
81
|
+// List<ColumnDataSourceDbo> list1 = new Vector<>(part1);
|
|
82
|
+// for (ColumnDataSourceDbo col : part2) {
|
|
83
|
+// if (!list1.contains(col)) {
|
|
84
|
+// list1.add(col);
|
|
85
|
+// }
|
|
86
|
+// }
|
|
87
|
+// if (list1.size() != list2.size()) {
|
|
88
|
+// return false;
|
|
89
|
+// }
|
|
90
|
+// list1.sort((o1, o2) -> o1.getName().compareTo(o2.getName()));
|
|
91
|
+// list2.sort((o1, o2) -> o1.getName().compareTo(o2.getName()));
|
|
92
|
+// return list1.equals(list2);
|
|
93
|
+// }
|
130
|
94
|
|
131
|
95
|
public static DataSourceDbo convertOptions(GenerateOptions options, TypesCastOptions types)
|
132
|
96
|
{
|
133
|
|
- DataSourceDbo dbo = new DataSourceDbo();
|
134
|
|
-
|
135
|
|
- dbo.setName(options.getSelection().getSource().getName());
|
136
|
|
- dbo.setTables(options.getSelection().getTables().stream().map(t -> convertTable(t, types)).collect(Collectors.toList()));
|
137
|
|
-
|
138
|
|
- List<TableDataSourceDbo> tables = dbo.getTables();
|
139
|
|
- List<TableSelection> tableSelections = options.getSelection().getTables();
|
140
|
|
- for (int i = 0; i < tables.size(); ++i) {
|
141
|
|
- TableDataSourceDbo table = tables.get(i);
|
142
|
|
- TableSelection tableSelection = tableSelections.get(i);
|
143
|
|
- table.setSourceForeignKeys(DasUtil.getForeignKeys(tableSelection.getTable()).toList().stream().map(t -> convertForeignKey(t, dbo)).collect(Collectors.toList()));
|
|
97
|
+ String dataSourceName = options.getSelection().getSource().getName();
|
|
98
|
+ DataSourceDbo dataSourceDbo = DataSourcesBusiness.getDataSourceDbo(dataSourceName);
|
|
99
|
+
|
|
100
|
+ for (TableDataSourceDbo table : dataSourceDbo.getTables())
|
|
101
|
+ {
|
|
102
|
+ TableSelection tableSelection = options.getSelection().getTables().stream()
|
|
103
|
+ .filter(t -> t.getTable().getName().equals(table.getName()))
|
|
104
|
+ .findFirst().orElse(null);
|
|
105
|
+ for (ColumnDataSourceDbo column : table.getColumns())
|
|
106
|
+ {
|
|
107
|
+ ColumnSelection columnSelection = tableSelection.getColumns().stream()
|
|
108
|
+ .filter(c -> c.getColumn().getName().equals(column.getName()))
|
|
109
|
+ .findFirst().orElse(null);
|
|
110
|
+ column.setType(convertSqlType(column.getSqlType(), types));
|
|
111
|
+ column.setTypeNotNull(types.getNonNullableTypes().contains(column.getType()));
|
|
112
|
+ column.setSelected(columnSelection.isSelected());
|
|
113
|
+ }
|
144
|
114
|
}
|
145
|
115
|
|
146
|
|
- return dbo;
|
|
116
|
+ return dataSourceDbo;
|
147
|
117
|
}
|
148
|
118
|
|
149
|
119
|
public static TypesCastOptions loadTypesCast(String file) throws IOException
|
|
@@ -199,20 +169,22 @@ public abstract class Generator {
|
199
|
169
|
|
200
|
170
|
public static void generate(GenerateOptions options, Project project) throws IOException
|
201
|
171
|
{
|
|
172
|
+ DataSourcesBusiness.getDataSourcesDbo();
|
202
|
173
|
String modelsAbsolutePath = Helper.getAbsolutePath(project, options.getModelsRelativePath());
|
203
|
174
|
String dataSourceTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getDataSourceTemplateRelativePath());
|
204
|
175
|
String modelsTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getModelsTemplateRelativePath());
|
205
|
176
|
String typesCastAbsolutePath = Helper.getAbsolutePath(project, options.getCastFileRelativePath());
|
|
177
|
+ String dataSourceAbsolutePath = Helper.getAbsolutePath(project, options.getDataSourceRelativePath());
|
206
|
178
|
|
207
|
179
|
TypesCastOptions types = loadTypesCast(typesCastAbsolutePath);
|
208
|
180
|
|
209
|
181
|
DataSourceDbo dbo = convertOptions(options, types);
|
210
|
182
|
|
211
|
|
- generateFile(dataSourceTemplateAbsolutePath, Helper.getAbsolutePath(project, options.getDataSourceRelativePath()), dbo, null);
|
|
183
|
+ generateFile(dataSourceTemplateAbsolutePath, dataSourceAbsolutePath, dbo, null);
|
212
|
184
|
for (TableDataSourceDbo table : dbo.getTables()) {
|
213
|
185
|
if (table.hasAny()) {
|
214
|
|
- generateFile(modelsTemplateAbsolutePath, modelsAbsolutePath + File.separator +
|
215
|
|
- table.getName() + "." + options.getFilesExtension(), dbo, table);
|
|
186
|
+ String filename = modelsAbsolutePath + File.separator + table.getName() + "." + options.getFilesExtension();
|
|
187
|
+ generateFile(modelsTemplateAbsolutePath, filename, dbo, table);
|
216
|
188
|
}
|
217
|
189
|
}
|
218
|
190
|
}
|