]> err.no Git - sope/blob - sope-gdl1/SQLite3/NSNumber+SQLiteVal.m
fixed fetch issue
[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 - (id)initWithSQLiteInt:(int)_value {
33   return [self initWithInt:_value];
34 }
35 - (id)initWithSQLiteDouble:(double)_value {
36   return [self initWithDouble:_value];
37 }
38 - (id)initWithSQLiteText:(const unsigned char *)_value {
39   return index(_value, '.') != NULL
40     ? [self initWithDouble:atof(_value)]
41     : [self initWithInt:atoi(_value)];
42 }
43
44 - (id)initWithSQLiteData:(const void *)_value length:(int)_length {
45   switch (_length) {
46   case 1: return [self initWithUnsignedChar:*(char *)_value];
47   case 2: return [self initWithShort:*(short *)_value];
48   case 4: return [self initWithInt:*(int *)_value];
49   case 8: return [self initWithDouble:*(double *)_value];
50   }
51   
52   [self release];
53   return nil;
54 }
55
56 - (NSString *)stringValueForSQLite3Type:(NSString *)_type
57   attribute:(EOAttribute *)_attribute
58 {
59   // TODO: can we avoid the lowercaseString?
60   unsigned len;
61   unichar  c1;
62   
63   if ((len = [_type length]) == 0)
64     return [self stringValue];
65   if (len < 4)
66     return [self stringValue];
67
68   c1 = [_type characterAtIndex:0];
69   switch (c1) {
70   case 'b': case 'B':
71     if (![[_type lowercaseString] hasPrefix:@"bool"])
72       break;
73     return [self boolValue] ? @"true" : @"false";
74     
75   case 'm': case 'M': {
76     if (![[_type lowercaseString] hasPrefix:@"money"])
77       break;
78     return [@"$" stringByAppendingString:[self stringValue]];
79   }
80   
81   case 'c': case 'C':
82   case 't': case 'T':
83   case 'v': case 'V': {
84     static NSMutableString *ms = nil; // reuse mstring, THREAD
85     
86     _type = [_type lowercaseString];
87     if (!([_type hasPrefix:@"char"] ||
88           [_type hasPrefix:@"varchar"] ||
89           [_type hasPrefix:@"text"]))
90       break;
91     
92     // TODO: can we get this faster?!
93     if (ms == nil) ms = [[NSMutableString alloc] initWithCapacity:256];
94     [ms setString:@"'"];
95     [ms appendString:[self stringValue]];
96     [ms appendString:@"'"];
97     return [[ms copy] autorelease];
98   }
99   }
100   return [self stringValue];
101 }
102
103 @end /* NSNumber(SQLiteValues) */