4 Copyright (C) 2003-2005 SKYRIX Software AG
6 Author: Helge Hess (helge.hess@skyrix.com)
8 This file is part of the MySQL4 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 #include "MySQL4Values.h"
27 #include "MySQL4Channel.h"
28 #import <Foundation/NSData.h>
31 @implementation NSData(MySQL4Values)
33 - (id)initWithMySQL4Type:(int)_type value:(const void *)_v length:(int)_len {
34 // Note: never used on lF (NSTemporaryString!)
40 return [self initWithBytes:_v length:_len];
44 - (id)initWithMySQL4Int:(int)_value {
45 return [self initWithBytes:&_value length:sizeof(int)];
47 - (id)initWithMySQL4Double:(double)_value {
48 return [self initWithBytes:&_value length:sizeof(double)];
50 - (id)initWithMySQL4Text:(const unsigned char *)_value {
51 return [self initWithBytes:_value length:strlen((char *)_value)];
53 - (id)initWithMySQL4Data:(const void *)_value length:(int)_length {
54 return [self initWithBytes:_value length:_length];
58 - (NSString *)stringValueForMySQL4Type:(NSString *)_type
59 attribute:(EOAttribute *)_attribute
62 // TODO: this method looks slow
63 static NSStringEncoding enc = 0;
68 if ((len = [self length]) == 0)
72 enc = [NSString defaultCStringEncoding];
73 NSLog(@"Note: MySQL4 adaptor using '%@' encoding for data=>string "
75 [NSString localizedNameOfStringEncoding:enc]);
78 str = [[NSString alloc] initWithData:self encoding:enc];
80 if (((len = [_type length]) == 0) || (len != 4 && len != 5 && len != 7))
81 return [str autorelease];
83 c1 = [_type characterAtIndex:0];
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];
97 return [t autorelease];
101 return [str autorelease];;
104 @end /* NSData(MySQL4Values) */