postgresql-jdbc.git / summary / log / commit / refs
commit 13ec63a117e038809a80e2935ac4d9d68fdfdbbb
Author: Kris Jurka <books@ejurka.com>
Date: Tue Apr 15 04:49:59 2008 +0000
DatabaseMetaData.getTypeInfo provides two columns for indicating the
prefix and suffix that should be wrapped around a literal when
putting it into a query. Return the correct quoting (') for all
non-numeric types.
Based on a patch by Mats Ekelund.
org/postgresql/core/TypeInfo.java | 4 +++-
.../jdbc2/AbstractJdbc2DatabaseMetaData.java | 10 +++++++++-
org/postgresql/jdbc2/TypeInfoCache.java | 20 +++++++++++++++++++-
org/postgresql/test/jdbc2/DatabaseMetaDataTest.java | 17 ++++++++++++++++-
4 files changed, 47 insertions(+), 4 deletions(-)
diff --git a/org/postgresql/core/TypeInfo.java b/org/postgresql/core/TypeInfo.java
index f5319417..fe4fff39 100644
--- a/org/postgresql/core/TypeInfo.java
+++ b/org/postgresql/core/TypeInfo.java
@@ -3,7 +3,7 @@
* Copyright (c) 2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
-* $PostgreSQL$
+* $PostgreSQL: pgjdbc/org/postgresql/core/TypeInfo.java,v 1.1 2008/04/15 04:23:54 jurka Exp $
*
*-------------------------------------------------------------------------
*/
@@ -89,4 +89,6 @@ public interface TypeInfo
public int getMaximumPrecision(int oid);
+ public boolean requiresQuoting(int oid) throws SQLException;
+
}
diff --git a/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java b/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java
index a7e6da49..b4e0d5f3 100644
--- a/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java
+++ b/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java
@@ -3,7 +3,7 @@
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
-* $PostgreSQL: pgjdbc/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java,v 1.44 2008/01/08 06:56:28 jurka Exp $
+* $PostgreSQL: pgjdbc/org/postgresql/jdbc2/AbstractJdbc2DatabaseMetaData.java,v 1.45 2008/04/15 04:23:57 jurka Exp $
*
*-------------------------------------------------------------------------
*/
@@ -3670,6 +3670,7 @@ public abstract class AbstractJdbc2DatabaseMetaData
byte b10[] = connection.encodeString("10");
byte bf[] = connection.encodeString("f");
byte bt[] = connection.encodeString("t");
+ byte bliteral[] = connection.encodeString("'");
byte bNullable[] = connection.encodeString(Integer.toString(java.sql.DatabaseMetaData.typeNullable));
byte bSearchable[] = connection.encodeString(Integer.toString(java.sql.DatabaseMetaData.typeSearchable));
@@ -3682,6 +3683,13 @@ public abstract class AbstractJdbc2DatabaseMetaData
tuple[0] = connection.encodeString(typname);
tuple[1] = connection.encodeString(Integer.toString(connection.getTypeInfo().getSQLType(typname)));
tuple[2] = connection.encodeString(Integer.toString(connection.getTypeInfo().getMaximumPrecision(typeOid)));
+
+ if (connection.getTypeInfo().requiresQuoting(typeOid))
+ {
+ tuple[3] = bliteral;
+ tuple[4] = bliteral;
+ }
+
tuple[6] = bNullable; // all types can be null
tuple[7] = connection.getTypeInfo().isCaseSensitive(typeOid) ? bt : bf;
tuple[8] = bSearchable; // any thing can be used in the WHERE clause
diff --git a/org/postgresql/jdbc2/TypeInfoCache.java b/org/postgresql/jdbc2/TypeInfoCache.java
index bf15c255..b16e4b44 100644
--- a/org/postgresql/jdbc2/TypeInfoCache.java
+++ b/org/postgresql/jdbc2/TypeInfoCache.java
@@ -3,7 +3,7 @@
* Copyright (c) 2005-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * $PostgreSQL: pgjdbc/org/postgresql/jdbc2/TypeInfoCache.java,v 1.12 2008/01/08 06:56:29 jurka Exp $
+ * $PostgreSQL: pgjdbc/org/postgresql/jdbc2/TypeInfoCache.java,v 1.13 2008/04/15 04:23:57 jurka Exp $
*
*-------------------------------------------------------------------------
*/
@@ -571,4 +571,22 @@ public class TypeInfoCache implements TypeInfo {
}
}
+ public boolean requiresQuoting(int oid) throws SQLException
+ {
+ int sqlType = getSQLType(oid);
+ switch(sqlType) {
+ case Types.BIGINT:
+ case Types.DOUBLE:
+ case Types.FLOAT:
+ case Types.INTEGER:
+ case Types.REAL:
+ case Types.SMALLINT:
+ case Types.TINYINT:
+ case Types.NUMERIC:
+ case Types.DECIMAL:
+ return false;
+ }
+ return true;
+ }
+
}
diff --git a/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java b/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java
index 636007fa..1dae7aac 100644
--- a/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java
+++ b/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java
@@ -3,7 +3,7 @@
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
-* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java,v 1.40 2008/01/08 06:56:31 jurka Exp $
+* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java,v 1.41 2008/01/20 15:13:29 jurka Exp $
*
*-------------------------------------------------------------------------
*/
@@ -906,4 +906,19 @@ public class DatabaseMetaDataTest extends TestCase
}
}
}
+
+ public void testTypeInfoQuoting() throws SQLException
+ {
+ DatabaseMetaData dbmd = con.getMetaData();
+ ResultSet rs = dbmd.getTypeInfo();
+ while (rs.next()) {
+ if ("int4".equals(rs.getString("TYPE_NAME"))) {
+ assertNull(rs.getString("LITERAL_PREFIX"));
+ } else if ("text".equals(rs.getString("TYPE_NAME"))) {
+ assertEquals("'", rs.getString("LITERAL_PREFIX"));
+ assertEquals("'", rs.getString("LITERAL_SUFFIX"));
+ }
+ }
+ }
+
}
[parent: 36024de52b2f]