]> err.no Git - sope/blob - sope-gdl1/GDLAccess/EOSelectSQLExpression.m
more directory hierarchy reorganisations,
[sope] / sope-gdl1 / GDLAccess / EOSelectSQLExpression.m
1 /* 
2    EOSQLExpression.m
3
4    Copyright (C) 1996 Free Software Foundation, Inc.
5
6    Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>
7    Date: September 1996
8
9    This file is part of the GNUstep Database Library.
10
11    This library is free software; you can redistribute it and/or
12    modify it under the terms of the GNU Library General Public
13    License as published by the Free Software Foundation; either
14    version 2 of the License, or (at your option) any later version.
15
16    This library is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    Library General Public License for more details.
20
21    You should have received a copy of the GNU Library General Public
22    License along with this library; see the file COPYING.LIB.
23    If not, write to the Free Software Foundation,
24    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27 #include "EOSQLExpression.h"
28 #include "EOAttribute.h"
29 #include "EOAdaptor.h"
30 #include "common.h"
31
32 #if LIB_FOUNDATION_LIBRARY
33 #  include <extensions/DefaultScannerHandler.h>
34 #  include <extensions/PrintfFormatScanner.h>
35 #else
36 #  include "DefaultScannerHandler.h"
37 #  include "PrintfFormatScanner.h"
38 #endif
39
40 @interface EOSelectScannerHandler : DefaultScannerHandler
41 {
42     EOAttribute *attribute;
43     EOAdaptor   *adaptor;
44     NSString    *alias;
45 }
46
47 - (void)setAttribute:(EOAttribute*)attribute
48   adaptor:(EOAdaptor*)adaptor
49   alias:(NSString*)alias;
50
51 @end
52
53 @implementation EOSelectSQLExpression
54
55 - (NSString *)expressionValueForAttribute:(EOAttribute *)attribute
56   context:(id)context
57 {
58   NSString *alias;
59   NSString *columnName;
60   NSString *selectFormat;
61   
62   alias        = [entitiesAndPropertiesAliases objectForKey:context];
63   selectFormat = [attribute selectFormat];
64   
65   //NSLog(@"entitiesAndPropertiesAliases: %@", entitiesAndPropertiesAliases);
66   
67   if (selectFormat) {
68     PrintfFormatScanner    *formatScanner;
69     EOSelectScannerHandler *scannerHandler;
70
71     formatScanner = 
72       [[[[PrintfFormatScanner alloc] init] setAllowOnlySpecifier:YES] 
73         autorelease];
74     scannerHandler = 
75       [[[EOSelectScannerHandler alloc] init] autorelease];
76     
77     [scannerHandler setAttribute:attribute adaptor:adaptor alias:alias];
78     [formatScanner  setFormatScannerHandler:scannerHandler];
79 #if defined(__s390__)
80     return [formatScanner performSelector:
81                             @selector(stringWithFormat:arguments:)
82                           withObject:selectFormat
83                           withObject:nil];
84 #else
85     return [formatScanner stringWithFormat:selectFormat arguments:NULL];
86 #endif
87   }
88   else {
89     columnName = adaptor
90       ? [adaptor formatAttribute:attribute]
91       : [attribute columnName];
92
93     if (alias) {
94       return [([[NSString alloc] initWithFormat:@"%@.%@",
95                                  alias, columnName]) autorelease];
96     }
97     
98     return columnName;
99   }
100 }
101
102 @end /* EOSelectSQLExpression */
103
104 @implementation EOSelectScannerHandler
105
106 - (id)init
107 {
108     [super init];
109
110     specHandler['A']
111             = [self methodForSelector:@selector(convertAttribute:scanner:)];
112     return self;
113 }
114
115 - (void)dealloc
116 {
117     RELEASE(self->attribute);
118     RELEASE(self->adaptor);
119     RELEASE(self->alias);
120     [super dealloc];
121 }
122
123 - (void)setAttribute:(EOAttribute*)_attribute
124   adaptor:(EOAdaptor*)_adaptor
125   alias:(NSString*)_alias
126 {
127     ASSIGN(self->attribute, _attribute);
128     ASSIGN(self->adaptor,   _adaptor);
129     ASSIGN(self->alias,     _alias);
130 }
131
132 - (NSString *)convertAttribute:(va_list *)pString
133   scanner:(FormatScanner *)scanner
134 {
135   NSString *columnName;
136
137   columnName = (adaptor)
138     ? [adaptor formatAttribute:self->attribute]
139     : [self->attribute columnName];
140
141   if (alias)
142     return [NSString stringWithFormat:@"%@.%@", alias, columnName];
143
144   return columnName;
145 }
146
147 @end /* EOSelectScannerHandler */