-
Notifications
You must be signed in to change notification settings - Fork 15
/
list.cfc
368 lines (242 loc) · 13 KB
/
list.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<cfcomponent>
<!-------------------------------------------------------------------------------------------->
<!----------------------------- Parameters and Initialization -------------------------------->
<!-------------------------------------------------------------------------------------------->
<!---------------------------------------------------------------------------------------- init
Description:
---------------------------------------------------------------------------------------------->
<cffunction name="init" access="public" returntype="void">
<cfargument name="object" type="model" required="yes" />
<cfargument name="query" type="query" required="yes" />
<cfset variables.object = arguments.object />
<cfset variables.query = arguments.query/>
<cfset variables.length = arguments.query.recordcount />
<cfif queryIsComplex()>
<cfquery name="variables.chunks" dbtype="query">
SELECT DISTINCT #variables.object.group_by#
FROM variables.query
WHERE #variables.object.group_by# IS NOT NULL
<cfif structKeyExists(variables, 'order_by')>
ORDER BY #variables.order_by# #variables.direction#
</cfif>
</cfquery>
<cfset variables.length = variables.chunks.recordcount />
</cfif>
<cfset reset() />
</cffunction>
<!-------------------------------------------------------------------------------------------->
<!---------------------------------- Basic List Functions ------------------------------------>
<!-------------------------------------------------------------------------------------------->
<!------------------------------------------------------------------------------------- current
Description:
---------------------------------------------------------------------------------------------->
<cffunction name="current" access="public" returntype="model" output="false">
<cfreturn variables.object />
</cffunction>
<!---------------------------------------------------------------------------------------- next
Description:
---------------------------------------------------------------------------------------------->
<cffunction name="next" access="public" returntype="boolean" output="false">
<cfif variables.current_row EQ variables.length>
<cfreturn false />
</cfif>
<cfset variables.current_row = variables.current_row + 1 />
<cfset loadCurrentValues() />
<cfreturn true />
</cffunction>
<!---------------------------------------------------------------------------------------- prev
Description:
---------------------------------------------------------------------------------------------->
<cffunction name="prev" access="public" returntype="boolean" output="false">
<cfif variables.current_row EQ 0>
<cfreturn false />
</cfif>
<cfset variables.current_row = variables.current_row - 1 />
<cfset loadCurrentValues() />
<cfreturn true />
</cffunction>
<!--------------------------------------------------------------------------------------- reset
Description:
---------------------------------------------------------------------------------------------->
<cffunction name="reset" access="public" returntype="void" output="false">
<cfset variables.current_row = 0 />
</cffunction>
<!-------------------------------------------------------------------------------------- length
Description:
---------------------------------------------------------------------------------------------->
<cffunction name="length" access="public" returntype="numeric" output="false">
<cfreturn variables.length />
</cffunction>
<!------------------------------------------------------------------------------------- isEmpty
Description:
---------------------------------------------------------------------------------------------->
<cffunction name="isEmpty" access="public" returntype="numeric" output="false">
<cfreturn variables.length eq 0 />
</cffunction>
<!---------------------------------------------------------------------------------------- copy
Description:
---------------------------------------------------------------------------------------------->
<cffunction name="copy" access="public" returntype="list" output="false">
<cfset var list = createObject('component', 'list') />
<cfset list.init(variables.object, Duplicate(variables.query)) />
<cfif structKeyExists(variables, 'order_by')>
<cfset list.order(variables.order_by, variables.direction) />
</cfif>
<cfreturn list />
</cffunction>
<!-------------------------------------------------------------------------------------------->
<!------------------------------ Query Manipulation Functions -------------------------------->
<!-------------------------------------------------------------------------------------------->
<!-------------------------------------------------------------------------------------- filter
Description:
---------------------------------------------------------------------------------------------->
<cffunction name="filter" access="public" returntype="void">
<cfargument name="condition" type="string" required="yes" />
<cfquery name="variables.query" dbtype="query">
SELECT *
FROM variables.query
WHERE #preserveSingleQuotes(arguments.condition)#
<cfif structKeyExists(variables, 'order_by')>
ORDER BY #variables.order_by#
</cfif>
</cfquery>
<cfset init(variables.object, variables.query) />
</cffunction>
<!--------------------------------------------------------------------------------------- order
Description:
---------------------------------------------------------------------------------------------->
<cffunction name="order" access="public" returntype="void" output="false">
<cfargument name="order_by" type="string" required="yes" />
<cfargument name="direction" type="string" default="" />
<cfset variables.order_by = arguments.order_by />
<cfset variables.direction = arguments.direction />
<cfquery name="variables.query" dbtype="query">
SELECT * FROM variables.query
ORDER BY #variables.order_by# #variables.direction#
</cfquery>
<cfset setQuery(variables.query) />
</cffunction>
<!-------------------------------------------------------------------------------------------->
<!---------------------------------------- Accessors ----------------------------------------->
<!-------------------------------------------------------------------------------------------->
<!----------------------------------------------------------------------------------- getObject
Description:
---------------------------------------------------------------------------------------------->
<cffunction name="getObject" access="public" returntype="datamodel" output="false">
<cfreturn variables.object />
</cffunction>
<!------------------------------------------------------------------------------------ setQuery
Description:
---------------------------------------------------------------------------------------------->
<cffunction name="setQuery" access="public" returntype="void" output="false">
<cfargument name="query" type="query" required="yes" />
<cfset init(variables.object,arguments.query) />
</cffunction>
<!----------------------------------------------------------------------------------- setObject
Description:
---------------------------------------------------------------------------------------------->
<cffunction name="setObject" access="public" returntype="void" output="false">
<cfargument name="object" type="datamodel" required="yes" />
<cfset init(arguments.object,variables.query) />
</cffunction>
<!-------------------------------------------------------------------------------------------->
<!--------------------------------------- Pagination ----------------------------------------->
<!-------------------------------------------------------------------------------------------->
<!------------------------------------------------------------------------------------ paginate
Description:
---------------------------------------------------------------------------------------------->
<cffunction name="paginate" access="public" returntype="void">
<cfargument name="page" type="numeric" required="yes" />
<cfargument name="offset" type="numeric" required="yes" />
<cfset variables.current_row = (arguments.page - 1) * (arguments.offset) />
<cfset variables.length = min(variables.query.recordcount, (arguments.offset) * (arguments.page)) />
</cffunction>
<!-------------------------------------------------------------------------------- currentIndex
Description:
---------------------------------------------------------------------------------------------->
<cffunction name="currentIndex" access="public" returntype="numeric" output="false">
<cfreturn variables.current_row />
</cffunction>
<!-------------------------------------------------------------------------------------------->
<!---------------------------------- Conversion Functions ------------------------------------>
<!-------------------------------------------------------------------------------------------->
<!------------------------------------------------------------------------------------- toArray
Description:
---------------------------------------------------------------------------------------------->
<cffunction name="toArray" access="public" returntype="array" output="false">
<cfset var array = ArrayNew(1) />
<cfset var saved_current_row = variables.current_row />
<cfset var i = 1 />
<cfset variables.current_row = 0 />
<cfloop condition="#next()#">
<cfset array[i] = current().clone() />
<cfset i = i + 1 />
</cfloop>
<cfset variables.current_row = saved_current_row />
<cfreturn array />
</cffunction>
<!------------------------------------------------------------------------------------- toQuery
Description:
---------------------------------------------------------------------------------------------->
<cffunction name="toQuery" access="public" returntype="query" output="false">
<cfreturn variables.query />
</cffunction>
<!----------------------------------------------------------------------------------- toOptions
Description:
---------------------------------------------------------------------------------------------->
<cffunction name="toOptions" access="public" returntype="query" output="false">
<cfargument name="value_field" type="string" required="yes" />
<cfargument name="label_field" type="string" required="yes" />
<cfset var result = "" />
<cfquery name="result" dbtype="query">
SELECT
#arguments.value_field# as [value],
#arguments.label_field# as [label]
FROM variables.query
</cfquery>
<cfreturn result />
</cffunction>
<!-------------------------------------------------------------------------------------------->
<!------------------------------------- Private Helpers -------------------------------------->
<!-------------------------------------------------------------------------------------------->
<!--------------------------------------------------------------------------- loadCurrentValues
Description:
---------------------------------------------------------------------------------------------->
<cffunction name="loadCurrentValues" access="private" returntype="void">
<cfset var subquery = "" />
<cfif queryIsComplex()>
<cfquery name="subquery" dbtype="query">
SELECT *
FROM variables.query
<cfif variables.chunks[variables.object.group_by][variables.current_row] NEQ "">
WHERE #variables.object.group_by# =
#variables.chunks[variables.object.group_by][variables.current_row]#
</cfif>
<cfif structKeyExists(variables, 'order_by')>
ORDER BY #variables.order_by#
</cfif>
</cfquery>
<cfset variables.object.load(subquery) />
<cfelse>
<cfset variables.object.load(rowToStruct(variables.query)) />
</cfif>
</cffunction>
<!--------------------------------------------------------------------------------- rowToStruct
Description:
---------------------------------------------------------------------------------------------->
<cffunction name="rowToStruct" access="private" returntype="struct" output="false">
<cfargument name="query" type="query" required="yes" />
<cfset var struct = structNew() />
<cfloop list="#query.columnlist#" index="column">
<cfset struct[column] = query[column][variables.current_row] />
</cfloop>
<cfreturn struct />
</cffunction>
<!------------------------------------------------------------------------------ queryIsComplex
Description:
---------------------------------------------------------------------------------------------->
<cffunction name="queryIsComplex" access="private" returntype="boolean">
<cfreturn structKeyExists(variables.object, 'group_by') AND
structKeyExists(variables.query, variables.object.group_by) />
</cffunction>
</cfcomponent>