1 // $Id: NSData+PGVal.m 1 2004-08-20 10:38:46Z znek $
3 #import <Foundation/NSData.h>
4 #include "PostgreSQL72Values.h"
5 #include "PostgreSQL72Channel.h"
8 @implementation NSData(PostgreSQL72Values)
10 static BOOL doDebug = NO;
11 static NSData *EmptyData = nil;
13 + (id)valueFromCString:(const char *)_cstr length:(int)_length
14 postgreSQLType:(NSString *)_type
15 attribute:(EOAttribute *)_attribute
16 adaptorChannel:(PostgreSQL72Channel *)_channel
19 if (EmptyData == nil) EmptyData = [[NSData alloc] init];
22 return [[[self alloc] initWithBytes:_cstr length:_length] autorelease];
25 + (id)valueFromBytes:(const void *)_bytes length:(int)_length
26 postgreSQLType:(NSString *)_type
27 attribute:(EOAttribute *)_attribute
28 adaptorChannel:(PostgreSQL72Channel *)_channel
31 if (EmptyData == nil) EmptyData = [[NSData alloc] init];
34 return [[[self alloc] initWithBytes:_bytes length:_length] autorelease];
37 - (NSString *)stringValueForPostgreSQLType:(NSString *)_type
38 attribute:(EOAttribute *)_attribute
41 // TODO: this method looks slow
42 // example type: "VARCHAR(4000)"
43 static NSStringEncoding enc = 0;
48 if ((len = [self length]) == 0)
52 enc = [NSString defaultCStringEncoding];
53 NSLog(@"Note: PostgreSQL72 adaptor using '%@' encoding for data=>string "
55 [NSString localizedNameOfStringEncoding:enc]);
58 str = [[NSString alloc] initWithData:self encoding:enc];
61 NSLog(@"Note: made string (len=%i) for data (len=%i), type %@",
62 [str length], [self length], _type);
65 if ((len = [_type length]) == 0) {
66 NSLog(@"WARNING(%s): missing type for data=>string conversion!",
68 return [str autorelease];
71 c1 = [_type characterAtIndex:0];
73 case 'c': case 'C': // char
74 case 'v': case 'V': // varchar
75 case 'm': case 'M': // money
76 case 't': case 'T': // text
77 t = [_type lowercaseString];
78 if ([t hasPrefix:@"char"] ||
79 [t hasPrefix:@"varchar"] ||
80 [t hasPrefix:@"money"] ||
81 [t hasPrefix:@"text"]) {
82 if (doDebug) NSLog(@" converting type: %@", t);
83 t = [[str stringValueForPostgreSQLType:_type
84 attribute:_attribute] copy];
86 if (doDebug) NSLog(@" result len %i", [t length]);
87 return [t autorelease];
91 NSLog(@"WARNING(%s): no processing of type '%@' for "
92 @"data=>string conversion!",
93 __PRETTY_FUNCTION__, _type);
94 return [str autorelease];;
97 @end /* NSData(PostgreSQL72Values) */