root/trunk/src/main/groovy/org/chama/builder/model/QueryBuilder.groovy

Revision 63, 5.2 kB (checked in by warner, 1 year ago)

Incomplete - # 27: Create model template for groovy model creation
http://warneronstine.com/cgi-bin/trac.cgi/ticket/27
just wrapping this up

Line 
1 package org.chama.builder.model
2
3 import org.codehaus.groovy.runtime.InvokerHelper
4 import groovy.lang.Closure
5
6 import java.util.List
7 import java.util.Map
8
9 import org.chama.Inflection
10
11 class QueryBuilder {
12     def tableName
13     def className
14     def returnType
15     def query
16     def criteria
17     def parent
18    
19     //conditionals
20     def static AND = "and"; // builder
21     def static IS_NULL = "isNull"; // builder
22         def static IS_NOT_NULL = "notNull"; // builder
23         def static NOT = "not";// builder
24         def static OR = "or"; // builder
25         def static ID_EQUALS = "idEq"; // builder
26         def static IS_EMPTY = "isEmpty"; //builder
27         def static IS_NOT_EMPTY = "isNotEmpty"; //builder
28        
29         def static SELECT = "select" //method
30         def static DELETE = "delete" //method
31     def static BETWEEN = "between" //method
32     def static EQUALS = "eq" //method
33     def static EQUALS_PROPERTY = "eqProperty" //method
34     def static GREATER_THAN = "gt" //method
35     def static GREATER_THAN_PROPERTY = "gtProperty" //method
36     def static GREATER_THAN_OR_EQUAL = "ge" //method
37     def static GREATER_THAN_OR_EQUAL_PROPERTY = "geProperty" //method
38     def static ILIKE = "ilike" //method
39     def static IN = "in" //method
40     def static LESS_THAN = "lt" //method
41     def static LESS_THAN_PROPERTY = "ltProperty" //method
42     def static LESS_THAN_OR_EQUAL = "le" //method
43     def static LESS_THAN_OR_EQUAL_PROPERTY = "leProperty" //method
44     def static LIKE = "like" //method
45     def static NOT_EQUAL = "ne" //method
46     def static NOT_EQUAL_PROPERTY = "neProperty" //method
47     def static SIZE_EQUALS = "sizeEq" //method
48     def static ORDER_DESCENDING = "desc"
49     def static ORDER_ASCENDING = "asc"
50    
51     def queryTypes = ['select', 'update', 'remove']
52        
53         def QueryBuilder(className, returnType) {
54             this.className = className
55             this.returnType = returnType
56         this.tableName = Inflection.pluralize(className)
57         }
58        
59     def Object invokeMethod(String methodName, arguments) {
60         List list = InvokerHelper.asList(arguments)
61         def name = methodName
62         def closure = null
63             println "attempting to call method: ${methodName} with arguments ${arguments} inside QueryBuilder"
64             if(criteria == null) {
65                 println "criteria is null"
66                 if(queryTypes.contains(name)) {
67                         this.criteria = new Criteria(this.className, name)
68                     }
69             }
70             switch (list.size()) {
71                 case 0:
72                             //probably will need this, just not yet
73                             break
74                 case 1:
75                                 def object = list.get(0)
76                                 if (object instanceof Closure) {
77                                     println "${methodName} closure getting called"
78                     closure = object
79                                     closure.delegate = this
80                                     closure.call()
81                 } else {
82                     println "creating restriction with ${name} ${object}"
83                     createRestriction(name, null, object)
84                 }
85                 break
86                 case 2:
87                     def object1 = list.get(0)
88                     def object2 = list.get(1)
89                     if (object2 instanceof Closure) {
90                     createRestriction(name, null, object1)
91                     closure = object2
92                         closure.delegate = this
93                     closure.call()
94                                 } else if (object2 instanceof Map) {
95                                     println "object 2 is a map"
96                 } else {
97                                     createRestriction(name, object1, object2)
98                                 }
99                         break
100             }
101             //figure out return type and return an empty version of that
102             def returnValue
103             if(returnType == int) {
104                 returnValue = 0
105             } else if(returnType == boolean) {
106                 returnValue = true
107             } else if(returnType == java.util.List) {
108                 returnValue = []
109             } else if(returnType instanceof Object) {
110                 returnValue = InvokerHelper.invokeNoArgumentsConstructorOf(returnType)
111             } else {
112                 returnValue = null
113             }
114             return returnValue
115         }
116    
117     def createRestriction(type, name, value) {
118         def currRestriction
119         println "createRestriction ${type}, ${name}, ${value}"
120         switch(type) {
121             case SELECT:
122                 // need to modify the criteria with columns
123                 criteria.columns = value
124                 break
125             case LIKE:
126                 currRestriction = Restriction.like(name, value)
127                 break
128             case ID_EQUALS:
129                 currRestriction = Restriction.pkEq(className, "id", value)
130                 break
131             case EQUALS:
132                 currRestriction = Restriction.eq(name, value)
133                 break
134         }
135         if(parent != null && currRestriction != null) {
136             restriction.add(currRestriction)
137         } else if(parent == null && currRestriction != null) {
138             //add it to the criteria instead
139             criteria.add(currRestriction)
140         }
141     }
142    
143     def buildCriteria() {
144         return buildReturn() + this.criteria.getQuery()
145     }
146    
147     def private buildReturn() {
148         println "return type is ${returnType}"
149         def returnStr = "        "
150             if(returnType == int) {
151                 returnStr += "return "
152             } else if(returnType == boolean) {
153                 returnStr += "return "
154             } else if(returnType == java.util.List) {
155                 returnStr += "return "
156             } else if(returnType instanceof Object) {
157                 //we are actually returning the class we are looking up
158                 returnStr += "return (${className}) "
159             }
160         return returnStr
161     }
162
163 }
Note: See TracBrowser for help on using the browser.