]> err.no Git - sope/blob - sope-gdl1/SQLite3/NSData+SQLiteVal.m
fixed for new -describeResults: API
[sope] / sope-gdl1 / SQLite3 / NSData+SQLiteVal.m
1 /* 
2    NSData+SQLiteVal.m
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 "SQLiteValues.h"
27 #include "SQLiteChannel.h"
28 #import <Foundation/NSData.h>
29 #include "common.h"
30
31 @implementation NSData(SQLiteValues)
32
33 - (id)initWithSQLiteInt:(int)_value {
34   return [self initWithBytes:&_value length:sizeof(int)];
35 }
36 - (id)initWithSQLiteDouble:(double)_value {
37   return [self initWithBytes:&_value length:sizeof(double)];
38 }
39 - (id)initWithSQLiteText:(const unsigned char *)_value {
40   return [self initWithBytes:_value length:strlen(_value)];
41 }
42 - (id)initWithSQLiteData:(const void *)_value length:(int)_length {
43   return [self initWithBytes:_value length:_length];
44 }
45
46 - (NSString *)stringValueForSQLite3Type:(NSString *)_type
47   attribute:(EOAttribute *)_attribute
48 {
49   // TODO: UNICODE
50   // TODO: this method looks slow
51   static NSStringEncoding enc = 0;
52   NSString *str, *t;
53   unsigned len;
54   unichar  c1;
55   
56   if ((len = [self length]) == 0)
57     return @"";
58   
59   if (enc == 0) {
60     enc = [NSString defaultCStringEncoding];
61     NSLog(@"Note: SQLite adaptor using '%@' encoding for data=>string "
62           @"conversion.",
63           [NSString localizedNameOfStringEncoding:enc]);
64   }
65   
66   str = [[NSString alloc] initWithData:self encoding:enc];
67   
68   if (((len = [_type length]) == 0) || (len != 4 && len != 5 && len != 7))
69     return [str autorelease];
70
71   c1 = [_type characterAtIndex:0];
72   switch (c1) {
73   case 'c': case 'C':
74   case 'v': case 'V':
75   case 'm': case 'M':
76   case 't': case 'T':
77     t = [_type lowercaseString];
78     if ([t hasPrefix:@"char"]    ||
79         [t hasPrefix:@"varchar"] ||
80         [t hasPrefix:@"money"]   ||
81         [t hasPrefix:@"text"]) {
82       t = [[str stringValueForSQLite3Type:_type 
83                 attribute:_attribute] retain];
84       [str release];
85       return [t autorelease];
86     }
87   }
88   
89   return [str autorelease];;
90 }
91
92 @end /* NSData(SQLiteValues) */