]> err.no Git - sope/blob - sope-gdl1/MySQL/MySQL4Values.m
use %p for pointer formats
[sope] / sope-gdl1 / MySQL / MySQL4Values.m
1 /* 
2    MySQL4Values.m
3
4    Copyright (C) 1999-2005 MDlink online service center GmbH and Helge Hess
5
6    Author: Helge Hess (helge@mdlink.de)
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 "common.h"
28 #include <mysql/mysql.h>
29
30 @implementation MySQL4DataTypeMappingException
31
32 - (id)initWithObject:(id)_obj
33   forAttribute:(EOAttribute *)_attr
34   andMySQL4Type:(NSString *)_dt
35   inChannel:(MySQL4Channel *)_channel;
36 {  
37   NSDictionary *ui;
38   NSString *typeName = nil;
39   NSString *r;
40
41   typeName = _dt;
42
43   if (typeName == nil)
44     typeName = [NSString stringWithFormat:@"Oid[%i]", _dt];
45
46   r = [NSString stringWithFormat:
47                               @"mapping between %@<Class:%@> and "
48                               @"MySQL4 type %@ is not supported",
49                               [_obj description],
50                               NSStringFromClass([_obj class]),
51                 typeName];
52   ui = [NSDictionary dictionaryWithObjectsAndKeys:
53                                     _attr,    @"attribute",
54                                     _channel, @"channel",
55                                     _obj,     @"object",
56                      nil];
57   
58   return [self initWithName:@"DataTypeMappingNotSupported" reason:r
59                userInfo:ui];
60 }
61
62 @end /* MySQL4DataTypeMappingException */
63
64 @implementation NSNull(MySQL4Values)
65
66 - (NSString *)stringValueForMySQL4Type:(NSString *)_type
67   attribute:(EOAttribute *)_attribute
68 {
69   return @"null";
70 }
71
72 @end /* NSNull(MySQL4Values) */
73
74 @implementation NSObject(MySQL4Values)
75
76 - (id)initWithMySQL4Type:(int)_type value:(const void *)_v length:(int)_len {
77   /* Note: called for NSTemporaryString! */
78
79   if (![self respondsToSelector:@selector(initWithUTF8String:)]) {
80     if (_v == NULL) {
81       [self release];
82       return nil;
83     }
84     NSLog(@"WARNING(%s): %@ falling back to NSString for MySQL4 value"
85           @" (type %i, 0x%p, len=%d)",
86           __PRETTY_FUNCTION__, NSStringFromClass([self class]),
87           _type, _v, _len);
88     
89     [self release];
90     return [[NSString alloc] initWithMySQL4Type:_type value:_v length:_len];
91   }
92
93   /* we assume NSTemporaryString here */
94   
95   switch (_type) {
96   case FIELD_TYPE_BLOB:
97   case FIELD_TYPE_TINY_BLOB:
98   case FIELD_TYPE_MEDIUM_BLOB:
99   case FIELD_TYPE_LONG_BLOB:
100     ; /* fall through */
101     
102   default:
103     /* we always fallback to the UTF-8 string ... */
104     return [(NSString *)self initWithUTF8String:_v];
105   }
106 }
107
108 #if 0
109 - (id)initWithMySQL4Int:(int)_value {
110   if ([self respondsToSelector:@selector(initWithInt:)])
111     return [(NSNumber *)self initWithInt:_value];
112   
113   if ([self respondsToSelector:@selector(initWithDouble:)])
114     return [(NSNumber *)self initWithDouble:_value];
115   
116   if ([self respondsToSelector:@selector(initWithString:)]) {
117     NSString *s;
118     char buf[256];
119
120     sprintf(buf, "%i", _value);
121     s = [[NSString alloc] initWithCString:buf];
122     self = [(NSString *)self initWithString:s];
123     [s release];
124     return self;
125   }
126   
127   [self release];
128   return nil;
129 }
130
131 - (id)initWithMySQL4Double:(double)_value {
132   if ([self respondsToSelector:@selector(initWithDouble:)])
133     return [(NSNumber *)self initWithDouble:_value];
134   
135   [self release];
136   return nil;
137 }
138
139 - (id)initWithMySQL4Text:(const unsigned char *)_value {
140   if ([self respondsToSelector:@selector(initWithString:)]) {
141     NSString *s;
142     
143     s = [[NSString alloc] initWithUTF8String:_value];
144     self = [(NSString *)self initWithString:s];
145     [s release];
146     return self;
147   }
148   
149   [self release];
150   return nil;
151 }
152
153 - (id)initWithMySQL4Data:(const void *)_data length:(int)_length {
154   if ([self respondsToSelector:@selector(initWithBytes:length:)])
155     return [(NSData *)self initWithBytes:_data length:_length];
156   
157   if ([self respondsToSelector:@selector(initWithData:)]) {
158     NSData *d;
159     
160     d = [[NSData alloc] initWithBytes:_data length:_length];
161     self = [(NSData *)self initWithData:d];
162     [d release];
163     return self;
164   }
165   
166   [self release];
167   return nil;
168 }
169 #endif
170
171 @end /* NSObject(MySQL4Values) */
172
173 void __link_MySQL4Values() {
174   // used to force linking of object file
175   __link_MySQL4Values();
176 }