tomdz 2004/12/07 10:15:28
Modified: sql/src/java/org/apache/commons/sql/model Column.java
sql/src/java/org/apache/commons/sql/io JdbcModelReader.java
Log:
Fixed NPE that occurs when using JdbcModelReader with numeric column types in
Hsqldb
Revision Changes Path
1.14 +12 -9
jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Column.java
Index: Column.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Column.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- Column.java 19 Jul 2004 22:14:00 -0000 1.13
+++ Column.java 7 Dec 2004 18:15:28 -0000 1.14
@@ -201,16 +201,19 @@
public void setSize(String size)
{
- int pos = size.indexOf(",");
-
- if (pos < 0)
+ if (size != null)
{
- this.size = size;
- }
- else
- {
- this.size = size.substring(0, pos);
- scale = Integer.parseInt(size.substring(pos + 1));
+ int pos = size.indexOf(",");
+
+ if (pos < 0)
+ {
+ this.size = size;
+ }
+ else
+ {
+ this.size = size.substring(0, pos);
+ scale = Integer.parseInt(size.substring(pos + 1));
+ }
}
}
1.10 +35 -13
jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/io/JdbcModelReader.java
Index: JdbcModelReader.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/io/JdbcModelReader.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- JdbcModelReader.java 10 Nov 2004 15:21:53 -0000 1.9
+++ JdbcModelReader.java 7 Dec 2004 18:15:28 -0000 1.10
@@ -21,6 +21,7 @@
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
+import java.sql.Types;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@@ -48,19 +49,38 @@
public class JdbcModelReader {
/** The Log to which logging calls will be made. */
- private static final Log log = LogFactory.getLog( JdbcModelReader.class
);
+ private final Log log = LogFactory.getLog( JdbcModelReader.class );
- Connection connection = null;
- String catalog = null;
- String schema = null;
- String[] tableTypes = { "TABLE", "VIEW" };
- Pattern defaultPattern = Pattern.compile("\\(\\'?(.*?)\\'?\\)"); //value
with parenthesis and/or quotes around it
-
- public JdbcModelReader() {
+ /** Contains default column sizes (minimum sizes that a JDBC-compliant
db must support) */
+ private HashMap defaultSizes = new HashMap();
+
+ private Connection connection = null;
+ private String catalog = null;
+ private String schema = null;
+ private String[] tableTypes = { "TABLE", "VIEW" };
+ private Pattern defaultPattern = Pattern.compile("\\(\\'?(.*?)\\'?\\)");
//value with parenthesis and/or quotes around it
+
+ public JdbcModelReader()
+ {
+ this(null);
}
- public JdbcModelReader(Connection conn) {
- this.connection = conn;
+ public JdbcModelReader(Connection conn)
+ {
+ connection = conn;
+ defaultSizes.put(new Integer(Types.CHAR), "254");
+ defaultSizes.put(new Integer(Types.VARCHAR), "254");
+ defaultSizes.put(new Integer(Types.LONGVARCHAR), "254");
+ defaultSizes.put(new Integer(Types.BINARY), "254");
+ defaultSizes.put(new Integer(Types.VARBINARY), "254");
+ defaultSizes.put(new Integer(Types.LONGVARBINARY), "254");
+ defaultSizes.put(new Integer(Types.INTEGER), "32");
+ defaultSizes.put(new Integer(Types.BIGINT), "64");
+ defaultSizes.put(new Integer(Types.REAL), "7,0");
+ defaultSizes.put(new Integer(Types.FLOAT), "15,0");
+ defaultSizes.put(new Integer(Types.DOUBLE), "15,0");
+ defaultSizes.put(new Integer(Types.DECIMAL), "15,15");
+ defaultSizes.put(new Integer(Types.NUMERIC), "15,15");
}
public void setCatalog(String catalog) {
@@ -243,7 +263,7 @@
String columnSize =
columnInfoColumns.contains("COLUMN_SIZE")
? columnData.getString("COLUMN_SIZE")
- : null;
+ : (String)defaultSizes.get(new Integer(columnType));
/* the number of fractional digits */
int columnScale =
columnInfoColumns.contains("DECIMAL_DIGITS")
@@ -338,6 +358,10 @@
Column col = new Column();
col.setName(columnName);
col.setTypeCode(columnType);
+ col.setPrecisionRadix(columnPrecision);
+ col.setScale(columnScale);
+ // we're setting the size after the precision and radix in
case
+ // the database prefers to return them in the size value
col.setSize(columnSize);
col.setRequired(!columnIsNullable);
if (primaryKeys.contains(col.getName())) {
@@ -355,8 +379,6 @@
}
col.setDefaultValue(columnDefaultValue);
}
- col.setPrecisionRadix(columnPrecision);
- col.setScale(columnScale);
columns.add(col);
}
return columns;
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]