-
Notifications
You must be signed in to change notification settings - Fork 1
/
DatabaseMethods.bas
167 lines (134 loc) · 4.67 KB
/
DatabaseMethods.bas
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
Attribute VB_Name = "DatabaseMethods"
Option Explicit
Public Function DataBaseSelect(ClassName As String, params As clsParams, Optional intLimit As Integer = 0, Optional strOrderBy As String = "") As Variant
Dim strSQL As String
Dim tableName As String
Dim i As Integer
Dim lastParamValue As Variant
Dim objReturn() As Variant
Dim objClass As IBaseClass
Set objClass = GetCreate(ClassName)
tableName = objClass.GetTableName()
strSQL = "SELECT * FROM " & tableName & " WHERE "
params.First
Do Until params.EOF
strSQL = strSQL & params.Name & " " & params.Operator & " '" & params.value & "' AND "
params.MoveNext
Loop
strSQL = Mid(strSQL, 1, Len(strSQL) - 4)
If Not strOrderBy = "" And InStr(1, UCase(strOrderBy), "ORDER BY") > 1 Then
strSQL = strSQL & strOrderBy
End If
If intLimit > 0 Then
strSQL = strSQL & " LIMIT " & intLimit
End If
DataBaseSelect = DataBaseSelectSQL(ClassName, strSQL)
End Function
Public Function DataBaseSelectSQL(ClassName As String, strSQL As String) As Variant
Dim rs As Object
Dim objClass As Object
Dim objReturn() As Variant
Dim field As Object
Dim i As Long
Set rs = CreateObject("ADODB.Recordset")
rs.Open strSQL, cn
If rs.EOF Then
DataBaseSelectSQL = objReturn
Exit Function
End If
i = 1
Do Until rs.EOF
Set objClass = GetCreate(ClassName)
ReDim Preserve objReturn(i) As Variant
For Each field In rs.Fields
Dim propName As String
propName = field.Name
If HasProperty(objClass, UCase(propName)) Then
CallByName objClass, propName, VbLet, field.value
End If
Next field
Set objReturn(i) = objClass
rs.MoveNext
i = i + 1
Loop
rs.Close
Set rs = Nothing
DataBaseSelectSQL = objReturn
End Function
Public Function DataBaseInsert(ClassName As String, objData As IBaseClass) As Boolean
Dim strSQL As String
Dim tableName As String
Dim prop As clsProperty
Dim strFields As String
Dim strValues As String
Dim prop2 As Variant
tableName = objData.GetTableName()
For Each prop2 In objData.Props.GetProperties
Set prop = prop2
If Not prop.isPrimaryKey Then
strFields = strFields & prop.Name & ","
strValues = strValues & "'" & prop.value & "',"
End If
Next prop2
strFields = Left(strFields, Len(strFields) - 1)
strValues = Left(strValues, Len(strValues) - 1)
strSQL = "INSERT INTO " & tableName & " (" & strFields & ") VALUES (" & strValues & ")"
On Error Resume Next
cn.Execute strSQL
If Err.Number = 0 Then
DataBaseInsert = True
Else
Debug.Print "Error inserting data: " & Err.Description
DataBaseInsert = False
End If
On Error GoTo 0
End Function
Public Function DataBaseUpdate(ClassName As String, objData As IBaseClass) As Boolean
Dim strSQL As String
Dim tableName As String
Dim prop As clsProperty
Dim strUpdateFields As String
Dim primaryKeyField As String
Dim primaryKeyValue As Variant
Dim prop2 As Variant
tableName = objData.GetTableName()
For Each prop2 In objData.Props.GetProperties
Set prop = prop2
If prop.isPrimaryKey Then
primaryKeyField = prop.Name
primaryKeyValue = prop.value
Else
strUpdateFields = strUpdateFields & prop.Name & " = '" & prop.value & "',"
End If
Next prop2
strUpdateFields = Left(strUpdateFields, Len(strUpdateFields) - 1)
strSQL = "UPDATE " & tableName & " SET " & strUpdateFields & " WHERE " & primaryKeyField & " = '" & primaryKeyValue & "'"
On Error Resume Next
cn.Execute strSQL
If Err.Number = 0 Then
DataBaseUpdate = True
Else
Debug.Print "Error updating data: " & Err.Description
DataBaseUpdate = False
End If
On Error GoTo 0
End Function
Public Function DataBaseSave(ClassName As String, objData As IBaseClass) As Boolean
Dim primaryKeyField As String
Dim primaryKeyValue As Variant
Dim prop As clsProperty
Dim prop2 As Variant
For Each prop2 In objData.Props.GetProperties
Set prop = prop2
If prop.isPrimaryKey Then
primaryKeyField = prop.Name
primaryKeyValue = prop.value
Exit For
End If
Next prop2
If IsEmpty(primaryKeyValue) Or IsNull(primaryKeyValue) Then
DataBaseSave = DataBaseInsert(ClassName, objData)
Else
DataBaseSave = DataBaseUpdate(ClassName, objData)
End If
End Function