Changeset: 03e42371b6fa for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=03e42371b6fa
Modified Files:
        java/src/main/java/nl/cwi/monetdb/jdbc/MonetPreparedStatement.java
        java/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java
Branch: Jun2016
Log Message:

Improved ResultSetMetaData methods: isAutoIncrement(), isSigned(), 
getSchemaName() and getTableName().
Removed throwing AssertionError() in methods getSchemaName() and getTableName().
Instead return "" as defined in the JDBC spec.


diffs (241 lines):

diff --git a/java/src/main/java/nl/cwi/monetdb/jdbc/MonetPreparedStatement.java 
b/java/src/main/java/nl/cwi/monetdb/jdbc/MonetPreparedStatement.java
--- a/java/src/main/java/nl/cwi/monetdb/jdbc/MonetPreparedStatement.java
+++ b/java/src/main/java/nl/cwi/monetdb/jdbc/MonetPreparedStatement.java
@@ -294,8 +294,8 @@ public class MonetPreparedStatement
        }
 
        /**
-        * Returns the index in the backing arrays for the given
-        * resultset column number
+        * Returns the index (0..size-1) in the backing arrays for the given
+        * resultset column number or an SQLException when not found
         */
        private int getColumnIdx(int colnr) throws SQLException {
                int curcol = 0;
@@ -309,8 +309,8 @@ public class MonetPreparedStatement
                throw new SQLException("No such column with index: " + colnr, 
"M1M05");
        }
        /**
-        * Returns the index in the backing arrays for the given
-        * parameter number
+        * Returns the index (0..size-1) in the backing arrays for the given
+        * parameter number or an SQLException when not found
         */
        private int getParamIdx(int paramnr) throws SQLException {
                int curparam = 0;
@@ -367,29 +367,25 @@ public class MonetPreparedStatement
                        }
 
                        /**
-                        * Indicates whether the designated column is 
automatically
-                        * numbered, thus read-only.
-                        * 
+                        * Indicates whether the designated column is 
automatically numbered.
+                        *
                         * @param column the first column is 1, the second is 
2, ...
                         * @return true if so; false otherwise
                         * @throws SQLException if a database access error 
occurs
                         */
                        @Override
                        public boolean isAutoIncrement(int column) throws 
