Skip to content

Commit acf1475

Browse files
author
synapticloop
committed
updated error messages, fixed behind-the-scenes hydration, added in quicker build files
1 parent 176b6ee commit acf1475

14 files changed

Lines changed: 138 additions & 44 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ plugins {
2121
group = 'synapticloop'
2222
archivesBaseName = 'h2zero'
2323
description = """lightweight ORM generator for mysql/sqlite, java with extensions for taglibs and routemaster"""
24-
version = '4.1.11'
24+
version = '4.2.0'
2525

2626
tasks.withType(Javadoc).all { enabled = false }
2727

build.h2zero.mysql.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ buildscript {
99
}
1010

1111
dependencies {
12-
classpath 'synapticloop:h2zero:4.1.10'
12+
classpath 'synapticloop:h2zero:4.2.0'
1313
// classpath 'synapticloop:h2zero-extension-taglibs:1.0.0'
1414
}
1515
}

build.mysql.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
22

3-
./gradlew --no-daemon assemble pTML -b build.gradle
4-
./gradlew --no-daemon -b build.h2zero.mysql.gradle h2zero
3+
./gradlew assemble pTML -b build.gradle
4+
./gradlew -b build.h2zero.mysql.gradle h2zero
55

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import synapticloop.h2zero.validator.field.FieldIgnoredKeysValidator;
6363
import synapticloop.h2zero.validator.field.FieldNameDuplicateValidator;
6464
import synapticloop.h2zero.validator.field.FieldNotNullLengthValidator;
65+
import synapticloop.h2zero.validator.field.FieldPopulateForeignKeyValidator;
6566
import synapticloop.h2zero.validator.field.FieldPopulatePrimaryKeyValidator;
6667
import synapticloop.h2zero.validator.field.SQLite3FieldBlobValidator;
6768
import synapticloop.h2zero.validator.field.SQLite3FieldClobValidator;
@@ -132,6 +133,7 @@ public class H2ZeroParser {
132133
// field validators
133134
validators.add(new FieldDefaultValueValidator());
134135
validators.add(new FieldPopulatePrimaryKeyValidator());
136+
validators.add(new FieldPopulateForeignKeyValidator());
135137
validators.add(new FieldNameDuplicateValidator());
136138
validators.add(new FieldIgnoredKeysValidator());
137139
validators.add(new FieldNotNullLengthValidator());
@@ -219,14 +221,18 @@ public class H2ZeroParser {
219221
}
220222
}
221223
}
224+
225+
private static final List<String> FATAL_MESSAGES = new ArrayList<>();
226+
227+
public H2ZeroParser() {};
222228

