]> err.no Git - sope/blob - sope-core/samples/EOQualTool.m
fixed gcc 4.1 warnings
[sope] / sope-core / samples / EOQualTool.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE 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   SOPE 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 SOPE; 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:(EOQualifier *)_q nesting:(int)_level {
63   [self indent:_level];
64   
65   if ([_q isKindOfClass:[EOAndQualifier class]]) {
66     printf("AND\n");
67     [self printQualifiers:[(EOAndQualifier *)_q qualifiers] 
68           nesting:_level + 1];
69   }
70   else if ([_q isKindOfClass:[EOOrQualifier class]]) {
71     printf("OR\n");
72     [self printQualifiers:[(EOOrQualifier *)_q qualifiers] nesting:_level + 1];
73   }
74   else if ([_q isKindOfClass:[EONotQualifier class]]) {
75     printf("NOT\n");
76     [self printQualifier:[(EONotQualifier *)_q qualifier] nesting:_level + 1];
77   }
78   else if ([_q isKindOfClass:[EOKeyValueQualifier class]]) {
79     id v = [(EOKeyValueQualifier *)_q value];
80     printf("key OP value\n");
81     _level++;
82     [self indent:_level];
83     printf("key:   %s\n", [[(EOKeyValueQualifier *)_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", 
90            [NSStringFromSelector([(EOKeyValueQualifier *)_q selector]) 
91                                  cString]);
92     _level--;
93   }
94   else if ([_q isKindOfClass:[EOKeyComparisonQualifier class]]) {
95     printf("key1 OP key1\n");
96     _level++;
97     [self indent:_level];
98     printf("left:  %s\n", [[(EOKeyComparisonQualifier *)_q leftKey] cString]);
99     [self indent:_level];
100     printf("right: %s\n", [[(EOKeyComparisonQualifier *)_q rightKey] cString]);
101     [self indent:_level];
102     printf("OP:    %s\n",
103            [NSStringFromSelector([(EOKeyComparisonQualifier *)_q selector]) 
104                                 cString]);
105     _level--;
106   }
107   else
108     printf("unknown: %s\n", [NSStringFromClass([_q class]) cString]);
109 }
110
111 - (void)processQualifier:(NSString *)_qs {
112   EOQualifier *q;
113   NSArray *args = nil;
114   
115   printf("qualifier: '%s'\n", [_qs cString]);
116   
117   if ((q = [EOQualifier qualifierWithQualifierFormat:_qs arguments:args])) {
118     printf("  parsed: %s\n", [[q description] cString]);
119
120     [self printQualifier:q nesting:1];
121   }
122   else
123     printf("  parsing failed !\n");
124 }
125
126 - (void)testExQualifier {
127   [self processQualifier:
128                  @"\"DAV:iscollection\" = False     and "
129                @"\"http://schemas.microsoft.com/mapi/proptag/x0c1e001f\" = "
130                @"'SMTP'        and "
131                @"\"http://schemas.microsoft.com/mapi/proptag/x0e230003\" > 0"];
132 }
133 - (void)testComplexCastQualifier {
134   [self processQualifier:
135                @"\"DAV:getlastmodified\" < "
136                @"  cast(\"1970-01-01T00:00:00Z\" as 'dateTime')  "
137                @" and \"DAV:contentclass\" = 'urn:content-classes:appointment'"
138                @" and (\"urn:schemas:calendar:instancetype\" = 0 "
139                @" or \"urn:schemas:calendar:instancetype\" = 1)"];
140 }
141
142 - (void)testQualifiers {
143   [self testExQualifier];
144   [self testComplexCastQualifier];
145 }
146
147 - (void)testSQL:(NSString *)_sql {
148   EOSQLParser *parser;
149   EOFetchSpecification *fs;
150
151   if ([_sql hasPrefix:@"test"]) {
152     SEL s;
153     
154     s = NSSelectorFromString(_sql);
155     if ([EOSQLParser respondsToSelector:s]) {
156       [EOSQLParser performSelector:s];
157       return;
158     }
159   }
160   
161   parser = [EOSQLParser sharedSQLParser];
162   
163   [self logWithFormat:@"parse SQL: %@", _sql];
164   [self logWithFormat:@"parser: %@", parser];
165   
166   fs = [parser parseSQLSelectStatement:_sql];
167   [self logWithFormat:@"got fs: %@", fs];
168 }
169
170 /* tool operation */
171
172 - (int)usage {
173   fprintf(stderr, "usage: eoqual <quals>\n");
174   return 1;
175 }
176
177 - (int)runWithArguments:(NSArray *)_args {
178   NSUserDefaults *ud;
179   unsigned i;
180   
181   _args = [_args subarrayWithRange:NSMakeRange(1, [_args count] - 1)];
182   if ([_args count] == 0)
183     return [self usage];
184   
185   ud = [NSUserDefaults standardUserDefaults];
186   
187   for (i = 0; i < [_args count]; i++) {
188     NSString *q;
189     
190     q = [_args objectAtIndex:i];
191     if ([q hasPrefix:@"sql:"])
192       [self testSQL:[q stringWithoutPrefix:@"sql:"]];
193     else if ([q isEqualToString:@"test"])
194       [self testQualifiers];
195     else
196       [self processQualifier:q];
197   }
198   
199   return 0;
200 }
201
202 @end /* EOQualTool */