4 Copyright (C) 2003-2005 SKYRIX Software AG
6 Author: Helge Hess (helge.hess@skyrix.com)
8 This file is part of the SQLite Adaptor Library
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.
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.
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.
26 #import <Foundation/NSString.h>
27 #include "SQLiteChannel.h"
30 @implementation NSNumber(SQLiteValues)
32 static Class NSNumberClass = Nil;
33 static NSNumber *yesNum = nil;
34 static NSNumber *noNum = nil;
36 + (id)valueFromCString:(const char *)_cstr length:(int)_length
37 sqlite3Type:(NSString *)_type
38 attribute:(EOAttribute *)_attribute
39 adaptorChannel:(SQLiteChannel *)_channel
41 // TODO: can we avoid the lowercaseString?
45 if ((len = [_type length]) == 0)
48 if (NSNumberClass == Nil) NSNumberClass = [NSNumber class];
50 c1 = [_type characterAtIndex:0];
55 if ([[_type lowercaseString] hasPrefix:@"float"])
56 return [NSNumberClass numberWithDouble:atof(_cstr)];
62 if ([[_type lowercaseString] hasPrefix:@"smallint"])
63 return [NSNumberClass numberWithShort:atoi(_cstr)];
69 if ([[_type lowercaseString] hasPrefix:@"int"])
70 return [NSNumberClass numberWithInt:atoi(_cstr)];
75 if (![[_type lowercaseString] hasPrefix:@"bool"])
78 if (yesNum == nil) yesNum = [[NSNumberClass numberWithBool:YES] retain];
79 if (noNum == nil) noNum = [[NSNumberClass numberWithBool:NO] retain];
97 + (id)valueFromBytes:(const void *)_bytes length:(int)_length
98 sqlite3Type:(NSString *)_type
99 attribute:(EOAttribute *)_attribute
100 adaptorChannel:(SQLiteChannel *)_channel
102 return [self notImplemented:_cmd];
105 - (NSString *)stringValueForSQLite3Type:(NSString *)_type
106 attribute:(EOAttribute *)_attribute
108 // TODO: can we avoid the lowercaseString?
112 if ((len = [_type length]) == 0)
113 return [self stringValue];
115 return [self stringValue];
117 c1 = [_type characterAtIndex:0];
120 if (![[_type lowercaseString] hasPrefix:@"bool"])
122 return [self boolValue] ? @"true" : @"false";
124 case 'm': case 'M': {
125 if (![[_type lowercaseString] hasPrefix:@"money"])
127 return [@"$" stringByAppendingString:[self stringValue]];
132 case 'v': case 'V': {
133 static NSMutableString *ms = nil; // reuse mstring, THREAD
135 _type = [_type lowercaseString];
136 if (!([_type hasPrefix:@"char"] ||
137 [_type hasPrefix:@"varchar"] ||
138 [_type hasPrefix:@"text"]))
141 // TODO: can we get this faster?!
142 if (ms == nil) ms = [[NSMutableString alloc] initWithCapacity:256];
144 [ms appendString:[self stringValue]];
145 [ms appendString:@"'"];
146 return [[ms copy] autorelease];
149 return [self stringValue];
152 @end /* NSNumber(SQLiteValues) */