svn commit: r1139918 - /ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/DatabaseUtil.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1139918 - /ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/DatabaseUtil.java

doogie-3
Author: doogie
Date: Sun Jun 26 19:15:34 2011
New Revision: 1139918

URL: http://svn.apache.org/viewvc?rev=1139918&view=rev
Log:
OPTIMIZE: Switch the database feature detection code to use a loop and a registry
of detectable features.  As a side effect, the output strings are now
aligned.

Modified:
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/DatabaseUtil.java

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/DatabaseUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/DatabaseUtil.java?rev=1139918&r1=1139917&r2=1139918&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/DatabaseUtil.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/DatabaseUtil.java Sun Jun 26 19:15:34 2011
@@ -19,6 +19,7 @@
 package org.ofbiz.entity.jdbc;
 
 import java.io.Serializable;
+import java.lang.reflect.Method;
 import java.sql.Connection;
 import java.sql.DatabaseMetaData;
 import java.sql.ResultSet;
@@ -847,6 +848,80 @@ public class DatabaseUtil {
         return dbData;
     }
 
+    private static final List<Detection> detections = new ArrayList<Detection>();
+    private static final String goodFormatStr;
+    private static final String badFormatStr;
+
+    private static class Detection {
+        protected final String name;
+        protected final boolean required;
+        protected final Method method;
+        protected final Object[] params;
+
+        protected Detection(String name, boolean required, String methodName, Object... params) throws NoSuchMethodException {
+            this.name = name;
+            this.required = required;
+            Class[] paramTypes = new Class[params.length];
+            for (int i = 0; i < params.length; i++) {
+                Class<?> paramType = params[i].getClass();
+                if (paramType == Integer.class) {
+                    paramType = Integer.TYPE;
+                }
+                paramTypes[i] = paramType;
+            }
+            method = DatabaseMetaData.class.getMethod(methodName, paramTypes);
+            this.params = params;
+        }
+    }
+
+    static {
+        try {
+            detections.add(new Detection("supports transactions", true, "supportsTransactions"));
+            detections.add(new Detection("isolation None", false, "supportsTransactionIsolationLevel", Connection.TRANSACTION_NONE));
+            detections.add(new Detection("isolation ReadCommitted", false, "supportsTransactionIsolationLevel", Connection.TRANSACTION_READ_COMMITTED));
+            detections.add(new Detection("isolation ReadUncommitted", false, "supportsTransactionIsolationLevel", Connection.TRANSACTION_READ_UNCOMMITTED));
+            detections.add(new Detection("isolation RepeatableRead", false, "supportsTransactionIsolationLevel", Connection.TRANSACTION_REPEATABLE_READ));
+            detections.add(new Detection("isolation Serializable", false, "supportsTransactionIsolationLevel", Connection.TRANSACTION_SERIALIZABLE));
+            detections.add(new Detection("forward only type", false, "supportsResultSetType", ResultSet.TYPE_FORWARD_ONLY));
+            detections.add(new Detection("scroll sensitive type", false, "supportsResultSetType", ResultSet.TYPE_SCROLL_SENSITIVE));
+            detections.add(new Detection("scroll insensitive type", false, "supportsResultSetType", ResultSet.TYPE_SCROLL_INSENSITIVE));
+            detections.add(new Detection("is case sensitive", false, "supportsMixedCaseIdentifiers"));
+            detections.add(new Detection("stores LowerCase", false, "storesLowerCaseIdentifiers"));
+            detections.add(new Detection("stores MixedCase", false, "storesMixedCaseIdentifiers"));
+            detections.add(new Detection("stores UpperCase", false, "storesUpperCaseIdentifiers"));
+            detections.add(new Detection("max table name length", false, "getMaxTableNameLength"));
+            detections.add(new Detection("max column name length", false, "getMaxColumnNameLength"));
+            detections.add(new Detection("concurrent connections", false, "getMaxConnections"));
+            detections.add(new Detection("concurrent statements", false, "getMaxStatements"));
+            detections.add(new Detection("ANSI SQL92 Entry", false, "supportsANSI92EntryLevelSQL"));
+            detections.add(new Detection("ANSI SQL92 Intermediate", false, "supportsANSI92IntermediateSQL"));
+            detections.add(new Detection("ANSI SQL92 Full", false, "supportsANSI92FullSQL"));
+            detections.add(new Detection("ODBC SQL Grammar Core", false, "supportsCoreSQLGrammar"));
+            detections.add(new Detection("ODBC SQL Grammar Extended", false, "supportsExtendedSQLGrammar"));
+            detections.add(new Detection("ODBC SQL Grammar Minimum", false, "supportsMinimumSQLGrammar"));
+            detections.add(new Detection("outer joins", true, "supportsOuterJoins"));
+            detections.add(new Detection("limited outer joins", false, "supportsLimitedOuterJoins"));
+            detections.add(new Detection("full outer joins", false, "supportsFullOuterJoins"));
+            detections.add(new Detection("group by", true, "supportsGroupBy"));
+            detections.add(new Detection("group by not in select", false, "supportsGroupByUnrelated"));
+            detections.add(new Detection("column aliasing", false, "supportsColumnAliasing"));
+            detections.add(new Detection("order by not in select", false, "supportsOrderByUnrelated"));
+            detections.add(new Detection("alter table add column", true, "supportsAlterTableWithAddColumn"));
+            detections.add(new Detection("non-nullable column", true, "supportsNonNullableColumns"));
+            //detections.add(new Detection("", false, "", ));
+        } catch (NoSuchMethodException e) {
+            throw (InternalError) new InternalError(e.getMessage()).initCause(e);
+        }
+        int maxWidth = 0;
+        for (Detection detection: detections) {
+            if (detection.name.length() > maxWidth) {
+                maxWidth = detection.name.length();
+            }
+        }
+        goodFormatStr = "- %-" + maxWidth + "s [%s]%s";
+        badFormatStr = "- %-" + maxWidth + "s [ DETECTION FAILED ]%s";
+    }
+
     public void printDbMiscData(DatabaseMetaData dbData, Connection con) {
         if (dbData == null) {
             return;
@@ -875,41 +950,16 @@ public class DatabaseUtil {
         // Db/Driver support settings
         if (Debug.infoOn()) {
                 Debug.logInfo("Database Setting/Support Information (those with a * should be true):", module);
-            try {
-                Debug.logInfo("- supports transactions    [" + dbData.supportsTransactions() + "]*", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- supports transactions    [ DETECTION FAILED ]*", module);
-            }
-            try {
-                Debug.logInfo("- isolation None           [" + dbData.supportsTransactionIsolationLevel(Connection.TRANSACTION_NONE) + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- isolation None           [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- isolation ReadCommitted  [" + dbData.supportsTransactionIsolationLevel(Connection.TRANSACTION_READ_COMMITTED) + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- isolation ReadCommitted  [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- isolation ReadUncommitted[" + dbData.supportsTransactionIsolationLevel(Connection.TRANSACTION_READ_UNCOMMITTED) + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- isolation ReadUncommitted[ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- isolation RepeatableRead [" + dbData.supportsTransactionIsolationLevel(Connection.TRANSACTION_REPEATABLE_READ) + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- isolation RepeatableRead [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- isolation Serializable   [" + dbData.supportsTransactionIsolationLevel(Connection.TRANSACTION_SERIALIZABLE) + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- isolation Serializable   [ DETECTION FAILED ]", module);
+            for (Detection detection: detections) {
+                String requiredFlag = detection.required ? "*" : "";
+                try {
+                    Object result = detection.method.invoke(dbData, detection.params);
+                    Debug.logInfo(String.format(goodFormatStr, detection.name, result, requiredFlag), module);
+                } catch (Exception e) {
+                    e.printStackTrace();
+                    Debug.logVerbose(e, module);
+                    Debug.logWarning(String.format(badFormatStr, detection.name, requiredFlag), module);
+                }
             }
             try {
                 Debug.logInfo("- default fetchsize        [" + con.createStatement().getFetchSize() + "]", module);
@@ -918,156 +968,6 @@ public class DatabaseUtil {
                 Debug.logWarning("- default fetchsize        [ DETECTION FAILED ]", module);
             }
             try {
-                Debug.logInfo("- forward only type        [" + dbData.supportsResultSetType(ResultSet.TYPE_FORWARD_ONLY) + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- forward only type        [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- scroll sensitive type    [" + dbData.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE) + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- scroll sensitive type    [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- scroll insensitive type  [" + dbData.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE) + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- scroll insensitive type  [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- is case sensitive        [" + dbData.supportsMixedCaseIdentifiers() + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- is case sensitive        [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- stores LowerCase         [" + dbData.storesLowerCaseIdentifiers() + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- stores LowerCase         [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- stores MixedCase         [" + dbData.storesMixedCaseIdentifiers() + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- stores MixedCase         [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- stores UpperCase         [" + dbData.storesUpperCaseIdentifiers() + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- stores UpperCase         [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- max table name length    [" + dbData.getMaxTableNameLength() + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- max table name length    [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- max column name length   [" + dbData.getMaxColumnNameLength() + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- max column name length   [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- max schema name length   [" + dbData.getMaxSchemaNameLength() + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- max schema name length   [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- concurrent connections   [" + dbData.getMaxConnections() + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- concurrent connections   [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- concurrent statements    [" + dbData.getMaxStatements() + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- concurrent statements    [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- ANSI SQL92 Entry         [" + dbData.supportsANSI92EntryLevelSQL() + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- ANSI SQL92 Entry         [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- ANSI SQL92 Intermediate  [" + dbData.supportsANSI92IntermediateSQL() + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- ANSI SQL92 Intermediate  [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- ANSI SQL92 Full          [" + dbData.supportsANSI92FullSQL() + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- ANSI SQL92 Full          [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- ODBC SQL Grammar Core    [" + dbData.supportsCoreSQLGrammar() + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- ODBC SQL Grammar Core    [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- ODBC SQL Grammar Extended[" + dbData.supportsExtendedSQLGrammar() + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- ODBC SQL Grammar Extended[ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- ODBC SQL Grammar Minimum [" + dbData.supportsMinimumSQLGrammar() + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- ODBC SQL Grammar Minimum [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- outer joins              [" + dbData.supportsOuterJoins() + "]*", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- outer joins              [ DETECTION FAILED]*", module);
-            }
-            try {
-                Debug.logInfo("- limited outer joins      [" + dbData.supportsLimitedOuterJoins() + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- limited outer joins      [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- full outer joins         [" + dbData.supportsFullOuterJoins() + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- full outer joins         [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- group by                 [" + dbData.supportsGroupBy() + "]*", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- group by                 [ DETECTION FAILED ]*", module);
-            }
-            try {
-                Debug.logInfo("- group by not in select   [" + dbData.supportsGroupByUnrelated() + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- group by not in select   [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- column aliasing          [" + dbData.supportsColumnAliasing() + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- column aliasing          [ DETECTION FAILED ]", module);
-            }
-            try {
-                Debug.logInfo("- order by not in select   [" + dbData.supportsOrderByUnrelated() + "]", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- order by not in select   [ DETECTION FAILED ]", module);
-            }
-            try {
                 //this doesn't work in HSQLDB, other databases?
                 //crashed (vm-death) with MS SQL Server 2000, runs properly with MS SQL Server 2005
                 //Debug.logInfo("- named parameters         [" + dbData.supportsNamedParameters() + "]", module);
@@ -1076,18 +976,6 @@ public class DatabaseUtil {
                 Debug.logVerbose(e, module);
                 Debug.logWarning("- named parameters JDBC-3  [ DETECTION FAILED ]", module);
             }
-            try {
-                Debug.logInfo("- alter table add column   [" + dbData.supportsAlterTableWithAddColumn() + "]*", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- alter table add column   [ DETECTION FAILED ]*", module);
-            }
-            try {
-                Debug.logInfo("- non-nullable column      [" + dbData.supportsNonNullableColumns() + "]*", module);
-            } catch (Exception e) {
-                Debug.logVerbose(e, module);
-                Debug.logWarning("- non-nullable column      [ DETECTION FAILED ]*", module);
-            }
         }
     }