Skip to content

Commit a4b6fe8

Browse files
committed
Added support for inline field constraints and default values in the Model parser
1 parent e856e79 commit a4b6fe8

File tree

1 file changed

+77
-47
lines changed

1 file changed

+77
-47
lines changed

src/javaxt/orm/Model.java

Lines changed: 77 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,19 @@ protected Model(String modelName, JSONObject modelInfo, String packageName, Hash
6161

6262
//Parse fields
6363
JSONArray arr = modelInfo.get("fields").toJSONArray();
64-
if (arr!=null)
65-
for (int i=0; i<arr.length(); i++){
66-
JSONObject json = arr.get(i).toJSONObject();
67-
Field field = new Field(json.get("name").toString(), json.get("type").toString());
68-
69-
70-
//Don't add ID field. It is added automatically.
71-
if (field.getName().equalsIgnoreCase("id")) continue;
72-
73-
74-
fields.add(field);
64+
if (arr!=null){
65+
for (JSONValue f : arr){
66+
String name = f.get("name").toString();
67+
String type = f.get("type").toString();
68+
69+
//Don't add ID field. It is added automatically.
70+
if (name.equalsIgnoreCase("id")) continue;
71+
72+
//Create field and update the fields array
73+
Field field = new Field(name, type);
74+
addConstraints(field, f.toJSONObject());
75+
this.fields.add(field);
76+
}
7577
}
7678

7779

@@ -85,58 +87,86 @@ protected Model(String modelName, JSONObject modelInfo, String packageName, Hash
8587
}
8688

8789

88-
8990
//Parse constraints
9091
JSONArray constraints = modelInfo.get("constraints").toJSONArray();
91-
if (constraints!=null)
92-
for (int i=0; i<constraints.length(); i++){
93-
JSONObject constraint = constraints.get(i).toJSONObject();
94-
String fieldName = constraint.get("name").toString();
95-
Boolean isRequired = constraint.get("required").toBoolean();
96-
if (isRequired==null) isRequired = constraint.get("nullable").toBoolean();
97-
Boolean isUnique = constraint.get("unique").toBoolean();
98-
Integer length = constraint.get("length").toInteger();
99-
if (length==null) length = constraint.get("size").toInteger();
100-
Integer srid = constraint.get("srid").toInteger();
101-
102-
for (Field field : fields){
103-
if (field.getName().equals(fieldName)){
104-
if (isRequired!=null) field.isRequired(isRequired);
105-
if (isUnique!=null) field.isUnique(isUnique);
106-
if (length!=null) field.setLength(length);
107-
if (srid!=null) field.setSRID(srid);
108-
109-
ForeignKey foreignKey = field.getForeignKey();
110-
if (foreignKey!=null){
111-
if (constraint.has("onDelete")){
112-
foreignKey.onDelete(constraint.get("onDelete").toString());
113-
}
92+
if (constraints!=null){
93+
for (JSONValue constraint : constraints){
94+
String fieldName = constraint.get("name").toString();
95+
96+
for (Field field : fields){
97+
if (field.getName().equals(fieldName)){
98+
addConstraints(field, constraint.toJSONObject());
99+
break;
114100
}
115-
116-
break;
117101
}
118102
}
119103
}
120104

121105

122106
//Parse default values
123107
JSONArray defaultValues = modelInfo.get("defaults").toJSONArray();
124-
if (defaultValues!=null)
125-
for (int i=0; i<defaultValues.length(); i++){
126-
JSONObject json = defaultValues.get(i).toJSONObject();
127-
String fieldName = json.get("name").toString();
128-
Object val = json.get("value").toObject();
129-
130-
for (Field field : fields){
131-
if (field.getName().equals(fieldName)){
132-
field.setDefaultValue(val);
133-
break;
108+
if (defaultValues!=null){
109+
for (JSONValue d : defaultValues){
110+
String fieldName = d.get("name").toString();
111+
112+
for (Field field : fields){
113+
if (field.getName().equals(fieldName)){
114+
field.setDefaultValue(d.get("value").toObject());
115+
break;
116+
}
134117
}
135118
}
136119
}
137120
}
138121

139122

123+
//**************************************************************************
124+
//** addConstraints
125+
//**************************************************************************
126+
/** Used to add constraints to a given field. Constraints can be defined in
127+
* a "constraints" array or inline with the field definition. If both are
128+
* present, the constraints in the "constraints" array may override
129+
* constraints defined inline.
130+
*/
131+
private void addConstraints(Field field, JSONObject constraint){
132+
133+
Boolean isRequired = constraint.get("required").toBoolean();
134+
if (isRequired==null) isRequired = constraint.get("nullable").toBoolean();
135+
Boolean isUnique = constraint.get("unique").toBoolean();
136+
Integer length = constraint.get("length").toInteger();
137+
if (length==null) length = constraint.get("size").toInteger();
138+
Integer srid = constraint.get("srid").toInteger();
139+
140+
141+
if (isRequired!=null) field.isRequired(isRequired);
142+
if (isUnique!=null) field.isUnique(isUnique);
143+
if (length!=null) field.setLength(length);
144+
if (srid!=null) field.setSRID(srid);
145+
146+
147+
ForeignKey foreignKey = field.getForeignKey();
148+
if (foreignKey!=null){
149+
String onDelete = constraint.get("onDelete").toString();
150+
151+
//Special case for models. Cascade delete by default
152+
if (onDelete==null && field.isModel()){
153+
onDelete = "cascade";
154+
}
155+
156+
if (onDelete!=null){
157+
foreignKey.onDelete(onDelete);
158+
}
159+
}
160+
161+
162+
if (constraint.has("default")){
163+
Object defaultValue = constraint.get("default").toObject();
164+
field.setDefaultValue(defaultValue);
165+
}
166+
167+
}
168+
169+
140170
//**************************************************************************
141171
//** getName
142172
//**************************************************************************

0 commit comments

Comments
 (0)