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 "MySQL4Channel.h"
27 #include <NGExtensions/NSString+Ext.h>
28 #import <Foundation/NSString.h>
30 #include <mysql/mysql.h>
32 @implementation NSString(MySQL4Values)
34 static Class EOExprClass = Nil;
36 - (id)initWithMySQL4Type:(int)_type value:(const void *)_v length:(int)_len {
37 // Note: never used on lF (NSTemporaryString!)
45 case FIELD_TYPE_TINY_BLOB:
46 case FIELD_TYPE_MEDIUM_BLOB:
47 case FIELD_TYPE_LONG_BLOB:
51 /* we always fallback to the UTF-8 string ... */
52 return [self initWithUTF8String:_v];
57 - (id)initWithMySQL4Int:(int)_value {
59 sprintf(buf, "%i", _value);
60 return [self initWithCString:buf];
62 - (id)initWithMySQL4Double:(double)_value {
64 sprintf(buf, "%g", _value);
65 return [self initWithCString:buf];
68 - (id)initWithMySQL4Text:(const unsigned char *)_value {
69 return [self initWithUTF8String:_value];
72 - (id)initWithMySQL4Data:(const void *)_value length:(int)_length {
75 d = [[NSData alloc] initWithBytes:_value length:_length];
76 self = [self initWithData:d encoding:NSUTF8StringEncoding];
82 /* generate SQL value */
84 - (NSString *)stringValueForMySQL4Type:(NSString *)_type
85 attribute:(EOAttribute *)_attribute
87 // TODO: all this looks slow ...
91 if ((len = [_type length]) == 0)
94 c1 = [_type characterAtIndex:0];
96 case 'c': case 'C': // char
97 case 'v': case 'V': // varchar
98 case 't': case 'T': { // text
105 _type = [_type lowercaseString];
107 if (!([_type hasPrefix:@"char"] ||
108 [_type hasPrefix:@"varchar"] ||
109 [_type hasPrefix:@"text"]))
112 /* TODO: creates too many autoreleased strings :-( */
114 expr = [self stringByReplacingString:@"\\" withString:@"\\\\"];
116 if (EOExprClass == Nil) EOExprClass = [EOQuotedExpression class];
117 expr = [[EOExprClass alloc] initWithExpression:expr
118 quote:@"'" escape:@"\\'"];
119 s = [[(EOQuotedExpression *)expr expressionValueForContext:nil] retain];
121 return [s autorelease];
123 case 'i': case 'I': { // int
125 sprintf(buf, "%i", [self intValue]);
126 return [NSString stringWithCString:buf];
129 NSLog(@"WARNING(%s): return string as is for type %@",
130 __PRETTY_FUNCTION__, _type);
136 @end /* NSString(MySQL4Values) */