-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSQLSelect.cls
More file actions
304 lines (273 loc) · 8.79 KB
/
SQLSelect.cls
File metadata and controls
304 lines (273 loc) · 8.79 KB
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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "SQLSelect"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit
' Class: SQLSelect
' A SQL Select query
Implements iSQLQuery
Private oSQL As SQLQuery
Private vFields() As Variant
Private bDistinct As Boolean
Private vGroupBy As Variant
Private oHaving As SQLCondition
Private oHavingGroup As SQLWhereGroup
Private aJoin() As Variant 'An array of arrays. Each array is a 'from_item'
Private aOrder() As Variant
Private sUnion() As Variant
'Limit
'Offset
' Property: Table
Property Let Table(sValue As String)
addTable sValue, ""
End Property
' Property: Fields
' The fields in the query
Property Let Fields(vValue)
Dim element As Variant
For Each element In vValue
AddField element
Next
End Property
' Property: GroupBy
' The field to use for aggregration
Property Let GroupBy(vValue)
vGroupBy = vValue
End Property
' Constructor: Class_Initialize
' Initializes class members
Private Sub Class_Initialize()
ReDim aJoin(0)
ReDim vFields(0)
ReDim aOrder(0)
vFields(0) = ""
aOrder(0) = ""
bDistinct = False
vGroupBy = Array()
Set oSQL = New SQLQuery
End Sub
' Sub: addField
' Add a field to the query statement
Public Sub AddField(sField, Optional sAlias As String = "")
Dim ArrLen As Integer
ArrLen = UBound(vFields)
If ArrLen = 0 Then
If Not IsArray(vFields(0)) Then
ArrLen = -1
End If
End If
ReDim Preserve vFields(0 To ArrLen + 1)
vFields(ArrLen + 1) = Array(sField, sAlias)
End Sub
' Sub: addTable
' Add a table to the query statement
Public Sub addTable(sName As String, Optional sAlias As String = "")
aJoin = ArrayPush(aJoin, Array("", sName, sAlias, ""))
End Sub
' Sub: AddHaving
' Add a having clause o the SQL statement
Public Sub AddHaving(Field, Value, Optional op As String = "=", Optional GroupType As String = "AND")
Dim NewHaving As New SQLCondition
If Not (oHaving Is Nothing) Then
NewHaving.Create Field, op, Value
Set oHavingGroup = New SQLWhereGroup
oHavingGroup.SetGroup oHaving, NewHaving, GroupType
'Clear SQLWhere since we are using SQLWhereGroup instead
Set oHaving = Nothing
ElseIf oHavingGroup Is Nothing Then
Set oHaving = New SQLCondition
oHaving.Create Field, op, Value
Else
NewHaving.Create Field, op, Value
oHavingGroup.AddWhere NewHaving, GroupType
End If
End Sub
' Sub: AddArgument
' Add an argument to the SQL Statement.
' An argument is a value for a placeholder.
Public Sub AddArgument(sName As String, vValue)
oSQL.AddArgument sName, vValue
End Sub
' Sub: AddJoin
' Add a join condition to the SQL query
Public Sub AddJoin(sType As String, sTable As String, Optional sAlias As String = "", Optional sCondition As String = "")
'Should Check that sType is either "INNER", "LEFT OUTER", or "RIGHT OUTER"
Dim JoinLen As Integer
JoinLen = UBound(aJoin)
ReDim Preserve aJoin(0 To JoinLen + 1)
aJoin(JoinLen + 1) = Array(sType, sTable, sAlias, sCondition)
End Sub
' Sub: InnerJoin
' Add an inner join to the SQL query
Public Sub InnerJoin(sTable As String, sAlias As String, sCondition As String)
AddJoin "INNER", sTable, sAlias, sCondition
End Sub
' Sub: LeftJoin
' Add a left join to the SQL query
Public Sub LeftJoin(sTable, sAlias, Optional sCondition As String = "")
AddJoin "LEFT OUTER", sTable, sAlias, sCondition
End Sub
' Sub: RightJoin
' Add a right join to the SQL query
' Left Joins are prefered over Right. Please edit query to use a left join
Public Sub RightJoin(sTable, sAlias, Optional sCondition As String = "")
AddJoin "RIGHT OUTER", sTable, sAlias, sCondition
End Sub
' Sub: Union
' Join two SQLSelect objects together with a union
' sType is either "", "ALL", or "DISTINCT"
Public Sub Union(oSelect As SQLSelect, Optional sType = "")
Dim UnionArray() As Variant
UnionArray = Array(SQLSelect, sType)
'Add UnionArray to aUnion
End Sub
' Sub: Distinct
' Set the distict flag to true or false
Public Sub Distinct(Optional bValue = True)
bDistinct = bValue
End Sub
' Sub: AddWhere
' Add a where clause to the SQL query
Public Sub AddWhere(Field, Value, Optional op As String = "=", Optional GroupType As String = "AND")
oSQL.AddWhere Field, Value, op, GroupType
End Sub
Public Sub AddWhereGroup(NewWhereGroup As SQLWhereGroup, Optional GroupType As String = "AND")
oSQL.AddWhereGroup NewWhereGroup, GroupType
End Sub
Public Sub OrderBy(sField As String, Optional sDirection As String = "ASC")
If sDirection = "DESC" Then
sDirection = "DESC"
Else
sDirection = "ASC"
End If
Dim ArrLen As Integer
ArrLen = UBound(aOrder)
If ArrLen = 0 Then
If aOrder(0) = "" Then
ArrLen = -1
End If
End If
ReDim Preserve aOrder(0 To ArrLen + 1)
aOrder(ArrLen + 1) = Array(sField, sDirection)
End Sub
' Function: getByProperty
' Generate a query of the form
' SELECT sField FROM sTableValue WHERE sProperty = vValue
'
' Parameters:
' sTableValue - The table to select the data from
' sField - The field name
' sProperty - The field to filter by
' vValue - The value to filter by
Public Sub getByProperty(sTableValue As String, sField As String, sProperty As String, vValue)
addTable sTableValue, ""
AddField sField
AddWhere sProperty, vValue
End Sub
' Function: iSQLQuery_ToString
' Create the query string
Public Function iSQLQuery_ToString() As String
Dim return_string As String
If UBound(vFields) < 0 Then
return_string = ""
Else
return_string = "SELECT " & DistinctString & FieldList & " FROM " & _
JoinString & oSQL.WhereString & GroupByString & HavingString & OrderByString
End If
iSQLQuery_ToString = oSQL.ReplaceArguments(return_string)
End Function
Private Function DistinctString() As String
Dim sDistinct As String
sDistinct = ""
If bDistinct Then
sDistinct = "DISTINCT "
End If
DistinctString = sDistinct
End Function
Private Function JoinString()
Dim R As Long
Dim I As Long
Dim Lines() As String
Dim Line As String
Dim LineArray As Variant
ReDim Lines(0 To UBound(aJoin))
For R = 0 To UBound(aJoin)
LineArray = aJoin(R)
Line = ""
If LineArray(0) <> "" Then
Line = LineArray(0) & " JOIN "
ElseIf R > 0 Then
Lines(R - 1) = Lines(R - 1) & ","
End If
Line = Line & LineArray(1)
If LineArray(2) <> "" Then
Line = Line & " " & LineArray(2)
End If
If LineArray(3) <> "" Then
Line = Line & " ON " & LineArray(3)
End If
'Add inicial open parenthesis as needed join
If R = 0 And UBound(aJoin) > 1 Then
For I = 2 To UBound(aJoin)
Line = " ( " & Line
Next I
Lines(R) = Line
'Add close parenthesis for join
ElseIf R > 0 And R < UBound(aJoin) And UBound(aJoin) > 1 Then
Lines(R) = Line & " ) "
Else
'Normal output
Lines(R) = Line
End If
Next R
JoinString = Join(Lines, " ")
End Function
Private Function GroupByString() As String
If UBound(vGroupBy) > -1 Then
GroupByString = " GROUP BY " & Join(vGroupBy, ", ")
End If
End Function
Private Function HavingString() As String
Dim sHaving As String
If Not (oHaving Is Nothing And oHavingGroup Is Nothing) Then
If Not (oHaving Is Nothing) Then
sHaving = oHaving.toString
Else
sHaving = oHavingGroup.toString
End If
HavingString = " HAVING " & sHaving
End If
End Function
Private Function OrderByString() As String
OrderByString = ""
If IsArray(aOrder(0)) Then
OrderByString = " ORDER BY " & JoinArrayofArrays(aOrder)
End If
End Function
Private Function FieldList()
Dim R As Long
Dim Lines() As String
Dim Line As String
Dim LineArray As Variant
ReDim Lines(0 To UBound(vFields))
For R = 0 To UBound(vFields)
LineArray = vFields(R)
Line = LineArray(0)
If LineArray(1) <> "" Then
Line = Line & " AS " & LineArray(1)
End If
Lines(R) = Line
Next R
FieldList = Join(Lines, ", ")
End Function
Private Function UnionString()
Dim NewSelect As iSQLQuery
aUnion = UnionArray(0)
Set NewSelect = aUnion(0)
UnionString = " UNION " & NewSelect.toString()
End Function