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

« back to all changes in this revision

Viewing changes to sope-gdl1/PostgreSQL/NSString+PGVal.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
   PostgreSQL72Channel.h
 
3
 
 
4
   Copyright (C) 1999 MDlink online service center GmbH and Helge Hess
 
5
   Copyright (C) 2000-2004 SKYRIX Software AG and Helge Hess
 
6
 
 
7
   Author: Helge Hess (helge.hess@opengroupware.org)
 
8
 
 
9
   This file is part of the PostgreSQL72 Adaptor Library
 
10
 
 
11
   This library is free software; you can redistribute it and/or
 
12
   modify it under the terms of the GNU Library General Public
 
13
   License as published by the Free Software Foundation; either
 
14
   version 2 of the License, or (at your option) any later version.
 
15
 
 
16
   This library is distributed in the hope that it will be useful,
 
17
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
19
   Library General Public License for more details.
 
20
 
 
21
   You should have received a copy of the GNU Library General Public
 
22
   License along with this library; see the file COPYING.LIB.
 
23
   If not, write to the Free Software Foundation,
 
24
   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
25
*/
 
26
 
 
27
#include "PostgreSQL72Channel.h"
 
28
#include "common.h"
 
29
 
 
30
@implementation NSString(PostgreSQL72Values)
 
31
 
 
32
static Class NSStringClass = Nil;
 
33
static Class EOExprClass   = Nil;
 
34
 
 
35
+ (id)valueFromCString:(const char *)_cstr length:(int)_length
 
36
  postgreSQLType:(NSString *)_type
 
37
  attribute:(EOAttribute *)_attribute
 
38
  adaptorChannel:(PostgreSQL72Channel *)_channel
 
39
{
 
40
  if (_cstr  == NULL) return nil;
 
41
  if (*_cstr == '\0') return @"";
 
42
  if (NSStringClass == Nil) NSStringClass = [NSString class];
 
43
 
 
44
  // TODO: cache IMP of selector
 
45
  return [NSStringClass stringWithCString:_cstr];
 
46
}
 
47
 
 
48
+ (id)valueFromBytes:(const void *)_bytes length:(int)_length
 
49
  postgreSQLType:(NSString *)_type
 
50
  attribute:(EOAttribute *)_attribute
 
51
  adaptorChannel:(PostgreSQL72Channel *)_channel
 
52
{
 
53
#if COCOA_Foundation_LIBRARY || NeXT_Foundation_LIBRARY
 
54
  NSLog(@"%s: not implemented!", __PRETTY_FUNCTION__);
 
55
  return nil;
 
56
#else
 
57
  return [self notImplemented:_cmd];
 
58
#endif
 
59
}
 
60
 
 
61
- (NSString *)stringValueForPostgreSQLType:(NSString *)_type
 
62
  attribute:(EOAttribute *)_attribute
 
63
{
 
64
  // TODO: all this looks slow ...
 
65
  unsigned len;
 
66
  unichar  c1;
 
67
  
 
68
  if ((len = [_type length]) == 0)
 
69
    return self;
 
70
  
 
71
  c1 = [_type characterAtIndex:0];
 
72
  switch (c1) {
 
73
  case 'c': case 'C':
 
74
  case 'v': case 'V':
 
75
  case 't': case 'T': {
 
76
    NSString           *s;
 
77
    EOQuotedExpression *expr;
 
78
    
 
79
    if (len < 4)
 
80
      return self;
 
81
    
 
82
    _type = [_type lowercaseString];
 
83
  
 
84
    if (!([_type hasPrefix:@"char"] ||
 
85
        [_type hasPrefix:@"varchar"] ||
 
86
        [_type hasPrefix:@"text"]))
 
87
      break;
 
88
    
 
89
    /* TODO: creates too many autoreleased strings :-( */
 
90
    
 
91
    s = [self stringByReplacingString:@"\\" withString:@"\\\\"];
 
92
    
 
93
    if (EOExprClass == Nil) EOExprClass = [EOQuotedExpression class];
 
94
    expr = [[EOExprClass alloc] initWithExpression:s quote:@"'" escape:@"\\'"];
 
95
    s = [[expr expressionValueForContext:nil] retain];
 
96
    [expr release];
 
97
    return [s autorelease];
 
98
  }
 
99
  case 'm': case 'M': {
 
100
    if (len < 5) {
 
101
      if ([[_type lowercaseString] hasPrefix:@"money"])
 
102
        return [@"$" stringByAppendingString:self];
 
103
    }
 
104
    break;
 
105
  }
 
106
  }
 
107
  return self;
 
108
}
 
109
 
 
110
@end /* NSString(PostgreSQL72Values) */