@@ -16,6 +16,7 @@ public class Model {
16
16
private String name ;
17
17
private TreeSet <String > implementations ;
18
18
private ArrayList <Field > fields ;
19
+ private ArrayList <JSONObject > indexes ;
19
20
private static final String template = getTemplate ();
20
21
private String tableName ;
21
22
private String escapedTableName ;
@@ -34,6 +35,7 @@ protected Model(String modelName, JSONObject modelInfo, String packageName, Hash
34
35
this .name = modelName ;
35
36
this .implementations = new TreeSet <>();
36
37
this .fields = new ArrayList <>();
38
+ this .indexes = new ArrayList <>();
37
39
this .options = options ;
38
40
this .packageName = packageName ;
39
41
this .tableName = Utils .camelCaseToUnderScore (name ).toLowerCase ();
@@ -91,7 +93,8 @@ protected Model(String modelName, JSONObject modelInfo, String packageName, Hash
91
93
JSONArray constraints = modelInfo .get ("constraints" ).toJSONArray ();
92
94
if (constraints !=null ){
93
95
for (JSONValue constraint : constraints ){
94
- String fieldName = constraint .get ("name" ).toString ();
96
+ String fieldName = constraint .get ("field" ).toString ();
97
+ if (fieldName ==null ) fieldName = constraint .get ("name" ).toString ();
95
98
96
99
for (Field field : fields ){
97
100
if (field .getName ().equals (fieldName )){
@@ -103,6 +106,32 @@ protected Model(String modelName, JSONObject modelInfo, String packageName, Hash
103
106
}
104
107
105
108
109
+ //Parse indexes
110
+ JSONArray indexes = modelInfo .get ("indexes" ).toJSONArray ();
111
+ if (indexes ==null ) indexes = modelInfo .get ("indices" ).toJSONArray ();
112
+ if (indexes !=null ){
113
+ for (JSONValue index : indexes ){
114
+
115
+ JSONObject idx ;
116
+ if (index .toObject () instanceof String ){
117
+ String fieldName = index .toString ();
118
+ idx = new JSONObject ();
119
+ idx .set ("field" , fieldName );
120
+ }
121
+ else {
122
+ idx = index .toJSONObject ();
123
+ if (idx !=null ){
124
+ if (idx .has ("fields" )){
125
+ idx .set ("field" , idx .remove ("fields" ));
126
+ }
127
+ }
128
+ }
129
+
130
+ if (idx !=null ) this .indexes .add (idx );
131
+ }
132
+ }
133
+
134
+
106
135
//Parse default values
107
136
JSONArray defaultValues = modelInfo .get ("defaults" ).toJSONArray ();
108
137
if (defaultValues !=null ){
@@ -1052,14 +1081,30 @@ public String getForeignKeySQL(){
1052
1081
public String getIndexSQL (){
1053
1082
StringBuilder str = new StringBuilder ();
1054
1083
String indexPrefix = "IDX_" + tableName .toUpperCase ()+ "_" ;
1055
- Iterator <Field > it = fields .iterator ();
1056
- while (it .hasNext ()){
1057
- Field field = it .next ();
1084
+ HashMap <String , String > columnNames = new HashMap <>();
1085
+
1086
+ //Create a hashmap of indexes using field names as keys
1087
+ HashMap <String , JSONObject > indexes = new HashMap <>();
1088
+ for (JSONObject index : this .indexes ){
1089
+ String fieldName = index .get ("field" ).toString ();
1090
+ indexes .put (fieldName , index );
1091
+ }
1092
+
1093
+
1094
+ //Add indexes to foreign key and geo fields
1095
+ for (Field field : fields ){
1096
+
1058
1097
if (!field .isArray ()){
1098
+
1059
1099
ForeignKey foreignKey = field .getForeignKey ();
1060
1100
if (foreignKey !=null ){
1101
+
1061
1102
String columnName = foreignKey .getColumnName ().toUpperCase ();
1062
1103
String foreignTable = foreignKey .getForeignTable ().toUpperCase ();
1104
+ columnNames .put (field .getName (), columnName );
1105
+
1106
+
1107
+ //Automatically index foreign key fields
1063
1108
str .append ("CREATE INDEX " );
1064
1109
str .append (indexPrefix );
1065
1110
str .append (foreignTable );
@@ -1068,9 +1113,15 @@ public String getIndexSQL(){
1068
1113
str .append ("(" );
1069
1114
str .append (columnName );
1070
1115
str .append (");\r \n " );
1116
+
1117
+ indexes .remove (field .getName ());
1071
1118
}
1072
1119
else {
1120
+
1073
1121
String columnName = field .getColumnName ().toUpperCase ();
1122
+ columnNames .put (field .getName (), columnName );
1123
+
1124
+ //Automatically index geospatial fields
1074
1125
if (field .getColumnType ().startsWith ("geo" )){
1075
1126
str .append ("CREATE INDEX " );
1076
1127
str .append (indexPrefix );
@@ -1080,13 +1131,75 @@ public String getIndexSQL(){
1080
1131
str .append (" USING GIST(" );
1081
1132
str .append (columnName );
1082
1133
str .append (");\r \n " );
1134
+
1135
+ indexes .remove (field .getName ());
1083
1136
}
1084
1137
}
1085
1138
}
1086
1139
else {
1087
1140
//Handled by getDiamondTableSQL()
1088
1141
}
1089
1142
}
1143
+
1144
+
1145
+
1146
+ //Add user defined indexes
1147
+ for (String key : indexes .keySet ()){
1148
+ JSONObject index = indexes .get (key );
1149
+ String name = index .get ("name" ).toString ();
1150
+ String type = index .get ("type" ).toString ();
1151
+
1152
+
1153
+ ArrayList <String > columns = new ArrayList <>();
1154
+ if (index .get ("field" ).toObject () instanceof String ){
1155
+ String fieldName = index .get ("field" ).toString ();
1156
+ String columnName = columnNames .get (fieldName );
1157
+ if (columnName !=null ){
1158
+ columns .add (columnName );
1159
+ }
1160
+ }
1161
+ else {
1162
+ JSONArray arr = index .get ("field" ).toJSONArray ();
1163
+ if (arr !=null ){
1164
+ for (JSONValue v : arr ){
1165
+ String fieldName = v .toString ();
1166
+ String columnName = columnNames .get (fieldName );
1167
+ if (columnName !=null ){
1168
+ columns .add (columnName );
1169
+ }
1170
+ }
1171
+ }
1172
+ }
1173
+ if (columns .isEmpty ()) continue ;
1174
+
1175
+
1176
+
1177
+
1178
+
1179
+ str .append ("CREATE " );
1180
+ if (type !=null ) str .append (type + " " );
1181
+ str .append ("INDEX " );
1182
+ if (name ==null ){
1183
+ str .append (indexPrefix );
1184
+ for (int i =0 ; i <columns .size (); i ++){
1185
+ if (i >0 ) str .append ("_" );
1186
+ str .append (columns .get (i ));
1187
+ }
1188
+ }
1189
+ else {
1190
+ str .append (name .toUpperCase ());
1191
+ }
1192
+
1193
+ str .append (" ON " );
1194
+ str .append (escapedTableName );
1195
+ str .append ("(" );
1196
+ for (int i =0 ; i <columns .size (); i ++){
1197
+ if (i >0 ) str .append (", " );
1198
+ str .append (columns .get (i ));
1199
+ }
1200
+ str .append (");\r \n " );
1201
+ }
1202
+
1090
1203
return str .toString ();
1091
1204
}
1092
1205
0 commit comments