Hello Empire Devs,
I have been using empire with some success lately and will share my findings
as soon as I get past my deadline. Still much to do!
In the meantime, I have a patch to offer for consideration. The patch is
against the 2.1.0-incubating release version, but should work against the
current code base too.
The problem is that the codegen parser does not set the decimal digits for
the size of DECIMAL columns. This leads to problems if you use empire-db to
generate your schema. You expect DECIMAL(10,2) but get DECIMAL(10,0).
The patch includes a test case to show the problem, as well as a proposed
solution.
Thanks for your consideration.
Matt DeHoust
P.S. While I'm on the subject, modelling the column size as a double has
inherent limitations. For example, how do you represent a DECIMAL value with
20 decimal digits? Are there any plans to address this going forward?
P.P.S. I also have a use case where it would be helpful for DBViewColumn to
include column size information. Column size it is not available in the
current DBViewColumn model. It's not urgent, but thought I'd mention it in
case someone wants to take a look.
### Eclipse Workspace Patch 1.0
#P empire-db-codegen
Index: src/test/java/org/apache/empire/db/codegen/CodeGenParserTest.java
===================================================================
--- src/test/java/org/apache/empire/db/codegen/CodeGenParserTest.java
(revision 0)
+++ src/test/java/org/apache/empire/db/codegen/CodeGenParserTest.java
(revision 0)
@@ -0,0 +1,39 @@
+package org.apache.empire.db.codegen;
+
+import static org.apache.empire.data.DataType.DECIMAL;
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+import org.apache.empire.db.DBColumn;
+import org.apache.empire.db.DBDatabase;
+import org.apache.empire.db.DBTable;
+import org.junit.Before;
+import org.junit.Test;
+
+
+public class CodeGenParserTest {
+ private CodeGenParser parser;
+ private DBDatabase db;
+
+ @Before
+ public void loadDbModel() {
+ final CodeGenConfig config = new CodeGenConfig();
+ config.setJdbcClass("org.hsqldb.jdbcDriver");
+
config.setJdbcURL("jdbc:hsqldb:file:src/test/resources/hsqldb/sample;shutdown=true");
+ config.setJdbcUser("sa");
+ config.setJdbcPwd("");
+ config.setDbSchema("PUBLIC");
+
+ parser = new CodeGenParser(config);
+ db = parser.loadDbModel();
+ }
+
+ @Test
+ public void shouldSizeDecimalColumn() {
+ final DBTable employees = db.getTable("EMPLOYEES");
+ final DBColumn salary = employees.getColumn("SALARY");
+
+ assertThat(salary.getDataType(), is(DECIMAL));
+ assertThat(salary.getSize(), is(10.2));
+ }
+}
Index: src/main/java/org/apache/empire/db/codegen/CodeGenParser.java
===================================================================
--- src/main/java/org/apache/empire/db/codegen/CodeGenParser.java
(revision 1164522)
+++ src/main/java/org/apache/empire/db/codegen/CodeGenParser.java
(working copy)
@@ -264,6 +264,7 @@
String name = rs.getString("COLUMN_NAME");
DataType empireType = getEmpireDataType(rs.getInt("DATA_TYPE"));
int colSize = rs.getInt("COLUMN_SIZE");
+ int decimalDigits = rs.getInt("DECIMAL_DIGITS");
boolean required = false;
String defaultValue = rs.getString("COLUMN_DEF");
if (rs.getString("IS_NULLABLE").equalsIgnoreCase("NO"))
@@ -294,13 +295,14 @@
}
}
-
+
// Move from the return statement below so we can add
// some AUTOINC meta data to the column to be used by
// the ParserUtil and ultimately the template.
log.info("\tCOLUMN:\t" + name + " ("+empireType+")");
- DBTableColumn col = t.addColumn(name, empireType, colSize,
required, defaultValue);
-
+ final double size = colSize + (decimalDigits / 10.0);
+ DBTableColumn col = t.addColumn(name, empireType, size,
required, defaultValue);
+
// We still need to know the base data type for this AUTOINC
// because the Record g/setters need to know this, right?
// So, let's add it as meta data every time the column is
AUTOINC