1/*
2**  otest.m
3**
4**  Copyright (c) 2007  Inverse groupe conseil inc. and Ludovic Marcotte
5**
6**  Author: Ludovic Marcotte <ludovic@inverse.ca>
7**
8**  This program is free software; you can redistribute it and/or modify
9**  it under the terms of the GNU General Public License as published by
10**  the Free Software Foundation; either version 2 of the License, or
11**  (at your option) any later version.
12**
13**  This program 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
16**  GNU General Public License for more details.
17**
18**  You should have received a copy of the GNU General Public License
19**  along with this program; if not, write to the Free Software
20**  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23#import <Foundation/Foundation.h>
24#import <GDLAccess/GDLAccess.h>
25
26//
27//
28//
29void evaluate(EOAdaptorChannel *c, NSString *s)
30{
31  NSLog(@"Evaluating:\t%@",s);
32  if ([c evaluateExpression: s] && [c isFetchInProgress])
33    {
34      NSDictionary *record;
35      NSArray *attributes;
36
37      attributes = [c describeResults];
38      NSLog(@"attributes = %@", [attributes description]);
39
40      while ((record = [c fetchAttributes: attributes  withZone: NULL]))
41	{
42	  NSLog(@"record = %@", record);
43	}
44    }
45}
46
47//
48//
49//
50void insert(EOAdaptorChannel *channel, EOEntity *entity, NSMutableDictionary *row)
51{
52  [row setObject: @"foo"  forKey: @"c_name"];
53  [row setObject: @"barrr"  forKey: @"c_content"];
54  [row setObject: [NSNumber numberWithInt: 1]  forKey: @"c_creationdate"];
55  [row setObject: [NSNumber numberWithInt: 2]  forKey: @"c_lastmodified"];
56  [row setObject: [NSNumber numberWithInt: 0]  forKey: @"c_version"];
57
58  [channel insertRow: row  forEntity: entity];
59}
60
61//
62//
63//
64void update(EOAdaptorChannel *channel, EOEntity *entity, NSMutableDictionary *row)
65{
66  EOSQLQualifier *qualifier;
67
68  qualifier = [[EOSQLQualifier alloc] initWithEntity: entity
69				      qualifierFormat: @"%A = 'foo'", @"c_name"];
70
71  [row setObject: @"bazzzzzzzzzzz"  forKey: @"c_content"];
72  [row setObject: [NSNumber numberWithInt: 2]  forKey: @"c_creationdate"];
73  [row setObject: [NSNumber numberWithInt: 3]  forKey: @"c_lastmodified"];
74  [row setObject: [NSNumber numberWithInt: 1]  forKey: @"c_version"];
75
76  [channel updateRow: row  describedByQualifier: qualifier];
77}
78
79//
80//
81//
82int main (int argc, char **argv, char **env)
83{
84  NSAutoreleasePool *pool;
85
86  EOAdaptorChannel *channel;
87  NSMutableDictionary *row;
88  EOAdaptorContext *ctx;
89  EOAdaptor *adaptor;
90  EOEntity *entity;
91  EOAttribute *attribute;
92
93  pool = [[NSAutoreleasePool alloc] init];
94  [NSProcessInfo initializeWithArguments: argv  count: argc  environment: env];
95
96  adaptor = [EOAdaptor adaptorWithName: @"Oracle8"];
97  [adaptor setConnectionDictionary: [NSDictionary dictionaryWithContentsOfFile: @"condict.plist"]];
98  ctx = [adaptor createAdaptorContext];
99  channel = [ctx createAdaptorChannel];
100
101  [channel openChannel];
102  [ctx beginTransaction];
103
104  //evaluate(channel, @"SELECT * FROM all_tables");
105  evaluate(channel, @"SELECT COUNT(*) FROM all_tables");
106  evaluate(channel, @"SELECT 1 FROM dual");
107  evaluate(channel, @"SELECT sysdate FROM dual");
108
109  evaluate(channel, @"DROP table otest_demo");
110  evaluate(channel, @"CREATE TABLE otest_demo (\nc_name VARCHAR2 (256) NOT NULL,\n c_content CLOB NOT NULL,\n c_creationdate INTEGER NOT NULL,\n c_lastmodified INTEGER NOT NULL,\n c_version INTEGER NOT NULL,\n c_deleted INTEGER  DEFAULT 0 NOT NULL\n)");
111
112  evaluate(channel, @"DELETE FROM otest_demo where c_name = 'foo'");
113
114  entity = [[EOEntity alloc] init];
115  [entity setName: @"otest_demo"];
116  [entity setExternalName: @"otest_demo"]; // table name
117
118  attribute = AUTORELEASE([[EOAttribute alloc] init]);
119  [attribute setName: @"c_name"];
120  [attribute setColumnName: @"c_name"];
121  [entity addAttribute: attribute];
122
123  row = [[NSMutableDictionary alloc] init];
124
125  insert(channel, entity, row);
126  evaluate(channel, @"SELECT * FROM otest_demo where c_name = 'foo'");
127  update(channel, entity, row);
128  evaluate(channel, @"SELECT * FROM otest_demo where c_name = 'foo'");
129
130  RELEASE(entity);
131  RELEASE(row);
132
133  [ctx commitTransaction];
134  [channel closeChannel];
135  [pool release];
136
137  return 0;
138}
139