-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSQLSelect.cls
More file actions
executable file
·264 lines (180 loc) · 6.37 KB
/
SQLSelect.cls
File metadata and controls
executable file
·264 lines (180 loc) · 6.37 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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "SQLSelect"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
' ___________________________________________________
'
' © Hi-Integrity Systems 2007. All rights reserved.
' www.hisystems.com.au - Toby Wicks
' ___________________________________________________
'
Option Explicit
Implements ISQLStatement
Public Enum SQLAggregateFunctionEnum
dboAggregateAverage = 1
dboAggregateCount
dboAggregateSum
dboAggregateMinimum
dboAggregateMaximum
dboAggregateStandardDeviation
dboAggregateVariance
End Enum
Public ConnectionType As ConnectionTypeEnum
Private pobjFields As SQLSelectFields
Private pobjTables As SQLSelectTables
Private pobjConditions As SQLConditions
Private pobjOrderByFields As SQLSelectOrderByFields
Private pobjGroupByFields As SQLSelectGroupByFields
Private pbDistinct As Boolean
Private plngTop As Long
Private pbPerformLocking As Boolean
Public Property Let Top(ByVal lngValue As Long)
If lngValue <= 0 Then
RaiseError dboErrorInvalidArgument, lngValue
End If
plngTop = lngValue
End Property
Public Property Get Top() As Long
Top = plngTop
End Property
''' <summary>
''' Indicates whether the rows that are selected are locked for reading and updating.
''' Equivalent to Serialiazable isolation level.
''' These rows cannot be read or updated until the lock is released.
''' Locks are released when the transaction has been committed or rolled back.
''' </summary>
Public Property Get PerformLocking() As Boolean
PerformLocking = pbPerformLocking
End Property
Public Property Let PerformLocking(ByVal bValue As Boolean)
pbPerformLocking = bValue
End Property
Public Property Get Distinct() As Boolean
Distinct = pbDistinct
End Property
Public Property Let Distinct(ByVal bValue As Boolean)
pbDistinct = bValue
End Property
Public Property Get Tables() As SQLSelectTables
Set Tables = pobjTables
End Property
Public Property Set Tables(ByVal objTables As SQLSelectTables)
If objTables Is Nothing Then
RaiseError dboErrorObjectIsNothing
Else
Set pobjTables = objTables
End If
End Property
Public Property Set Fields(ByVal objValue As SQLSelectFields)
If objValue Is Nothing Then
RaiseError dboErrorObjectIsNothing
End If
Set pobjFields = objValue
End Property
Public Property Get Fields() As SQLSelectFields
Set Fields = pobjFields
End Property
Public Property Get Where() As SQLConditions
Set Where = pobjConditions
End Property
Public Property Set Where(ByVal objValue As SQLConditions)
Set pobjConditions = objValue
End Property
Public Property Get OrderBy() As SQLSelectOrderByFields
Set OrderBy = pobjOrderByFields
End Property
Public Property Set OrderBy(ByVal objValue As SQLSelectOrderByFields)
Set pobjOrderByFields = objValue
End Property
Public Property Get GroupBy() As SQLSelectGroupByFields
Set GroupBy = pobjGroupByFields
End Property
Public Property Set GroupBy(ByVal objValue As SQLSelectGroupByFields)
Set pobjGroupByFields = objValue
End Property
Public Property Get SQL() As String
Attribute SQL.VB_UserMemId = 0
Dim intIndex As Integer
Dim strSQL As String
Dim strConditions As String
Dim strOrderBy As String
Dim strGroupBy As String
If pobjTables.Count = 0 Then
RaiseError dboErrorGeneral, "The table has not been set."
End If
strSQL = _
"SELECT" & DistinctClause & TopClause & " " & pobjFields.SQL(Me.ConnectionType) & _
" FROM " & pobjTables.SQL(Me.ConnectionType)
If pbPerformLocking Then
Select Case Me.ConnectionType
Case dboConnectionTypeSQLServer
strSQL = strSQL & " WITH (HOLDLOCK, ROWLOCK)"
Case dboConnectionTypeMicrosoftAccess
'Unsure of the MSAccess equivalent
RaiseError dboErrorGeneral, "PerformLocking is unavailable for Microsoft Access"
End Select
End If
If Not pobjConditions Is Nothing Then
strConditions = pobjConditions.SQL(Me.ConnectionType)
If strConditions <> vbNullString Then
strSQL = strSQL & " WHERE " & strConditions
End If
End If
If Not pobjGroupByFields Is Nothing Then
strGroupBy = pobjGroupByFields.SQL(Me.ConnectionType)
If strGroupBy <> vbNullString Then
strSQL = strSQL & " GROUP BY " & strGroupBy
End If
End If
If Not pobjOrderByFields Is Nothing Then
strOrderBy = pobjOrderByFields.SQL(Me.ConnectionType)
If strOrderBy <> vbNullString Then
strSQL = strSQL & " ORDER BY " & strOrderBy
End If
End If
If pbPerformLocking Then
Select Case Me.ConnectionType
Case dboConnectionTypeMySQL
strSQL = strSQL & " FOR UPDATE"
End Select
End If
SQL = strSQL
End Property
Private Function TopClause() As String
If plngTop > 0 Then
TopClause = " TOP " & plngTop
End If
End Function
Private Function DistinctClause() As String
If pbDistinct Then
DistinctClause = "DISTINCT "
Else
DistinctClause = vbNullString
End If
End Function
Private Sub Class_Initialize()
Me.ConnectionType = modMisc.ConnectionType
Set pobjFields = New SQLSelectFields
Set pobjTables = New SQLSelectTables
Set pobjConditions = New SQLConditions
Set pobjOrderByFields = New SQLSelectOrderByFields
Set pobjGroupByFields = New SQLSelectGroupByFields
End Sub
Private Property Get ISQLStatement_ConnectionType() As ConnectionTypeEnum
ISQLStatement_ConnectionType = Me.ConnectionType
End Property
Private Property Let ISQLStatement_ConnectionType(ByVal RHS As ConnectionTypeEnum)
Me.ConnectionType = RHS
End Property
Private Property Get ISQLStatement_SQL() As String
ISQLStatement_SQL = Me.SQL
End Property