FileDocCategorySizeDatePackage
DBVendorTypeHelper.javaAPI DocGlassfish v2 API12941Fri May 04 22:35:22 BST 2007com.sun.jdo.spi.persistence.utility.database

DBVendorTypeHelper

public class DBVendorTypeHelper extends Object
author
Mitesh Meswani This class defines string constants representing database names. This class is responsible to translate given database name to internal name.

Fields Summary
public static final int
OTHER_ENUM
public static final int
DEFAULT_DB_ENUM
public static final int
ORACLE_ENUM
public static final int
POINTBASE_ENUM
public static final int
MSSQL_ENUM
public static final int
SYBASE_ENUM
public static final int
DB2_ENUM
public static final int
MYSQL_ENUM
public static final int
INFORMIX_ENUM
public static final int
INGRES_ENUM
public static final int
DERBY_ENUM
public static final int
MAX_KNOWN_DB
private static final String[]
enumToStringMapping
Array that defines mapping from given string name to enum. Please make sure that array indexes and enum values are kept in sync.
public static final String
DEFAULT_DB
public static final String
ORACLE
public static final String
POINTBASE
public static final String
MSSQL
public static final String
SYBASE
public static final String
DB2
public static final String
MYSQL
public static final String
INFORMIX
public static final String
INGRES
public static final String
DERBY
private static final String
PROPERTY_PATH
private static final String
VENDOR_NAME_TO_TYPE_PROPERTY
private static final String
VENDOR_NAME_TO_TYPE_RESOURCE_PROPERTY
private static final String
VENDOR_NAME_TO_TYPE_RESOURCE_DEFAULT_NAME
private static final com.sun.jdo.spi.persistence.utility.logging.Logger
logger
The logger.
private static Properties
_nameToVendorType
Holds mapping between possible vendor names to internal types defined above. vendor names are treated as regular expressions.
Constructors Summary
Methods Summary
public static java.lang.StringgetDBType(java.lang.String vendorName)
Get Database Vendor Type from vendor name.

