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