]> err.no Git - sope/blob - sope-gdl1/PostgreSQL/EOAttribute+PostgreSQL72.m
added an ivar to WOComponent, minor cleanups
[sope] / sope-gdl1 / PostgreSQL / EOAttribute+PostgreSQL72.m
1 /* 
2    EOAttribute+PostgreSQL72.m
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 // $Id: EOAttribute+PostgreSQL72.m 1 2004-08-20 10:38:46Z znek $
27
28 #include "common.h"
29 #import "EOAttribute+PostgreSQL72.h"
30
31 #if 0
32 #include <pg_type.h>
33 #else
34 #include <postgres_types.h>
35 #endif
36 #include <sql3types.h>
37
38 // Sybase dateformat: (need to be corrected for PostgreSQL)
39 static NSString *PGSQL_DATETIME_FORMAT  = @"%b %d %Y %I:%M:%S:000%p";
40 static NSString *PGSQL_TIMESTAMP_FORMAT = @"%Y-%m-%d %H:%M:%S%z";
41
42 @implementation EOAttribute(PostgreSQL72AttributeAdditions)
43
44 - (void)loadValueClassAndTypeUsingPostgreSQLType:(Oid)_type
45   size:(int)_size
46   modification:(int)_modification
47   binary:(BOOL)_isBinary
48 {
49   if (_isBinary)
50     [self setValueClassName:@"NSData"];
51
52   switch (_type) {
53     /* where in the PostgreSQL72 headers are these OIDs defined ??? */
54   case BOOLOID:
55       [self setExternalType:@"bool"];
56       [self setValueClassName:@"NSNumber"];
57       [self setValueType:@"i"];
58       return;
59   case NAMEOID:
60       [self setExternalType:@"name"];
61       [self setValueClassName:@"NSString"];
62       return;
63   case TEXTOID:
64       [self setExternalType:@"textoid"];
65       [self setValueClassName:@"NSString"];
66       return;
67   case INT2OID:
68       [self setExternalType:@"int2"];
69       [self setValueClassName:@"NSNumber"];
70       [self setValueType:@"d"];
71       return;
72   case INT4OID:
73       [self setExternalType:@"int4"];
74       [self setValueClassName:@"NSNumber"];
75       [self setValueType:@"d"];
76       return;
77   case INT8OID:
78       [self setExternalType:@"int8"];
79       [self setValueClassName:@"NSNumber"];
80       [self setValueType:@"d"];
81       return;
82       
83     case CHAROID:
84       [self setExternalType:@"char"];
85       [self setValueClassName:@"NSString"];
86       break;
87     case VARCHAROID:
88       [self setExternalType:@"varchar"];
89       [self setValueClassName:@"NSString"];
90       break;
91     case NUMERICOID:
92       [self setExternalType:@"numeric"];
93       [self setValueClassName:@"NSNumber"];
94       [self setValueType:@"d"];
95       break;
96     case FLOAT4OID:
97       [self setExternalType:@"float4"];
98       [self setValueClassName:@"NSNumber"];
99       [self setValueType:@"f"];
100       break;
101     case FLOAT8OID:
102       [self setExternalType:@"float8"];
103       [self setValueClassName:@"NSNumber"];
104       [self setValueType:@"f"];
105       break;
106
107     case DATEOID:
108       [self setExternalType:@"datetime"];
109       [self setValueClassName:@"NSCalendarDate"];
110       [self setCalendarFormat:PGSQL_DATETIME_FORMAT];
111       break;
112     case TIMEOID:
113       [self setExternalType:@"time"];
114       [self setValueClassName:@"NSCalendarDate"];
115       [self setCalendarFormat:PGSQL_DATETIME_FORMAT];
116       break;
117     case TIMESTAMPOID:
118       [self setExternalType:@"timestamp"];
119       [self setValueClassName:@"NSCalendarDate"];
120       [self setCalendarFormat:PGSQL_DATETIME_FORMAT];
121       break;
122     case TIMESTAMPTZOID:
123       [self setExternalType:@"timestamptz"];
124       [self setValueClassName:@"NSCalendarDate"];
125       [self setCalendarFormat:PGSQL_DATETIME_FORMAT];
126       break;
127     case BITOID:
128       [self setExternalType:@"bit"];
129       break;
130     default:
131     NSLog(@"What is PGSQL Oid %i ???", _type);
132     break;
133   }
134 }
135
136 - (void)loadValueClassForExternalPostgreSQLType:(NSString *)_type {
137   if ([_type isEqualToString:@"bool"]) {
138     [self setValueClassName:@"NSNumber"];
139     [self setValueType:@"i"];
140   }
141   else if ([_type isEqualToString:@"int2"]) {
142     [self setValueClassName:@"NSNumber"];
143     [self setValueType:@"i"];
144   }
145   else if ([_type isEqualToString:@"int4"]) {
146     [self setValueClassName:@"NSNumber"];
147     [self setValueType:@"i"];
148   }
149   else if ([_type isEqualToString:@"float4"]) {
150     [self setValueClassName:@"NSNumber"];
151     [self setValueType:@"f"];
152   }
153   else if ([_type isEqualToString:@"float8"]) {
154     [self setValueClassName:@"NSNumber"];
155     [self setValueType:@"d"];
156   }
157   else if ([_type isEqualToString:@"decimal"]) {
158     [self setValueClassName:@"NSNumber"];
159     [self setValueType:@"d"];
160   }
161   else if ([_type isEqualToString:@"numeric"]) {
162     [self setValueClassName:@"NSNumber"];
163     [self setValueType:@"d"];
164   }
165   else if ([_type isEqualToString:@"cardinal_number"]) {
166     // TODO: not sure whether this is correct (comes from sql_features table)
167     [self setValueClassName:@"NSNumber"];
168     [self setValueType:@"d"];
169   }
170   else if ([_type isEqualToString:@"name"]) {
171     [self setExternalType:@"name"];
172     [self setValueClassName:@"NSString"];
173   }
174   else if ([_type isEqualToString:@"varchar"]) {
175     [self setExternalType:@"varchar"];
176     [self setValueClassName:@"NSString"];
177   }
178   else if ([_type isEqualToString:@"char"]) {
179     [self setExternalType:@"char"];
180     [self setValueClassName:@"NSString"];
181   }
182   else if ([_type isEqualToString:@"character_data"]) {
183     // TODO: not sure whether this is correct (comes from sql_features table)
184     [self setExternalType:@"character_data"];
185     [self setValueClassName:@"NSString"];
186   }
187   else if ([_type isEqualToString:@"timestamp"]) {
188     [self setValueClassName:@"NSCalendarDate"];
189     [self setCalendarFormat:PGSQL_TIMESTAMP_FORMAT];
190   }
191   else if ([_type isEqualToString:@"timestamptz"]) {
192     [self setValueClassName:@"NSCalendarDate"];
193     [self setCalendarFormat:PGSQL_TIMESTAMP_FORMAT];
194   }
195   else if ([_type isEqualToString:@"datetime"]) {
196     [self setValueClassName:@"NSCalendarDate"];
197     [self setCalendarFormat:PGSQL_DATETIME_FORMAT];
198   }
199   else if ([_type isEqualToString:@"date"]) {
200     [self setValueClassName:@"NSString"];
201   }
202   else if ([_type isEqualToString:@"time"]) {
203     [self setValueClassName:@"NSString"];
204   }
205   else if ([_type isEqualToString:@"text"]) {
206     [self setValueClassName:@"NSString"];
207   }
208   else {
209     NSLog(@"%s: invalid argument %@", __PRETTY_FUNCTION__, _type);
210
211 #if LIB_FOUNDATION_LIBRARY
212     [InvalidArgumentException raise:@"InvalidArgumentException"
213                               format:
214                                 @"invalid PostgreSQL72 type %@ passed to "
215                                 @"-loadValueClassForExternalPostgreSQLType:",
216                                 _type];
217 #else
218     [NSException raise:@"InvalidArgumentException"
219                  format:
220                    @"invalid PostgreSQL72 type %@ passed to "
221                    @"-loadValueClassForExternalPostgreSQLType:",
222                    _type];
223 #endif
224   }
225 }
226
227 @end /* EOAttribute(PostgreSQL72) */
228
229 void __link_EOAttributePostgreSQL72() {
230   // used to force linking of object file
231   __link_EOAttributePostgreSQL72();
232 }