-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSQLTableField.cls
More file actions
executable file
·555 lines (447 loc) · 19 KB
/
SQLTableField.cls
File metadata and controls
executable file
·555 lines (447 loc) · 19 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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "SQLTableField"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
' ___________________________________________________
'
' © Hi-Integrity Systems 2007. All rights reserved.
' www.hisystems.com.au - Toby Wicks
' ___________________________________________________
'
Option Explicit
Public Enum DataTypeEnum
dboDataTypeTinyInteger 'Integer data from 0 through 255
dboDataTypeSmallInteger 'Integer data from 2^15 (-32,768) through 2^15 - 1 (32,767)
dboDataTypeInteger 'Integer (whole number) data from -2^31 (-2,147,483,648) through 2^31 - 1 (2,147,483,647)
dboDataTypeBigInteger 'Integer (whole number) data from -2^63 (-9223372036854775808) through 2^63-1 (9223372036854775807)
dboDataTypeCharacter '* SQLServer limitation: Fixed-length non-Unicode character data with a maximum length of 8,000 characters
dboDataTypeUnicodeCharacter '* SQLServer limitation: Fixed-length Unicode data with a maximum length of 4,000 characters
dboDataTypeVariableCharacter '* SQLServer limitation: Variable-length non-Unicode data with a maximum of 8,000 characters
dboDataTypeUnicodeVariableCharacter '* SQLServer limitation: Variable-length Unicode data with a maximum length of 4,000 characters
dboDataTypeDecimal '* Fixed precision and scale numeric data
dboDataTypeReal 'Floating precision number data from -3.40E + 38 through 3.40E + 38 (double)
dboDataTypeFloat 'Floating precision number data from -1.79E + 308 through 1.79E + 308 (single / float)
dboDataTypeSmallMoney 'Monetary data values from -214,748.3648 through +214,748.3647, with accuracy to a ten-thousandth of a monetary unit
dboDataTypeMoney 'Monetary data values from -2^63 (-922,337,203,685,477.5808) through 2^63 - 1 (+922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit
dboDataTypeBoolean 'Integer data with either a 1 or 0 value
dboDataTypeSmallDateTime 'Date and time data from January 1, 1900, through June 6, 2079, with an accuracy of one minute
dboDataTypeDateTime 'Date and time data from January 1, 1753, through December 31, 9999, with an accuracy of three-hundredths of a second, or 3.33 milliseconds
dboDataTypeTimeStamp
dboDataTypeText 'Variable-length non-Unicode data with a maximum length of 2^31 - 1 (2,147,483,647) characters
dboDataTypeUnicodeText 'Variable-length Unicode data with a maximum length of 2^30 - 1 (1,073,741,823) characters
dboDataTypeBinary '* SQLServer limitation: Fixed-length binary data with a maximum length of 8,000 bytes
dboDataTypeVariableBinary '* SQLServer limitation: Variable-length binary data with a maximum length of 8,000 bytes
dboDataTypeImage 'Variable-length binary data with a maximum length of 2^31 - 1 (2,147,483,647) bytes
End Enum
Public Enum KeyTypeEnum
dboKeyTypeNone = 0
dboKeyTypePrimary
dboKeyTypeUnique
End Enum
Private pstrName As String
Private peType As DataTypeEnum
Private peKeyType As KeyTypeEnum
Private pintSize As Integer
Private pintScale As Integer
Private pintPrecision As Integer
Private pbAutoIncrements As Boolean
Private pbAcceptsNull As Boolean
Private pvDefault As Variant
'Private pbDefaultExistingRows As Boolean 'sets existing rows to pvDefault (which is the default value for new rows)
Public Property Let Name(ByVal strValue As String)
pstrName = Trim$(strValue)
End Property
Public Property Get Name() As String
Name = pstrName
End Property
Public Property Get DataType() As DataTypeEnum
DataType = peType
End Property
Public Property Let DataType(ByVal eValue As DataTypeEnum)
peType = eValue
End Property
Public Property Let KeyType(ByVal eValue As KeyTypeEnum)
peKeyType = eValue
If eValue = dboKeyTypePrimary Then
'need to set to false as true is the default - see class_initialize
pbAcceptsNull = False
End If
End Property
Public Property Get KeyType() As KeyTypeEnum
KeyType = peKeyType
End Property
Public Property Let Size(ByVal intValue As Integer)
EnsureCharacterDataType
If intValue <= 0 Then
RaiseError dboErrorInvalidArgument
End If
pintSize = intValue
End Property
Public Property Get Size() As Integer
EnsureCharacterDataType
Size = pintSize
End Property
'Public Property Let DefaultExistingRows(ByVal bValue As Boolean)
'
' pbDefaultExistingRows = bValue
'
'End Property
'
'Public Property Get DefaultExistingRows() As Boolean
'
' DefaultExistingRows = pbDefaultExistingRows
'
'End Property
Public Property Let ScaleLength(ByVal intValue As Integer)
EnsureDecimalDataType
If intValue <= 0 Then
RaiseError dboErrorInvalidArgument
End If
pintScale = intValue
End Property
Public Property Get ScaleLength() As Integer
EnsureDecimalDataType
ScaleLength = pintScale
End Property
Public Property Let Precision(ByVal intValue As Integer)
EnsureDecimalDataType
If intValue <= 0 Then
RaiseError dboErrorInvalidArgument
End If
pintPrecision = intValue
End Property
Public Property Get Precision() As Integer
EnsureDecimalDataType
Precision = pintPrecision
End Property
Public Property Let AutoIncrements(ByVal bValue As Boolean)
EnsureIntegerDataType
pbAutoIncrements = bValue
pbAcceptsNull = Not bValue
End Property
Public Property Get AutoIncrements() As Boolean
EnsureIntegerDataType
AutoIncrements = pbAutoIncrements
End Property
Public Property Let AcceptsNull(ByVal bValue As Boolean)
pbAcceptsNull = bValue
End Property
Public Property Get AcceptsNull() As Boolean
AcceptsNull = pbAcceptsNull
End Property
Public Property Let Default(ByVal vValue As Variant)
pvDefault = vValue
'default to updating the existing rows with the default value also
'pbDefaultExistingRows = True
End Property
Public Property Get Default() As Variant
Default = pvDefault
End Property
Private Function DataTypeString( _
ByVal eConnection As ConnectionTypeEnum, _
ByVal eDataType As DataTypeEnum) As String
Dim strDataType As String
Select Case eDataType
Case dboDataTypeTinyInteger
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
strDataType = "BYTE"
Case dboConnectionTypeMySQL
strDataType = "TINYINT UNSIGNED"
Case dboConnectionTypeSQLServer
strDataType = "TINYINT"
End Select
Case dboDataTypeSmallInteger
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
strDataType = "SMALLINT"
Case dboConnectionTypeMySQL
strDataType = "SMALLINT"
Case dboConnectionTypeSQLServer
strDataType = "SMALLINT"
End Select
Case dboDataTypeInteger
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
strDataType = "INTEGER"
Case dboConnectionTypeMySQL
strDataType = "INT"
Case dboConnectionTypeSQLServer
strDataType = "INTEGER"
End Select
Case dboDataTypeBigInteger
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
strDataType = "NUMERIC(19,0)"
Case dboConnectionTypeMySQL
strDataType = "BIGINT"
Case dboConnectionTypeSQLServer
strDataType = "BIGINT"
End Select
Case dboDataTypeCharacter
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
strDataType = "TEXT(" & pintSize & ")"
Case dboConnectionTypeMySQL
strDataType = "CHAR(" & pintSize & ")"
Case dboConnectionTypeSQLServer
strDataType = "CHAR(" & pintSize & ")"
End Select
Case dboDataTypeUnicodeCharacter
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
strDataType = "TEXT(" & pintSize & ")" 'Unicode is only supported in Microsoft Access 2000+
Case dboConnectionTypeMySQL
strDataType = "NCHAR(" & pintSize & ")"
Case dboConnectionTypeSQLServer
strDataType = "NCHAR(" & pintSize & ")"
End Select
Case dboDataTypeVariableCharacter
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
strDataType = "TEXT(" & pintSize & ")"
Case dboConnectionTypeMySQL
strDataType = "VARCHAR(" & pintSize & ")"
Case dboConnectionTypeSQLServer
strDataType = "VARCHAR(" & pintSize & ")"
End Select
Case dboDataTypeUnicodeVariableCharacter
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
strDataType = "TEXT(" & pintSize & ")" 'Unicode is only supported in Microsoft Access 2000+
Case dboConnectionTypeMySQL
strDataType = "NVARCHAR(" & pintSize & ")"
Case dboConnectionTypeSQLServer
strDataType = "NVARCHAR(" & pintSize & ")"
End Select
Case dboDataTypeDecimal
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
strDataType = "NUMERIC(" & pintPrecision & "," & pintScale & ")"
Case dboConnectionTypeMySQL
strDataType = "DECIMAL(" & pintPrecision & "," & pintScale & ")"
Case dboConnectionTypeSQLServer
strDataType = "NUMERIC(" & pintPrecision & "," & pintScale & ")"
End Select
Case dboDataTypeReal
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
strDataType = "REAL"
Case dboConnectionTypeMySQL
strDataType = "FLOAT"
Case dboConnectionTypeSQLServer
strDataType = "REAL"
End Select
Case dboDataTypeFloat
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
strDataType = "FLOAT"
Case dboConnectionTypeMySQL
strDataType = "DOUBLE"
Case dboConnectionTypeSQLServer
strDataType = "FLOAT"
End Select
Case dboDataTypeSmallMoney
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
strDataType = "NUMERIC(10,4)"
Case dboConnectionTypeMySQL
strDataType = "DECIMAL(10,4)"
Case dboConnectionTypeSQLServer
strDataType = "SMALLMONEY"
End Select
Case dboDataTypeMoney
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
strDataType = "NUMERIC(19,4)"
Case dboConnectionTypeMySQL
strDataType = "DECIMAL(19,4)"
Case dboConnectionTypeSQLServer
strDataType = "MONEY"
End Select
Case dboDataTypeBoolean
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
strDataType = "YESNO"
Case dboConnectionTypeMySQL
strDataType = "BIT"
Case dboConnectionTypeSQLServer
strDataType = "BIT"
End Select
Case dboDataTypeSmallDateTime
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
strDataType = "DATETIME"
Case dboConnectionTypeMySQL
strDataType = "DATETIME"
Case dboConnectionTypeSQLServer
strDataType = "SMALLDATETIME"
End Select
Case dboDataTypeDateTime
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
strDataType = "DATETIME"
Case dboConnectionTypeMySQL
strDataType = "DATETIME"
Case dboConnectionTypeSQLServer
strDataType = "DATETIME"
End Select
Case dboDataTypeTimeStamp
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
RaiseError dboErrorNotSupported, "TIMESTAMP"
Case dboConnectionTypeMySQL
strDataType = "TIMESTAMP"
Case dboConnectionTypeSQLServer
strDataType = "TIMESTAMP"
End Select
Case dboDataTypeText
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
strDataType = "MEMO"
Case dboConnectionTypeMySQL
strDataType = "LONGTEXT"
Case dboConnectionTypeSQLServer
strDataType = "TEXT"
End Select
Case dboDataTypeUnicodeText
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
strDataType = "MEMO" 'Unicode is only supported in Microsoft Access 2000+
Case dboConnectionTypeMySQL
strDataType = "LONGTEXT CHARACTER SET UTF8"
Case dboConnectionTypeSQLServer
strDataType = "NTEXT"
End Select
Case dboDataTypeBinary
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
strDataType = "OLEOBJECT"
Case dboConnectionTypeMySQL
strDataType = "BLOB"
Case dboConnectionTypeSQLServer
strDataType = "BINARY"
End Select
Case dboDataTypeVariableBinary
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
strDataType = "OLEOBJECT"
Case dboConnectionTypeMySQL
strDataType = "BLOB"
Case dboConnectionTypeSQLServer
strDataType = "VARBINARY"
End Select
Case dboDataTypeImage
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
strDataType = "IMAGE"
Case dboConnectionTypeMySQL
strDataType = "LONGBLOB"
Case dboConnectionTypeSQLServer
strDataType = "IMAGE"
End Select
End Select
DataTypeString = strDataType
End Function
Friend Property Get SQL(ByVal eConnectionType As ConnectionTypeEnum, ByVal bOnlyFieldName As Boolean) As String
Dim strName As String
Dim strDataType As String
Dim strColumnOptions As String
Dim strSQL As String
If Me.Name = vbNullString Then
RaiseError dboErrorGeneral, "Field Name has not been set."
End If
strName = SQLConvertIdentifierName(Me.Name, eConnectionType)
If bOnlyFieldName Then
strSQL = strName
Else
strDataType = DataTypeString(eConnectionType, peType)
strColumnOptions = ColumnOptions(eConnectionType)
strSQL = strName & " " & strDataType & strColumnOptions
End If
SQL = strSQL
End Property
Private Function ColumnOptions( _
ByVal eConnection As ConnectionTypeEnum) As String
Dim colOptions As Collection
Dim vOption As Variant
Dim strOptions As String
Set colOptions = New Collection
If pbAcceptsNull Then
colOptions.Add "NULL"
Else
colOptions.Add "NOT NULL"
End If
If DefaultValueIsSet Then
colOptions.Add "DEFAULT " & SQLConvertValue(pvDefault, eConnection)
End If
If IsIntegerDataType And pbAutoIncrements Then
Select Case eConnection
Case dboConnectionTypeMicrosoftAccess
colOptions.Add "IDENTITY"
Case dboConnectionTypeMySQL
colOptions.Add "AUTO_INCREMENT"
'must be set to a key
If peKeyType = dboKeyTypeNone Then
peKeyType = dboKeyTypeUnique
End If
Case dboConnectionTypeSQLServer
colOptions.Add "IDENTITY"
End Select
End If
Select Case peKeyType
Case dboKeyTypePrimary
colOptions.Add "PRIMARY KEY"
Case dboKeyTypeUnique
colOptions.Add "UNIQUE"
End Select
For Each vOption In colOptions
strOptions = strOptions & " " & vOption
Next
ColumnOptions = strOptions
End Function
Friend Property Get DefaultValueIsSet() As Boolean
DefaultValueIsSet = Not IsEmpty(pvDefault)
End Property
Private Sub EnsureDecimalDataType()
If peType <> dboDataTypeDecimal Then
RaiseError dboErrorMethodOrPropertyLocked, "First set TheType to dboDataTypeDecimal"
End If
End Sub
Private Sub EnsureCharacterDataType()
Select Case peType
Case dboDataTypeCharacter, dboDataTypeUnicodeCharacter, _
dboDataTypeVariableCharacter, dboDataTypeUnicodeVariableCharacter
Case Else
RaiseError dboErrorMethodOrPropertyLocked, "Data Type is not character based"
End Select
End Sub
Private Sub EnsureIntegerDataType()
If Not IsIntegerDataType Then
RaiseError dboErrorMethodOrPropertyLocked
End If
End Sub
Private Property Get IsIntegerDataType() As Boolean
Select Case peType
Case dboDataTypeBigInteger, dboDataTypeTinyInteger, dboDataTypeSmallInteger, dboDataTypeInteger
IsIntegerDataType = True
Case Else
IsIntegerDataType = False
End Select
End Property
Private Sub Class_Initialize()
peType = dboDataTypeVariableCharacter
pintSize = 1
pintPrecision = 18
pintScale = 0
pbAcceptsNull = True
End Sub