]> err.no Git - sope/blob - sope-gdl1/SQLite3/NSString+SQLiteVal.m
basic implementation is working now
[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 EOExprClass = Nil;
33
34 - (id)initWithSQLiteInt:(int)_value {
35   char buf[256];
36   sprintf(buf, "%i", _value);
37   return [self initWithCString:buf];
38 }
39 - (id)initWithSQLiteDouble:(double)_value {
40   char buf[256];
41   sprintf(buf, "%g", _value);
42   return [self initWithCString:buf];
43 }
44
45 - (id)initWithSQLiteText:(const unsigned char *)_value {
46   return [self initWithUTF8String:_value];
47 }
48
49 - (id)initWithSQLiteData:(const void *)_value length:(int)_length {
50   NSData *d;
51   
52   d = [[NSData alloc] initWithBytes:_value length:_length];
53   self = [self initWithData:d encoding:NSUTF8StringEncoding];
54   [d release];
55   return self;
56 }
57
58 - (NSString *)stringValueForSQLite3Type:(NSString *)_type
59   attribute:(EOAttribute *)_attribute
60 {
61   // TODO: all this looks slow ...
62   unsigned len;
63   unichar  c1;
64   
65   if ((len = [_type length]) == 0)
66     return self;
67   
68   c1 = [_type characterAtIndex:0];
69   switch (c1) {
70   case 'c': case 'C':
71   case 'v': case 'V':
72   case 't': case 'T': {
73     NSString *s;
74     id expr;
75     
76     if (len < 4)
77       return self;
78     
79     _type = [_type lowercaseString];
80   
81     if (!([_type hasPrefix:@"char"] ||
82         [_type hasPrefix:@"varchar"] ||
83         [_type hasPrefix:@"text"]))
84       break;
85     
86     /* TODO: creates too many autoreleased strings :-( */
87       
88     expr = [self stringByReplacingString:@"\\" withString:@"\\\\"];
89       
90     if (EOExprClass == Nil) EOExprClass = [EOQuotedExpression class];
91     expr = [[EOExprClass alloc] initWithExpression:expr 
92                                 quote:@"'" escape:@"\\'"];
93     s = [[expr expressionValueForContext:nil] retain];
94     [expr release];
95     return [s autorelease];
96   }
97   case 'm': case 'M': {
98     if (len < 5) {
99       if ([[_type lowercaseString] hasPrefix:@"money"])
100         return [@"$" stringByAppendingString:self];
101     }
102     break;
103   }
104   }
105   return self;
106 }
107
108 @end /* NSString(SQLiteValues) */