]> err.no Git - sope/blob - sope-core/samples/EOQualTool.m
bumbed versions to 4.5
[sope] / sope-core / samples / EOQualTool.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
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
22 #import "EOQualTool.h"
23 #import "common.h"
24 #include <EOControl/EOControl.h>
25 #include <EOControl/EOSQLParser.h>
26
27 @interface dateTime : NSDate
28 @end
29
30 @implementation dateTime
31
32 - (id)initWithString:(NSString *)_s {
33   NSCalendarDate *date;
34   NSString *fmt = @"%Y-%m-%dT%H:%M:%SZ";
35   [self release];
36   date = [NSCalendarDate dateWithString:_s calendarFormat:fmt];
37   [date setCalendarFormat:@"%Y-%m-%d %H:%M %Z"];
38   return [date retain];
39 }
40
41 @end
42
43 @implementation EOQualTool
44
45 /* ops */
46
47 - (void)indent:(int)_level {
48   int i;
49   for (i = 0; i < _level; i++)
50     printf("  ");
51 }
52
53 - (void)printQualifiers:(NSArray *)_qs nesting:(int)_level {
54   NSEnumerator *e;
55   EOQualifier *q;
56
57   e = [_qs objectEnumerator];
58   while ((q = [e nextObject]))
59     [self printQualifier:q nesting:_level];
60 }
61
62 - (void)printQualifier:(id)_q nesting:(int)_level {
63   [self indent:_level];
64   
65   if ([_q isKindOfClass:[EOAndQualifier class]]) {
66     printf("AND\n");
67     [self printQualifiers:[_q qualifiers] nesting:_level + 1];
68   }
69   else if ([_q isKindOfClass:[EOOrQualifier class]]) {
70     printf("OR\n");
71     [self printQualifiers:[_q qualifiers] nesting:_level + 1];
72   }
73   else if ([_q isKindOfClass:[EONotQualifier class]]) {
74     printf("NOT\n");
75     [self printQualifier:[_q qualifier] nesting:_level + 1];
76   }
77   else if ([_q isKindOfClass:[EOKeyValueQualifier class]]) {
78     id v = [_q value];
79     printf("key OP value\n");
80     _level++;
81     [self indent:_level];
82     printf("key:   %s\n", [[_q key] cString]);
83     [self indent:_level];
84     printf("value: '%s' (class=%s)\n",
85            [[v stringValue] cString],
86            [NSStringFromClass([v class]) cString]);
87     [self indent:_level];
88     printf("OP:    %s\n", [NSStringFromSelector([_q selector]) cString]);
89     _level--;
90   }
91   else if ([_q isKindOfClass:[EOKeyComparisonQualifier class]]) {
92     printf("key1 OP key1\n");
93     _level++;
94     [self indent:_level];
95     printf("left:  %s\n", [[_q leftKey] cString]);
96     [self indent:_level];
97     printf("right: %s\n", [[_q rightKey] cString]);
98     [self indent:_level];
99     printf("OP:    %s\n", [NSStringFromSelector([_q selector]) cString]);
100     _level--;
101   }
102   else
103     printf("unknown: %s\n", [NSStringFromClass([_q class]) cString]);
104 }
105
106 - (void)processQualifier:(NSString *)_qs {
107   EOQualifier *q;
108   NSArray *args = nil;
109   
110   printf("qualifier: '%s'\n", [_qs cString]);
111   
112   if ((q = [EOQualifier qualifierWithQualifierFormat:_qs arguments:args])) {
113     printf("  parsed: %s\n", [[q description] cString]);
114
115     [self printQualifier:q nesting:1];
116   }
117   else
118     printf("  parsing failed !\n");
119 }
120
121 - (void)testExQualifier {
122   [self processQualifier:
123                  @"\"DAV:iscollection\" = False     and "
124                @"\"http://schemas.microsoft.com/mapi/proptag/x0c1e001f\" = "
125                @"'SMTP'        and "
126                @"\"http://schemas.microsoft.com/mapi/proptag/x0e230003\" > 0"];
127 }
128 - (void)testComplexCastQualifier {
129   [self processQualifier:
130                @"\"DAV:getlastmodified\" < "
131                @"  cast(\"1970-01-01T00:00:00Z\" as 'dateTime')  "
132                @" and \"DAV:contentclass\" = 'urn:content-classes:appointment'"
133                @" and (\"urn:schemas:calendar:instancetype\" = 0 "
134                @" or \"urn:schemas:calendar:instancetype\" = 1)"];
135 }
136
137 - (void)testQualifiers {
138   [self testExQualifier];
139   [self testComplexCastQualifier];
140 }
141
142 - (void)testSQL:(NSString *)_sql {
143   EOSQLParser *parser;
144   EOFetchSpecification *fs;
145
146   if ([_sql hasPrefix:@"test"]) {
147     SEL s;
148     
149     s = NSSelectorFromString(_sql);
150     if ([EOSQLParser respondsToSelector:s]) {
151       [EOSQLParser performSelector:s];
152       return;
153     }
154   }
155   
156   parser = [EOSQLParser sharedSQLParser];
157   
158   [self logWithFormat:@"parse SQL: %@", _sql];
159   [self logWithFormat:@"parser: %@", parser];
160   
161   fs = [parser parseSQLSelectStatement:_sql];
162   [self logWithFormat:@"got fs: %@", fs];
163 }
164
165 /* tool operation */
166
167 - (int)usage {
168   fprintf(stderr, "usage: eoqual <quals>\n");
169   return 1;
170 }
171
172 - (int)runWithArguments:(NSArray *)_args {
173   NSUserDefaults *ud;
174   unsigned i;
175   
176   _args = [_args subarrayWithRange:NSMakeRange(1, [_args count] - 1)];
177   if ([_args count] == 0)
178     return [self usage];
179   
180   ud = [NSUserDefaults standardUserDefaults];
181   
182   for (i = 0; i < [_args count]; i++) {
183     NSString *q;
184     
185     q = [_args objectAtIndex:i];
186     if ([q hasPrefix:@"sql:"])
187       [self testSQL:[q stringWithoutPrefix:@"sql:"]];
188     else if ([q isEqualToString:@"test"])
189       [self testQualifiers];
190     else
191       [self processQualifier:q];
192   }
193   
194   return 0;
195 }
196
197 @end /* EOQualTool */