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
#import <Foundation/NSValue.h>
27
#include "MySQL4Channel.h"
29
#include <mysql/mysql.h>
31
@implementation NSNumber(MySQL4Values)
33
- (id)initWithMySQL4Type:(int)_type value:(const void *)_v length:(int)_len {
40
case FIELD_TYPE_TINY: return [self initWithChar:atoi(_v)];
41
case FIELD_TYPE_SHORT: return [self initWithShort:atoi(_v)];
42
case FIELD_TYPE_LONG: return [self initWithLong:strtol(_v, NULL, 10)];
44
case FIELD_TYPE_LONGLONG:
45
return [self initWithLongLong:strtoll(_v, NULL, 10)];
47
case FIELD_TYPE_FLOAT: return [self initWithFloat:atof(_v)];
48
case FIELD_TYPE_DOUBLE: return [self initWithDouble:atof(_v)];
51
NSLog(@"ERROR(%s): unsupported MySQL type: %i (len=%d)",
52
__PRETTY_FUNCTION__, _type, _len);
60
- (NSString *)stringValueForMySQL4Type:(NSString *)_type
61
attribute:(EOAttribute *)_attribute
63
// TODO: can we avoid the lowercaseString?
67
if ((len = [_type length]) == 0)
68
return [self stringValue];
70
return [self stringValue];
72
c1 = [_type characterAtIndex:0];
75
if (![[_type lowercaseString] hasPrefix:@"bool"])
77
return [self boolValue] ? @"true" : @"false";
80
if (![[_type lowercaseString] hasPrefix:@"money"])
82
return [@"$" stringByAppendingString:[self stringValue]];
88
static NSMutableString *ms = nil; // reuse mstring, THREAD
90
_type = [_type lowercaseString];
91
if (!([_type hasPrefix:@"char"] ||
92
[_type hasPrefix:@"varchar"] ||
93
[_type hasPrefix:@"text"]))
96
// TODO: can we get this faster?!
97
if (ms == nil) ms = [[NSMutableString alloc] initWithCapacity:256];
99
[ms appendString:[self stringValue]];
100
[ms appendString:@"'"];
101
return [[ms copy] autorelease];
104
return [self stringValue];
107
@end /* NSNumber(MySQL4Values) */