Skip to content

Commit 70dcd1d

Browse files
committed
- Fixed bug in the Model class. The sql in the init method was missing the id so the query to find objects in a hasMany relationship was failing
- Updated the static init methods used to initialize models to throw a SQLException - Minor update to the Console.log() method. Double values are now formatted and rounded to the the 8th decimal place git-svn-id: svn://192.168.0.80/JavaXT/javaxt-core@1214 2c7b0aa6-e0b2-3c4e-bb4a-8b65b6c465ff
1 parent ec43d44 commit 70dcd1d

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/javaxt/sql/Model.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,14 @@ protected final void init(long id, String...fieldNames) throws SQLException {
137137

138138

139139
StringBuilder sql = new StringBuilder("select ");
140+
boolean addID = true;
140141
for (int i=0; i<fieldNames.length; i++){
141142
if (i>0) sql.append(", ");
142143
String fieldName = fieldNames[i];
144+
if (fieldName.equalsIgnoreCase("id")) addID = false;
143145
sql.append(escape(fieldName));
144146
}
147+
if (addID) sql.append(", id");
145148
sql.append(" from ");
146149
sql.append(tableName);
147150
sql.append(" where id=");
@@ -743,8 +746,8 @@ protected static Connection getConnection(Class c) throws SQLException {
743746
//**************************************************************************
744747
//** init
745748
//**************************************************************************
746-
/** Used to associate a model with a database connection pool. This allows
747-
* queries and other database metadata to be cached.
749+
/** Used to initialize a Model and associate it with a database connection
750+
* pool. This allows queries and other database metadata to be cached.
748751
<pre>
749752
for (Jar.Entry entry : jar.getEntries()){
750753
String name = entry.getName();
@@ -758,7 +761,7 @@ protected static Connection getConnection(Class c) throws SQLException {
758761
}
759762
</pre>
760763
*/
761-
public static void init(Class c, ConnectionPool connectionPool){
764+
public static void init(Class c, ConnectionPool connectionPool) throws SQLException {
762765

763766
String className = c.getName();
764767

@@ -796,19 +799,21 @@ public static void init(Class c, ConnectionPool connectionPool){
796799
}
797800
catch(Exception e){
798801
if (conn!=null) conn.close();
799-
e.printStackTrace();
802+
SQLException ex = new SQLException("Failed to initialize Model: " + className);
803+
ex.setStackTrace(e.getStackTrace());
804+
throw ex;
800805
}
801806
}
802807

803808

804809
//**************************************************************************
805810
//** init
806811
//**************************************************************************
807-
/** Used to associate all the models found in a JAR file with a database
808-
* connection pool. This allows queries and other database metadata to be
809-
* cached.
812+
/** Used to initialize all the Models found in a jar file and associate them
813+
* with a database connection pool. This allows queries and other database
814+
* metadata to be cached.
810815
*/
811-
public static void init(javaxt.io.Jar jar, ConnectionPool connectionPool){
816+
public static void init(javaxt.io.Jar jar, ConnectionPool connectionPool) throws SQLException {
812817
for (Class c : jar.getClasses()){
813818
if (javaxt.sql.Model.class.isAssignableFrom(c)){
814819
init(c, connectionPool);

src/javaxt/utils/Console.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import java.io.BufferedReader;
33
import java.io.IOException;
44
import java.io.InputStreamReader;
5+
import java.text.DecimalFormat;
56

67
//******************************************************************************
78
//** Console
@@ -14,7 +15,8 @@
1415
public class Console {
1516

1617
private String me = this.getClass().getName();
17-
private String indent = " "; // 25 spaces
18+
private static final String indent = " "; // 25 spaces
19+
private static final DecimalFormat df = new DecimalFormat("#.##");
1820

1921

2022
//**************************************************************************
@@ -33,7 +35,15 @@ public void log(Object obj){
3335
source += " ";
3436
}
3537
String str = null;
36-
if (obj!=null) str = obj.toString();
38+
if (obj!=null){
39+
if (obj instanceof Double){
40+
df.setMaximumFractionDigits(8);
41+
str = df.format((Double) obj);
42+
}
43+
else{
44+
str = obj.toString();
45+
}
46+
}
3747
System.out.println(source + str);
3848
}
3949

0 commit comments

Comments
 (0)