param
vendorName Input vendor name. Typically this is obtained by querying DatabaseMetaData.
return
Database type that corresponds to vendorName. If vendorName does not match any of predefined vendor names, the database type returned is generated using following logic.
detectedDbType = vendorName.toUpperCase();
int i = detectedDbType.indexOf('/');
if (i > -1) {
detectedDbType = detectedDbType.substring(0, i);
}
If vendorName is null, DEFAULT_DB is returned.


                                                                                                                      
         
        boolean debug = logger.isLoggable();
        if(debug) {
            logger.fine("utility.database.DBVendorTypeHelper.inputVendorName", //NOI18N
            vendorName);
        }
        String detectedDbType = DEFAULT_DB;
        if(vendorName != null) {
            detectedDbType = matchVendorNameInProperties(vendorName, _nameToVendorType);
            //If not able to detect dbType from properties, invent one by
            //manipulating input vendorName.
            if(detectedDbType == null) {
                detectedDbType = vendorName.toUpperCase();
                int i = detectedDbType.indexOf('/");
                if (i > -1) {
                    detectedDbType = detectedDbType.substring(0, i);
                }
            }
        }
        if(debug)
            logger.fine("utility.database.DBVendorTypeHelper.detectedVendorType",detectedDbType);  //NOI18N
        return detectedDbType;
    
public static intgetEnumDBType(java.sql.DatabaseMetaData metaData)
Gets enumerated database type for given metaData

param
metaData Input DataBaseMetaData.
return
Enumerated database type as described by {@link #getEnumDBType(java.lang.String)}.

        String dbType = getDBType(metaData.getDatabaseProductName());
        return getEnumDBType(dbType);
     
public static intgetEnumDBType(java.lang.String dbType)
Gets enumerated database type for given dbType

param
dbType Input database Type. This should have been obtained from a previous call to {@link #getDBType(java.lang.String)}
return
dbType translated to one of the enumerations above. If dbType does not correspond to one of the predefined types, OTHER is returned

        int enumDBType = OTHER_ENUM;
        //Search through the array for dbType
        for(int i = 0; i < MAX_KNOWN_DB - 1 && enumDBType == OTHER_ENUM; i++) {
            if(enumToStringMapping[i].equals(dbType) )
                enumDBType = i;
        }
        return enumDBType;
    
private static java.util.PropertiesinitializeNameToVendorType()
Allocate and initialize nameToVendorType if not already done.

        synchronized(DBVendorTypeHelper.class) {
            if(_nameToVendorType == null) {
                _nameToVendorType = new Properties();
                String resourceName = System.getProperty(VENDOR_NAME_TO_TYPE_RESOURCE_PROPERTY,
                                    VENDOR_NAME_TO_TYPE_RESOURCE_DEFAULT_NAME);
                try {
                    PropertyHelper.loadFromResource(_nameToVendorType,resourceName,
                                            DBVendorTypeHelper.class.getClassLoader() );
                } catch (IOException e) {
                    if(logger.isLoggable() ) {
                        logger.fine("utility.database.DBVendorTypeHelper.couldNotLoadResource",  // NOI18N
                            resourceName,e);
                    }
                }
                overrideWithSystemProperties(_nameToVendorType);
            }
        }

        return _nameToVendorType;
    
private static booleanmatchPattern(java.lang.String regExp, java.lang.String target)
Matches target to pattern specified regExp. Returns false if there is any error compiling regExp.

param
regExp The regular expression.
param
target The target against which we are trying to match regExp.
return
false if there is error compiling regExp or target does not match regExp. true if regExp matches pattern.

        boolean matches = false;
        try {
            matches = Pattern.matches(regExp,target);
        } catch (PatternSyntaxException e){
            logger.fine("utility.database.DBVendorTypeHelper.patternSyntaxException",e); // NOI18N
        }
        return matches;
    
private static java.lang.StringmatchVendorNameInProperties(java.lang.String vendorName, java.util.Properties nameToVendorType)
Match vendorName in properties specifieid by nameToVendorType.

        String dbType = null;
        //Iterate over all properties till we find match.
        for( Iterator iterator = nameToVendorType.entrySet().iterator();
                dbType == null && iterator.hasNext();) {
            Map.Entry entry = (Map.Entry) iterator.next();
            String regExpr = (String) entry.getKey();
            String value = (String) entry.getValue();
            if(logger.isLoggable(Logger.FINEST) )
                logger.finest("utility.database.DBVendorTypeHelper.regExprDbType",regExpr,value); // NOI18N
            if( matchPattern(regExpr,vendorName) ) {
                dbType = value;
            }
        }
        return dbType;
    
private static voidoverrideWithSystemProperties(java.util.Properties nameToVendorType)
Overrides nameToVendorType with any system properties defined.

        String vendorNameToType = null;
        boolean debug = logger.isLoggable();

        int counter = 1;
        do {
            String vendorNameToTypeProperty = VENDOR_NAME_TO_TYPE_PROPERTY + counter++;
            vendorNameToType = System.getProperty(vendorNameToTypeProperty);
            if(vendorNameToType != null) {
                //Split the vendorNameToType into two at char '='
                String[] parsedProperty = vendorNameToType.split("=",2); //NOI18N
                if( parsedProperty.length >= 2) {
                    String suggestedDbType = parsedProperty[0];
                    String regExp = parsedProperty[1];
                    if(debug) {
                        logger.fine("utility.database.DBVendorTypeHelper.traceVendorNameToTypeProperty", //NOI18N
                        vendorNameToTypeProperty,regExp,suggestedDbType);
                    }
                    nameToVendorType.put(regExp,suggestedDbType);
                }
                else {
                    if(debug)
                        logger.fine("utility.database.DBVendorTypeHelper.errorParsingVendorNameToTypeProperty", //NOI18N
                            vendorNameToTypeProperty,vendorNameToType);
                }
            }
        } while (vendorNameToType != null);
     
public static booleanrequireUpperCaseSchema(java.sql.DatabaseMetaData dmd)
Determines whether to use uppercase schema name for a give database.

param
dmd The DatabaseMetaData for the database
return
true if upper case schemaname is to be used. False otherwise.
throws
SQLException

        int vendorTypeEnum = getEnumDBType(dmd);
        return ORACLE_ENUM == vendorTypeEnum ||
                POINTBASE_ENUM == vendorTypeEnum;