]> err.no Git - sope/blob - sope-core/samples/EOQualTool.m
more code directory reorganizations
[sope] / sope-core / samples / EOQualTool.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #import "EOQualTool.h"
24 #import "common.h"
25 #include <EOControl/EOControl.h>
26 #include <EOControl/EOSQLParser.h>
27
28 @interface dateTime : NSDate
29 @end
30
31 @implementation dateTime
32
33 - (id)initWithString:(NSString *)_s {
34   NSCalendarDate *date;
35   NSString *fmt = @"%Y-%m-%dT%H:%M:%SZ";
36   [self release];
37   date = [NSCalendarDate dateWithString:_s calendarFormat:fmt];
38   [date setCalendarFormat:@"%Y-%m-%d %H:%M %Z"];
39   return [date retain];
40 }
41
42 @end
43
44 @implementation EOQualTool
45
46 /* ops */
47
48 - (void)indent:(int)_level {
49   int i;
50   for (i = 0; i < _level; i++)
51     printf("  ");
52 }
53
54 - (void)printQualifiers:(NSArray *)_qs nesting:(int)_level {
55   NSEnumerator *e;
56   EOQualifier *q;
57
58   e = [_qs objectEnumerator];
59   while ((q = [e nextObject]))
60     [self printQualifier:q nesting:_level];
61 }
62
63 - (void)printQualifier:(id)_q nesting:(int)_level {
64   [self indent:_level];
65   
66   if ([_q isKindOfClass:[EOAndQualifier class]]) {
67     printf("AND\n");
68     [self printQualifiers:[_q qualifiers] nesting:_level + 1];
69   }
70   else if ([_q isKindOfClass:[EOOrQualifier class]]) {
71     printf("OR\n");
72     [self printQualifiers:[_q qualifiers] nesting:_level + 1];
73   }
74   else if ([_q isKindOfClass:[EONotQualifier class]]) {
75     printf("NOT\n");
76     [self printQualifier:[_q qualifier] nesting:_level + 1];
77   }
78   else if ([_q isKindOfClass:[EOKeyValueQualifier class]]) {
79     id v = [_q value];
80     printf("key OP value\n");
81     _level++;
82     [self indent:_level];
83     printf("key:   %s\n", [[_q key] cString]);
84     [self indent:_level];
85     printf("value: '%s' (class=%s)\n",
86            [[v stringValue] cString],
87            [NSStringFromClass([v class]) cString]);
88     [self indent:_level];
89     printf("OP:    %s\n", [NSStringFromSelector([_q selector]) cString]);
90     _level--;
91   }
92   else if ([_q isKindOfClass:[EOKeyComparisonQualifier class]]) {
93     printf("key1 OP key1\n");
94     _level++;
95     [self indent:_level];
96     printf("left:  %s\n", [[_q leftKey] cString]);
97     [self indent:_level];
98     printf("right: %s\n", [[_q rightKey] cString]);
99     [self indent:_level];
100     printf("OP:    %s\n", [NSStringFromSelector([_q selector]) cString]);
101     _level--;
102   }
103   else
104     printf("unknown: %s\n", [NSStringFromClass([_q class]) cString]);
105 }
106
107 - (void)processQualifier:(NSString *)_qs {
108   EOQualifier *q;
109   NSArray *args = nil;
110   
111   printf("qualifier: '%s'\n", [_qs cString]);
112   
113   if ((q = [EOQualifier qualifierWithQualifierFormat:_qs arguments:args])) {
114     printf("  parsed: %s\n", [[q description] cString]);
115
116     [self printQualifier:q nesting:1];
117   }
118   else
119     printf("  parsing failed !\n");
120 }
121
122 - (void)testExQualifier {
123   [self processQualifier:
124                  @"\"DAV:iscollection\" = False     and "
125                @"\"http://schemas.microsoft.com/mapi/proptag/x0c1e001f\" = "
126                @"'SMTP'        and "
127                @"\"http://schemas.microsoft.com/mapi/proptag/x0e230003\" > 0"];
128 }
129 - (void)testComplexCastQualifier {
130   [self processQualifier:
131                @"\"DAV:getlastmodified\" < "
132                @"  cast(\"1970-01-01T00:00:00Z\" as 'dateTime')  "
133                @" and \"DAV:contentclass\" = 'urn:content-classes:appointment'"
134                @" and (\"urn:schemas:calendar:instancetype\" = 0 "
135                @" or \"urn:schemas:calendar:instancetype\" = 1)"];
136 }
137
138 - (void)testQualifiers {
139   [self testExQualifier];
140   [self testComplexCastQualifier];
141 }
142
143 - (void)testSQL:(NSString *)_sql {
144   EOSQLParser *parser;
145   EOFetchSpecification *fs;
146
147   if ([_sql hasPrefix:@"test"]) {
148     SEL s;
149     
150     s = NSSelectorFromString(_sql);
151     if ([EOSQLParser respondsToSelector:s]) {
152       [EOSQLParser performSelector:s];
153       return;
154     }
155   }
156   
157   parser = [EOSQLParser sharedSQLParser];
158   
159   [self logWithFormat:@"parse SQL: %@", _sql];
160   [self logWithFormat:@"parser: %@", parser];
161   
162   fs = [parser parseSQLSelectStatement:_sql];
163   [self logWithFormat:@"got fs: %@", fs];
164 }
165
166 /* tool operation */
167
168 - (int)usage {
169   fprintf(stderr, "usage: eoqual <quals>\n");
170   return 1;
171 }
172
173 - (int)runWithArguments:(NSArray *)_args {
174   NSUserDefaults *ud;
175   unsigned i;
176   
177   _args = [_args subarrayWithRange:NSMakeRange(1, [_args count] - 1)];
178   if ([_args count] == 0)
179     return [self usage];
180   
181   ud = [NSUserDefaults standardUserDefaults];
182   
183   for (i = 0; i < [_args count]; i++) {
184     NSString *q;
185     
186     q = [_args objectAtIndex:i];
187     if ([q hasPrefix:@"sql:"])
188       [self testSQL:[q stringWithoutPrefix:@"sql:"]];
189     else if ([q isEqualToString:@"test"])
190       [self testQualifiers];
191     else
192       [self processQualifier:q];
193   }
194   
195   return 0;
196 }
197
198 @end /* EOQualTool */