Commits

Tuukka Norri committed 91d75e9

A non-functional revision
- Made use of the ISO8601 parser class.
- Partially fixed a bug in BaseTen schema.

  • Participants
  • Parent commits 685cbf2
  • Branches PGTS

Comments (0)

Files changed (6)

File Framework/Contrib/ISO-8601-parser/NSCalendarDate+ISO8601Parsing.h

 @interface NSCalendarDate(ISO8601Parsing)
 
 //This method is the one that does all the work. All the others are convenience methods.
-+ (NSCalendarDate *)calendarDateWithString:(NSString *)str strictly:(BOOL)strict getRange:(out NSRange *)outRange;
-+ (NSCalendarDate *)calendarDateWithString:(NSString *)str strictly:(BOOL)strict;
++ (NSCalendarDate *)ISO8601CalendarDateWithString:(NSString *)str strictly:(BOOL)strict getRange:(out NSRange *)outRange;
++ (NSCalendarDate *)ISO8601CalendarDateWithString:(NSString *)str strictly:(BOOL)strict;
 
 //Strictly: NO.
-+ (NSCalendarDate *)calendarDateWithString:(NSString *)str timeSeparator:(unichar)timeSep getRange:(out NSRange *)outRange;
-+ (NSCalendarDate *)calendarDateWithString:(NSString *)str timeSeparator:(unichar)timeSep;
-+ (NSCalendarDate *)calendarDateWithString:(NSString *)str getRange:(out NSRange *)outRange;
-+ (NSCalendarDate *)calendarDateWithString:(NSString *)str;
++ (NSCalendarDate *)ISO8601CalendarDateWithString:(NSString *)str timeSeparator:(unichar)timeSep getRange:(out NSRange *)outRange;
++ (NSCalendarDate *)ISO8601CalendarDateWithString:(NSString *)str timeSeparator:(unichar)timeSep;
++ (NSCalendarDate *)ISO8601CalendarDateWithString:(NSString *)str getRange:(out NSRange *)outRange;
++ (NSCalendarDate *)ISO8601CalendarDateWithString:(NSString *)str;
 
 @end

File Framework/Contrib/ISO-8601-parser/NSCalendarDate+ISO8601Parsing.m

  * //Day only of implied week
  *  -W-d
  */
-+ (NSCalendarDate *)calendarDateWithString:(NSString *)str strictly:(BOOL)strict timeSeparator:(unichar)timeSep getRange:(out NSRange *)outRange {
++ (NSCalendarDate *)ISO8601CalendarDateWithString:(NSString *)str strictly:(BOOL)strict timeSeparator:(unichar)timeSep getRange:(out NSRange *)outRange {
 	NSCalendarDate *now = [NSCalendarDate calendarDate];
 	unsigned
 		//Date
 													day:day
 												   hour:hour
 												 minute:minute
-												 second:second
+												 second:0
 											   timeZone:timeZone];
+					date = [date addTimeInterval: second];
 					break;
 
 				case week:;
 													day:1
 												   hour:hour
 												 minute:minute
-												 second:second
+												 second:0
 											   timeZone:timeZone];
 					date = [date dateByAddingYears:0
 											months:0
 											 hours:0
 										   minutes:0
 										   seconds:0];
+					date = [date addTimeInterval: second];
 					break;
 			}
 		}
 	return date;
 }
 
