]> err.no Git - sope/blob - sope-gdl1/GDLContentStore/EOQualifier+GCS.m
support OR and icase-like qualifiers
[sope] / sope-gdl1 / GDLContentStore / EOQualifier+GCS.m
1 /*
2   Copyright (C) 2004-2005 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 #include "EOQualifier+GCS.h"
23 #include "common.h"
24
25 @implementation EOQualifier(GCS)
26
27 - (void)_appendAndQualifier:(EOAndQualifier *)_q 
28   toString:(NSMutableString *)_ms
29 {
30   // TODO: move to EOQualifier category
31   NSArray *qs;
32   unsigned i, count;
33   
34   qs = [_q qualifiers];
35   if ((count = [qs count]) == 0)
36     return;
37   
38   for (i = 0; i < count; i++) {
39     if (i != 0) [_ms appendString:@" AND "];
40     if (count > 1) [_ms appendString:@"("];
41     [[qs objectAtIndex:i] _gcsAppendToString:_ms];
42     if (count > 1) [_ms appendString:@")"];
43   }
44 }
45 - (void)_appendOrQualifier:(EOAndQualifier *)_q 
46   toString:(NSMutableString *)_ms
47 {
48   // TODO: move to EOQualifier category
49   NSArray *qs;
50   unsigned i, count;
51   
52   qs = [_q qualifiers];
53   if ((count = [qs count]) == 0)
54     return;
55   
56   for (i = 0; i < count; i++) {
57     if (i != 0) [_ms appendString:@" OR "];
58     if (count > 1) [_ms appendString:@"("];
59     [[qs objectAtIndex:i] _gcsAppendToString:_ms];
60     if (count > 1) [_ms appendString:@")"];
61   }
62 }
63 - (void)_appendKeyValueQualifier:(EOKeyValueQualifier *)_q 
64   toString:(NSMutableString *)_ms
65 {
66   id val;
67   
68   [_ms appendString:[_q key]];
69   
70   if ((val = [_q value])) {
71     SEL op = [_q selector];
72     
73     if ([val isNotNull]) {
74       if (sel_eq(op, EOQualifierOperatorEqual))
75         [_ms appendString:@" = "];
76       else if (sel_eq(op, EOQualifierOperatorNotEqual))
77         [_ms appendString:@" != "];
78       else if (sel_eq(op, EOQualifierOperatorLessThan))
79         [_ms appendString:@" < "];
80       else if (sel_eq(op, EOQualifierOperatorGreaterThan))
81         [_ms appendString:@" > "];
82       else if (sel_eq(op, EOQualifierOperatorLessThanOrEqualTo))
83         [_ms appendString:@" <= "];
84       else if (sel_eq(op, EOQualifierOperatorGreaterThanOrEqualTo))
85         [_ms appendString:@" >= "];
86       else if (sel_eq(op, EOQualifierOperatorLike))
87         [_ms appendString:@" LIKE "];
88       else if (sel_eq(op, EOQualifierOperatorCaseInsensitiveLike)) {
89         // TODO: this is PostgreSQL specific, use UPPER?
90         [_ms appendString:@" ILIKE "];
91       }
92       else {
93         [self logWithFormat:@"ERROR(%s): unsupported operation for null: %@",
94               __PRETTY_FUNCTION__, NSStringFromSelector(op)];
95       }
96
97       if ([val isKindOfClass:[NSNumber class]])
98         [_ms appendString:[val stringValue]];
99       else if ([val isKindOfClass:[NSString class]]) {
100         [_ms appendString:@"'"];
101         [_ms appendString:val];
102         [_ms appendString:@"'"];
103       }
104       else {
105         [self logWithFormat:@"ERROR(%s): unsupported value class: %@",
106               __PRETTY_FUNCTION__, NSStringFromClass([val class])];
107       }
108     }
109     else {
110       if (sel_eq(op, EOQualifierOperatorEqual))
111         [_ms appendString:@" IS NULL"];
112       else if (sel_eq(op, EOQualifierOperatorEqual))
113         [_ms appendString:@" IS NOT NULL"];
114       else {
115         [self logWithFormat:@"ERROR(%s): invalid operation for null: %@",
116               __PRETTY_FUNCTION__, NSStringFromSelector(op)];
117       }
118     }
119   }
120   else
121     [_ms appendString:@" IS NULL"];
122 }
123
124 - (void)_appendQualifier:(EOQualifier *)_q toString:(NSMutableString *)_ms {
125   if (_q == nil) return;
126   
127   if ([_q isKindOfClass:[EOAndQualifier class]])
128     [self _appendAndQualifier:(id)_q toString:_ms];
129   else if ([_q isKindOfClass:[EOOrQualifier class]])
130     [self _appendOrQualifier:(id)_q toString:_ms];
131   else if ([_q isKindOfClass:[EOKeyValueQualifier class]])
132     [self _appendKeyValueQualifier:(id)_q toString:_ms];
133   else
134     NSLog(@"ERROR: unknown qualifier: %@", _q);
135 }
136
137 - (void)_gcsAppendToString:(NSMutableString *)_ms {
138   [self _appendQualifier:self toString:_ms];
139 }
140
141 @end /* EOQualifier(GCS) */