]> err.no Git - sope/blob - sope-gdl1/PostgreSQL/NSData+PGVal.m
fixed a Tiger warning
[sope] / sope-gdl1 / PostgreSQL / NSData+PGVal.m
1 // $Id: NSData+PGVal.m 1 2004-08-20 10:38:46Z znek $
2
3 #import <Foundation/NSData.h>
4 #include "PostgreSQL72Values.h"
5 #include "PostgreSQL72Channel.h"
6 #include "common.h"
7
8 @implementation NSData(PostgreSQL72Values)
9
10 static BOOL   doDebug    = NO;
11 static NSData *EmptyData = nil;
12
13 + (id)valueFromCString:(const char *)_cstr length:(int)_length
14   postgreSQLType:(NSString *)_type
15   attribute:(EOAttribute *)_attribute
16   adaptorChannel:(PostgreSQL72Channel *)_channel
17 {
18   if (_length == 0) {
19     if (EmptyData == nil) EmptyData = [[NSData alloc] init];
20     return EmptyData;
21   }
22   return [[[self alloc] initWithBytes:_cstr length:_length] autorelease];
23 }
24
25 + (id)valueFromBytes:(const void *)_bytes length:(int)_length
26   postgreSQLType:(NSString *)_type
27   attribute:(EOAttribute *)_attribute
28   adaptorChannel:(PostgreSQL72Channel *)_channel
29 {
30   if (_length == 0) {
31     if (EmptyData == nil) EmptyData = [[NSData alloc] init];
32     return EmptyData;
33   }
34   return [[[self alloc] initWithBytes:_bytes length:_length] autorelease];
35 }
36
37 - (NSString *)stringValueForPostgreSQLType:(NSString *)_type
38   attribute:(EOAttribute *)_attribute
39 {
40   // TODO: UNICODE
41   // TODO: this method looks slow
42   // example type: "VARCHAR(4000)"
43   static NSStringEncoding enc = 0;
44   NSString *str, *t;
45   unsigned len;
46   unichar  c1;
47   
48   if ((len = [self length]) == 0)
49     return @"";
50   
51   if (enc == 0) {
52     enc = [NSString defaultCStringEncoding];
53     NSLog(@"Note: PostgreSQL72 adaptor using '%@' encoding for data=>string "
54           @"conversion.",
55           [NSString localizedNameOfStringEncoding:enc]);
56   }
57   
58   str = [[NSString alloc] initWithData:self encoding:enc];
59
60   if (doDebug) {
61     NSLog(@"Note: made string (len=%i) for data (len=%i), type %@",
62           [str length], [self length], _type);
63   }
64   
65   if ((len = [_type length]) == 0) {
66     NSLog(@"WARNING(%s): missing type for data=>string conversion!",
67           __PRETTY_FUNCTION__);
68     return [str autorelease];
69   }
70   
71   c1 = [_type characterAtIndex:0];
72   switch (c1) {
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];
85       [str release];
86       if (doDebug) NSLog(@"  result len %i", [t length]);
87       return [t autorelease];
88     }
89   }
90   
91   NSLog(@"WARNING(%s): no processing of type '%@' for "
92         @"data=>string conversion!",
93         __PRETTY_FUNCTION__, _type);
94   return [str autorelease];;
95 }
96
97 @end /* NSData(PostgreSQL72Values) */