1 // $Id: NSNumber+PGVal.m 1 2004-08-20 10:38:46Z znek $
3 #import <Foundation/NSString.h>
4 #include "PostgreSQL72Channel.h"
7 @implementation NSNumber(PostgreSQL72Values)
9 static BOOL debugOn = NO;
10 static Class NSNumberClass = Nil;
11 static NSNumber *yesNum = nil;
12 static NSNumber *noNum = nil;
14 + (id)valueFromCString:(const char *)_cstr length:(int)_length
15 postgreSQLType:(NSString *)_type
16 attribute:(EOAttribute *)_attribute
17 adaptorChannel:(PostgreSQL72Channel *)_channel
19 // TODO: can we avoid the lowercaseString?
23 if ((len = [_type length]) == 0)
26 if (NSNumberClass == Nil) NSNumberClass = [NSNumber class];
28 c1 = [_type characterAtIndex:0];
33 if ([[_type lowercaseString] hasPrefix:@"float"])
34 return [NSNumberClass numberWithDouble:atof(_cstr)];
40 if ([[_type lowercaseString] hasPrefix:@"smallint"])
41 return [NSNumberClass numberWithShort:atoi(_cstr)];
47 if ([[_type lowercaseString] hasPrefix:@"int"])
48 return [NSNumberClass numberWithInt:atoi(_cstr)];
53 if (![[_type lowercaseString] hasPrefix:@"bool"])
56 if (yesNum == nil) yesNum = [[NSNumberClass numberWithBool:YES] retain];
57 if (noNum == nil) noNum = [[NSNumberClass numberWithBool:NO] retain];
75 + (id)valueFromBytes:(const void *)_bytes length:(int)_length
76 postgreSQLType:(NSString *)_type
77 attribute:(EOAttribute *)_attribute
78 adaptorChannel:(PostgreSQL72Channel *)_channel
80 #if COCOA_Foundation_LIBRARY || NeXT_Foundation_LIBRARY
81 NSLog(@"%s: not implemented!", __PRETTY_FUNCTION__);
84 return [self notImplemented:_cmd];
88 - (NSString *)stringValueForPostgreSQLType:(NSString *)_type
89 attribute:(EOAttribute *)_attribute
91 // TODO: can we avoid the lowercaseString?
96 NSLog(@"%s(type=%@,attr=%@)", __PRETTY_FUNCTION__, _type, _attribute);
98 if ((len = [_type length]) == 0) {
99 if (debugOn) NSLog(@" no type, return string");
100 return [self stringValue];
102 if (len < 4) { /* apparently this is 'INT'? */
103 if (debugOn) NSLog(@" type len < 4 (%@), return string", _type);
104 #if GNUSTEP_BASE_LIBRARY
106 on gstep-base -stringValue of bool's return YES or NO, which seems to
107 be different on Cocoa and liBFoundation.
110 Class BoolClass = Nil;
112 if (BoolClass == Nil) BoolClass = NSClassFromString(@"NSBoolNumber");
113 if ([self isKindOfClass:BoolClass])
114 return [self boolValue] ? @"1" : @"0";
117 return [self stringValue];
120 c1 = [_type characterAtIndex:0];
121 if (debugOn) NSLog(@" typecode '%c' ...", c1);
124 if (![[_type lowercaseString] hasPrefix:@"bool"])
126 return [self boolValue] ? @"true" : @"false";
128 case 'm': case 'M': {
129 if (![[_type lowercaseString] hasPrefix:@"money"])
131 return [@"$" stringByAppendingString:[self stringValue]];
136 case 'v': case 'V': {
137 static NSMutableString *ms = nil; // reuse mstring, THREAD
139 _type = [_type lowercaseString];
140 if (!([_type hasPrefix:@"char"] ||
141 [_type hasPrefix:@"varchar"] ||
142 [_type hasPrefix:@"text"]))
145 // TODO: can we get this faster?!
146 if (ms == nil) ms = [[NSMutableString alloc] initWithCapacity:256];
148 [ms appendString:[self stringValue]];
149 [ms appendString:@"'"];
150 return [[ms copy] autorelease];
153 return [self stringValue];
156 @end /* NSNumber(PostgreSQL72Values) */