]> err.no Git - sope/blob - sope-gdl1/MySQL/NSData+MySQL4Val.m
fixed some NGMail framework build issue
[sope] / sope-gdl1 / MySQL / NSData+MySQL4Val.m
1 /* 
2    NSData+MySQL4Val.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 MySQL4 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 "MySQL4Values.h"
27 #include "MySQL4Channel.h"
28 #import <Foundation/NSData.h>
29 #include "common.h"
30
31 @implementation NSData(MySQL4Values)
32
33 - (id)initWithMySQL4Type:(int)_type value:(const void *)_v length:(int)_len {
34   // Note: never used on lF (NSTemporaryString!)
35   if (_v == NULL) {
36     [self release];
37     return nil;
38   }
39
40   return [self initWithBytes:_v length:_len];
41 }
42
43 #if 0 // unused?
44 - (id)initWithMySQL4Int:(int)_value {
45   return [self initWithBytes:&_value length:sizeof(int)];
46 }
47 - (id)initWithMySQL4Double:(double)_value {
48   return [self initWithBytes:&_value length:sizeof(double)];
49 }
50 - (id)initWithMySQL4Text:(const unsigned char *)_value {
51   return [self initWithBytes:_value length:strlen((char *)_value)];
52 }
53 - (id)initWithMySQL4Data:(const void *)_value length:(int)_length {
54   return [self initWithBytes:_value length:_length];
55 }
56 #endif
57
58 - (NSString *)stringValueForMySQL4Type:(NSString *)_type
59   attribute:(EOAttribute *)_attribute
60 {
61   // TODO: UNICODE
62   // TODO: this method looks slow
63   static NSStringEncoding enc = 0;
64   NSString *str, *t;
65   unsigned len;
66   unichar  c1;
67   
68   if ((len = [self length]) == 0)
69     return @"";
70   
71   if (enc == 0) {
72     enc = [NSString defaultCStringEncoding];
73     NSLog(@"Note: MySQL4 adaptor using '%@' encoding for data=>string "
74           @"conversion.",
75           [NSString localizedNameOfStringEncoding:enc]);
76   }
77   
78   str = [[NSString alloc] initWithData:self encoding:enc];
79   
80   if (((len = [_type length]) == 0) || (len != 4 && len != 5 && len != 7))
81     return [str autorelease];
82
83   c1 = [_type characterAtIndex:0];
84   switch (c1) {
85   case 'c': case 'C':
86   case 'v': case 'V':
87   case 'm': case 'M':
88   case 't': case 'T':
89     t = [_type lowercaseString];
90     if ([t hasPrefix:@"char"]    ||
91         [t hasPrefix:@"varchar"] ||
92         [t hasPrefix:@"money"]   ||
93         [t hasPrefix:@"text"]) {
94       t = [[str stringValueForMySQL4Type:_type 
95                 attribute:_attribute] retain];
96       [str release];
97       return [t autorelease];
98     }
99   }
100   
101   return [str autorelease];;
102 }
103
104 @end /* NSData(MySQL4Values) */