2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
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
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.
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
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:(id)_q nesting:(int)_level {
65 if ([_q isKindOfClass:[EOAndQualifier class]]) {
67 [self printQualifiers:[_q qualifiers] nesting:_level + 1];
69 else if ([_q isKindOfClass:[EOOrQualifier class]]) {
71 [self printQualifiers:[_q qualifiers] nesting:_level + 1];
73 else if ([_q isKindOfClass:[EONotQualifier class]]) {
75 [self printQualifier:[_q qualifier] nesting:_level + 1];
77 else if ([_q isKindOfClass:[EOKeyValueQualifier class]]) {
79 printf("key OP value\n");
82 printf("key: %s\n", [[_q key] cString]);
84 printf("value: '%s' (class=%s)\n",
85 [[v stringValue] cString],
86 [NSStringFromClass([v class]) cString]);
88 printf("OP: %s\n", [NSStringFromSelector([_q selector]) cString]);
91 else if ([_q isKindOfClass:[EOKeyComparisonQualifier class]]) {
92 printf("key1 OP key1\n");
95 printf("left: %s\n", [[_q leftKey] cString]);
97 printf("right: %s\n", [[_q rightKey] cString]);
99 printf("OP: %s\n", [NSStringFromSelector([_q selector]) cString]);
103 printf("unknown: %s\n", [NSStringFromClass([_q class]) cString]);
106 - (void)processQualifier:(NSString *)_qs {
110 printf("qualifier: '%s'\n", [_qs cString]);
112 if ((q = [EOQualifier qualifierWithQualifierFormat:_qs arguments:args])) {
113 printf(" parsed: %s\n", [[q description] cString]);
115 [self printQualifier:q nesting:1];
118 printf(" parsing failed !\n");
121 - (void)testExQualifier {
122 [self processQualifier:
123 @"\"DAV:iscollection\" = False and "
124 @"\"http://schemas.microsoft.com/mapi/proptag/x0c1e001f\" = "
126 @"\"http://schemas.microsoft.com/mapi/proptag/x0e230003\" > 0"];
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)"];
137 - (void)testQualifiers {
138 [self testExQualifier];
139 [self testComplexCastQualifier];
142 - (void)testSQL:(NSString *)_sql {
144 EOFetchSpecification *fs;
146 if ([_sql hasPrefix:@"test"]) {
149 s = NSSelectorFromString(_sql);
150 if ([EOSQLParser respondsToSelector:s]) {
151 [EOSQLParser performSelector:s];
156 parser = [EOSQLParser sharedSQLParser];
158 [self logWithFormat:@"parse SQL: %@", _sql];
159 [self logWithFormat:@"parser: %@", parser];
161 fs = [parser parseSQLSelectStatement:_sql];
162 [self logWithFormat:@"got fs: %@", fs];
168 fprintf(stderr, "usage: eoqual <quals>\n");
172 - (int)runWithArguments:(NSArray *)_args {
176 _args = [_args subarrayWithRange:NSMakeRange(1, [_args count] - 1)];
177 if ([_args count] == 0)
180 ud = [NSUserDefaults standardUserDefaults];
182 for (i = 0; i < [_args count]; i++) {
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];
191 [self processQualifier:q];
197 @end /* EOQualTool */