]> err.no Git - sope/blob - sope-gdl1/MySQL/NSString+MySQL4Val.m
fixed some NGMail framework build issue
[sope] / sope-gdl1 / MySQL / NSString+MySQL4Val.m
1 /* 
2    MySQL4Adaptor.h
3
4    Copyright (C) 2003-2005 SKYRIX Software AG
5
6    Author: Helge Hess (helge.hess@skyrix.com)
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 "MySQL4Channel.h"
27 #include <NGExtensions/NSString+Ext.h>
28 #import <Foundation/NSString.h>
29 #include "common.h"
30 #include <mysql/mysql.h>
31
32 @implementation NSString(MySQL4Values)
33
34 static Class EOExprClass = Nil;
35
36 - (id)initWithMySQL4Type:(int)_type value:(const void *)_v length:(int)_len {
37   // Note: never used on lF (NSTemporaryString!)
38   if (_v == NULL) {
39     [self release];
40     return nil;
41   }
42
43   switch (_type) {
44   case FIELD_TYPE_BLOB:
45   case FIELD_TYPE_TINY_BLOB:
46   case FIELD_TYPE_MEDIUM_BLOB:
47   case FIELD_TYPE_LONG_BLOB:
48     ; /* fall through */
49     
50   default:
51     /* we always fallback to the UTF-8 string ... */
52     return [self initWithUTF8String:_v];
53   }
54 }
55
56 #if 0
57 - (id)initWithMySQL4Int:(int)_value {
58   char buf[256];
59   sprintf(buf, "%i", _value);
60   return [self initWithCString:buf];
61 }
62 - (id)initWithMySQL4Double:(double)_value {
63   char buf[256];
64   sprintf(buf, "%g", _value);
65   return [self initWithCString:buf];
66 }
67
68 - (id)initWithMySQL4Text:(const unsigned char *)_value {
69   return [self initWithUTF8String:_value];
70 }
71
72 - (id)initWithMySQL4Data:(const void *)_value length:(int)_length {
73   NSData *d;
74   
75   d = [[NSData alloc] initWithBytes:_value length:_length];
76   self = [self initWithData:d encoding:NSUTF8StringEncoding];
77   [d release];
78   return self;
79 }
80 #endif
81
82 /* generate SQL value */
83
84 - (NSString *)stringValueForMySQL4Type:(NSString *)_type
85   attribute:(EOAttribute *)_attribute
86 {
87   // TODO: all this looks slow ...
88   unsigned len;
89   unichar  c1;
90   
91   if ((len = [_type length]) == 0)
92     return self;
93   
94   c1 = [_type characterAtIndex:0];
95   switch (c1) {
96   case 'c': case 'C': // char
97   case 'v': case 'V': // varchar
98   case 't': case 'T': { // text
99     NSString *s;
100     id expr;
101     
102     if (len < 4)
103       return self;
104     
105     _type = [_type lowercaseString];
106   
107     if (!([_type hasPrefix:@"char"] ||
108         [_type hasPrefix:@"varchar"] ||
109         [_type hasPrefix:@"text"]))
110       break;
111     
112     /* TODO: creates too many autoreleased strings :-( */
113       
114     expr = [self stringByReplacingString:@"\\" withString:@"\\\\"];
115       
116     if (EOExprClass == Nil) EOExprClass = [EOQuotedExpression class];
117     expr = [[EOExprClass alloc] initWithExpression:expr 
118                                 quote:@"'" escape:@"\\'"];
119     s = [[(EOQuotedExpression *)expr expressionValueForContext:nil] retain];
120     [expr release];
121     return [s autorelease];
122   }
123   case 'i': case 'I': { // int
124     char buf[128];
125     sprintf(buf, "%i", [self intValue]);
126     return [NSString stringWithCString:buf];
127   }
128   default:
129     NSLog(@"WARNING(%s): return string as is for type %@", 
130           __PRETTY_FUNCTION__, _type);
131     break;
132   }
133   return self;
134 }
135
136 @end /* NSString(MySQL4Values) */