223229
/**
224230
* Parse a .h2zero file
225231
*
226232
* @param file The file to be parsed
227233
* @throws H2ZeroParseException if there was an error parsing the file
228234
*/
229-
public H2ZeroParser(File file) throws H2ZeroParseException {
235+
public void parse(File file) throws H2ZeroParseException {
230236
JSONObject jsonObject = null;
231237
try {
232238
jsonObject = getJSONFileContents(file);
@@ -255,7 +261,7 @@ public H2ZeroParser(File file) throws H2ZeroParseException {
255261
boolean isValid = checkAndLogValidators();
256262

257263
if(!isValid) {
258-
throw new H2ZeroParseException("Validators found FATAL warnings, exiting...");
264+
throw new H2ZeroParseException("Validators found the following FATAL warnings: (Exiting...)");
259265
}
260266
}
261267

@@ -279,7 +285,9 @@ private boolean checkAndLogValidators() {
279285
} else if(message.getType().equals(SimpleLogger.WARN)){
280286
SimpleLogger.logWarn(LoggerType.VALIDATOR, String.format("[ %-" + maxValidatorClassNameLength + "s ] %s", validator.getClass().getSimpleName(), message.getContent()));
281287
} else if(message.getType().equals(SimpleLogger.FATAL)){
282-
SimpleLogger.logFatal(LoggerType.VALIDATOR, String.format("[ %-" + maxValidatorClassNameLength + "s ] %s", validator.getClass().getSimpleName(), message.getContent()));
288+
String fatalMessage = String.format("[ %-" + maxValidatorClassNameLength + "s ] %s", validator.getClass().getSimpleName(), message.getContent());
289+
FATAL_MESSAGES.add(fatalMessage);
290+
SimpleLogger.logFatal(LoggerType.VALIDATOR, fatalMessage);
283291
}
284292
}
285293
}
@@ -400,6 +408,8 @@ public JSONObject getJSONFileContents(File file) throws H2ZeroParseException {
400408
* @return the number of fatal messages in generation
401409
*/
402410
public int getNumFatal() { return(numFatal); }
411+
412+
public List<String> getFatalMessages() { return(FATAL_MESSAGES); }
403413

404414
/**
405415
* Retrieve a validator by name from the lookup cache (or null if it can not be found)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ public boolean requiresImport(String fieldType) {
410410
public List<BaseField> getPopulateFields() { return(populateFields); }
411411
public List<BaseField> getNonPopulateFields() { return(nonPopulateFields); }
412412

413+
public boolean getShouldHydrate() { return(nonPopulateFields.size() != 0); }
413414
/**
414415
* Return whether this schema object has any questions on it
415416
*

src/main/java/synapticloop/h2zero/model/field/BaseField.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ public String getLengthFormat() {
308308
public int getFieldIndex() { return(this.fieldIndex); }
309309
public void setFieldIndex(int fieldIndex) { this.fieldIndex = fieldIndex; }
310310

311+
public boolean getIsForeignKey() {return(getForeignKeyField() != null); }
312+
311313
public List<String> getFoundIgnoredKeys() { return foundIgnoredKeys; }
312314
public String getReplacementForKey(String key) { return(replacementKeys.get(key)); }
313315

src/main/java/synapticloop/h2zero/plugin/BaseH2ZeroGenerator.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ public BaseH2ZeroGenerator(File h2ZeroFile, File outFile, boolean verbose) {
7272
*/
7373
public void generateH2zero() {
7474
// otherwise we are good to go
75-
H2ZeroParser h2zeroParser = null;
75+
H2ZeroParser h2zeroParser = new H2ZeroParser();
7676
TemplarConfiguration templarConfiguration = null;
7777
TemplarContext templarContext = null;
7878
Database database = null;
7979
Options options = null;
8080

8181
try {
82-
h2zeroParser = new H2ZeroParser(h2ZeroFile);
82+
h2zeroParser.parse(h2ZeroFile);
8383

8484
logDatabaseInfo(h2zeroParser);
8585

@@ -114,6 +114,10 @@ public void generateH2zero() {
114114
SimpleLogger.logFatal(SimpleLogger.LoggerType.PARSE, "H2ZeroParseException: There was an error parsing the '" + h2ZeroFile.getName() + "'.");
115115
SimpleLogger.logFatal(SimpleLogger.LoggerType.PARSE, "The message was:");
116116
SimpleLogger.logFatal(SimpleLogger.LoggerType.PARSE, " " + h2zpex.getMessage());
117+
List<String> fatalMessages = h2zeroParser.getFatalMessages();
118+
for (String fatalMessage : fatalMessages) {
119+
SimpleLogger.logFatal(SimpleLogger.LoggerType.PARSE, " " + fatalMessage);
120+
}
117121
throw new BuildException(h2zpex);
118122
} catch (ParseException pex) {
119123
SimpleLogger.logFatal(SimpleLogger.LoggerType.TEMPLAR_PARSE, "ParseException: There was an error parsing the '" + h2ZeroFile.getName() + "'.");
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package synapticloop.h2zero.validator.field;
2+
3+
/*
4+
* Copyright (c) 2012-2020 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+
import java.util.List;
22+
23+
import synapticloop.h2zero.model.Database;
24+
import synapticloop.h2zero.model.Options;
25+
import synapticloop.h2zero.model.Table;
26+
import synapticloop.h2zero.model.field.BaseField;
27+
import synapticloop.h2zero.validator.BaseValidator;
28+
29+
public class FieldPopulateForeignKeyValidator extends BaseValidator {
30+
31+
@Override
32+
public void validate(Database database, Options options) {
33+
List<Table> tables = database.getTables();
34+
for (Table table : tables) {
35+
List<BaseField> fields = table.getFields();
36+
for (BaseField baseField : fields) {
37+
if(baseField.getIsForeignKey() && !baseField.getPopulate()) {
38+
isValid = false;
39+
addFatalMessage("The foreign key field '" + table.getName() + "." + baseField.getName() + "' has an attribute: \"populate\" with a value of 'false' . It __MUST_ALWAYS__ be 'true'");
40+
}
41+
}
42+
}
43+
}
44+
}

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,14 @@ public class {table.javaClassName} {if !table.isConstant}extends ModelBase {endi
139139
{endloop}";{\n}
140140
{\n}
141141

142-
{\t}private static final String SQL_SELECT_HYDRATE = "select
143-
{loop table.nonPopulateFields as field}
144-
{field.name}
145-
{if !fieldStatus.last}, {endif}
146-
{endloop}
147-
from {table.name} where " + PRIMARY_KEY_FIELD + " = ?";{\n}
142+
{if table.shouldHydrate}
143+
{\t}private static final String SQL_SELECT_HYDRATE = "select
144+
{loop table.nonPopulateFields as field}
145+
{field.name}
146+
{if !fieldStatus.last}, {endif}
147+
{endloop}
148+
from {table.name} where " + PRIMARY_KEY_FIELD + " = ?";{\n}
149+
{endif}{-- end of check whether we should hydrate some of the fields }
148150

149151
{\n}// Static lookups for fields in the hit counter.{\n}
150152
{\t}public static final int HIT_TOTAL = 0;{\n}
@@ -158,7 +160,11 @@ public class {table.javaClassName} {if !table.isConstant}extends ModelBase {endi
158160
{\t}// the number of read-hits for a particular field{\n}
159161
{\t}private static int[] HIT_COUNTS = {{ 0, {loop table.fields as field}0{if fieldStatus.last}{else}, {endif}{endloop} };{\n}
160162
{set fn:length[table.nonPopulateFields] as nonPopulateFieldsLength}
161-
{\t}private boolean isHydrated = {if fn:=[nonPopulateFieldsLength, '0']}true{else}false{endif};{\n}
163+
164+
{if table.shouldHydrate}
165+
{\t}private boolean isHydrated = {if fn:=[nonPopulateFieldsLength, '0']}true{else}false{endif};{\n}
166+
{endif}{-- end of check whether we should hydrate some of the fields }
167+
162168
{endif}{-- end of !if.isConstant}
163169
{\n}
164170
{-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -423,11 +429,11 @@ public class {table.javaClassName} {if !table.isConstant}extends ModelBase {endi
423429
{\t}{\t}} catch (SQLException sqlex) {{{\n}
424430
{\t}{\t}{\t}throw sqlex;{\n}
425431
{\t}{\t}} finally {{{\n}
432+
{\t}{\t}this.isHydrated = true;
426433
{\t}{\t}{\t}ConnectionManager.closeAll(resultSet, preparedStatement);{\n}
427434
{\t}{\t}}{\n}
428-
{\n}
429435
{\t}}{\n}
430-
436+
{\t}
431437
{endif}
432438

433439

src/test/java/synapticloop/sample/h2zero/mysql/finder/UserFinder.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class UserFinder {
3535
private static final String BINDER = Constants.USER_BINDER;
3636

3737
private static final Logger LOGGER = LoggerFactory.getLogger(UserFinder.class);
38-
private static final String SQL_SELECT_START = "select id_user, id_user_type, fl_is_alive, num_age, nm_username, txt_address_email, txt_password, dtm_signup from user";
38+
private static final String SQL_SELECT_START = "select id_user, id_user_type, fl_is_alive, nm_username, txt_address_email, txt_password from user";
3939
private static final String SQL_BUILTIN_FIND_BY_PRIMARY_KEY = SQL_SELECT_START + " where id_user = ?";
4040

4141
private static final String SQL_FIND_BY_NUM_AGE = SQL_SELECT_START + " where num_age = ?";
@@ -1158,11 +1158,11 @@ private static User uniqueResult(ResultSet resultSet) throws H2ZeroFinderExcepti
11581158
Long idUser = ConnectionManager.getNullableResultLong(resultSet, 1);
11591159
Long idUserType = ConnectionManager.getNullableResultLong(resultSet, 2);
11601160
Boolean flIsAlive = ConnectionManager.getNullableResultBoolean(resultSet, 3);
1161-
Integer numAge = ConnectionManager.getNullableResultInt(resultSet, 4);
1162-
String nmUsername = ConnectionManager.getNullableResultString(resultSet, 5);
1163-
String txtAddressEmail = ConnectionManager.getNullableResultString(resultSet, 6);
1164-
String txtPassword = ConnectionManager.getNullableResultString(resultSet, 7);
1165-
Timestamp dtmSignup = ConnectionManager.getNullableResultTimestamp(resultSet, 8);
1161+
String nmUsername = ConnectionManager.getNullableResultString(resultSet, 4);
1162+
String txtAddressEmail = ConnectionManager.getNullableResultString(resultSet, 5);
1163+
String txtPassword = ConnectionManager.getNullableResultString(resultSet, 6);
1164+
Integer numAge = null;
1165+
Timestamp dtmSignup = null;
11661166

11671167
User user = new User(idUser, idUserType, flIsAlive, numAge, nmUsername, txtAddressEmail, txtPassword, dtmSignup);
11681168

@@ -1190,15 +1190,24 @@ private static User uniqueResult(ResultSet resultSet) throws H2ZeroFinderExcepti
11901190
private static List<User> list(ResultSet resultSet) throws SQLException {
11911191
List<User> arrayList = new ArrayList<User>();
11921192
while(resultSet.next()) {
1193-
arrayList.add(new User(
1194-
ConnectionManager.getNullableResultLong(resultSet, 1),
1195-
ConnectionManager.getNullableResultLong(resultSet, 2),
1196-
ConnectionManager.getNullableResultBoolean(resultSet, 3),
1197-
ConnectionManager.getNullableResultInt(resultSet, 4),
1198-
ConnectionManager.getNullableResultString(resultSet, 5),
1199-
ConnectionManager.getNullableResultString(resultSet, 6),
1200-
ConnectionManager.getNullableResultString(resultSet, 7),
1201-
ConnectionManager.getNullableResultTimestamp(resultSet, 8)));
1193+
Long idUser = ConnectionManager.getNullableResultLong(resultSet, 1);
1194+
Long idUserType = ConnectionManager.getNullableResultLong(resultSet, 2);
1195+
Boolean flIsAlive = ConnectionManager.getNullableResultBoolean(resultSet, 3);
1196+
String nmUsername = ConnectionManager.getNullableResultString(resultSet, 4);
1197+
String txtAddressEmail = ConnectionManager.getNullableResultString(resultSet, 5);
1198+
String txtPassword = ConnectionManager.getNullableResultString(resultSet, 6);
1199+
Integer numAge = null;
1200+
Timestamp dtmSignup = null;
1201+
arrayList.add(new User(
1202+
idUser,
1203+
idUserType,
1204+
flIsAlive,
1205+
numAge,
1206+
nmUsername,
1207+
txtAddressEmail,
1208+
txtPassword,
1209+
dtmSignup
1210+
));
12021211
}
12031212
return(arrayList);
12041213
}

0 commit comments

Comments
 (0)