Revision: 5936
http://squirrel-sql.svn.sourceforge.net/squirrel-sql/?rev=5936&view=rev
Author: manningr
Date: 2010-10-30 17:53:10 +0000 (Sat, 30 Oct 2010)
Log Message:
-----------
New utility method to detect if the jdbc type is "standard" (that is, if it is defined in java.sql.Types which is JVM-dependent).
Modified Paths:
--------------
trunk/sql12/fw/src/main/java/net/sourceforge/squirrel_sql/fw/sql/JDBCTypeMapper.java
trunk/sql12/fw/src/test/java/net/sourceforge/squirrel_sql/fw/sql/JDBCTypeMapperTest.java
Modified: trunk/sql12/fw/src/main/java/net/sourceforge/squirrel_sql/fw/sql/JDBCTypeMapper.java
===================================================================
--- trunk/sql12/fw/src/main/java/net/sourceforge/squirrel_sql/fw/sql/JDBCTypeMapper.java 2010-10-30 17:51:41 UTC (rev 5935)
+++ trunk/sql12/fw/src/main/java/net/sourceforge/squirrel_sql/fw/sql/JDBCTypeMapper.java 2010-10-30 17:53:10 UTC (rev 5936)
@@ -33,7 +33,7 @@
/** logger for this class */
private static ILogger s_log = LoggerController.createLogger(JDBCTypeMapper.class);
-
+
/**
* Returns a list of Type names that are found in the java.sql.Types class using reflection.
*
@@ -41,12 +41,13 @@
*/
public static String[] getJdbcTypeList()
{
- ArrayList<String> result = new ArrayList<String>();
+ ArrayList<String> result = new ArrayList<String>();
Field[] fields = java.sql.Types.class.getDeclaredFields();
- for (int i = 0; i < fields.length; i++) {
- Field field = fields[i];
- result.add(field.getName());
- }
+ for (int i = 0; i < fields.length; i++)
+ {
+ Field field = fields[i];
+ result.add(field.getName());
+ }
return result.toArray(new String[result.size()]);
}
@@ -68,63 +69,61 @@
}
catch (SecurityException e)
{
- s_log.error("getJdbcTypeName: unexpected exception: "+e.getMessage(), e);
+ s_log.error("getJdbcTypeName: unexpected exception: " + e.getMessage(), e);
}
catch (IllegalArgumentException e)
{
- s_log.error("getJdbcTypeName: unexpected exception: "+e.getMessage(), e);
+ s_log.error("getJdbcTypeName: unexpected exception: " + e.getMessage(), e);
}
catch (IllegalAccessException e)
{
- s_log.error("getJdbcTypeName: unexpected exception: "+e.getMessage(), e);
+ s_log.error("getJdbcTypeName: unexpected exception: " + e.getMessage(), e);
}
return result;
}
-
+
/**
- * Returns the java.sql.Types.java type of the specified type name. If the type is not found then
+ * Returns the java.sql.Types.java type of the specified type name. If the type is not found then
* defaultVal is returned.
*
- * @param jdbcTypeName the name to lookup.
- *
+ * @param jdbcTypeName
+ * the name to lookup.
* @return the type code
*/
- public static int getJdbcType(String jdbcTypeName, int defaultVal) {
-
- if (jdbcTypeName == null)
- {
- return Types.NULL;
- }
+ public static int getJdbcType(String jdbcTypeName, int defaultVal)
+ {
+
+ if (jdbcTypeName == null) { return Types.NULL; }
int result = defaultVal;
-
+
try
{
Field[] fields = java.sql.Types.class.getDeclaredFields();
for (int i = 0; i < fields.length; i++)
{
Field field = fields[i];
- if (field.getName().equalsIgnoreCase(jdbcTypeName)) {
+ if (field.getName().equalsIgnoreCase(jdbcTypeName))
+ {
result = field.getInt(null);
}
}
}
catch (IllegalArgumentException e)
{
- s_log.error("getJdbcTypeName: unexpected exception: "+e.getMessage(), e);
+ s_log.error("getJdbcTypeName: unexpected exception: " + e.getMessage(), e);
}
catch (IllegalAccessException e)
{
- s_log.error("getJdbcTypeName: unexpected exception: "+e.getMessage(), e);
+ s_log.error("getJdbcTypeName: unexpected exception: " + e.getMessage(), e);
}
return result;
}
-
-
+
/**
- * Returns a type code for the specified type name. If not found then this will return Types.NULL.
+ * Returns a type code for the specified type name. If not found then this will return Types.NULL.
*
- * @param jdbcTypeName the name to lookup.
- *
+ * @param jdbcTypeName
+ * the name to lookup.
* @return the type code
*/
public static int getJdbcType(String jdbcTypeName)
@@ -215,4 +214,36 @@
throw new IllegalArgumentException("Unknown index sort order: " + sortOrder);
}
+
+ /**
+ * Returns a boolean indicating whether or not the specified typeCode is a field in the java.sql.Types
+ * class as implemented in the current JVM.
+ *
+ * @param typeCode
+ * the Java sql type code as reported by the JDBC driver.
+ * @return true if the specified typeCode is standard; false otherwise.
+ */
+ public static boolean isStandardType(int typeCode)
+ {
+ boolean result = false;
+ Field[] fields = Types.class.getDeclaredFields();
+ for (Field field : fields)
+ {
+ String fieldName = field.getName();
+ try
+ {
+ int fieldValue = field.getInt(null);
+ if (fieldValue == typeCode)
+ {
+ result = true;
+ }
+ }
+ catch (Exception e)
+ {
+ s_log.error("isStandardType: unable to get value for java.sql.Types." + fieldName + " : "
+ + e.getMessage(), e);
+ }
+ }
+ return result;
+ }
}
Modified: trunk/sql12/fw/src/test/java/net/sourceforge/squirrel_sql/fw/sql/JDBCTypeMapperTest.java
===================================================================
--- trunk/sql12/fw/src/test/java/net/sourceforge/squirrel_sql/fw/sql/JDBCTypeMapperTest.java 2010-10-30 17:51:41 UTC (rev 5935)
+++ trunk/sql12/fw/src/test/java/net/sourceforge/squirrel_sql/fw/sql/JDBCTypeMapperTest.java 2010-10-30 17:53:10 UTC (rev 5936)
@@ -140,6 +140,10 @@
}
}
+ public void testIsStandardType() throws Exception {
+ assertTrue(JDBCTypeMapper.isStandardType(Types.VARCHAR));
+ }
+
private interface TypeCheck {
boolean isType(int type);
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|