1 | /* Copyright 2010 the original author or authors. |
---|
2 | * |
---|
3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | * you may not use this file except in compliance with the License. |
---|
5 | * You may obtain a copy of the License at |
---|
6 | * |
---|
7 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | * |
---|
9 | * Unless required by applicable law or agreed to in writing, software |
---|
10 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | * See the License for the specific language governing permissions and |
---|
13 | * limitations under the License. |
---|
14 | */ |
---|
15 | |
---|
16 | /** |
---|
17 | * Provides a DSL for building and managing HQL strings. |
---|
18 | * For usage examples see the HqlBuilderTests. |
---|
19 | * HQL reference see http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/queryhql.html |
---|
20 | * |
---|
21 | * Primary goals: |
---|
22 | * 1. Easy to read and understand in code. |
---|
23 | * 2. Easy to read and understand when printed (e.g when displayed in a report). |
---|
24 | * 3. Easy to execute with correct paginateParams and namedParams. |
---|
25 | * 4. Easy to change a clause and execute again. |
---|
26 | * |
---|
27 | * @author Gavin Kromhout |
---|
28 | * @version DraftA |
---|
29 | * |
---|
30 | */ |
---|
31 | class HqlBuilder { |
---|
32 | |
---|
33 | // Query clauses. |
---|
34 | // Each clause is a key with a list of terms. |
---|
35 | def clauses = [:] |
---|
36 | |
---|
37 | // HQL namedParams. |
---|
38 | // HQL requires the namedParams to match exactly with the clause expressions. |
---|
39 | def namedParams = [:] |
---|
40 | |
---|
41 | // HQL paginateParams. |
---|
42 | def paginateParams = [max: 1000, offset: 0] |
---|
43 | |
---|
44 | // It is easier and more flexible to simply add order as a clause, e.g: order 'by id asc' |
---|
45 | //def sort = "" // e.g. instanceName.id |
---|
46 | //def order = "" // e.g. asc or desc |
---|
47 | |
---|
48 | def HqlBuilder(debug = false) { |
---|
49 | if(!super.metaClass.hasMetaProperty('log')) |
---|
50 | mockLogging(debug) |
---|
51 | log.debug "HqlBuilder()" |
---|
52 | } |
---|
53 | |
---|
54 | def call() { |
---|
55 | log.debug "call()" |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * Call with closure as last arg. |
---|
60 | * A typically used build call, e.g: q { } is equivalent to q.call() { } |
---|
61 | */ |
---|
62 | def call(Closure cl) { |
---|
63 | log.debug "call(Closure cl)" |
---|
64 | handleClosure(cl) |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * Domain specific build method. |
---|
69 | * Has no real use other than to prevent obscure errors |
---|
70 | * when user makes a call to query() and Groovy calls query(Closure cl) |
---|
71 | * @returns This object. |
---|
72 | */ |
---|
73 | def query() { |
---|
74 | log.debug "query()" |
---|
75 | return this // Must return this object to q. |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * Domain specific build method. |
---|
80 | * A typically used build call, e.g: def q = new HqlBuilder().query { } |
---|
81 | * |
---|
82 | * @param cl The supplied Closure. |
---|
83 | * @returns This object. |
---|
84 | * |
---|
85 | */ |
---|
86 | def query(Closure cl) { |
---|
87 | log.debug "query(Closure cl)" |
---|
88 | handleClosure(cl) |
---|
89 | return this // Must return this object to q. |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * InvokeMethod resolves all undefined methods. |
---|
94 | * Which include the clause methods, e.g select 'book' is equivalent to select('book'). |
---|
95 | * Note that defined methods will be called directly since this class does not implement GroovyInterceptable. |
---|
96 | * If class was "HqlBuilder implements GroovyInterceptable" then even println would be intercepted and |
---|
97 | * several exlusions might be needed. e.g: if(methodName != 'call' && methodName != 'println') |
---|
98 | */ |
---|
99 | def invokeMethod(String name, args) { |
---|
100 | |
---|
101 | log.debug "invokeMethod(${methodName}, ${args})" |
---|
102 | |
---|
103 | if(!this.clauses[name]) |
---|
104 | this.clauses[name] = [] |
---|
105 | |
---|
106 | for(arg in args) { |
---|
107 | if(arg instanceof String) |
---|
108 | this.clauses[name] << arg |
---|
109 | } |
---|
110 | |
---|
111 | if(args[-1] instanceof Closure) |
---|
112 | handleClosure(args[-1]) |
---|
113 | |
---|
114 | } // invokeMethod() |
---|
115 | |
---|
116 | /** |
---|
117 | * PropertyMissing. |
---|
118 | * Allows clauses to be added after build, e.g: q.order = 'by book.name asc' |
---|
119 | * and clauses to be removed, e.g: q.order = null |
---|
120 | */ |
---|
121 | def propertyMissing(String name, value) { |
---|
122 | log.debug "propertyMissing(${propertyName}, ${value})" |
---|
123 | if(value == null) { |
---|
124 | clauses.remove(name) |
---|
125 | return |
---|
126 | } |
---|
127 | |
---|
128 | if(value instanceof String) |
---|
129 | clauses[name] = [value] |
---|
130 | } |
---|
131 | |
---|
132 | /** |
---|
133 | * PropertyMissing. |
---|
134 | * Allow clauses to be accessed directly by name, e.g: println q.order. |
---|
135 | * Since clauses is a Map null is simply returned for a non-existant clause. |
---|
136 | */ |
---|
137 | def propertyMissing(String name) { |
---|
138 | log.debug "propertyMissing(${name})" |
---|
139 | clauses[name] |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | * HandleClosure. |
---|
144 | * Setting delegate and DELEGATE_FIRST allows closure to access this object's properties first. |
---|
145 | */ |
---|
146 | private handleClosure(Closure cl) { |
---|
147 | cl.delegate = this |
---|
148 | cl.resolveStrategy = Closure.DELEGATE_FIRST |
---|
149 | cl.call() |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * MockLogging. |
---|
154 | * This class has super cow powers and can mock out it's own debug logging. |
---|
155 | */ |
---|
156 | private mockLogging(debug = false) { |
---|
157 | def mockLogger = {} |
---|
158 | if(debug) { |
---|
159 | mockLogger = {msg -> |
---|
160 | println "${super.getClass()} - DEBUG: $msg" |
---|
161 | } |
---|
162 | } |
---|
163 | super.metaClass.log = [debug: mockLogger] |
---|
164 | log.debug "Internal mockLogger configured." |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * GetQuery. |
---|
169 | * Assemble and return the query in a format that can be directly executed. |
---|
170 | * E.g: executeQuery(q.query, q.namedParams, q.paginateParams). |
---|
171 | */ |
---|
172 | def getQuery() { |
---|
173 | this.clauses.collect { |
---|
174 | it.key + ' ' + it.value.join(' ') |
---|
175 | }.join(' ') |
---|
176 | } |
---|
177 | |
---|
178 | /** |
---|
179 | * GetPrintFormattedQuery. |
---|
180 | * Assemble and return the query in a format that can be more easily printed and read by a person. |
---|
181 | * E.g: println q.printFormattedQuery or when displayed in a report. |
---|
182 | */ |
---|
183 | def getPrintFormattedQuery() { |
---|
184 | this.clauses.collect { |
---|
185 | it.key + ' ' + it.value.join(' \n') |
---|
186 | }.join(' \n') |
---|
187 | } |
---|
188 | |
---|
189 | } // end class |
---|