SQLException {
-                               // the only column I know of is a 'secret' 
column called rowid
-                               // with datatype oid
-                               // avoid nullpointer exception here
-                               if 
("oid".equals(monetdbType[getColumnIdx(column)])) {
-                                       return true;
-                               } else {
-                                       return false;
-                               }
+                               /* TODO: in MonetDB only numeric (int, decimal) 
columns could be autoincrement/serial
+                                * This however requires an expensive 
dbmd.getColumns(null, schema, table, column)
+                                * query call to pull the IS_AUTOINCREMENT 
value for this column.
+                                * See also ResultSetMetaData.isAutoIncrement()
+                                */
+                               // For now we simply allways return false.
+                               return false;
                        }
 
                        /**
-                        * Indicates whether a column's case matters. This 
holds for all
-                        * columns in MonetDB resultsets since the mapping is 
done case
-                        * insensitive, therefore this method will always 
return false.
+                        * Indicates whether a column's case matters.
                         *
                         * @param column the first column is 1, the second is 
2, ...
                         * @returns false
@@ -410,11 +406,8 @@ public class MonetPreparedStatement
                        /**
                         * Indicates whether the designated column can be used 
in a
                         * where clause.
-                        * It is unknown to me what kind ot columns they regard 
to,
-                        * as I think all columns are useable in a where clause.
-                        * Returning true for all here, for the time being.
-                        * Possible thought; maybe they want to know here if 
it's a
-                        * real column existing in a table or not...
+                        *
+                        * Returning true for all here, even for CLOB, BLOB.
                         *
                         * @param column the first column is 1, the second is 
2, ...
                         * @returns true
@@ -442,13 +435,19 @@ public class MonetPreparedStatement
                        /**
                         * Indicates whether values in the designated column 
are signed
                         * numbers.
-                        * Within MonetDB all numeric types are signed.
+                        * Within MonetDB all numeric types (except oid and 
ptr) are signed.
                         *
                         * @param column the first column is 1, the second is 
2, ...
                         * @return true if so; false otherwise
                         */
                        @Override
                        public boolean isSigned(int column) throws SQLException 
{
+                               String monettype = getColumnTypeName(column);
+                               if (monettype != null) {
+                                       if ("oid".equals(monettype)
+                                        || "ptr".equals(monettype))
+                                               return false;
+                               }
                                // we can hardcode this, based on the colum type
                                switch (javaType[getColumnIdx(column)]) {
                                        case Types.NUMERIC:
diff --git a/java/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java 
b/java/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java
--- a/java/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java
+++ b/java/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java
@@ -1146,13 +1146,14 @@ public class MonetResultSet extends Mone
                        private int[] _precision        = new 
int[columns.length +1];
                        private int[] _scale            = new 
int[columns.length +1];
                        private int[] _isNullable       = new 
int[columns.length +1];
+                       private boolean[] _isAutoincrement = new 
boolean[columns.length +1];
                        private Connection conn = null;
                        private DatabaseMetaData dbmd = null;
 
                        /**
-                        * A private method to fetch the precision, scale and 
isNuallble value for a fully qualified column.
+                        * A private method to fetch the precision, scale, 
isNullable and isAutoincrement value for a fully qualified column.
                         * As md.getColumns() is an expensive method we call it 
only once per column
-                        * and cache the precision, scale and isNullable values 
in the above array chaches.
+                        * and cache the precision, scale, isNullable and 
isAutoincrement values in the above array chaches.
                         * Also we only call md.getColumns() when we have a non 
empty schema name and table name and column name.
                         */
                        private void fetchColumnInfo(int column) throws 
SQLException
@@ -1164,6 +1165,7 @@ public class MonetResultSet extends Mone
                                _precision[column] = 0;
                                _scale[column] = 0;
                                _isNullable[column] = columnNullableUnknown;
+                               _isAutoincrement[column] = false;
 
                                // we can only call dbmd.getColumns() when we 
have a specific schema name and table name and column name
                                String schName = getSchemaName(column);
@@ -1184,7 +1186,7 @@ public class MonetResultSet extends Mone
                                                                dbmd = 
conn.getMetaData();
                                                        }
                                                        if (dbmd != null) {
-                                                               // for 
precision, scale and isNullable we query the information from data dictionary
+                                                               // for 
precision, scale, isNullable and isAutoincrement we query the information from 
data dictionary
                                                                ResultSet 
colInfo = dbmd.getColumns(null, schName, tblName, colName);
                                                                if (colInfo != 
null) {
                                                                        // we 
expect exactly one row in the resultset
@@ -1192,6 +1194,9 @@ public class MonetResultSet extends Mone
                                                                                
_precision[column] = colInfo.getInt(7);  // col 7 is "COLUMN_SIZE"
                                                                                
_scale[column] = colInfo.getInt(9);  // col 9 is "DECIMAL_DIGITS"
                                                                                
_isNullable[column] = colInfo.getInt(11);  // col 11 is "NULLABLE"
+                                                                               
String strVal = colInfo.getString(23);  // col 23 is "IS_AUTOINCREMENT"
+                                                                               
if (strVal != null && "YES".equals(strVal))
+                                                                               
        _isAutoincrement[column] = true;
                                                                        }
                                                                        
colInfo.close();  // close the resultset to release resources
                                                                }
@@ -1220,14 +1225,10 @@ public class MonetResultSet extends Mone
                         */
                        @Override
                        public boolean isAutoIncrement(int column) throws 
SQLException {
-                               // the only column I know of is a 'secret' 
column called rowid
-                               // with datatype oid
-                               // avoid nullpointer exception here
-                               if ("oid".equals(getColumnTypeName(column))) {
-                                       return true;
-                               } else {
-                                       return false;
+                               if (_is_fetched[column] != true) {
+                                       fetchColumnInfo(column);
                                }
+                               return _isAutoincrement[column];
                        }
 
                        /**
@@ -1284,13 +1285,19 @@ public class MonetResultSet extends Mone
                        /**
                         * Indicates whether values in the designated column 
are signed
                         * numbers.
-                        * Within MonetDB all numeric types are signed.
+                        * Within MonetDB all numeric types (except oid and 
ptr) are signed.
                         *
                         * @param column the first column is 1, the second is 
2, ...
                         * @return true if so; false otherwise
                         */
                        @Override
                        public boolean isSigned(int column) throws SQLException 
{
+                               String monettype = getColumnTypeName(column);
+                               if (monettype != null) {
+                                       if ("oid".equals(monettype)
+                                        || "ptr".equals(monettype))
+                                               return false;
+                               }
                                // we can hardcode this, based on the colum type
                                switch (getColumnType(column)) {
                                        case Types.NUMERIC:
@@ -1339,7 +1346,7 @@ public class MonetResultSet extends Mone
                        }
 
                        /**
-                        * Get the designated column's table's schema.
+                        * Get the designated column's schema name.
                         *
                         * @param column the first column is 1, the second is 
2, ...
                         * @return schema name or "" if not applicable
@@ -1355,17 +1362,15 @@ public class MonetResultSet extends Mone
                                // figure the name out
                                try {
                                        schema = header.getTableNames()[column 
- 1];
+                                       if (schema != null) {
+                                               int dot = schema.indexOf(".");
+                                               return (dot >= 0) ? 
schema.substring(0, dot) : "";
+                                       }
                                } catch (IndexOutOfBoundsException e) {
                                        throw new SQLException("No such column 
" + column, "M1M05");
                                }
 
-                               if (schema == null) throw
-                                       new AssertionError("table_name header 
is empty!");
-                               int dot = schema.indexOf(".");
-                               if (dot == -1) throw
-                                       new AssertionError("table_name is not 
fully qualified! (" + schema + ")");
-
-                               return schema.substring(0, dot);
+                               return "";
                        }
 
                        /**
@@ -1384,17 +1389,15 @@ public class MonetResultSet extends Mone
                                // figure the name out
                                try {
                                        table = header.getTableNames()[column - 
1];
+                                       if (table != null) {
+                                               int dot = table.indexOf(".");
+                                               return (dot >= 0) ? 
table.substring(dot + 1) : table;
+                                       }
                                } catch (IndexOutOfBoundsException e) {
                                        throw new SQLException("No such column 
" + column, "M1M05");
                                }
 
-                               if (table == null) throw
-                                       new AssertionError("table_name header 
is empty!");
-                               int dot = table.indexOf(".");
-                               if (dot == -1) throw
-                                       new AssertionError("table_name is not 
fully qualified! (" + table + ")");
-
-                               return table.substring(dot + 1);
+                               return "";
                        }
 
                        /**
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to