-+ (NSCalendarDate *)calendarDateWithString:(NSString *)str {
-	return [self calendarDateWithString:str strictly:NO getRange:NULL];
++ (NSCalendarDate *)ISO8601CalendarDateWithString:(NSString *)str {
+	return [self ISO8601CalendarDateWithString:str strictly:NO getRange:NULL];
 }
-+ (NSCalendarDate *)calendarDateWithString:(NSString *)str strictly:(BOOL)strict {
-	return [self calendarDateWithString:str strictly:strict getRange:NULL];
++ (NSCalendarDate *)ISO8601CalendarDateWithString:(NSString *)str strictly:(BOOL)strict {
+	return [self ISO8601CalendarDateWithString:str strictly:strict getRange:NULL];
 }
-+ (NSCalendarDate *)calendarDateWithString:(NSString *)str strictly:(BOOL)strict getRange:(out NSRange *)outRange {
-	return [self calendarDateWithString:str strictly:strict timeSeparator:ISO8601ParserDefaultTimeSeparatorCharacter getRange:NULL];
++ (NSCalendarDate *)ISO8601CalendarDateWithString:(NSString *)str strictly:(BOOL)strict getRange:(out NSRange *)outRange {
+	return [self ISO8601CalendarDateWithString:str strictly:strict timeSeparator:ISO8601ParserDefaultTimeSeparatorCharacter getRange:NULL];
 }
 
-+ (NSCalendarDate *)calendarDateWithString:(NSString *)str timeSeparator:(unichar)timeSep getRange:(out NSRange *)outRange {
-	return [self calendarDateWithString:str strictly:NO timeSeparator:timeSep getRange:outRange];
++ (NSCalendarDate *)ISO8601CalendarDateWithString:(NSString *)str timeSeparator:(unichar)timeSep getRange:(out NSRange *)outRange {
+	return [self ISO8601CalendarDateWithString:str strictly:NO timeSeparator:timeSep getRange:outRange];
 }
-+ (NSCalendarDate *)calendarDateWithString:(NSString *)str timeSeparator:(unichar)timeSep {
-	return [self calendarDateWithString:str strictly:NO timeSeparator:timeSep getRange:NULL];
++ (NSCalendarDate *)ISO8601CalendarDateWithString:(NSString *)str timeSeparator:(unichar)timeSep {
+	return [self ISO8601CalendarDateWithString:str strictly:NO timeSeparator:timeSep getRange:NULL];
 }
-+ (NSCalendarDate *)calendarDateWithString:(NSString *)str getRange:(out NSRange *)outRange {
-	return [self calendarDateWithString:str strictly:NO timeSeparator:ISO8601ParserDefaultTimeSeparatorCharacter getRange:outRange];
++ (NSCalendarDate *)ISO8601CalendarDateWithString:(NSString *)str getRange:(out NSRange *)outRange {
+	return [self ISO8601CalendarDateWithString:str strictly:NO timeSeparator:ISO8601ParserDefaultTimeSeparatorCharacter getRange:outRange];
 }
 
 @end

File Framework/Framework.xcodeproj/project.pbxproj

 		53D9B4C808B5492C00ADB959 /* PGTSTypeInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = 53D9B4C608B5492C00ADB959 /* PGTSTypeInfo.h */; settings = {ATTRIBUTES = (Public, ); }; };
 		53D9B4C908B5492C00ADB959 /* PGTSTypeInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 53D9B4C708B5492C00ADB959 /* PGTSTypeInfo.m */; };
 		53D9B52D08B553A900ADB959 /* datatypeassociations.plist in Resources */ = {isa = PBXBuildFile; fileRef = 53D9B52C08B553A900ADB959 /* datatypeassociations.plist */; };
+		53DE4FCA0B57CE3800A15759 /* NSCalendarDate+ISO8601Parsing.m in Sources */ = {isa = PBXBuildFile; fileRef = 53DE4FC60B57CE3800A15759 /* NSCalendarDate+ISO8601Parsing.m */; };
+		53DE4FCB0B57CE3800A15759 /* NSCalendarDate+ISO8601Unparsing.h in Headers */ = {isa = PBXBuildFile; fileRef = 53DE4FC70B57CE3800A15759 /* NSCalendarDate+ISO8601Unparsing.h */; };
+		53DE4FCC0B57CE3800A15759 /* NSCalendarDate+ISO8601Unparsing.m in Sources */ = {isa = PBXBuildFile; fileRef = 53DE4FC80B57CE3800A15759 /* NSCalendarDate+ISO8601Unparsing.m */; };
+		53DE4FCD0B57CE3800A15759 /* NSCalendarDate+ISO8601Parsing.h in Headers */ = {isa = PBXBuildFile; fileRef = 53DE4FC90B57CE3800A15759 /* NSCalendarDate+ISO8601Parsing.h */; };
 		53E0D1C908979FF300501E8E /* PGTSMutableResultRow.h in Headers */ = {isa = PBXBuildFile; fileRef = 53E0D1C708979FF300501E8E /* PGTSMutableResultRow.h */; settings = {ATTRIBUTES = (Public, ); }; };
 		53E0D1CA08979FF300501E8E /* PGTSMutableResultRow.m in Sources */ = {isa = PBXBuildFile; fileRef = 53E0D1C808979FF300501E8E /* PGTSMutableResultRow.m */; };
 		53E11A100A7919CE00D0107C /* ExceptionHandling.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 53E11A0C0A7919CE00D0107C /* ExceptionHandling.framework */; };
 		53D9B4C608B5492C00ADB959 /* PGTSTypeInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PGTSTypeInfo.h; path = Sources/PGTSTypeInfo.h; sourceTree = "<group>"; };
 		53D9B4C708B5492C00ADB959 /* PGTSTypeInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = PGTSTypeInfo.m; path = Sources/PGTSTypeInfo.m; sourceTree = "<group>"; };
 		53D9B52C08B553A900ADB959 /* datatypeassociations.plist */ = {isa = PBXFileReference; fileEncoding = 5; lastKnownFileType = text.xml; path = datatypeassociations.plist; sourceTree = "<group>"; };
+		53DE4FC60B57CE3800A15759 /* NSCalendarDate+ISO8601Parsing.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; name = "NSCalendarDate+ISO8601Parsing.m"; path = "Contrib/ISO-8601-parser/NSCalendarDate+ISO8601Parsing.m"; sourceTree = "<group>"; };
+		53DE4FC70B57CE3800A15759 /* NSCalendarDate+ISO8601Unparsing.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = "NSCalendarDate+ISO8601Unparsing.h"; path = "Contrib/ISO-8601-parser/NSCalendarDate+ISO8601Unparsing.h"; sourceTree = "<group>"; };
+		53DE4FC80B57CE3800A15759 /* NSCalendarDate+ISO8601Unparsing.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; name = "NSCalendarDate+ISO8601Unparsing.m"; path = "Contrib/ISO-8601-parser/NSCalendarDate+ISO8601Unparsing.m"; sourceTree = "<group>"; };
+		53DE4FC90B57CE3800A15759 /* NSCalendarDate+ISO8601Parsing.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = "NSCalendarDate+ISO8601Parsing.h"; path = "Contrib/ISO-8601-parser/NSCalendarDate+ISO8601Parsing.h"; sourceTree = "<group>"; };
 		53E0D1C708979FF300501E8E /* PGTSMutableResultRow.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PGTSMutableResultRow.h; path = Sources/PGTSMutableResultRow.h; sourceTree = "<group>"; };
 		53E0D1C808979FF300501E8E /* PGTSMutableResultRow.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = PGTSMutableResultRow.m; path = Sources/PGTSMutableResultRow.m; sourceTree = "<group>"; };
 		53E11A0C0A7919CE00D0107C /* ExceptionHandling.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ExceptionHandling.framework; path = /System/Library/Frameworks/ExceptionHandling.framework; sourceTree = "<absolute>"; };
 				5359784908AF660F000CAC2B /* Metadata */,
 				53D9B37E08B5251C00ADB959 /* Result */,
 				53353A590896D174003EE146 /* WriteDelegate */,
+				53DE4FBE0B57CE1000A15759 /* RunLoopMessenger */,
+				53DE4FCE0B57CE3A00A15759 /* ISO 8601 Parser */,
 			);
 			name = Classes;
 			sourceTree = "<group>";
 		53D9B37D08B5250C00ADB959 /* Other */ = {
 			isa = PBXGroup;
 			children = (
-				530323C3088062270024E7EF /* NDRunLoopMessenger.h */,
-				530323C4088062270024E7EF /* NDRunLoopMessenger.m */,
-				537FB1F60A76218E00441E2E /* TSRunloopMessenger.h */,
-				537FB1F70A76218E00441E2E /* TSRunloopMessenger.m */,
 				53BA4FEF08804EA000BFAFB5 /* PGTSAdditions.h */,
 				53BA4FDB08804EA000BFAFB5 /* PGTSAdditions.m */,
 				536B892D0A518E4A00379C00 /* PGTSExceptions.h */,
 			name = Result;
 			sourceTree = "<group>";
 		};
+		53DE4FBE0B57CE1000A15759 /* RunLoopMessenger */ = {
+			isa = PBXGroup;
+			children = (
+				530323C3088062270024E7EF /* NDRunLoopMessenger.h */,
+				530323C4088062270024E7EF /* NDRunLoopMessenger.m */,
+				537FB1F60A76218E00441E2E /* TSRunloopMessenger.h */,
+				537FB1F70A76218E00441E2E /* TSRunloopMessenger.m */,
+			);
+			name = RunLoopMessenger;
+			sourceTree = "<group>";
+		};
+		53DE4FCE0B57CE3A00A15759 /* ISO 8601 Parser */ = {
+			isa = PBXGroup;
+			children = (
+				53DE4FC90B57CE3800A15759 /* NSCalendarDate+ISO8601Parsing.h */,
+				53DE4FC60B57CE3800A15759 /* NSCalendarDate+ISO8601Parsing.m */,
+				53DE4FC70B57CE3800A15759 /* NSCalendarDate+ISO8601Unparsing.h */,
+				53DE4FC80B57CE3800A15759 /* NSCalendarDate+ISO8601Unparsing.m */,
+			);
+			name = "ISO 8601 Parser";
+			sourceTree = "<group>";
+		};
 /* End PBXGroup section */
 
 /* Begin PBXHeadersBuildPhase section */
 				53298DE00B3307A800DE7CC8 /* PGTSRoleDescription.h in Headers */,
 				53298DF20B330B6F00DE7CC8 /* PGTSAbstractObjectDescription.h in Headers */,
 				53298E0F0B330EFB00DE7CC8 /* PGTSACLItem.h in Headers */,
+				53DE4FCB0B57CE3800A15759 /* NSCalendarDate+ISO8601Unparsing.h in Headers */,
+				53DE4FCD0B57CE3800A15759 /* NSCalendarDate+ISO8601Parsing.h in Headers */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
 				53298DE10B3307A800DE7CC8 /* PGTSRoleDescription.m in Sources */,
 				53298DF30B330B6F00DE7CC8 /* PGTSAbstractObjectDescription.m in Sources */,
 				53298E100B330EFB00DE7CC8 /* PGTSACLItem.m in Sources */,
+				53DE4FCA0B57CE3800A15759 /* NSCalendarDate+ISO8601Parsing.m in Sources */,
+				53DE4FCC0B57CE3800A15759 /* NSCalendarDate+ISO8601Unparsing.m in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

File Framework/Sources/PGTSAdditions.m

 #import "PGTSFieldInfo.h"
 #import "PGTSDatabaseInfo.h"
 #import "PGTSACLItem.h"
+#import "NSCalendarDate+ISO8601Parsing.h"
 #import <Log4Cocoa/Log4Cocoa.h>
 
 
 @implementation NSObject (PGTSAdditions)
 + (id) newForPGTSResultSet: (PGTSResultSet *) set withCharacters: (char *) value typeInfo: (PGTSTypeInfo *) typeInfo
 {
+	if (YES == [[NSObject class] isSubclassOfClass: self])
+	{
+		log4Warn (@"newForPGTSResultSet: was called in NSObject. "
+				  "Perhaps type %@ doesn't have an entry in datatypeassociations.plist?", 
+				  [typeInfo name]);
+	}
+	else
+	{
+		log4Error (@"NSObject's newForPGTSResultSet: was called instead of %@'s for type %@. "
+				   "The method should be overriden in the subclass.", self, [typeInfo name]);
+	}
     return nil;
 }
 
 @end
 
 
-@implementation NSDate (PGTSAdditions)
-- (char *) PGTSParameterLength: (int *) length connection: (PGTSConnection *) connection
-{
-    return [[self descriptionWithCalendarFormat: @"%Y-%m-%d %H:%M:%S.%F"
-                                       timeZone: [NSTimeZone localTimeZone]
-                                         locale: nil] PGTSParameterLength: length connection: connection];
-}
-
-+ (id) newForPGTSResultSet: (PGTSResultSet *) set withCharacters: (char *) value typeInfo: (PGTSTypeInfo *) typeInfo
-{
-    NSMutableString* dateString = [NSMutableString stringWithUTF8String: value];
-    switch ([dateString length])
-    {
-        //PostgreSQL seems to count significant numbers in its timestamp,
-        //which Foundation is unable to interpret
-        case 19:
-            [dateString appendString: @".00"];
-        case 22:
-            [dateString appendString: @"0"];
-        case 23:
-        default:
-            break;
-    }
-    id rval = [NSCalendarDate dateWithString: dateString
-                              calendarFormat: @"%Y-%m-%d %H:%M:%S.%F"];
-    NSAssert (nil != rval, @"Failed matching string to date format");
-    return [NSDate dateWithTimeIntervalSinceReferenceDate: [rval timeIntervalSinceReferenceDate]];
-}
-@end
-
-
 @implementation NSCalendarDate (PGTSAdditions)
 + (id) newForPGTSResultSet: (PGTSResultSet *) set withCharacters: (char *) value typeInfo: (PGTSTypeInfo *) typeInfo
 {
-    id rval = [[self class] dateWithString: [NSString stringWithUTF8String: value]
-                            calendarFormat: @"%Y-%m-%d %H:%M:%S+%z"];
-    NSAssert (nil != rval, @"Failed matching string to date format");
-    return rval;
+	id rval = [[self class] ISO8601CalendarDateWithString: [NSString stringWithUTF8String: value]
+												 strictly: YES];
+    NSAssert1 (nil != rval, @"Failed to match the string to date format: %s", value);
+	return rval;
 }
 
 - (char *) PGTSParameterLength: (int *) length connection: (PGTSConnection *) connection
 {
-    NSString* description = [self descriptionWithCalendarFormat: @"%Y-%m-%d %H:%M:%S%z"];
+	NSTimeInterval interval = [self timeIntervalSince1970];
+	double i = 0.0;
+	interval = modf (interval, &i);
+	char* stringRep = NULL;
+	asprintf (&stringRep, "%.6f", interval);
+    NSString* description = [self descriptionWithCalendarFormat: @"%Y-%m-%d %H:%M:%S.%%s%z"];
+	description = [NSString stringWithFormat: description, &stringRep [2]];
+	free (stringRep);
+	
+	NSLog (@"date: %@", description);
     return [description PGTSParameterLength: length connection: connection];
 }
 @end

File Framework/Sources/PGTSConnectionPrivate.m

             PQsetnonblocking (connection, 0); //We don't want to call PQsendquery etc. multiple times
             PQsetClientEncoding (connection, "UNICODE"); //Use UTF-8
             //FIXME: set other things, such as the date format, as well
+			PGresult* res = PQexec (connection, "SET datestyle TO 'ISO, DMY'");
+			if (NULL == res || PGRES_COMMAND_OK != PQresultStatus (res))
+			{
+				log4Error (@"Could not set the datestyle parameter. Response: %s (%s)", 
+						   PQresStatus (PQresultStatus (res)), PQresultErrorMessage (res));
+			}
             PQsetNoticeProcessor (connection, &PGTSNoticeProcessor, (void *) self);
 
             socket = [[NSFileHandle alloc] initWithFileDescriptor: bsdSocket];

File Framework/datatypeassociations.plist

 	<key>text</key>
 	<string>NSString</string>
 	<key>timestamp</key>
-	<string>NSDate</string>
+	<string>NSCalendarDate</string>
 	<key>timestamptz</key>
 	<string>NSCalendarDate</string>
 	<key>tinterval</key>