-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmodMisc.bas
More file actions
executable file
·312 lines (246 loc) · 11.1 KB
/
modMisc.bas
File metadata and controls
executable file
·312 lines (246 loc) · 11.1 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
305
306
307
308
309
310
311
312
Attribute VB_Name = "modMisc"
' ___________________________________________________
'
' © Hi-Integrity Systems 2007. All rights reserved.
' www.hisystems.com.au - Toby Wicks
' ___________________________________________________
'
Option Explicit
Public Enum SQLTableFieldsAlterModeEnum
dboTableFieldsModeAdd
dboTableFieldsModeAlter
dboTableFieldsModeDrop
End Enum
'This is used as the default connection type when manually instantiating
'a SQLSelect, SQLDelete, SQLUpdate or SQLInsert command and is set by the last
'Database.Connect function call's connectiontype argument
Public ConnectionType As ConnectionTypeEnum
Public Function SQLConvertIdentifierName( _
ByVal strIdentifierName As String, _
ByVal eConnectionType As ConnectionTypeEnum) As String
'This function places tags around a field name or table name to ensure it doesn't
'conflict with a reserved word or if it contains spaces it is not misinterpreted
Select Case eConnectionType
Case dboConnectionTypeMicrosoftAccess, dboConnectionTypeSQLServer
SQLConvertIdentifierName = "[" & Trim$(strIdentifierName) & "]"
Case dboConnectionTypeMySQL
SQLConvertIdentifierName = "`" & Trim$(strIdentifierName) & "`"
End Select
End Function
Public Function SQLConvertAggregate( _
ByVal eAggregate As SQLAggregateFunctionEnum) As String
Dim strAggregate As String
Select Case eAggregate
Case dboAggregateAverage
strAggregate = "AVG"
Case dboAggregateCount
strAggregate = "COUNT"
Case dboAggregateMaximum
strAggregate = "MAX"
Case dboAggregateMinimum
strAggregate = "MIN"
Case dboAggregateStandardDeviation
strAggregate = "STDEV"
Case dboAggregateSum
strAggregate = "SUM"
Case dboAggregateVariance
strAggregate = "VAR"
End Select
SQLConvertAggregate = strAggregate
End Function
Public Function SQLConvertValue( _
ByVal vValue As Variant, _
ByVal eConnectionType As ConnectionTypeEnum) As String
Dim strValue As String
If SQLValueIsNull(vValue) Then
strValue = "NULL"
Else
Select Case VarType(vValue)
Case vbByte, vbCurrency, vbDecimal, vbDouble, vbInteger, vbLong, vbSingle
strValue = vValue
Case vbString
Select Case eConnectionType
Case dboConnectionTypeMicrosoftAccess, dboConnectionTypeSQLServer
strValue = "'" & Replace$(vValue, "'", "''") & "'"
Case dboConnectionTypeMySQL
strValue = "'" & Replace$(Replace$(vValue, "\", "\\"), "'", "\'") & "'"
Case Else
RaiseError dboErrorNotSupported, "Connection Type: " & eConnectionType
End Select
Case vbDate
strValue = Year(vValue) & "-" & Month(vValue) & "-" & Day(vValue)
If Hour(vValue) <> 0 Or Minute(vValue) <> 0 Or Second(vValue) <> 0 Then
strValue = strValue & " " & Hour(vValue) & ":" & Minute(vValue) & ":" & Second(vValue)
End If
If eConnectionType = dboConnectionTypeMicrosoftAccess Then
strValue = "#" & strValue & "#"
Else
strValue = "'" & strValue & "'"
End If
Case vbBoolean
strValue = IIf(vValue, "1", "0")
Case (vbByte Or vbArray)
Dim bytData() As Byte
bytData = vValue
strValue = SQLConvertByteArray(bytData)
Case Else
RaiseError dboErrorGeneral, "Invalid Variant Datatype: " & vValue
End Select
End If
SQLConvertValue = strValue
End Function
Private Function SQLConvertByteArray(ByRef bytData() As Byte) As String
Dim lngIndex As Long
Dim objHexString As StringBuilder
Set objHexString = New StringBuilder
'Make it so that only 1 chunk is allocated as we know the final size of the string
objHexString.ChunkSize = (UBound(bytData) - LBound(bytData) + 1) * 2 + 2
objHexString.Append "0x"
For lngIndex = LBound(bytData) To UBound(bytData)
objHexString.Append SQLConvertByteToHex(bytData(lngIndex))
Next
SQLConvertByteArray = objHexString.Value
End Function
Private Function SQLConvertByteToHex(ByVal bytData As Byte) As String
Dim strValue As String
strValue = Hex$(bytData)
If Len(strValue) = 1 Then
strValue = "0" & strValue
End If
SQLConvertByteToHex = strValue
End Function
Public Function SQLValueIsNull(ByVal vValue As Variant) As Boolean
Select Case VarType(vValue)
Case vbObject
SQLValueIsNull = vValue Is Nothing
Case vbNull
SQLValueIsNull = True
End Select
End Function
Public Function SQLConvertCompare( _
ByVal eCompare As SQLComparisonOperatorEnum) As String
Dim strCompare As String
Select Case eCompare
Case dboComparisonEqualTo
strCompare = "="
Case dboComparisonGreaterThan
strCompare = ">"
Case dboComparisonGreaterThanOrEqualTo
strCompare = ">="
Case dboComparisonLessThan
strCompare = "<"
Case dboComparisonLessThanOrEqualTo
strCompare = "<="
Case dboComparisonNotEqualTo
strCompare = "<>"
Case dboComparisonLike
strCompare = "LIKE"
Case dboComparisonNotLike
strCompare = "NOT LIKE"
Case Else
RaiseError dboErrorGeneral, "Invalid SQLComparisonOperatorEnum value " & eCompare
End Select
SQLConvertCompare = strCompare
End Function
Public Function SQLConvertLogicalOperator( _
ByVal eLogicalOperator As SQLLogicalOperatorEnum) As String
Dim strLogicalOperator As String
Select Case eLogicalOperator
Case dboLogicalAnd
strLogicalOperator = "AND"
Case dboLogicalOr
strLogicalOperator = "OR"
End Select
SQLConvertLogicalOperator = strLogicalOperator
End Function
Public Function SQLFieldNameAndTablePrefix( _
ByVal objTable As SQLSelectTable, _
ByVal strFieldName As String, _
ByVal eConnectionType As ConnectionTypeEnum) As String
Dim strTablePrefix As String
If Not objTable Is Nothing Then
'If Trim$(objTable.Alias) = vbNullString Then
strTablePrefix = objTable.Name
'Else
' strTablePrefix = objTable.Alias
'End If
strTablePrefix = SQLConvertIdentifierName(strTablePrefix, eConnectionType) & "."
End If
SQLFieldNameAndTablePrefix = strTablePrefix & SQLConvertIdentifierName(strFieldName, eConnectionType)
End Function
'Must copy the value into the variant because sometimes it will require the use of the 'Set' keyword
Public Sub SQLConditionValue(ByVal vValue As Variant, ByRef vCopyInto As Variant)
Select Case VarType(vValue)
Case vbObject
If vValue Is Nothing Then
Set vCopyInto = Nothing
ElseIf TypeOf vValue Is SQLFieldValue Then
Dim objSQLFieldValue As SQLFieldValue
Set objSQLFieldValue = vValue
Set vCopyInto = objSQLFieldValue.Value
Else
RaiseError dboErrorGeneral, "Invalid Object Type"
End If
Case vbArray, vbDataObject, vbEmpty, vbError, vbUserDefinedType, vbVariant
RaiseError dboErrorGeneral, "Invalid Data-Type"
Case Else
vCopyInto = vValue
End Select
End Sub
Public Sub CompareValuePairAssertValid( _
ByVal eCompare As SQLComparisonOperatorEnum, _
ByRef vValue As Variant)
If VarType(vValue) <> vbString And (eCompare = dboComparisonLike Or eCompare = dboComparisonNotLike) Then
RaiseError dboErrorGeneral, "The LIKE operator cannot be used in conjunction with a non-string data type"
ElseIf VarType(vValue) = vbBoolean And Not (eCompare = dboComparisonEqualTo Or eCompare = dboComparisonNotEqualTo) Then
RaiseError dboErrorGeneral, "A boolean value can only be used in conjunction with the dboComparisonEqualTo or dboComparisonNotEqualTo operators"
End If
End Sub
Public Sub SQLConvertBooleanValue( _
ByRef vValue As Variant, _
ByRef eCompare As SQLComparisonOperatorEnum)
'If a boolean variable set to true then use the opposite
'operator and compare it to 0. ie. if the condition is 'field = true' then
'SQL code should be 'field <> 0'
'-1 is true in MSAccess and 1 is true in SQLServer.
If VarType(vValue) = vbBoolean Then
If vValue = True Then
If eCompare = dboComparisonEqualTo Then
eCompare = dboComparisonNotEqualTo
Else
eCompare = dboComparisonEqualTo
End If
vValue = False
End If
End If
End Sub
Public Function CollectionRemoveItem( _
ByVal objCollection As Collection, _
ByVal objItem As Object) As Boolean
Dim intIndex As Integer
For intIndex = 1 To objCollection.Count
If objCollection(intIndex) Is objItem Then
objCollection.Remove intIndex
CollectionRemoveItem = True
Exit For
End If
Next
End Function
Public Sub RaiseError( _
ByVal eError As ErrorEnum, _
Optional ByVal strExtra As String)
Select Case eError
Case dboErrorGeneral: Err.Raise dboErrorGeneral, , strExtra
Case dboErrorIndexOutOfBounds: Err.Raise dboErrorIndexOutOfBounds, , "Index out of bounds " & strExtra
Case dboErrorNotIntegerOrString: Err.Raise dboErrorNotIntegerOrString, , "Invalid data type, expected Integer or String"
Case dboErrorObjectIsNothing: Err.Raise dboErrorObjectIsNothing, , "Object is Nothing"
Case dboErrorObjectAlreadyExists: Err.Raise dboErrorObjectAlreadyExists, , "Object already exists " & strExtra
Case dboErrorObjectDoesNotExist: Err.Raise dboErrorObjectDoesNotExist, , "Object does not exist " & strExtra
Case dboErrorInvalidPropertyValue: Err.Raise dboErrorInvalidPropertyValue, , "Invalid property value " & strExtra
Case dboErrorInvalidArgument: Err.Raise dboErrorInvalidArgument, , "Invalid argument " & strExtra
Case dboErrorObjectNotDeletable: Err.Raise dboErrorObjectNotDeletable, , "Object is not deletable " & strExtra
Case dboErrorObjectNotSaved: Err.Raise dboErrorObjectNotSaved, , "Objects not saved " & strExtra
Case dboErrorNotSupported: Err.Raise dboErrorNotSupported, , "Method or Property not supported " & strExtra
Case dboErrorMethodOrPropertyLocked: Err.Raise dboErrorMethodOrPropertyLocked, , "Method or Property locked " & strExtra
End Select
End Sub