Browse Source

foreign keys

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

+ 37
- 1
src/com/rthoni/intellij/codefromds/business/Generator.java View File

@@ -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
 

+ 8
- 0
src/com/rthoni/intellij/codefromds/business/Helper.java View File

@@ -105,4 +105,12 @@ public abstract class Helper {
105 105
     {
106 106
         return getAbsolutePath(project.getBasePath(), relative);
107 107
     }
108
+
109
+    public static String pluralize(String str)
110
+    {
111
+        if (str.endsWith("y")) {
112
+            return str.substring(1, str.length() - 1) + "ies";
113
+        }
114
+        return str + "s";
115
+    }
108 116
 }

+ 0
- 4
src/com/rthoni/intellij/codefromds/dbo/options/ColumnSelection.java View File

@@ -14,10 +14,6 @@ public class ColumnSelection {
14 14
 
15 15
     private boolean _selected;
16 16
 
17
-    public ColumnSelection() {
18
-
19
-    }
20
-
21 17
     public ColumnSelection(DasColumn column) {
22 18
         _column = column;
23 19
         _selected = true;

+ 1
- 9
src/com/rthoni/intellij/codefromds/dbo/options/DataSourceSelection.java View File

@@ -1,6 +1,5 @@
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 4
 import com.intellij.database.util.DasUtil;
6 5
 import com.rthoni.intellij.codefromds.business.Helper;
@@ -23,14 +22,7 @@ public class DataSourceSelection {
23 22
 
24 23
     public DataSourceSelection(DbDataSource source) {
25 24
         _source = source;
26
-        final List<? extends DasTable> tables = DasUtil.getTables(source).toList();
27
-        _tables = new Vector<>();
28
-        for (DasTable table : tables) {
29
-            TableSelection tableSelection = new TableSelection();
30
-            tableSelection.setTable(table);
31
-            tableSelection.setColumns(DasUtil.getColumns(table).toList().stream().map(ColumnSelection::new).collect(Collectors.toList()));
32
-            _tables.add(tableSelection);
33
-        }
25
+        _tables = DasUtil.getTables(source).toList().stream().map(TableSelection::new).collect(Collectors.toList());
34 26
     }
35 27
 
36 28
     public HashMap<String, Object> toMap()

+ 0
- 15
src/com/rthoni/intellij/codefromds/dbo/options/GenerateOptions.java View File

@@ -36,21 +36,6 @@ public class GenerateOptions {
36 36
         _configAbsolutePath = source.getProject().getBasePath() + File.separator + "code-from-ds.json";
37 37
         _filesExtension = "cs";
38 38
         _selection = new DataSourceSelection(source);
39
-        _selection.setSource(source);
40
-        final List<? extends DasTable> tables = DasUtil.getTables(source).toList();
41
-        List<TableSelection> tableSelections = new Vector<>();
42
-        for (DasTable table : tables) {
43
-            TableSelection tableSelection = new TableSelection();
44
-            tableSelection.setTable(table);
45
-            tableSelection.setColumns(DasUtil.getColumns(table).toList().stream().map(c -> {
46
-                ColumnSelection selection = new ColumnSelection();
47
-                selection.setColumn(c);
48
-                selection.setSelected(true);
49
-                return selection;
50
-            }).collect(Collectors.toList()));
51
-            tableSelections.add(tableSelection);
52
-        }
53
-        _selection.setTables(tableSelections);
54 39
     }
55 40
 
56 41
     public HashMap<String, Object> toMap()

+ 8
- 0
src/com/rthoni/intellij/codefromds/dbo/options/TableSelection.java View File

@@ -1,6 +1,7 @@
1 1
 package com.rthoni.intellij.codefromds.dbo.options;
2 2
 
3 3
 import com.intellij.database.model.DasTable;
4
+import com.intellij.database.util.DasUtil;
4 5
 import com.rthoni.intellij.codefromds.business.Helper;
5 6
 import org.json.JSONArray;
6 7
 import org.json.JSONObject;
@@ -8,6 +9,7 @@ import org.json.JSONObject;
8 9
 import java.util.HashMap;
9 10
 import java.util.List;
10 11
 import java.util.Vector;
12
+import java.util.stream.Collectors;
11 13
 
12 14
 /**
13 15
  * Created by robin on 11/15/16.
@@ -18,6 +20,12 @@ public class TableSelection {
18 20
 
19 21
     private List<ColumnSelection> _columns;
20 22
 
23
+    public TableSelection(DasTable table)
24
+    {
25
+        _table = table;
26
+        _columns = DasUtil.getColumns(table).toList().stream().map(ColumnSelection::new).collect(Collectors.toList());
27
+    }
28
+
21 29
     public HashMap<String, Object> toMap()
22 30
     {
23 31
         HashMap<String, Object> map = new HashMap<>();

+ 85
- 0
src/com/rthoni/intellij/codefromds/dbo/template/ForeignKeyDbo.java View File

@@ -0,0 +1,85 @@
1
+package com.rthoni.intellij.codefromds.dbo.template;
2
+
3
+import java.util.List;
4
+
5
+/**
6
+ * Created by robin on 11/18/16.
7
+ */
8
+public class ForeignKeyDbo {
9
+
10
+    private TableDataSourceDbo _sourceTable;
11
+
12
+    private List<ColumnDataSourceDbo> _sourceColumns;
13
+
14
+    private TableDataSourceDbo _targetTable;
15
+
16
+    private List<ColumnDataSourceDbo> _targetColumns;
17
+
18
+    private String _sourceForeignKeyName;
19
+
20
+    private String _targetForeignKeyName;
21
+
22
+    private String _name;
23
+
24
+    public TableDataSourceDbo getSourceTable() {
25
+        return _sourceTable;
26
+    }
27
+
28
+    public boolean isSelected()
29
+    {
30
+        return _sourceColumns.stream().allMatch(ColumnDataSourceDbo::isSelected) &&
31
+                _targetColumns.stream().allMatch(ColumnDataSourceDbo::isSelected);
32
+    }
33
+
34
+    public void setSourceTable(TableDataSourceDbo sourceTable) {
35
+        _sourceTable = sourceTable;
36
+    }
37
+
38
+    public List<ColumnDataSourceDbo> getSourceColumns() {
39
+        return _sourceColumns;
40
+    }
41
+
42
+    public void setSourceColumns(List<ColumnDataSourceDbo> sourceColumns) {
43
+        _sourceColumns = sourceColumns;
44
+    }
45
+
46
+    public TableDataSourceDbo getTargetTable() {
47
+        return _targetTable;
48
+    }
49
+
50
+    public void setTargetTable(TableDataSourceDbo targetTable) {
51
+        _targetTable = targetTable;
52
+    }
53
+
54
+    public List<ColumnDataSourceDbo> getTargetColumns() {
55
+        return _targetColumns;
56
+    }
57
+
58
+    public void setTargetColumns(List<ColumnDataSourceDbo> targetColumns) {
59
+        _targetColumns = targetColumns;
60
+    }
61
+
62
+    public String getSourceForeignKeyName() {
63
+        return _sourceForeignKeyName;
64
+    }
65
+
66
+    public void setSourceForeignKeyName(String sourceForeignKeyName) {
67
+        _sourceForeignKeyName = sourceForeignKeyName;
68
+    }
69
+
70
+    public String getTargetForeignKeyName() {
71
+        return _targetForeignKeyName;
72
+    }
73
+
74
+    public void setTargetForeignKeyName(String targetForeignKeyName) {
75
+        _targetForeignKeyName = targetForeignKeyName;
76
+    }
77
+
78
+    public String getName() {
79
+        return _name;
80
+    }
81
+
82
+    public void setName(String name) {
83
+        _name = name;
84
+    }
85
+}

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

@@ -12,6 +12,10 @@ public class TableDataSourceDbo {
12 12
 
13 13
     private List<ColumnDataSourceDbo> _primaryKeys;
14 14
 
15
+    private List<ForeignKeyDbo> _sourceForeignKeys;
16
+
17
+    private List<ForeignKeyDbo> _targetForeignKeys;
18
+
15 19
     private boolean _hasAny;
16 20
 
17 21
     public String getName() {
@@ -45,4 +49,20 @@ public class TableDataSourceDbo {
45 49
     public void setHasAny(boolean hasAny) {
46 50
         _hasAny = hasAny;
47 51
     }
52
+
53
+    public List<ForeignKeyDbo> getSourceForeignKeys() {
54
+        return _sourceForeignKeys;
55
+    }
56
+
57
+    public void setSourceForeignKeys(List<ForeignKeyDbo> sourceForeignKeys) {
58
+        _sourceForeignKeys = sourceForeignKeys;
59
+    }
60
+
61
+    public List<ForeignKeyDbo> getTargetForeignKeys() {
62
+        return _targetForeignKeys;
63
+    }
64
+
65
+    public void setTargetForeignKeys(List<ForeignKeyDbo> targetForeignKeys) {
66
+        _targetForeignKeys = targetForeignKeys;
67
+    }
48 68
 }

Loading…
Cancel
Save