5
5
import org .jetbrains .annotations .NotNull ;
6
6
7
7
import java .util .ArrayList ;
8
+ import java .util .HashMap ;
8
9
import java .util .List ;
9
10
10
11
/**
@@ -17,11 +18,13 @@ public abstract class Table {
17
18
private final List <Column > columns ;
18
19
private String primaryKey ;
19
20
private final List <ForeignKey > foreignKeys ;
21
+ private final HashMap <String ,String []> uniqueKeys ;
20
22
21
23
public Table (String name ) {
22
24
this .name = name ;
23
25
this .columns = new ArrayList <>();
24
26
this .foreignKeys = new ArrayList <>();
27
+ this .uniqueKeys = new HashMap <>();
25
28
}
26
29
27
30
public void addColumn (@ NotNull Column column ) {
@@ -33,6 +36,9 @@ private ForeignKey foreignKey(String key) {
33
36
foreignKeys .add (entry );
34
37
return entry ;
35
38
}
39
+ public void uniqueKey (String key , String ... columns ) {
40
+ uniqueKeys .put (key , columns );
41
+ }
36
42
37
43
public void addColumns (@ NotNull Column ... columns ) {
38
44
for (Column column : columns ) {
@@ -71,6 +77,19 @@ public String toString() {
71
77
}
72
78
}
73
79
}
80
+ if (!uniqueKeys .isEmpty ()) {
81
+ for (String key : uniqueKeys .keySet ()) {
82
+ String [] columns = uniqueKeys .get (key );
83
+ builder .append (", UNIQUE KEY `" ).append (key ).append ("` (" );
84
+ for (int i = 0 ; i < columns .length ; i ++) {
85
+ builder .append ("`" ).append (columns [i ]).append ("`" );
86
+ if (i < columns .length - 1 ) {
87
+ builder .append (", " );
88
+ }
89
+ }
90
+ builder .append (")" );
91
+ }
92
+ }
74
93
builder .append (")" );
75
94
return builder .toString ();
76
95
}
0 commit comments