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