|
@@ -1,5 +1,7 @@
|
1
|
1
|
package com.rthoni.intellij.codefromds.business;
|
2
|
2
|
|
|
3
|
+import com.intellij.database.model.DasForeignKey;
|
|
4
|
+import com.intellij.database.model.MultiRef;
|
3
|
5
|
import com.intellij.database.psi.DbDataSource;
|
4
|
6
|
import com.intellij.database.util.DasUtil;
|
5
|
7
|
import com.intellij.openapi.project.Project;
|
|
@@ -8,6 +10,7 @@ import com.rthoni.intellij.codefromds.dbo.options.GenerateOptions;
|
8
|
10
|
import com.rthoni.intellij.codefromds.dbo.options.TableSelection;
|
9
|
11
|
import com.rthoni.intellij.codefromds.dbo.template.ColumnDataSourceDbo;
|
10
|
12
|
import com.rthoni.intellij.codefromds.dbo.template.DataSourceDbo;
|
|
13
|
+import com.rthoni.intellij.codefromds.dbo.template.ForeignKeyDbo;
|
11
|
14
|
import com.rthoni.intellij.codefromds.dbo.template.TableDataSourceDbo;
|
12
|
15
|
import org.json.JSONObject;
|
13
|
16
|
import org.jtwig.JtwigModel;
|
|
@@ -20,7 +23,10 @@ import java.nio.charset.StandardCharsets;
|
20
|
23
|
import java.nio.file.Files;
|
21
|
24
|
import java.nio.file.Paths;
|
22
|
25
|
import java.util.HashMap;
|
|
26
|
+import java.util.List;
|
|
27
|
+import java.util.Vector;
|
23
|
28
|
import java.util.stream.Collectors;
|
|
29
|
+import java.util.stream.StreamSupport;
|
24
|
30
|
|
25
|
31
|
/**
|
26
|
32
|
* Created by robin on 11/15/16.
|
|
@@ -54,6 +60,27 @@ public abstract class Generator {
|
54
|
60
|
return options;
|
55
|
61
|
}
|
56
|
62
|
|
|
63
|
+ public static ForeignKeyDbo convertForeignKey(DasForeignKey fk, DataSourceDbo source)
|
|
64
|
+ {
|
|
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
|
+
|
57
|
84
|
public static ColumnDataSourceDbo convertColumn(ColumnSelection columnSelection)
|
58
|
85
|
{
|
59
|
86
|
ColumnDataSourceDbo dbo = new ColumnDataSourceDbo();
|
|
@@ -68,8 +95,9 @@ public abstract class Generator {
|
68
|
95
|
TableDataSourceDbo dbo = new TableDataSourceDbo();
|
69
|
96
|
dbo.setName(tableSelection.getTable().getName());
|
70
|
97
|
dbo.setColumns(tableSelection.getColumns().stream().map(Generator::convertColumn).collect(Collectors.toList()));
|
71
|
|
- dbo.setPrimaryKeys(tableSelection.getColumns().stream().filter(c -> DasUtil.isPrimary(c.getColumn())).map(Generator::convertColumn).collect(Collectors.toList()));
|
|
98
|
+ dbo.setPrimaryKeys(dbo.getColumns().stream().filter(ColumnDataSourceDbo::isPrimary).collect(Collectors.toList()));
|
72
|
99
|
dbo.setHasAny(!tableSelection.hasNone());
|
|
100
|
+ dbo.setTargetForeignKeys(new Vector<>());
|
73
|
101
|
return dbo;
|
74
|
102
|
}
|
75
|
103
|
|
|
@@ -80,6 +108,14 @@ public abstract class Generator {
|
80
|
108
|
dbo.setName(options.getSelection().getSource().getName());
|
81
|
109
|
dbo.setTables(options.getSelection().getTables().stream().map(Generator::convertTable).collect(Collectors.toList()));
|
82
|
110
|
|
|
111
|
+ List<TableDataSourceDbo> tables = dbo.getTables();
|
|
112
|
+ List<TableSelection> tableSelections = options.getSelection().getTables();
|
|
113
|
+ for (int i = 0; i < tables.size(); ++i) {
|
|
114
|
+ TableDataSourceDbo table = tables.get(i);
|
|
115
|
+ TableSelection tableSelection = tableSelections.get(i);
|
|
116
|
+ table.setSourceForeignKeys(DasUtil.getForeignKeys(tableSelection.getTable()).toList().stream().map(t -> convertForeignKey(t, dbo)).collect(Collectors.toList()));
|
|
117
|
+ }
|
|
118
|
+
|
83
|
119
|
return dbo;
|
84
|
120
|
}
|
85
|
121
|
|