]> err.no Git - sope/blob - sope-gdl1/SQLite3/NSNumber+SQLiteVal.m
fixed gdltest
[sope] / sope-gdl1 / SQLite3 / NSNumber+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 #import <Foundation/NSString.h>
27 #include "SQLiteChannel.h"
28 #include "common.h"
29
30 @implementation NSNumber(SQLiteValues)
31   
32 static Class    NSNumberClass = Nil;
33 static NSNumber *yesNum = nil;
34 static NSNumber *noNum  = nil;
35
36 + (id)valueFromCString:(const char *)_cstr length:(int)_length
37   sqlite3Type:(NSString *)_type
38   attribute:(EOAttribute *)_attribute
39   adaptorChannel:(SQLiteChannel *)_channel
40 {
41   // TODO: can we avoid the lowercaseString?
42   unsigned len;
43   unichar  c1;
44
45   if ((len = [_type length]) == 0)
46     return nil;
47
48   if (NSNumberClass == Nil) NSNumberClass = [NSNumber class];
49   
50   c1 = [_type characterAtIndex:0];
51   switch (c1) {
52   case 'f': case 'F': {
53     if (len < 5)
54       break;
55     if ([[_type lowercaseString] hasPrefix:@"float"])
56       return [NSNumberClass numberWithDouble:atof(_cstr)];
57     break;
58   }
59   case 's': case 'S': {
60     if (len < 8)
61       break;
62     if ([[_type lowercaseString] hasPrefix:@"smallint"])
63       return [NSNumberClass numberWithShort:atoi(_cstr)];
64     break;
65   }
66   case 'i': case 'I': {
67     if (len < 3)
68       break;
69     if ([[_type lowercaseString] hasPrefix:@"int"])
70       return [NSNumberClass numberWithInt:atoi(_cstr)];
71   }
72   case 'b': case 'B': {
73     if (len < 4)
74       break;
75     if (![[_type lowercaseString] hasPrefix:@"bool"])
76       break;
77     
78     if (yesNum == nil) yesNum = [[NSNumberClass numberWithBool:YES] retain];
79     if (noNum  == nil) noNum  = [[NSNumberClass numberWithBool:NO]  retain];
80     
81     if (_length == 0)
82       return noNum;
83     
84     switch (*_cstr) {
85     case 't': case 'T':
86     case 'y': case 'Y':
87     case '1':
88       return yesNum;
89     default:
90       return noNum;
91     }
92   }
93   }
94   return nil;
95 }
96
97 + (id)valueFromBytes:(const void *)_bytes length:(int)_length
98   sqlite3Type:(NSString *)_type
99   attribute:(EOAttribute *)_attribute
100   adaptorChannel:(SQLiteChannel *)_channel
101 {
102   return [self notImplemented:_cmd];
103 }
104
105 - (NSString *)stringValueForSQLite3Type:(NSString *)_type
106   attribute:(EOAttribute *)_attribute
107 {
108   // TODO: can we avoid the lowercaseString?
109   unsigned len;
110   unichar  c1;
111   
112   if ((len = [_type length]) == 0)
113     return [self stringValue];
114   if (len < 4)
115     return [self stringValue];
116
117   c1 = [_type characterAtIndex:0];
118   switch (c1) {
119   case 'b': case 'B':
120     if (![[_type lowercaseString] hasPrefix:@"bool"])
121       break;
122     return [self boolValue] ? @"true" : @"false";
123     
124   case 'm': case 'M': {
125     if (![[_type lowercaseString] hasPrefix:@"money"])
126       break;
127     return [@"$" stringByAppendingString:[self stringValue]];
128   }
129   
130   case 'c': case 'C':
131   case 't': case 'T':
132   case 'v': case 'V': {
133     static NSMutableString *ms = nil; // reuse mstring, THREAD
134     
135     _type = [_type lowercaseString];
136     if (!([_type hasPrefix:@"char"] ||
137           [_type hasPrefix:@"varchar"] ||
138           [_type hasPrefix:@"text"]))
139       break;
140     
141     // TODO: can we get this faster?!
142     if (ms == nil) ms = [[NSMutableString alloc] initWithCapacity:256];
143     [ms setString:@"'"];
144     [ms appendString:[self stringValue]];
145     [ms appendString:@"'"];
146     return [[ms copy] autorelease];
147   }
148   }
149   return [self stringValue];
150 }
151
152 @end /* NSNumber(SQLiteValues) */