Skip to content

Commit 47f33ec

Browse files
author
synapticloop
committed
added in autoUpdaters, fixed java output
1 parent ba11182 commit 47f33ec

File tree

10 files changed

+175
-18
lines changed

10 files changed

+175
-18
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ plugins {
1919
group = 'synapticloop'
2020
archivesBaseName = 'h2zero'
2121
description = """lightweight ORM generator for mysql, java and optionally jsp/servlets"""
22-
version = '1.3.2'
22+
version = '1.4.0'
2323

2424
sourceCompatibility = 1.7
2525
targetCompatibility = 1.7

src/main/java/synapticloop/h2zero/H2ZeroParser.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
package synapticloop.h2zero;
22

3+
/*
4+
* Copyright (c) 2013-2017 synapticloop.
5+
*
6+
* All rights reserved.
7+
*
8+
* This source code and any derived binaries are covered by the terms and
9+
* conditions of the Licence agreement ("the Licence"). You may not use this
10+
* source code or any derived binaries except in compliance with the Licence.
11+
* A copy of the Licence is available in the file named LICENCE shipped with
12+
* this source code or binaries.
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the Licence is distributed on an "AS IS" BASIS, WITHOUT
16+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17+
* Licence for the specific language governing permissions and limitations
18+
* under the Licence.
19+
*/
20+
321
import java.io.BufferedReader;
422
import java.io.File;
523
import java.io.FileReader;

src/main/java/synapticloop/h2zero/Main.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
package synapticloop.h2zero;
22

3-
import java.io.BufferedReader;
4-
import java.io.IOException;
5-
import java.io.InputStream;
6-
import java.io.InputStreamReader;
7-
import java.util.HashMap;
8-
import java.util.Map;
9-
10-
import synapticloop.h2zero.plugin.ant.H2ZeroTask;
11-
import synapticloop.h2zero.util.SimpleLogger;
12-
import synapticloop.h2zero.util.SimpleLogger.LoggerType;
13-
14-
153
/*
164
* Copyright (c) 2012-2017 synapticloop.
175
*
@@ -30,6 +18,18 @@
3018
* under the Licence.
3119
*/
3220

21+
22+
import java.io.BufferedReader;
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.io.InputStreamReader;
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
29+
import synapticloop.h2zero.plugin.ant.H2ZeroTask;
30+
import synapticloop.h2zero.util.SimpleLogger;
31+
import synapticloop.h2zero.util.SimpleLogger.LoggerType;
32+
3333
public class Main {
3434

3535
private static final String USAGE_TXT = "/usage.txt";

src/main/java/synapticloop/h2zero/model/BaseSchemaObject.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,16 @@ public abstract class BaseSchemaObject {
2929
protected List<String> autoGeneratedUniqueFinders = new ArrayList<String>(); // a list of all of the automatically generated 'unique' result finders
3030
protected List<String> autoGeneratedMultipleFinders = new ArrayList<String>(); // a list of all of the automatically generated 'multiple' result finders
3131

32+
protected List<String> autoGeneratedUniqueUpdaters = new ArrayList<String>(); // a list of all of the automatically generated 'unique' result finders
33+
protected List<String> autoGeneratedMultipleUpdaters = new ArrayList<String>(); // a list of all of the automatically generated 'multiple' result finders
34+
3235
protected Map<String, BaseField> fieldLookup = new HashMap<String, BaseField>(); // a quick lookup map of all of the fields for this table
3336
protected Map<String, BaseField> inFieldLookup = new HashMap<String, BaseField>(); // a quick lookup for all of the 'in' fields
3437

3538
protected List<BaseField> fields = new ArrayList<BaseField>(); // a list of all of the fields on this table
3639

40+
protected BaseField primaryKeyField = null; // the field that is the primary key
41+
3742
protected Set<String> referencedFieldTypes = new HashSet<String>(); // this is a set of all of the referenced field types
3843
private int defaultStatementCacheSize;
3944

@@ -74,7 +79,6 @@ protected void populateFieldFinders(JSONObject jsonObject) throws H2ZeroParseExc
7479
} else if(autoFinder.compareToIgnoreCase(JSONKeyConstants.MULTIPLE) == 0) {
7580
autoGeneratedMultipleFinders.add(autoFinderName);
7681
} else {
77-
// TODO - probably want to change this into a switch??
7882
throw new H2ZeroParseException("Found an auto generate finder on '" + this.name + "." + autoFinderName + "' with a value of '" + autoFinder + "'. The allowable values are '" + JSONKeyConstants.UNIQUE + "', '" + JSONKeyConstants.SINGLE + "' or '" + JSONKeyConstants.MULTIPLE + "'.");
7983
}
8084
}

src/main/java/synapticloop/h2zero/model/Finder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ public class Finder extends BaseQueryObject {
2929
private boolean isAutoFinder = false;
3030
private int statementCacheSize = 1024; // by default we use a cache of 1024
3131

32+
/**
33+
* Create a finder model object
34+
*
35+
* @param baseSchemaObject The base schema object to attach to
36+
* @param finderObject The JSON object that encapsulates the finder
37+
*
38+
* @throws H2ZeroParseException If there was an error parsing the JSON finder
39+
* object
40+
*/
3241
public Finder(BaseSchemaObject baseSchemaObject, JSONObject finderObject) throws H2ZeroParseException {
3342
super(baseSchemaObject, finderObject);
3443
// if we have a select clause then we are returning a bean...

src/main/java/synapticloop/h2zero/model/Table.java

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.ArrayList;
66
import java.util.HashMap;
77
import java.util.HashSet;
8+
import java.util.Iterator;
89
import java.util.List;
910
import java.util.Map;
1011
import java.util.Set;
@@ -26,6 +27,7 @@
2627
*
2728
*/
2829
public class Table extends BaseSchemaObject {
30+
2931
private static List<String> ignoredKeys = new ArrayList<String>();
3032
static {
3133
ignoredKeys.add("comment");
@@ -141,6 +143,7 @@ public Table(JSONObject jsonObject, int defaultStatementCacheSize) throws H2Zero
141143
public void populateActions() throws H2ZeroParseException {
142144
populateFieldFinders(jsonObject);
143145
populateFinders(jsonObject);
146+
populateFieldUpdaters(jsonObject);
144147
populateUpdaters(jsonObject);
145148
populateDeleters(jsonObject);
146149
populateInserters(jsonObject);
@@ -199,6 +202,8 @@ private void populateFields(JSONObject jsonObject) throws H2ZeroParseException {
199202

200203
if(!baseField.getPrimary()) {
201204
nonPrimaryFields.add(baseField);
205+
} else {
206+
this.primaryKeyField = baseField;
202207
}
203208

204209
if(baseField.getIsLargeObject()) {
@@ -263,7 +268,16 @@ private void populateUpdaters(JSONObject jsonObject) throws H2ZeroParseException
263268
try {
264269
updaterJson = jsonObject.getJSONArray(JSONKeyConstants.UPDATERS);
265270
} catch (JSONException ojjsonex) {
266-
// do nothing - no finders is ok
271+
// do nothing - no updaters are ok
272+
}
273+
274+
// now go through and addUpdaters based on the multiple and unique updaters
275+
for (String multipleUpdater : autoGeneratedMultipleUpdaters) {
276+
generateAutomaticUpdater(multipleUpdater, false);
277+
}
278+
279+
for (String uniqueUpdater : autoGeneratedUniqueUpdaters) {
280+
generateAutomaticUpdater(uniqueUpdater, true);
267281
}
268282

269283
for (int i = 0; i < updaterJson.length(); i++) {
@@ -276,6 +290,89 @@ private void populateUpdaters(JSONObject jsonObject) throws H2ZeroParseException
276290
}
277291
}
278292

293+
private void generateAutomaticUpdater(String updaterFieldName, boolean unique) throws H2ZeroParseException {
294+
JSONObject autoUpdater = new JSONObject();
295+
JSONArray whereFieldsArray = new JSONArray();
296+
JSONArray setFieldsArray = new JSONArray();
297+
298+
try {
299+
StringBuilder updaterNameBuilder = new StringBuilder();
300+
updaterNameBuilder.append("update");
301+
updaterNameBuilder.append(NamingHelper.getFirstUpper(updaterFieldName));
302+
303+
StringBuilder setClauseBuilder = new StringBuilder();
304+
setClauseBuilder.append("set ");
305+
setClauseBuilder.append(updaterFieldName);
306+
setClauseBuilder.append(" = ? ");
307+
308+
setFieldsArray.put(updaterFieldName);
309+
310+
StringBuilder whereClauseBuilder = new StringBuilder();
311+
312+
String primaryKeyFieldName = primaryKeyField.getName();
313+
314+
if(unique) {
315+
updaterNameBuilder.append("By");
316+
updaterNameBuilder.append(NamingHelper.getFirstUpper(primaryKeyFieldName));
317+
whereClauseBuilder.append("where ");
318+
whereClauseBuilder.append(primaryKeyFieldName);
319+
whereClauseBuilder.append(" = ?");
320+
321+
whereFieldsArray.put(primaryKeyFieldName);
322+
}
323+
324+
325+
autoUpdater.put(JSONKeyConstants.NAME, updaterNameBuilder.toString());
326+
327+
autoUpdater.put(JSONKeyConstants.SET_CLAUSE, setClauseBuilder.toString());
328+
autoUpdater.put(JSONKeyConstants.SET_FIELDS, setFieldsArray);
329+
330+
if(unique) {
331+
autoUpdater.put(JSONKeyConstants.WHERE_CLAUSE, whereClauseBuilder.toString());
332+
autoUpdater.put(JSONKeyConstants.WHERE_FIELDS, whereFieldsArray);
333+
}
334+
335+
Updater updater = new Updater(this, autoUpdater);
336+
337+
updater.setIsAutoUpdater(true);
338+
updaters.add(updater);
339+
} catch (JSONException jsonex) {
340+
throw new H2ZeroParseException("Could not generate the field updater for '" + updaterFieldName + "'.", jsonex);
341+
}
342+
}
343+
344+
private void populateFieldUpdaters(JSONObject jsonObject) throws H2ZeroParseException {
345+
JSONArray updaterJson = new JSONArray();
346+
try {
347+
updaterJson = jsonObject.getJSONArray(JSONKeyConstants.FIELD_UPDATERS);
348+
} catch (JSONException ojjsonex) {
349+
// do nothing - no field updaters are ok
350+
}
351+
352+
for (int i = 0; i < updaterJson.length(); i++) {
353+
354+
JSONObject fieldUpdaterObject = updaterJson.optJSONObject(i);
355+
if(null == fieldUpdaterObject) {
356+
throw new H2ZeroParseException("Found a '" + JSONKeyConstants.FIELD_UPDATERS + "' json array on table '" + this.name + "', however the value at index '" + i + "' is not a json object.");
357+
}
358+
359+
Iterator<String> keys = fieldUpdaterObject.keys();
360+
// should only be one key
361+
String autoUpdaterName = (String)keys.next();
362+
String autoUpdater = fieldUpdaterObject.optString(autoUpdaterName);
363+
364+
if(null != autoUpdater) {
365+
if(autoUpdater.compareToIgnoreCase(JSONKeyConstants.UNIQUE) == 0 || autoUpdater.compareToIgnoreCase(JSONKeyConstants.SINGLE) == 0) {
366+
autoGeneratedUniqueUpdaters.add(autoUpdaterName);
367+
} else if(autoUpdater.compareToIgnoreCase(JSONKeyConstants.MULTIPLE) == 0) {
368+
autoGeneratedMultipleUpdaters.add(autoUpdaterName);
369+
} else {
370+
throw new H2ZeroParseException("Found an auto generate updater on '" + this.name + "." + autoUpdaterName + "' with a value of '" + autoUpdater + "'. The allowable values are '" + JSONKeyConstants.UNIQUE + "', '" + JSONKeyConstants.SINGLE + "' or '" + JSONKeyConstants.MULTIPLE + "'.");
371+
}
372+
}
373+
}
374+
}
375+
279376
private void populateDeleters(JSONObject jsonObject) throws H2ZeroParseException {
280377
JSONArray deleterJson = new JSONArray();
281378
try {
@@ -389,7 +486,7 @@ public boolean getHasQuestionInfields() {
389486
return(false);
390487
}
391488

392-
489+
393490

394491
// boring old getters and setters
395492
public String getEngine() { return(this.engine); }

src/main/java/synapticloop/h2zero/model/Updater.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import synapticloop.h2zero.model.util.JSONKeyConstants;
2828

2929
public class Updater extends BaseQueryObject {
30+
private boolean isAutoUpdater = false;
3031

3132
public Updater(Table table, JSONObject jsonObject) throws H2ZeroParseException {
3233
super(table, jsonObject);
@@ -95,7 +96,12 @@ public Updater(Table table, JSONObject jsonObject) throws H2ZeroParseException {
9596
}
9697
}
9798

99+
@Override
98100
public String getType() {
99101
return("Updater");
100102
}
103+
104+
public boolean getIsAutoUpdater() { return isAutoUpdater; }
105+
106+
public void setIsAutoUpdater(boolean isAutoUpdater) { this.isAutoUpdater = isAutoUpdater; }
101107
}

src/main/java/synapticloop/h2zero/model/util/JSONKeyConstants.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ private JSONKeyConstants() {}
1010
public static final String FIELDS = "fields";
1111

1212
public static final String FINDERS = "finders";
13+
public static final String FIELD_FINDERS = "fieldFinders";
14+
1315
public static final String QUESTIONS = "questions";
1416
public static final String COUNTERS = "counters";
1517
public static final String DELETERS = "deleters";
1618
public static final String INSERTERS = "inserters";
19+
1720
public static final String UPDATERS = "updaters";
21+
public static final String FIELD_UPDATERS = "fieldUpdaters";
22+
1823
public static final String CONSTANTS = "constants";
19-
public static final String FIELD_FINDERS = "fieldFinders";
2024

2125
public static final String WHERE_FIELDS = "whereFields";
2226
public static final String SELECT_FIELDS = "selectFields";

src/main/java/synapticloop/h2zero/validator/bean/Message.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
package synapticloop.h2zero.validator.bean;
22

3+
/*
4+
* Copyright (c) 2013-2017 synapticloop.
5+
*
6+
* All rights reserved.
7+
*
8+
* This source code and any derived binaries are covered by the terms and
9+
* conditions of the Licence agreement ("the Licence"). You may not use this
10+
* source code or any derived binaries except in compliance with the Licence.
11+
* A copy of the Licence is available in the file named LICENCE shipped with
12+
* this source code or binaries.
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the Licence is distributed on an "AS IS" BASIS, WITHOUT
16+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17+
* Licence for the specific language governing permissions and limitations
18+
* under the Licence.
19+
*/
20+
21+
322
public class Message {
423
private String type;
524
private String content;

src/main/resources/java-create-updater.templar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public class {table.javaClassName}Updater {{{\n}
7777
{\n}
7878

7979
{\t}{\t}{\t}numRowsUpdated = preparedStatement.executeUpdate();{\n}
80-
{\t}} catch (SQLException sqlex) {{{\n}
80+
{\t}{\t}} catch (SQLException sqlex) {{{\n}
8181
{\t}{\t}{\t}throw sqlex;{\n}
8282
{\t}{\t}} finally {{{\n}
8383
{\t}{\t}{\t}ConnectionManager.closeAll(preparedStatement);{\n}

0 commit comments

Comments
 (0)