2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
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
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.
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
22 #import "EOQualTool.h"
24 #include <EOControl/EOControl.h>
25 #include <EOControl/EOSQLParser.h>
27 @interface dateTime : NSDate
30 @implementation dateTime
32 - (id)initWithString:(NSString *)_s {
34 NSString *fmt = @"%Y-%m-%dT%H:%M:%SZ";
36 date = [NSCalendarDate dateWithString:_s calendarFormat:fmt];
37 [date setCalendarFormat:@"%Y-%m-%d %H:%M %Z"];
43 @implementation EOQualTool
47 - (void)indent:(int)_level {
49 for (i = 0; i < _level; i++)
53 - (void)printQualifiers:(NSArray *)_qs nesting:(int)_level {
57 e = [_qs objectEnumerator];
58 while ((q = [e nextObject]))
59 [self printQualifier:q nesting:_level];
62 - (void)printQualifier:(EOQualifier *)_q nesting:(int)_level {
65 if ([_q isKindOfClass:[EOAndQualifier class]]) {
67 [self printQualifiers:[(EOAndQualifier *)_q qualifiers]
70 else if ([_q isKindOfClass:[EOOrQualifier class]]) {
72 [self printQualifiers:[(EOOrQualifier *)_q qualifiers] nesting:_level + 1];
74 else if ([_q isKindOfClass:[EONotQualifier class]]) {
76 [self printQualifier:[(EONotQualifier *)_q qualifier] nesting:_level + 1];
78 else if ([_q isKindOfClass:[EOKeyValueQualifier class]]) {
79 id v = [(EOKeyValueQualifier *)_q value];
80 printf("key OP value\n");
83 printf("key: %s\n", [[(EOKeyValueQualifier *)_q key] cString]);
85 printf("value: '%s' (class=%s)\n",
86 [[v stringValue] cString],
87 [NSStringFromClass([v class]) cString]);
90 [NSStringFromSelector([(EOKeyValueQualifier *)_q selector])
94 else if ([_q isKindOfClass:[EOKeyComparisonQualifier class]]) {
95 printf("key1 OP key1\n");
98 printf("left: %s\n", [[(EOKeyComparisonQualifier *)_q leftKey] cString]);
100 printf("right: %s\n", [[(EOKeyComparisonQualifier *)_q rightKey] cString]);
101 [self indent:_level];
103 [NSStringFromSelector([(EOKeyComparisonQualifier *)_q selector])
108 printf("unknown: %s\n", [NSStringFromClass([_q class]) cString]);
111 - (void)processQualifier:(NSString *)_qs {
115 printf("qualifier: '%s'\n", [_qs cString]);
117 if ((q = [EOQualifier qualifierWithQualifierFormat:_qs arguments:args])) {
118 printf(" parsed: %s\n", [[q description] cString]);
120 [self printQualifier:q nesting:1];
123 printf(" parsing failed !\n");
126 - (void)testExQualifier {
127 [self processQualifier:
128 @"\"DAV:iscollection\" = False and "
129 @"\"http://schemas.microsoft.com/mapi/proptag/x0c1e001f\" = "
131 @"\"http://schemas.microsoft.com/mapi/proptag/x0e230003\" > 0"];
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)"];
142 - (void)testQualifiers {
143 [self testExQualifier];
144 [self testComplexCastQualifier];
147 - (void)testSQL:(NSString *)_sql {
149 EOFetchSpecification *fs;
151 if ([_sql hasPrefix:@"test"]) {
154 s = NSSelectorFromString(_sql);
155 if ([EOSQLParser respondsToSelector:s]) {
156 [EOSQLParser performSelector:s];
161 parser = [EOSQLParser sharedSQLParser];
163 [self logWithFormat:@"parse SQL: %@", _sql];
164 [self logWithFormat:@"parser: %@", parser];
166 fs = [parser parseSQLSelectStatement:_sql];
167 [self logWithFormat:@"got fs: %@", fs];
173 fprintf(stderr, "usage: eoqual <quals>\n");
177 - (int)runWithArguments:(NSArray *)_args {
181 _args = [_args subarrayWithRange:NSMakeRange(1, [_args count] - 1)];
182 if ([_args count] == 0)
185 ud = [NSUserDefaults standardUserDefaults];
187 for (i = 0; i < [_args count]; i++) {
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];
196 [self processQualifier:q];
202 @end /* EOQualTool */