@@ -16,6 +16,7 @@ public class Model {
1616 private String name ;
1717 private TreeSet <String > implementations ;
1818 private ArrayList <Field > fields ;
19+ private ArrayList <JSONObject > indexes ;
1920 private static final String template = getTemplate ();
2021 private String tableName ;
2122 private String escapedTableName ;
@@ -34,6 +35,7 @@ protected Model(String modelName, JSONObject modelInfo, String packageName, Hash
3435 this .name = modelName ;
3536 this .implementations = new TreeSet <>();
3637 this .fields = new ArrayList <>();
38+ this .indexes = new ArrayList <>();
3739 this .options = options ;
3840 this .packageName = packageName ;
3941 this .tableName = Utils .camelCaseToUnderScore (name ).toLowerCase ();
@@ -91,7 +93,8 @@ protected Model(String modelName, JSONObject modelInfo, String packageName, Hash
9193 JSONArray constraints = modelInfo .get ("constraints" ).toJSONArray ();
9294 if (constraints !=null ){
9395 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 ();
9598
9699 for (Field field : fields ){
97100 if (field .getName ().equals (fieldName )){
@@ -103,6 +106,32 @@ protected Model(String modelName, JSONObject modelInfo, String packageName, Hash
103106 }
104107
105108
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+
106135 //Parse default values
107136 JSONArray defaultValues = modelInfo .get ("defaults" ).toJSONArray ();
108137 if (defaultValues !=null ){
@@ -1052,14 +1081,30 @@ public String getForeignKeySQL(){
10521081 public String getIndexSQL (){
10531082 StringBuilder str = new StringBuilder ();
10541083 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+
10581097 if (!field .isArray ()){
1098+
10591099 ForeignKey foreignKey = field .getForeignKey ();
10601100 if (foreignKey !=null ){
1101+
10611102 String columnName = foreignKey .getColumnName ().toUpperCase ();
10621103 String foreignTable = foreignKey .getForeignTable ().toUpperCase ();
1104+ columnNames .put (field .getName (), columnName );
1105+
1106+
1107+ //Automatically index foreign key fields
10631108 str .append ("CREATE INDEX " );
10641109 str .append (indexPrefix );
10651110 str .append (foreignTable );
@@ -1068,9 +1113,15 @@ public String getIndexSQL(){
10681113 str .append ("(" );
10691114 str .append (columnName );
10701115 str .append (");\r \n " );
1116+
1117+ indexes .remove (field .getName ());
10711118 }
10721119 else {
1120+
10731121 String columnName = field .getColumnName ().toUpperCase ();
1122+ columnNames .put (field .getName (), columnName );
1123+
1124+ //Automatically index geospatial fields
10741125 if (field .getColumnType ().startsWith ("geo" )){
10751126 str .append ("CREATE INDEX " );
10761127 str .append (indexPrefix );
@@ -1080,13 +1131,75 @@ public String getIndexSQL(){
10801131 str .append (" USING GIST(" );
10811132 str .append (columnName );
10821133 str .append (");\r \n " );
1134+
1135+ indexes .remove (field .getName ());
10831136 }
10841137 }
10851138 }
10861139 else {
10871140 //Handled by getDiamondTableSQL()
10881141 }
10891142 }
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+
10901203 return str .toString ();
10911204 }
10921205
0 commit comments