diff --git a/Frameworks/PlugIns/FrontBasePlugIn/Sources/com/webobjects/jdbcadaptor/FrontBaseTypes.java b/Frameworks/PlugIns/FrontBasePlugIn/Sources/com/webobjects/jdbcadaptor/FrontBaseTypes.java index e59e3da7041..d2bfcae5b88 100644 --- a/Frameworks/PlugIns/FrontBasePlugIn/Sources/com/webobjects/jdbcadaptor/FrontBaseTypes.java +++ b/Frameworks/PlugIns/FrontBasePlugIn/Sources/com/webobjects/jdbcadaptor/FrontBaseTypes.java @@ -74,7 +74,6 @@ else if (externalType.equals("BLOB")) return FB_BLOB; else if (externalType.equals("CLOB")) return FB_CLOB; - else - return -1; + return -1; } } diff --git a/Frameworks/PlugIns/FrontBasePlugIn/Sources/com/webobjects/jdbcadaptor/_FrontBasePlugIn.java b/Frameworks/PlugIns/FrontBasePlugIn/Sources/com/webobjects/jdbcadaptor/_FrontBasePlugIn.java index 7ad20c0e9cd..420db3693af 100644 --- a/Frameworks/PlugIns/FrontBasePlugIn/Sources/com/webobjects/jdbcadaptor/_FrontBasePlugIn.java +++ b/Frameworks/PlugIns/FrontBasePlugIn/Sources/com/webobjects/jdbcadaptor/_FrontBasePlugIn.java @@ -40,7 +40,6 @@ import com.webobjects.foundation.NSPropertyListSerialization; import com.webobjects.foundation.NSRange; import com.webobjects.foundation.NSSelector; -import com.webobjects.foundation.NSTimeZone; import com.webobjects.foundation.NSTimestamp; import com.webobjects.foundation._NSUtilities; @@ -71,6 +70,36 @@ public class _FrontBasePlugIn extends JDBCPlugIn { static final String _frontbaseSqlStatementForGettingTableNames = System.getProperty("jdbcadaptor.frontbase.sqlStatementForGettingTableNames", null); static final String _frontbaseContainsOperatorFix = System.getProperty("jdbcadaptor.frontbase.frontbaseContainsOperatorFix", null); + /** + * Formatter to use when handling date columns. Each thread has its own copy. + */ + private static final ThreadLocal DATE_FORMATTER = new ThreadLocal() { + @Override + protected SimpleDateFormat initialValue() { + return new SimpleDateFormat("yyyy-MM-dd"); + } + }; + + /** + * Formatter to use when handling timestamp columns. Each thread has its own copy. + */ + private static final ThreadLocal TIMESTAMP_FORMATTER = new ThreadLocal() { + @Override + protected SimpleDateFormat initialValue() { + return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); + } + }; + + /** + * Formatter to use when handling time only columns. Each thread has its own copy. + */ + private static final ThreadLocal TIME_FORMATTER = new ThreadLocal() { + @Override + protected SimpleDateFormat initialValue() { + return new SimpleDateFormat("HH:mm:ss.SSS"); + } + }; + public _FrontBasePlugIn(JDBCAdaptor jdbcadaptor) { super(jdbcadaptor); } @@ -316,7 +345,7 @@ public void updateLOBs(JDBCChannel channel, JDBCExpression expression, NSDiction try { Connection con = ((JDBCContext) channel.adaptorContext()).connection(); - NSMutableDictionary d = new NSMutableDictionary(); + NSMutableDictionary d = new NSMutableDictionary(); for (int i = 0; i < array.count(); i += 2) { d.setObjectForKey(getLobHandle(con, array.objectAtIndex(i), array.objectAtIndex(i + 1)), ((EOAttribute) array.objectAtIndex(i)).name()); @@ -342,11 +371,11 @@ String getLobHandle(Connection con, Object attribute, Object value) throws SQLEx // MS: This is weird, but to allow for people to build FrontBasePlugIn without actually // having the FrontBase JDBC driver installed, I've switched these two calls to be reflection. try { - switch (internalTypeForExternal(((EOAttribute) attribute).externalType())) { - case FB_BLOB: + switch (FrontBaseTypes.internalTypeForExternal(((EOAttribute) attribute).externalType())) { + case FrontBaseTypes.FB_BLOB: Method writeBLOBBytes = con.getClass().getMethod("writeBLOB", new Class[] { byte[].class }); return (String) writeBLOBBytes.invoke(con, new Object[] { ((NSData) value).bytes() }); - case FB_CLOB: + case FrontBaseTypes.FB_CLOB: Method writeCLOBString = con.getClass().getMethod("writeCLOB", new Class[] { String.class }); return (String) writeCLOBString.invoke(con, new Object[] { (String) value }); default: @@ -494,28 +523,51 @@ private boolean _newPrimaryKeys(int keyBatchSize, EOEntity eoentity, JDBCChannel return pksGenerated; } + @Deprecated protected static final int FB_Boolean = 1; + @Deprecated protected static final int FB_Integer = 2; + @Deprecated protected static final int FB_SmallInteger = 3; + @Deprecated protected static final int FB_Float = 4; + @Deprecated protected static final int FB_Real = 5; + @Deprecated protected static final int FB_Double = 6; + @Deprecated protected static final int FB_Numeric = 7; + @Deprecated protected static final int FB_Decimal = 8; + @Deprecated protected static final int FB_Character = 9; + @Deprecated protected static final int FB_VCharacter = 10; + @Deprecated protected static final int FB_Bit = 11; + @Deprecated protected static final int FB_VBit = 12; + @Deprecated protected static final int FB_Date = 13; + @Deprecated protected static final int FB_Time = 14; + @Deprecated protected static final int FB_TimeTZ = 15; + @Deprecated protected static final int FB_Timestamp = 16; + @Deprecated protected static final int FB_TimestampTZ = 17; + @Deprecated protected static final int FB_YearMonth = 18; + @Deprecated protected static final int FB_DayTime = 19; + @Deprecated protected static final int FB_CLOB = 20; + @Deprecated protected static final int FB_BLOB = 21; + @Deprecated protected static final int FB_TinyInteger = 22; + @Deprecated protected static final int FB_LongInteger = 23; protected static String notNullConstraintName(EOAttribute attribute) { @@ -541,6 +593,10 @@ protected static String quoteTableName(String s) { return "\"" + s.substring(0, i) + "\".\"" + s.substring(i + 1, s.length()) + "\""; } + /** + * @deprecated user {@link FrontBaseTypes#internalTypeForExternal(String)} instead + */ + @Deprecated protected static int internalTypeForExternal(String externalType) { String upperExternalType = externalType.toUpperCase(); if (upperExternalType.equals("BOOLEAN")) @@ -612,7 +668,7 @@ public static boolean boolValueForKeyDefault(NSDictionary nsdictionary, String s public String schemaCreationScriptForEntities(NSArray allEntities, NSDictionary options) { StringBuffer result = new StringBuffer(); if (options == null) { - options = NSDictionary.EmptyDictionary; + options = NSDictionary.emptyDictionary(); } NSArray statements = schemaCreationStatementsForEntities(allEntities, options); int i = 0; @@ -650,30 +706,31 @@ public NSArray schemaCreationStatementsForEntities(NSArray> entityGroups = primaryKeyEntityGroupsForEntities(entities); + result.addObjectsFromArray(dropPrimaryKeySupportStatementsForEntityGroups(entityGroups)); } if (boolValueForKeyDefault(options, "dropTables", true)) { - NSArray nsarray2 = tableEntityGroupsForEntities(entities); - result.addObjectsFromArray(dropTableStatementsForEntityGroups(nsarray2)); + NSArray> entityGroups = tableEntityGroupsForEntities(entities); + result.addObjectsFromArray(dropTableStatementsForEntityGroups(entityGroups)); } if (boolValueForKeyDefault(options, "createTables", true)) { - NSArray nsarray3 = tableEntityGroupsForEntities(entities); - result.addObjectsFromArray(createTableStatementsForEntityGroups(nsarray3)); - result.addObjectsFromArray(createIndexStatementsForEntityGroups(nsarray3)); + NSArray> entityGroups = tableEntityGroupsForEntities(entities); + result.addObjectsFromArray(createTableStatementsForEntityGroups(entityGroups)); + result.addObjectsFromArray(createIndexStatementsForEntityGroups(entityGroups)); } if (boolValueForKeyDefault(options, "createPrimaryKeySupport", true)) { - NSArray nsarray4 = primaryKeyEntityGroupsForEntities(entities); - result.addObjectsFromArray(primaryKeySupportStatementsForEntityGroups(nsarray4)); + NSArray> entityGroups = primaryKeyEntityGroupsForEntities(entities); + result.addObjectsFromArray(primaryKeySupportStatementsForEntityGroups(entityGroups)); } if (boolValueForKeyDefault(options, "primaryKeyConstraints", true)) { - NSArray nsarray5 = tableEntityGroupsForEntities(entities); - result.addObjectsFromArray(primaryKeyConstraintStatementsForEntityGroups(nsarray5)); + NSArray> entityGroups = tableEntityGroupsForEntities(entities); + result.addObjectsFromArray(primaryKeyConstraintStatementsForEntityGroups(entityGroups)); } if (boolValueForKeyDefault(options, "foreignKeyConstraints", false)) { - NSArray nsarray6 = tableEntityGroupsForEntities(entities); - for (int i = 0; i < nsarray6.count(); i++) - result.addObjectsFromArray(_foreignKeyConstraintStatementsForEntityGroup((NSArray) nsarray6.objectAtIndex(i))); + NSArray> entityGroups = tableEntityGroupsForEntities(entities); + for (NSArray entityGroup : entityGroups) { + result.addObjectsFromArray(_foreignKeyConstraintStatementsForEntityGroup(entityGroup)); + } } result.addObject(_expressionForString("COMMIT")); return result; @@ -720,7 +777,7 @@ public NSArray dropTableStatementsForEntityGroup(NSArray primaryKeySupportStatementsForEntityGroup(NSArray entityGroup) { if (entityGroup == null) - return NSArray.EmptyArray; + return NSArray.emptyArray(); NSMutableArray result = new NSMutableArray(); @@ -820,7 +877,7 @@ public NSArray foreignKeyConstraintStatementsForRelationship(EO return new NSArray(_expressionForString(sql.toString())); } - return NSArray.EmptyArray; + return NSArray.emptyArray(); } /** @@ -848,7 +905,7 @@ public NSArray createTableStatementsForEntityGroup(NSArray createIndexStatementsForEntityGroup(NSArray statementsToInsertColumnForAttribute(EOAttribute } private String statementToCreateDataTypeClause(EOSchemaSynchronization.ColumnTypes columntypes) { - switch (internalTypeForExternal(columntypes.name())) { - case FB_Decimal: - case FB_Numeric: + switch (FrontBaseTypes.internalTypeForExternal(columntypes.name())) { + case FrontBaseTypes.FB_Decimal: + case FrontBaseTypes.FB_Numeric: int j = columntypes.precision(); if (j == 0) return columntypes.name(); @@ -1004,11 +1061,11 @@ private String statementToCreateDataTypeClause(EOSchemaSynchronization.ColumnTyp else return columntypes.name() + "(" + j + "," + k + ")"; - case FB_Float: - case FB_Bit: - case FB_VBit: - case FB_Character: - case FB_VCharacter: + case FrontBaseTypes.FB_Float: + case FrontBaseTypes.FB_Bit: + case FrontBaseTypes.FB_VBit: + case FrontBaseTypes.FB_Character: + case FrontBaseTypes.FB_VCharacter: int l = columntypes.width(); if (l == 0) l = columntypes.precision(); @@ -1016,7 +1073,7 @@ private String statementToCreateDataTypeClause(EOSchemaSynchronization.ColumnTyp return columntypes.name(); else return columntypes.name() + "(" + l + ")"; - case FB_Timestamp: + case FrontBaseTypes.FB_Timestamp: int m = columntypes.precision(); if (m == 0) return columntypes.name(); @@ -1099,7 +1156,7 @@ public NSArray primaryKeyConstraintStatementsForEntityGroup(NSA return new NSArray(_expressionForString(sql.toString())); } } - return NSArray.EmptyArray; + return NSArray.emptyArray(); } } @@ -1130,8 +1187,6 @@ public void addCreateClauseForAttribute(EOAttribute attribute) { sql.append(columnTypeStringForAttribute(attribute)); NSDictionary dictionary = attribute.userInfo(); - int internalType = internalTypeForExternal(attribute.externalType()); - boolean isLOB = internalType == FB_BLOB || internalType == FB_CLOB; if (dictionary == null) { _appendNotNullConstraintIfNecessary(attribute, sql); } @@ -1199,9 +1254,7 @@ private void _appendNotNullConstraintIfNecessary(EOAttribute attribute, StringBu } sql.append(" NOT NULL"); - int internalType = internalTypeForExternal(attribute.externalType()); - boolean isLOB = internalType == FB_BLOB || internalType == FB_CLOB; - if (isLOB) + if (isLOBAttribute(attribute)) sql.append(" DEFERRABLE INITIALLY DEFERRED"); } } @@ -1609,8 +1662,8 @@ public boolean shouldUseBindVariableForAttribute(EOAttribute eoattribute) { } private boolean isLOBAttribute(EOAttribute att) { - int internalType = internalTypeForExternal(att.externalType()); - return internalType == FB_BLOB || internalType == FB_CLOB; + int internalType = FrontBaseTypes.internalTypeForExternal(att.externalType()); + return internalType == FrontBaseTypes.FB_BLOB || internalType == FrontBaseTypes.FB_CLOB; } @Override @@ -1705,18 +1758,18 @@ public String formatValueForAttribute(Object obj, EOAttribute eoattribute) { throw new EOGeneralAdaptorException("Attribute " + eoattribute.name() + " on entity " + eoattribute.entity().name() + " with prototype named " + eoattribute.prototypeName() + " has no external type defined"); } - switch (internalTypeForExternal(eoattribute.externalType())) { - case FB_Character: - case FB_VCharacter: { + switch (FrontBaseTypes.internalTypeForExternal(eoattribute.externalType())) { + case FrontBaseTypes.FB_Character: + case FrontBaseTypes.FB_VCharacter: { return escapedString(obj); } - case FB_DayTime: { + case FrontBaseTypes.FB_DayTime: { return escapedString(obj); } - case FB_BLOB: - case FB_CLOB: { + case FrontBaseTypes.FB_BLOB: + case FrontBaseTypes.FB_CLOB: { if (!(obj instanceof String) && eoattribute.valueFactoryMethod() != null) { - Class valueClass = _NSUtilities.classWithName(eoattribute.className()); + Class valueClass = _NSUtilities.classWithName(eoattribute.className()); if (valueClass.isAssignableFrom(obj.getClass())) { obj = eoattribute.adaptorValueByConvertingAttributeValue(obj); } @@ -1730,8 +1783,8 @@ public String formatValueForAttribute(Object obj, EOAttribute eoattribute) { _lobList.addObject(obj); return "NULL"; } - case FB_VBit: - case FB_Bit: { + case FrontBaseTypes.FB_VBit: + case FrontBaseTypes.FB_Bit: { if (obj instanceof NSData) { return formatBit((NSData) obj); } @@ -1739,51 +1792,48 @@ public String formatValueForAttribute(Object obj, EOAttribute eoattribute) { return "ERROR: Can not convert value from " + obj + " to Bit or Byte data type"; } } - case FB_Time: { + case FrontBaseTypes.FB_Time: { StringBuffer time = new StringBuffer("TIME '"); Date d = (Date)eoattribute.adaptorValueByConvertingAttributeValue(obj); - SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss.SSS"); - format.format(d, time, new FieldPosition(0)); + TIME_FORMATTER.get().format(d, time, new FieldPosition(0)); time.append("'"); return time.toString(); } - case FB_TimeTZ: { + case FrontBaseTypes.FB_TimeTZ: { StringBuffer time = new StringBuffer("TIME '"); Date d = (Date)eoattribute.adaptorValueByConvertingAttributeValue(obj); - SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss.SSS"); - format.format(d, time, new FieldPosition(0)); - time.append(getTimeZone(NSTimeZone.defaultTimeZone())); + SimpleDateFormat formatter = TIME_FORMATTER.get(); + formatter.format(d, time, new FieldPosition(0)); + time.append(getTimeZone(formatter.getTimeZone())); time.append("'"); return time.toString(); } - case FB_Timestamp: { + case FrontBaseTypes.FB_Timestamp: { StringBuffer time = new StringBuffer("TIMESTAMP '"); Date d = (Date)eoattribute.adaptorValueByConvertingAttributeValue(obj); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); - format.format(d, time, new FieldPosition(0)); + TIMESTAMP_FORMATTER.get().format(d, time, new FieldPosition(0)); time.append("'"); return time.toString(); } - case FB_TimestampTZ: { + case FrontBaseTypes.FB_TimestampTZ: { StringBuffer time = new StringBuffer("TIMESTAMP '"); Date d = (Date)eoattribute.adaptorValueByConvertingAttributeValue(obj); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); - format.format(d, time, new FieldPosition(0)); - time.append(getTimeZone(NSTimeZone.defaultTimeZone())); + SimpleDateFormat formatter = TIMESTAMP_FORMATTER.get(); + formatter.format(d, time, new FieldPosition(0)); + time.append(getTimeZone(formatter.getTimeZone())); time.append("'"); return time.toString(); } - case FB_Date: { + case FrontBaseTypes.FB_Date: { StringBuffer time = new StringBuffer("DATE '"); Date d = (Date)eoattribute.adaptorValueByConvertingAttributeValue(obj); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - format.format(d, time, new FieldPosition(0)); + DATE_FORMATTER.get().format(d, time, new FieldPosition(0)); time.append("'"); return time.toString(); } - case FB_Boolean: { + case FrontBaseTypes.FB_Boolean: { if (obj instanceof Boolean) { return obj.toString(); } @@ -1809,15 +1859,15 @@ else if (((Number) obj).intValue() == 0) { return "TRUE"; } } - case FB_SmallInteger: - case FB_Float: - case FB_Real: - case FB_Double: - case FB_LongInteger: - case FB_TinyInteger: - case FB_Numeric: - case FB_Integer: - case FB_Decimal: { + case FrontBaseTypes.FB_SmallInteger: + case FrontBaseTypes.FB_Float: + case FrontBaseTypes.FB_Real: + case FrontBaseTypes.FB_Double: + case FrontBaseTypes.FB_LongInteger: + case FrontBaseTypes.FB_TinyInteger: + case FrontBaseTypes.FB_Numeric: + case FrontBaseTypes.FB_Integer: + case FrontBaseTypes.FB_Decimal: { if (obj instanceof BigDecimal) { return ((BigDecimal) obj).setScale(eoattribute.scale(), BigDecimal.ROUND_HALF_UP).toString(); }