1/*
2**  OracleAdaptor.h
3**
4**  Copyright (c) 2007  Inverse groupe conseil inc. Ludovic Marcotte
5**
6**  Author: Ludovic Marcotte <ludovic@inverse.ca>
7**
8**  This library is free software; you can redistribute it and/or
9**  modify it under the terms of the GNU Lesser General Public
10**  License as published by the Free Software Foundation; either
11**  version 2.1 of the License, or (at your option) any later version.
12**
13**  This library is distributed in the hope that it will be useful,
14**  but WITHOUT ANY WARRANTY; without even the implied warranty of
15**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16**  Lesser General Public License for more details.
17**
18**  You should have received a copy of the GNU Lesser General Public
19**  License along with this library; if not, write to the Free Software
20**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21*/
22
23#include "OracleAdaptor.h"
24
25#import "OracleAdaptorChannel.h"
26#import "OracleAdaptorContext.h"
27#import "OracleSQLExpression.h"
28#import "OracleValues.h"
29
30//
31//
32//
33@interface OracleAdaptor (Private)
34
35- (ub2) typeForName: (NSString *) theName;
36
37@end
38
39@implementation OracleAdaptor (Private)
40
41- (ub2) typeForName: (NSString *) theName
42{
43  ub2 t;
44
45  theName = [theName uppercaseString];
46
47  if ([theName hasPrefix: @"VARCHAR2"])
48    {
49      t = SQLT_CHR;
50    }
51  else if ([theName hasPrefix: @"VARCHAR"])
52    {
53      t = SQLT_VCS;
54    }
55  else if ([theName isEqualToString: @"LONG VARCHAR"])
56    {
57      t = SQLT_LVC;
58    }
59  else if ([theName isEqualToString: @"CLOB"])
60    {
61      t = SQLT_CLOB;
62    }
63  else if ([theName isEqualToString: @"DATE"])
64    {
65      t = SQLT_DAT;
66    }
67  else if ([theName isEqualToString: @"INTEGER"])
68    {
69      t = SQLT_INT;
70    }
71  else if ([theName isEqualToString: @"NUMBER"])
72    {
73      t = SQLT_NUM;
74    }
75  else if ([theName isEqualToString: @"STRING"])
76    {
77      t = SQLT_STR;
78    }
79  else if ([theName isEqualToString: @"TIMESTAMP"])
80    {
81      t = SQLT_TIMESTAMP;
82    }
83  else if ([theName isEqualToString: @"TIMESTAMP WITH TIME ZONE"])
84    {
85      t = SQLT_TIMESTAMP_TZ;
86    }
87  else if ([theName isEqualToString: @"TIMESTAMP WITH LOCAL TIME ZONE"])
88    {
89      t = SQLT_TIMESTAMP_LTZ;
90    }
91  else
92    {
93      [self logWithFormat: @"unknown Oracle type: %@ (fallback to SQLT_CHR)", theName];
94      t = SQLT_CHR;
95    }
96
97  return t;
98}
99
100@end
101
102
103//
104//
105//
106@implementation OracleAdaptor
107
108- (id) initWithName: (NSString *) theName
109{
110  if ((self = [super initWithName: theName]))
111    {
112      // On Oracle, we must set the NLS_LANG in order to let Oracle perform
113      // charset transformations for us. Since, when we write data to the database
114      // and when we read data from it we assume that we are using UTF-8, we
115      // set NLS_LANG to the proper value.
116      setenv("NLS_LANG", "AMERICAN_AMERICA.UTF8", 1);
117
118      return self;
119    }
120
121  return nil;
122}
123
124//
125//
126//
127- (id) copyWithZone: (NSZone *) theZone
128{
129  return [self retain];
130}
131
132//
133//
134//
135- (Class) adaptorContextClass
136{
137  return [OracleAdaptorContext class];
138}
139
140//
141//
142//
143- (Class) adaptorChannelClass
144{
145  return [OracleAdaptorChannel class];
146}
147
148//
149//
150//
151- (Class) expressionClass
152{
153  return [OracleSQLExpression class];
154}
155
156//
157//
158//
159- (EOAdaptorContext *) createAdaptorContext
160{
161  return AUTORELEASE([[OracleAdaptorContext alloc] initWithAdaptor: self]);
162}
163
164//
165//
166//
167- (NSString *) databaseName
168{
169  return [[self connectionDictionary] objectForKey: @"databaseName"];
170}
171
172//
173//
174//
175- (id) formatValue: (id) theValue  forAttribute: (EOAttribute *) theAttribute
176{
177  NSString *s;
178
179  s = [theValue stringValueForOracleType: [self typeForName: [theAttribute externalType]]
180		attribute: theAttribute];
181
182  // NSLog(@"Formatted %@ (%@) to %@ (NSString)", theValue, NSStringFromClass([theValue class]), s);
183
184  return s;
185}
186
187//
188// We don't check the values inside the connection
189// dictionary for now.s
190//
191- (BOOL) hasValidConnectionDictionary
192{
193  return ([self connectionDictionary] ? YES : NO);
194}
195
196//
197//
198//
199- (BOOL) isValidQualifierType: (NSString *) theTypeName
200{
201  return YES;
202}
203
204//
205//
206//
207- (NSString *) loginName;
208{
209  return [[self connectionDictionary] objectForKey: @"userName"];
210}
211
212//
213//
214//
215- (NSString *) loginPassword
216{
217  return [[self connectionDictionary] objectForKey: @"password"];
218}
219
220//
221//
222//
223- (NSString *) port
224{
225  return [[self connectionDictionary] objectForKey: @"port"];
226}
227
228//
229//
230//
231- (NSString *) serverName
232{
233  return [[self connectionDictionary] objectForKey: @"hostName"];
234}
235
236@end
237