View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Michael Powers
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  package net.wotonomy.access;
19  
20  import net.wotonomy.control.EOAndQualifier;
21  import net.wotonomy.control.EOKeyComparisonQualifier;
22  import net.wotonomy.control.EOKeyValueQualifier;
23  import net.wotonomy.control.EONotQualifier;
24  import net.wotonomy.control.EOOrQualifier;
25  import net.wotonomy.control.EOQualifier;
26  import net.wotonomy.foundation.NSArray;
27  import net.wotonomy.foundation.NSMutableDictionary;
28  
29  /***
30  * @author ezamudio@nasoft.com
31  * @author $Author: cgruber $
32  * @version $Revision: 894 $
33  */
34  
35  public interface EOQualifierSQLGeneration {
36  
37  	public EOQualifier qualifierMigratedFromEntityRelationshipPath( 
38  				EOEntity entity, String path);
39  
40  	public EOQualifier schemaBasedQualifierWithRootEntity(EOEntity entity);
41  
42  	public String sqlStringForSQLExpression(EOSQLExpression expression);
43  
44  	public abstract class Support {
45  
46  		private static NSMutableDictionary _classes = new NSMutableDictionary();
47  
48  		static {
49  			setSupportForClass(new KeyValueQualifierSupport(), EOKeyValueQualifier.class);
50  			setSupportForClass(new KeyComparisonQualifierSupport(), EOKeyComparisonQualifier.class);
51  			setSupportForClass(new AndQualifierSupport(), EOAndQualifier.class);
52  			setSupportForClass(new OrQualifierSupport(), EOOrQualifier.class);
53  			setSupportForClass(new NotQualifierSupport(), EONotQualifier.class);
54  		}
55  
56  		public Support() {
57  			super();
58  		}
59  
60  		public static void setSupportForClass(Support sup, Class aClass) {
61  			_classes.setObjectForKey(sup, aClass.getName());
62  		}
63  		public static Support supportForClass(Class aClass) {
64  			return (Support)_classes.objectForKey(aClass.getName());
65  		}
66  
67  		public abstract String sqlStringForSQLExpression(EOQualifier q, EOSQLExpression exp);
68  
69  		public abstract EOQualifier schemaBasedQualifierWithRootEntity(EOQualifier q, EOEntity e);
70  
71  		public abstract EOQualifier qualifierMigratedFromEntityRelationshipPath(EOQualifier q, EOEntity e, String path);
72  
73  	}
74  
75  	public class KeyValueQualifierSupport extends Support {
76  
77  		public KeyValueQualifierSupport() {
78  			super();
79  		}
80  
81  		public String sqlStringForSQLExpression(EOQualifier qualifier, EOSQLExpression exp) {
82  			EOKeyValueQualifier q = (EOKeyValueQualifier)qualifier;
83  			String sql1 = exp.sqlStringForAttributeNamed(q.key());
84  			String sql2 = exp.sqlStringForSelector(q.selector(), q.value());
85  			String sql3 = exp.sqlStringForValue(q.value(), q.key());
86  			if (q.selector() == EOQualifier.QualifierOperatorCaseInsensitiveLike)
87  				return exp.sqlStringForCaseInsensitiveLike(sql1, sql3);
88  			else if (q.selector() == EOQualifier.QualifierOperatorLike)
89  				sql3 = exp.sqlPatternFromShellPattern(sql3);
90  			return  sql1 + sql2 + sql3;
91  		}
92  
93  		public EOQualifier schemaBasedQualifierWithRootEntity(EOQualifier q, EOEntity e) {
94  			return q;
95  		}
96  
97  		public EOQualifier qualifierMigratedFromEntityRelationshipPath(EOQualifier q, EOEntity e, String path) {
98  			return q;
99  		}
100 
101 	}
102 
103 	public class KeyComparisonQualifierSupport extends Support {
104 
105 		public KeyComparisonQualifierSupport() {
106 			super();
107 		}
108 
109 		public String sqlStringForSQLExpression(EOQualifier qualifier, EOSQLExpression exp) {
110 			EOKeyComparisonQualifier q = (EOKeyComparisonQualifier)qualifier;
111 			return exp.sqlStringForAttributeNamed(q.leftKey()) +
112 				exp.sqlStringForSelector(q.selector(), null) +
113 				exp.sqlStringForAttributeNamed(q.rightKey());
114 		}
115 
116 		public EOQualifier schemaBasedQualifierWithRootEntity(EOQualifier q, EOEntity e) {
117 			return q;
118 		}
119 
120 		public EOQualifier qualifierMigratedFromEntityRelationshipPath(EOQualifier q, EOEntity e, String path) {
121 			return q;
122 		}
123 
124 	}
125 
126 	public class NotQualifierSupport extends Support {
127 
128 		public NotQualifierSupport() {
129 			super();
130 		}
131 
132 		public String sqlStringForSQLExpression(EOQualifier qualifier, EOSQLExpression exp) {
133 			EONotQualifier q = (EONotQualifier)qualifier;
134 			return "NOT (" + EOQualifierSQLGeneration.Support.supportForClass(q.qualifier().getClass()).sqlStringForSQLExpression(q.qualifier(), exp) + ")";
135 		}
136 
137 		public EOQualifier schemaBasedQualifierWithRootEntity(EOQualifier qualifier, EOEntity e) {
138 			EONotQualifier q = (EONotQualifier)qualifier;
139 			return new EONotQualifier(EOQualifierSQLGeneration.Support.supportForClass(q.qualifier().getClass()).schemaBasedQualifierWithRootEntity(q.qualifier(), e));
140 		}
141 
142 		public EOQualifier qualifierMigratedFromEntityRelationshipPath(EOQualifier qualifier, EOEntity e, String path) {
143 			EONotQualifier q = (EONotQualifier)qualifier;
144 			return new EONotQualifier(EOQualifierSQLGeneration.Support.supportForClass(q.qualifier().getClass()).qualifierMigratedFromEntityRelationshipPath(q.qualifier(), e, path));
145 		}
146 
147 	}
148 
149 	public class AndQualifierSupport extends Support {
150 
151 		public AndQualifierSupport() {
152 			super();
153 		}
154 
155 		public String sqlStringForSQLExpression(EOQualifier qualifier, EOSQLExpression exp) {
156 			EOAndQualifier q = (EOAndQualifier)qualifier;
157 			NSArray qus = q.qualifiers();
158 			StringBuffer buf = new StringBuffer();
159 			for (int i = 0; i < qus.count(); i++) {
160 				EOQualifier sub = (EOQualifier)qus.objectAtIndex(i);
161 				EOQualifierSQLGeneration.Support sup = EOQualifierSQLGeneration.Support.supportForClass(sub.getClass());
162 				if (sup == null)
163 					throw new IllegalStateException("Cannot find support class for " + sub.getClass().getName());
164 				buf.append(sup.sqlStringForSQLExpression(sub, exp));
165 				if (i < qus.count()-1)
166 					buf.append(" AND ");
167 			}
168 			if (qus.count() > 1) {
169 				buf.insert(0, '(');
170 				buf.append(')');
171 			}
172 			return buf.toString();
173 		}
174 
175 		public EOQualifier schemaBasedQualifierWithRootEntity(EOQualifier q, EOEntity e) {
176 			return q;
177 		}
178 
179 		public EOQualifier qualifierMigratedFromEntityRelationshipPath(EOQualifier q, EOEntity e, String path) {
180 			return q;
181 		}
182 
183 	}
184 
185 	public class OrQualifierSupport extends Support {
186 
187 		public OrQualifierSupport() {
188 			super();
189 		}
190 
191 		public String sqlStringForSQLExpression(EOQualifier qualifier, EOSQLExpression exp) {
192 			EOOrQualifier q = (EOOrQualifier)qualifier;
193 			NSArray qus = q.qualifiers();
194 			StringBuffer buf = new StringBuffer();
195 			for (int i = 0; i < qus.count(); i++) {
196 				EOQualifier sub = (EOQualifier)qus.objectAtIndex(i);
197 				EOQualifierSQLGeneration.Support sup = EOQualifierSQLGeneration.Support.supportForClass(sub.getClass());
198 				if (sup == null)
199 					throw new IllegalStateException("Cannot find support class for " + sub.getClass().getName());
200 				buf.append(sup.sqlStringForSQLExpression(sub, exp));
201 				if (i < qus.count()-1)
202 					buf.append(" OR ");
203 			}
204 			if (qus.count() > 1) {
205 				buf.insert(0, '(');
206 				buf.append(')');
207 			}
208 			return buf.toString();
209 		}
210 
211 		public EOQualifier schemaBasedQualifierWithRootEntity(EOQualifier q, EOEntity e) {
212 			return q;
213 		}
214 
215 		public EOQualifier qualifierMigratedFromEntityRelationshipPath(EOQualifier q, EOEntity e, String path) {
216 			return q;
217 		}
218 
219 	}
220 
221 }
222 /*
223  * $Log$
224  * Revision 1.2  2006/02/16 16:47:14  cgruber
225  * Move some classes in to "internal" packages and re-work imports, etc.
226  *
227  * Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions.
228  *
229  * Revision 1.1  2006/02/16 13:19:57  cgruber
230  * Check in all sources in eclipse-friendly maven-enabled packages.
231  *
232  * Revision 1.3  2003/08/14 02:13:10  chochos
233  * KeyValueQualifierSupport generates proper SQL
234  *
235  * Revision 1.2  2003/08/14 01:05:51  chochos
236  * added abstract Support inner class, with incomplete implementations for the main qualifiers (not, and, or, key-value, key-comparison)
237  *
238  * Revision 1.1  2003/08/12 01:45:49  chochos
239  * interface to be implemented by qualifiers (or support classes) to indicate they can generate SQL
240  *
241  */