~ubuntu-branches/ubuntu/edgy/sope/edgy

« back to all changes in this revision

Viewing changes to sope-gdl1/MySQL/NSNumber+MySQL4Val.m

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Ley
  • Date: 2005-08-19 16:53:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050819165331-hs683wz1osm708pw
Tags: upstream-4.4rc.2
ImportĀ upstreamĀ versionĀ 4.4rc.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#import <Foundation/NSValue.h>
 
27
#include "MySQL4Channel.h"
 
28
#include "common.h"
 
29
#include <mysql/mysql.h>
 
30
 
 
31
@implementation NSNumber(MySQL4Values)
 
32
 
 
33
- (id)initWithMySQL4Type:(int)_type value:(const void *)_v length:(int)_len {
 
34
  if (_v == NULL) {
 
35
    [self release];
 
36
    return nil;
 
37
  }
 
38
 
 
39
  switch (_type) {
 
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)];
 
43
 
 
44
  case FIELD_TYPE_LONGLONG: 
 
45
    return [self initWithLongLong:strtoll(_v, NULL, 10)];
 
46
    
 
47
  case FIELD_TYPE_FLOAT:  return [self initWithFloat:atof(_v)];
 
48
  case FIELD_TYPE_DOUBLE: return [self initWithDouble:atof(_v)];
 
49
    
 
50
  default:
 
51
    NSLog(@"ERROR(%s): unsupported MySQL type: %i (len=%d)", 
 
52
          __PRETTY_FUNCTION__, _type, _len);
 
53
    [self release];
 
54
    return nil;
 
55
  }
 
56
}
 
57
 
 
58
/* generation */
 
59
 
 
60
- (NSString *)stringValueForMySQL4Type:(NSString *)_type
 
61
  attribute:(EOAttribute *)_attribute
 
62
{
 
63
  // TODO: can we avoid the lowercaseString?
 
64
  unsigned len;
 
65
  unichar  c1;
 
66
  
 
67
  if ((len = [_type length]) == 0)
 
68
    return [self stringValue];
 
69
  if (len < 4)
 
70
    return [self stringValue];
 
71
 
 
72
  c1 = [_type characterAtIndex:0];
 
73
  switch (c1) {
 
74
  case 'b': case 'B':
 
75
    if (![[_type lowercaseString] hasPrefix:@"bool"])
 
76
      break;
 
77
    return [self boolValue] ? @"true" : @"false";
 
78
    
 
79
  case 'm': case 'M': {
 
80
    if (![[_type lowercaseString] hasPrefix:@"money"])
 
81
      break;
 
82
    return [@"$" stringByAppendingString:[self stringValue]];
 
83
  }
 
84
  
 
85
  case 'c': case 'C':
 
86
  case 't': case 'T':
 
87
  case 'v': case 'V': {
 
88
    static NSMutableString *ms = nil; // reuse mstring, THREAD
 
89
    
 
90
    _type = [_type lowercaseString];
 
91
    if (!([_type hasPrefix:@"char"] ||
 
92
          [_type hasPrefix:@"varchar"] ||
 
93
          [_type hasPrefix:@"text"]))
 
94
      break;
 
95
    
 
96
    // TODO: can we get this faster?!
 
97
    if (ms == nil) ms = [[NSMutableString alloc] initWithCapacity:256];
 
98
    [ms setString:@"'"];
 
99
    [ms appendString:[self stringValue]];
 
100
    [ms appendString:@"'"];
 
101
    return [[ms copy] autorelease];
 
102
  }
 
103
  }
 
104
  return [self stringValue];
 
105
}
 
106
 
 
107
@end /* NSNumber(MySQL4Values) */