]> err.no Git - sope/blob - sope-gdl1/PostgreSQL/NSString+PGVal.m
macosx fixes
[sope] / sope-gdl1 / PostgreSQL / NSString+PGVal.m
1 /* 
2    PostgreSQL72Channel.h
3
4    Copyright (C) 1999 MDlink online service center GmbH and Helge Hess
5    Copyright (C) 2000-2004 SKYRIX Software AG and Helge Hess
6
7    Author: Helge Hess (helge.hess@opengroupware.org)
8
9    This file is part of the PostgreSQL72 Adaptor Library
10
11    This library is free software; you can redistribute it and/or
12    modify it under the terms of the GNU Library General Public
13    License as published by the Free Software Foundation; either
14    version 2 of the License, or (at your option) any later version.
15
16    This library is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    Library General Public License for more details.
20
21    You should have received a copy of the GNU Library General Public
22    License along with this library; see the file COPYING.LIB.
23    If not, write to the Free Software Foundation,
24    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27 #include "PostgreSQL72Channel.h"
28 #include <NGExtensions/NSString+Ext.h>
29 #include "common.h"
30
31 @implementation NSString(PostgreSQL72Values)
32
33 static Class NSStringClass = Nil;
34 static Class EOExprClass   = Nil;
35
36 + (id)valueFromCString:(const char *)_cstr length:(int)_length
37   postgreSQLType:(NSString *)_type
38   attribute:(EOAttribute *)_attribute
39   adaptorChannel:(PostgreSQL72Channel *)_channel
40 {
41   if (_cstr  == NULL) return nil;
42   if (*_cstr == '\0') return @"";
43   if (NSStringClass == Nil) NSStringClass = [NSString class];
44
45   // TODO: cache IMP of selector
46   return [NSStringClass stringWithCString:_cstr];
47 }
48
49 + (id)valueFromBytes:(const void *)_bytes length:(int)_length
50   postgreSQLType:(NSString *)_type
51   attribute:(EOAttribute *)_attribute
52   adaptorChannel:(PostgreSQL72Channel *)_channel
53 {
54 #if COCOA_Foundation_LIBRARY || NeXT_Foundation_LIBRARY
55   NSLog(@"%s: not implemented!", __PRETTY_FUNCTION__);
56   return nil;
57 #else
58   return [self notImplemented:_cmd];
59 #endif
60 }
61
62 - (NSString *)stringValueForPostgreSQLType:(NSString *)_type
63   attribute:(EOAttribute *)_attribute
64 {
65   // TODO: all this looks slow ...
66   unsigned len;
67   unichar  c1;
68   
69   if ((len = [_type length]) == 0)
70     return self;
71   
72   c1 = [_type characterAtIndex:0];
73   switch (c1) {
74   case 'c': case 'C':
75   case 'v': case 'V':
76   case 't': case 'T': {
77     NSString           *s;
78     EOQuotedExpression *expr;
79     
80     if (len < 4)
81       return self;
82     
83     _type = [_type lowercaseString];
84   
85     if (!([_type hasPrefix:@"char"] ||
86         [_type hasPrefix:@"varchar"] ||
87         [_type hasPrefix:@"text"]))
88       break;
89     
90     /* TODO: creates too many autoreleased strings :-( */
91     
92     s = [self stringByReplacingString:@"\\" withString:@"\\\\"];
93     
94     if (EOExprClass == Nil) EOExprClass = [EOQuotedExpression class];
95     expr = [[EOExprClass alloc] initWithExpression:s quote:@"'" escape:@"\\'"];
96     s = [[expr expressionValueForContext:nil] retain];
97     [expr release];
98     return [s autorelease];
99   }
100   case 'm': case 'M': {
101     if (len < 5) {
102       if ([[_type lowercaseString] hasPrefix:@"money"])
103         return [@"$" stringByAppendingString:self];
104     }
105     break;
106   }
107   }
108   return self;
109 }
110
111 @end /* NSString(PostgreSQL72Values) */