Coverage Report - net.wotonomy.access.EOQualifierSQLGeneration
 
Classes in this File Line Coverage Branch Coverage Complexity
EOQualifierSQLGeneration
0% 
0% 
1.448
 
 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  0
         public abstract class Support {
 45  
 
 46  0
                 private static NSMutableDictionary _classes = new NSMutableDictionary();
 47  
 
 48  
                 static {
 49  0
                         setSupportForClass(new KeyValueQualifierSupport(), EOKeyValueQualifier.class);
 50  0
                         setSupportForClass(new KeyComparisonQualifierSupport(), EOKeyComparisonQualifier.class);
 51  0
                         setSupportForClass(new AndQualifierSupport(), EOAndQualifier.class);
 52  0
                         setSupportForClass(new OrQualifierSupport(), EOOrQualifier.class);
 53  0
                         setSupportForClass(new NotQualifierSupport(), EONotQualifier.class);
 54  0
                 }
 55  
 
 56  
                 public Support() {
 57  0
                         super();
 58  0
                 }
 59  
 
 60  
                 public static void setSupportForClass(Support sup, Class aClass) {
 61  0
                         _classes.setObjectForKey(sup, aClass.getName());
 62  0
                 }
 63  
                 public static Support supportForClass(Class aClass) {
 64  0
                         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  0
                         super();
 79  0
                 }
 80  
 
 81  
                 public String sqlStringForSQLExpression(EOQualifier qualifier, EOSQLExpression exp) {
 82  0
                         EOKeyValueQualifier q = (EOKeyValueQualifier)qualifier;
 83  0
                         String sql1 = exp.sqlStringForAttributeNamed(q.key());
 84  0
                         String sql2 = exp.sqlStringForSelector(q.selector(), q.value());
 85  0
                         String sql3 = exp.sqlStringForValue(q.value(), q.key());
 86  0
                         if (q.selector() == EOQualifier.QualifierOperatorCaseInsensitiveLike)
 87  0
                                 return exp.sqlStringForCaseInsensitiveLike(sql1, sql3);
 88  0
                         else if (q.selector() == EOQualifier.QualifierOperatorLike)
 89  0
                                 sql3 = exp.sqlPatternFromShellPattern(sql3);
 90  0
                         return  sql1 + sql2 + sql3;
 91  
                 }
 92  
 
 93  
                 public EOQualifier schemaBasedQualifierWithRootEntity(EOQualifier q, EOEntity e) {
 94  0
                         return q;
 95  
                 }
 96  
 
 97  
                 public EOQualifier qualifierMigratedFromEntityRelationshipPath(EOQualifier q, EOEntity e, String path) {
 98  0
                         return q;
 99  
                 }
 100  
 
 101  
         }
 102  
 
 103  
         public class KeyComparisonQualifierSupport extends Support {
 104  
 
 105  
                 public KeyComparisonQualifierSupport() {
 106  0
                         super();
 107  0
                 }
 108  
 
 109  
                 public String sqlStringForSQLExpression(EOQualifier qualifier, EOSQLExpression exp) {
 110  0
                         EOKeyComparisonQualifier q = (EOKeyComparisonQualifier)qualifier;
 111  0
                         return exp.sqlStringForAttributeNamed(q.leftKey()) +
 112  0
                                 exp.sqlStringForSelector(q.selector(), null) +
 113  0
                                 exp.sqlStringForAttributeNamed(q.rightKey());
 114  
                 }
 115  
 
 116  
                 public EOQualifier schemaBasedQualifierWithRootEntity(EOQualifier q, EOEntity e) {
 117  0
                         return q;
 118  
                 }
 119  
 
 120  
                 public EOQualifier qualifierMigratedFromEntityRelationshipPath(EOQualifier q, EOEntity e, String path) {
 121  0
                         return q;
 122  
                 }
 123  
 
 124  
         }
 125  
 
 126  
         public class NotQualifierSupport extends Support {
 127  
 
 128  
                 public NotQualifierSupport() {
 129  0
                         super();
 130  0
                 }
 131  
 
 132  
                 public String sqlStringForSQLExpression(EOQualifier qualifier, EOSQLExpression exp) {
 133  0
                         EONotQualifier q = (EONotQualifier)qualifier;
 134  0
                         return "NOT (" + EOQualifierSQLGeneration.Support.supportForClass(q.qualifier().getClass()).sqlStringForSQLExpression(q.qualifier(), exp) + ")";
 135  
                 }
 136  
 
 137  
                 public EOQualifier schemaBasedQualifierWithRootEntity(EOQualifier qualifier, EOEntity e) {
 138  0
                         EONotQualifier q = (EONotQualifier)qualifier;
 139  0
                         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  0
                         EONotQualifier q = (EONotQualifier)qualifier;
 144  0
                         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  0
                         super();
 153  0
                 }
 154  
 
 155  
                 public String sqlStringForSQLExpression(EOQualifier qualifier, EOSQLExpression exp) {
 156  0
                         EOAndQualifier q = (EOAndQualifier)qualifier;
 157  0
                         NSArray qus = q.qualifiers();
 158  0
                         StringBuffer buf = new StringBuffer();
 159  0
                         for (int i = 0; i < qus.count(); i++) {
 160  0
                                 EOQualifier sub = (EOQualifier)qus.objectAtIndex(i);
 161  0
                                 EOQualifierSQLGeneration.Support sup = EOQualifierSQLGeneration.Support.supportForClass(sub.getClass());
 162  0
                                 if (sup == null)
 163  0
                                         throw new IllegalStateException("Cannot find support class for " + sub.getClass().getName());
 164  0
                                 buf.append(sup.sqlStringForSQLExpression(sub, exp));
 165  0
                                 if (i < qus.count()-1)
 166  0
                                         buf.append(" AND ");
 167  
                         }
 168  0
                         if (qus.count() > 1) {
 169  0
                                 buf.insert(0, '(');
 170  0
                                 buf.append(')');
 171  
                         }
 172  0
                         return buf.toString();
 173  
                 }
 174  
 
 175  
                 public EOQualifier schemaBasedQualifierWithRootEntity(EOQualifier q, EOEntity e) {
 176  0
                         return q;
 177  
                 }
 178  
 
 179  
                 public EOQualifier qualifierMigratedFromEntityRelationshipPath(EOQualifier q, EOEntity e, String path) {
 180  0
                         return q;
 181  
                 }
 182  
 
 183  
         }
 184  
 
 185  
         public class OrQualifierSupport extends Support {
 186  
 
 187  
                 public OrQualifierSupport() {
 188  0
                         super();
 189  0
                 }
 190  
 
 191  
                 public String sqlStringForSQLExpression(EOQualifier qualifier, EOSQLExpression exp) {
 192  0
                         EOOrQualifier q = (EOOrQualifier)qualifier;
 193  0
                         NSArray qus = q.qualifiers();
 194  0
                         StringBuffer buf = new StringBuffer();
 195  0
                         for (int i = 0; i < qus.count(); i++) {
 196  0
                                 EOQualifier sub = (EOQualifier)qus.objectAtIndex(i);
 197  0
                                 EOQualifierSQLGeneration.Support sup = EOQualifierSQLGeneration.Support.supportForClass(sub.getClass());
 198  0
                                 if (sup == null)
 199  0
                                         throw new IllegalStateException("Cannot find support class for " + sub.getClass().getName());
 200  0
                                 buf.append(sup.sqlStringForSQLExpression(sub, exp));
 201  0
                                 if (i < qus.count()-1)
 202  0
                                         buf.append(" OR ");
 203  
                         }
 204  0
                         if (qus.count() > 1) {
 205  0
                                 buf.insert(0, '(');
 206  0
                                 buf.append(')');
 207  
                         }
 208  0
                         return buf.toString();
 209  
                 }
 210  
 
 211  
                 public EOQualifier schemaBasedQualifierWithRootEntity(EOQualifier q, EOEntity e) {
 212  0
                         return q;
 213  
                 }
 214  
 
 215  
                 public EOQualifier qualifierMigratedFromEntityRelationshipPath(EOQualifier q, EOEntity e, String path) {
 216  0
                         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  
  */