4 Copyright (C) 1999-2005 MDlink online service center GmbH and Helge Hess
6 Author: Helge Hess (helge@mdlink.de)
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"
28 #include <mysql/mysql.h>
30 @implementation MySQL4DataTypeMappingException
32 - (id)initWithObject:(id)_obj
33 forAttribute:(EOAttribute *)_attr
34 andMySQL4Type:(NSString *)_dt
35 inChannel:(MySQL4Channel *)_channel;
38 NSString *typeName = nil;
44 typeName = [NSString stringWithFormat:@"Oid[%i]", _dt];
46 r = [NSString stringWithFormat:
47 @"mapping between %@<Class:%@> and "
48 @"MySQL4 type %@ is not supported",
50 NSStringFromClass([_obj class]),
52 ui = [NSDictionary dictionaryWithObjectsAndKeys:
58 return [self initWithName:@"DataTypeMappingNotSupported" reason:r
62 @end /* MySQL4DataTypeMappingException */
64 @implementation NSNull(MySQL4Values)
66 - (NSString *)stringValueForMySQL4Type:(NSString *)_type
67 attribute:(EOAttribute *)_attribute
72 @end /* NSNull(MySQL4Values) */
74 @implementation NSObject(MySQL4Values)
76 - (id)initWithMySQL4Type:(int)_type value:(const void *)_v length:(int)_len {
77 /* Note: called for NSTemporaryString! */
79 if (![self respondsToSelector:@selector(initWithUTF8String:)]) {
84 NSLog(@"WARNING(%s): %@ falling back to NSString for MySQL4 value"
85 @" (type %i, 0x%p, len=%d)",
86 __PRETTY_FUNCTION__, NSStringFromClass([self class]),
90 return [[NSString alloc] initWithMySQL4Type:_type value:_v length:_len];
93 /* we assume NSTemporaryString here */
97 case FIELD_TYPE_TINY_BLOB:
98 case FIELD_TYPE_MEDIUM_BLOB:
99 case FIELD_TYPE_LONG_BLOB:
103 /* we always fallback to the UTF-8 string ... */
104 return [(NSString *)self initWithUTF8String:_v];
109 - (id)initWithMySQL4Int:(int)_value {
110 if ([self respondsToSelector:@selector(initWithInt:)])
111 return [(NSNumber *)self initWithInt:_value];
113 if ([self respondsToSelector:@selector(initWithDouble:)])
114 return [(NSNumber *)self initWithDouble:_value];
116 if ([self respondsToSelector:@selector(initWithString:)]) {
120 sprintf(buf, "%i", _value);
121 s = [[NSString alloc] initWithCString:buf];
122 self = [(NSString *)self initWithString:s];
131 - (id)initWithMySQL4Double:(double)_value {
132 if ([self respondsToSelector:@selector(initWithDouble:)])
133 return [(NSNumber *)self initWithDouble:_value];
139 - (id)initWithMySQL4Text:(const unsigned char *)_value {
140 if ([self respondsToSelector:@selector(initWithString:)]) {
143 s = [[NSString alloc] initWithUTF8String:_value];
144 self = [(NSString *)self initWithString:s];
153 - (id)initWithMySQL4Data:(const void *)_data length:(int)_length {
154 if ([self respondsToSelector:@selector(initWithBytes:length:)])
155 return [(NSData *)self initWithBytes:_data length:_length];
157 if ([self respondsToSelector:@selector(initWithData:)]) {
160 d = [[NSData alloc] initWithBytes:_data length:_length];
161 self = [(NSData *)self initWithData:d];
171 @end /* NSObject(MySQL4Values) */
173 void __link_MySQL4Values() {
174 // used to force linking of object file
175 __link_MySQL4Values();