-
Notifications
You must be signed in to change notification settings - Fork 3
/
M_omSQLFunctions.def
275 lines (239 loc) · 9.71 KB
/
M_omSQLFunctions.def
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
Option Compare Database
Option Explicit
Public Function WhereAnd(whereClause1 As String, whereClause2 As String, Optional embraceWhereClause1 = False, Optional embraceWhereClause2 = False)
If Len(whereClause1) > 0 And embraceWhereClause1 Then
whereClause1 = "(" & whereClause1 & ")"
End If
If Len(whereClause2) > 0 And embraceWhereClause2 Then
whereClause2 = "(" & whereClause2 & ")"
End If
If Len(whereClause1) = 0 Then
WhereAnd = whereClause2
ElseIf Len(whereClause2) = 0 Then
WhereAnd = whereClause1
ElseIf Len(whereClause1) <> 0 And Len(whereClause2) <> 0 Then
WhereAnd = whereClause1 & " AND " & whereClause2
End If
End Function
Public Function WhereOr(whereClause1 As String, whereClause2 As String, Optional embraceWhereClause1 = False, Optional embraceWhereClause2 = False)
If Len(whereClause1) > 0 And embraceWhereClause1 Then
whereClause1 = "(" & whereClause1 & ")"
End If
If Len(whereClause2) > 0 And embraceWhereClause2 Then
whereClause2 = "(" & whereClause2 & ")"
End If
If Len(whereClause1) = 0 Then
WhereOr = whereClause2
ElseIf Len(whereClause2) = 0 Then
WhereOr = whereClause1
ElseIf Len(whereClause1) <> 0 And Len(whereClause2) <> 0 Then
WhereOr = whereClause1 & " OR " & whereClause2
End If
End Function
Public Function InnerJoin(joinTable As String, Table1 As String, Field1 As String, Table2 As String, Field2 As String)
Dim strJoin As String
strJoin = "(!JoinTable! INNER JOIN [!Table2!] ON [!Table1!].[!Field1!]=[!Table2!].[!Field2!])"
strJoin = Replace(strJoin, "!JoinTable!", IIf(joinTable = "", "[" & Table1 & "]", joinTable))
strJoin = Replace(strJoin, "!Table1!", Table1)
strJoin = Replace(strJoin, "!Field1!", Field1)
strJoin = Replace(strJoin, "!Table2!", Table2)
strJoin = Replace(strJoin, "!Field2!", Field2)
InnerJoin = strJoin
End Function
Public Function LeftJoin(joinTable As String, Table1 As String, Field1 As String, Table2 As String, Field2 As String)
Dim strJoin As String
strJoin = "(!JoinTable! LEFT JOIN [!Table2!] ON [!Table1!].[!Field1!]=[!Table2!].[!Field2!])"
strJoin = Replace(strJoin, "!JoinTable!", IIf(joinTable = "", "[" & Table1 & "]", joinTable))
strJoin = Replace(strJoin, "!Table1!", Table1)
strJoin = Replace(strJoin, "!Field1!", Field1)
strJoin = Replace(strJoin, "!Table2!", Table2)
strJoin = Replace(strJoin, "!Field2!", Field2)
LeftJoin = strJoin
End Function
Public Function BuildSQL(ByVal selectClause As String, Optional ByVal fromClause As String = "", Optional ByVal whereClause As String = "", Optional ByVal orderClause As String = "", Optional ByVal insertClause As String = "", Optional ByVal deleteClause As String = "") As String
selectClause = NormalizeSQL(selectClause)
fromClause = NormalizeSQL(fromClause)
whereClause = NormalizeSQL(whereClause)
insertClause = NormalizeSQL(insertClause)
deleteClause = NormalizeSQL(deleteClause)
If Len(selectClause) > 0 Then
BuildSQL = IIf(Left(selectClause, 6) <> "select", "SELECT ", " ") & selectClause
End If
If Len(deleteClause) > 0 Then
BuildSQL = IIf(Left(deleteClause, 6) <> "delete", "DELETE ", " ") & deleteClause
End If
If Len(insertClause) > 0 Then
BuildSQL = "INSERT INTO " & insertClause & " " & BuildSQL
End If
If Len(fromClause) > 0 Then
BuildSQL = BuildSQL & IIf(Left(fromClause, 4) <> "from", " FROM ", " ") & fromClause
End If
If Len(whereClause) > 0 Then
BuildSQL = BuildSQL & IIf(Left(whereClause, 5) <> "where", " WHERE ", " ") & whereClause
End If
If Len(orderClause) > 0 Then
BuildSQL = BuildSQL & IIf(Left(orderClause, 8) <> "order by", " ORDER BY ", " ") & orderClause
End If
BuildSQL = Replace(BuildSQL, " ", " ")
End Function
Public Function GetSelect(sql As String) As String
Dim posFrom As Long
posFrom = GetSQLClausePos(sql, "FROM")
GetSelect = ReplaceSQLClause(Mid(sql, 1, posFrom - 1), "SELECT", "")
End Function
Public Function GetFrom(sql As String) As String
Dim posFrom As Long
Dim poswhere As Long
Dim posOrderBy As Long
Dim posGroupBy As Long
Dim posUse As Long
posFrom = GetSQLClausePos(sql, "FROM")
poswhere = GetSQLClausePos(sql, "WHERE")
posOrderBy = GetSQLClausePos(sql, "ORDER BY", poswhere + 1)
posGroupBy = GetSQLClausePos(sql, "GROUP BY", poswhere + 1)
If poswhere <> 0 Or posOrderBy <> 0 Or posGroupBy <> 0 Then
If poswhere <> 0 Then
posUse = poswhere
End If
If posOrderBy <> 0 Then
posUse = posOrderBy
End If
If posGroupBy <> 0 Then
posUse = posGroupBy
End If
End If
If posUse = 0 Then
posUse = Len(sql) + 1
End If
GetFrom = ReplaceSQLClause(Mid(sql, posFrom, posUse - (posFrom - 1)), "FROM", "")
End Function
Public Function GetWhere(sql As String) As String
Dim poswhere As Long
Dim posOrderBy As Long
Dim posGroupBy As Long
Dim posUse As Long
poswhere = GetSQLClausePos(sql, "WHERE")
If poswhere = 0 Then
Exit Function
End If
posOrderBy = GetSQLClausePos(sql, "ORDER BY", poswhere)
posGroupBy = GetSQLClausePos(sql, "GROUP BY", poswhere)
If posOrderBy <> 0 Or posGroupBy <> 0 Then
If posOrderBy <> 0 Then
posUse = posOrderBy
End If
If posUse <> 0 And posGroupBy <> 0 Then
posUse = posGroupBy
End If
End If
If posUse = 0 Then
posUse = Len(sql) + 1
End If
GetWhere = ReplaceSQLClause(Mid(sql, poswhere, posUse - (poswhere - 1)), "WHERE", "")
End Function
Public Function GetOrderBy(ByVal sql As String) As String
Dim posOrderBy As Long
'SELECT Course.Course_ID, Course.Course_Year
'FROM Course
'where
'GROUP BY Course.Course_ID, Course.Course_Year
'HAVING (((Course.Course_Year) = [1]))
'ORDER BY Course.Course_Year;
sql = NormalizeSQL(sql)
posOrderBy = GetSQLClausePos(sql, "ORDER BY")
If posOrderBy <> 0 Then
GetOrderBy = ReplaceSQLClause(Mid(sql, posOrderBy + 1, Len(sql) - (posOrderBy - 1)), "ORDER BY", "")
End If
End Function
Public Function GetAccessDate(dt As Date)
GetAccessDate = "#" & Date & "#"
End Function
Public Function GetSQLServerDate(dt As Date)
GetSQLServerDate = "'" & Format(dt, "yyyy-mm-dd hh:mm:ss") & "'"
End Function
Public Function GetAccessString(str As String, Optional prefixWildcard As Boolean = True, Optional suffixWildcard As Boolean = True)
GetAccessString = Chr(34) & IIf(prefixWildcard, "*", "") & str & IIf(suffixWildcard, "*", "") & Chr(34)
End Function
Public Function GetSQLServerString(str As String, Optional prefixWildcard As Boolean = True, Optional suffixWildcard As Boolean = True)
GetSQLServerString = "'" & IIf(prefixWildcard, "%", "") & str & IIf(suffixWildcard, "%", "") & "'"
End Function
Public Function GetAccessWeekFunction()
GetAccessWeekFunction = "datepart(" & Chr(34) & "ww" & Chr(34) & ",<<Value>>)"
End Function
Public Function GetSQLServerWeekFunction()
GetSQLServerWeekFunction = "datepart(wk,<<Value>>)"
End Function
Public Function GetSQLClausePos(ByVal sql As String, clause As String, Optional startPos As Long = 1) As Long
Dim pos As Long
sql = NormalizeSQL(sql)
pos = InStr(startPos, sql, " " & clause & " ")
If pos = 0 Then
pos = InStr(startPos, sql, clause & " ")
End If
GetSQLClausePos = pos
End Function
Public Function ReplaceSQLClause(ByVal sql As String, clause As String, Optional replaceString As String = "", Optional startPos As Long = 0) As String
Dim pos As Long
Dim posEnd As Long
Dim result As String
sql = NormalizeSQL(sql)
If startPos = 0 Then
pos = GetSQLClausePos(sql, clause)
Else
pos = startPos
End If
If pos <> 0 Then
posEnd = InStr(pos + Len(clause) - 1, sql, " ")
If posEnd = 0 Then
posEnd = InStr(pos, sql, vbCrLf)
End If
If posEnd = 0 Then
posEnd = InStr(pos, sql, vbCr)
End If
result = Mid(sql, posEnd)
End If
ReplaceSQLClause = Trim(result)
End Function
Public Function NormalizeSQL(ByVal sql As String) As String
sql = Replace(sql, vbCrLf, " ")
sql = Replace(sql, vbCr, " ")
sql = Replace(sql, ";", " ")
sql = Replace(sql, " ", " ")
NormalizeSQL = Trim(sql)
End Function
Public Function WhereOperatorForString(str As String) As String
WhereOperatorForString = IIf(InStr(1, str, "*") = 0 And InStr(1, str, "?") = 0, " = ", " LIKE ")
End Function
Public Function ReplaceDatabaseInUseClause(ByVal sql As String, sourceDatabase As String, destinationDatabase As String) As String
Dim startPos As Long
Dim endPos As Long
Dim checkPattern As String
Dim i As Long
checkPattern = " " & vbCrLf & "/;"
startPos = InStr(startPos + 1, sql, "USE ")
endPos = startPos
While startPos > 0
' check character before USE is blank, vbcr,vbcrlf,vblf, /
If startPos > 1 Then
If Not omStringFunctions.IsStringInPattern(Mid(sql, startPos - 1, 1), checkPattern) Then
startPos = 0
End If
End If
If startPos <> 0 Then
For i = startPos + 4 To Len(sql)
endPos = i
If omStringFunctions.IsStringInPattern(Mid(sql, i, 1), checkPattern) Then
endPos = endPos - 1
Exit For
End If
Next i
If Mid(sql, startPos + 4, endPos + 1 - startPos - 4) = sourceDatabase Then
sql = Left(sql, startPos - 1) & "USE " & destinationDatabase & Mid(sql, endPos + 1)
End If
End If
startPos = endPos
startPos = InStr(startPos + 1, sql, "USE ")
endPos = startPos
Wend
ReplaceDatabaseInUseClause = sql
End Function