-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConData.vb
334 lines (259 loc) · 14.5 KB
/
ConData.vb
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
Option Explicit On
Option Strict On
Imports System.Data.OleDb
Imports System.Text
Module Module1
Sub Main()
Dim oConn As New OleDbConnection ' Data objects
Dim oCmd As New OleDbCommand
Dim oReader As OleDbDataReader
Dim sQry As String = ""
Dim sQryType As String = "" ' SELECT, INSERT, UPDATE or DELETE
Dim SelectQry As Boolean = True ' Assume query is of type Select - for safety sake.
Dim cSuperType As Hashtable = New Hashtable() ' Type/SubType hashtable
cSuperType.Add("Boolean", "Boolean")
cSuperType.Add("String", "String") : cSuperType.Add("Char", "String") : cSuperType.Add("Guid", "String")
cSuperType.Add("DateTime", "Date") : cSuperType.Add("TimeSpan", "Date")
cSuperType.Add("Byte", "Number") : cSuperType.Add("SByte", "Number")
cSuperType.Add("Double", "Number") : cSuperType.Add("Decimal", "Number") : cSuperType.Add("Single", "Number")
cSuperType.Add("Int64", "Number") : cSuperType.Add("Int32", "Number") : cSuperType.Add("Int16", "Number")
cSuperType.Add("UInt64", "Number") : cSuperType.Add("UInt32", "Number") : cSuperType.Add("UInt16", "Number")
cSuperType.Add("Byte[]", "Date") ' To handle SQL server's annoying datestamp type.
'cSuperType.Add("Null", "null") - handled by oReader.IsDBNull
Dim E As Integer = 0
Dim E_Desc As String = ""
Dim nFieldCount As Integer = 0 ' temp vars
Dim nRecordCount As Integer = 0
Dim sShortType As String = "" ' temp vars
Dim sShortSubType As String = ""
Dim sVal As String = ""
Dim sVal_Qt As String = ""
Dim nLooper As Integer = 0
Dim sDBType As String
Dim sMdb As String = "" ' "C:\Path\Somefile.mdb" (also from MDB://C:\Path\Somefile.mdb)
Dim sSQLFullURI As String ' SQL://Server\Instance/Database
Dim sSQLServInst As String = "" ' Server\Instance
Dim sSQLDatabase As String = "" ' Database
Dim sParams As String
' Examples...
'sParams = "C:\SomeFolder\MyData.mdb"
'sParams = "MDB://C:\SomeFolder\MyData.mdb"
'sParams = "SQL://Server\Instance/Database"
sParams = Command().Trim()
If sParams.Substring(0, 6).ToUpper = "SQL://" Then
sDBType = "SQL"
sSQLFullURI = sParams.Substring(6, sParams.Length - 6) ' Server\Instance/Database
sSQLServInst = sSQLFullURI.Split(CType("/", Char()))(0) ' Server\Instance
sSQLDatabase = sSQLFullURI.Split(CType("/", Char()))(1) ' Database
ElseIf sParams.Substring(0, 6).ToUpper() = "MDB://" Then
sDBType = "MDB"
sMdb = sParams.Substring(6, sParams.Length - 6)
Else
sDBType = "MDB"
sMdb = sParams
End If
sQry = Console.In.ReadToEnd.Trim()
sQry = sQry.Replace(vbCrLf, " ") _
.Replace(vbCr, " ") _
.Replace(vbLf, " ") _
.Replace(vbTab, " ") _
.Trim()
sQryType = sQry.Substring(0, 6).ToUpper()
SelectQry = Not (sQryType = "INSERT" Or sQryType = "UPDATE" Or sQryType = "DELETE")
' Only these are Non-select (writing) queries.
' Detect potential error conditions...
If E = 0 And sQry = "" Then ' Check for Query errors with STDIN
E = -65539
E_Desc = " Note from ConData: Query text input is blank!" &
" Query text must be provided through STDIN (standard input). "
End If
If E = 0 And Not (sQryType = "SELECT" Or sQryType = "INSERT" Or
sQryType = "UPDATE" Or sQryType = "DELETE") Then
E = -65540
E_Desc = " Note from ConData: Query type not supported! " &
" Please try one of the 4 types instead: SELECT, INSERT, UPDATE or DELETE. " &
" Your data: " & sQry & " "
End If
If sDBType = "MDB" Then
If E = 0 And sMdb = "" Then ' Check for Mdb file errors with Command parameter
E = -65536
E_Desc = " Note from ConData: Parameter is blank! Mdb file not specified! " &
" Should be full path name of Mdb file. " &
" Example: ConData.exe c:\folder\myfile.mdb "
End If
If E = 0 And Not (sMdb.Substring(sMdb.Length - 4).ToUpper = ".MDB" _
Or sMdb.Substring(sMdb.Length - 4).ToUpper = ".ACCDB") Then
E = -65538
E_Desc = " Note from ConData: File specified is not of type .mdb or .accdb! " &
" Should be full path name of mdb/accdb file. " &
" Example: ConData.exe c:\folder\myfile.mdb " &
" Your data: " & sMdb & " "
End If
' AndAlso >- - - - - - - - - - - - ----> If E != 0, Don't bother checking Dir(sMbd).
' A.k.a "short circuiting logic" / "smart if"
If E = 0 AndAlso Dir(sMdb) = "" Then ' Not exists?
E = -65537
E_Desc = " Note from ConData: File (FileName=" & sMdb & ") not found! " &
" Should be full path name of Mdb file. " &
" Example: ConData.exe c:\folder\myfile.mdb "
End If
End If
If sDBType = "SQL" Then
If E = 0 And sSQLServInst = "" Then
E = -32001
E_Desc = " Note from ConData: Server Instance should not be blank. "
End If
If E = 0 And sSQLDatabase = "" Then
E = -32002
E_Desc = " Note from ConData: Database should not be blank. "
End If
End If
' RUN QUERY ############################################################################
' ( If no errors then Attempt to open the database and run the Query... )
If E = 0 Then ' Attempt to open the database...
If sDBType = "MDB" Then 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source= ' for legacy use this.
oConn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sMdb)
ElseIf sDBType = "SQL" Then
oConn = New OleDbConnection("Provider=SQLNCLI10;Trusted_Connection=Yes;Database=" _
& sSQLDatabase & ";SERVER=" & sSQLServInst)
Else
E = -1509
E_Desc = "Note from ConData: URI Scheme not understood."
End If
oCmd = New OleDbCommand(sQry, oConn)
Try
oConn.Open()
Catch
E = Err.Number
E_Desc = Err.Description.Replace("""", "").Replace("\", "") &
" ... Note from ConData / oConn.Open(): " &
" Meaning: Error opening the database. "
End Try
End If
If E = 0 Then ' Attempt Query...
If SelectQry Then ' SELECT
Try
oReader = oCmd.ExecuteReader()
Catch
E = Err.Number
E_Desc = Err.Description.Replace("""", "").Replace("\", "") &
" ... Note from ConData / oCmd.ExecuteReader(): " &
" Meaning: There was an error executing a SELECT Query " &
" (for which results were expected back). "
End Try
Else ' INSERT, UPDATE or DELETE
Try
nRecordCount = oCmd.ExecuteNonQuery() ' Number of records effected
Catch
nRecordCount = 0
E = Err.Number
E_Desc = Err.Description.Replace("""", "").Replace("\", "") &
" ... Note from ConData / oCmd.ExecuteNonQuery(): " &
" Meaning: There was an error executing a NON-SELECT Query, " &
" that is -- An UPDATE, INSERT, or DELETE. "
End Try
End If
End If
E_Desc = E_Desc.Replace("\", "\\") ' escape backslashes in path names.
Console.WriteLine("{")
Console.WriteLine(" ""ErrNum"": " & E & ",")
Console.WriteLine(" ""ErrDesc"": " & """" & E_Desc & """" & ",")
Console.WriteLine(" ""StartTime"": " & """" & Now() & """" & ",")
If E = 0 And SelectQry Then ' Start building the Values 2D array...
nRecordCount = 0
While oReader.Read() ' Each line...
nRecordCount += 1 ' Increment row count
If nRecordCount = 1 Then ' --------------------------------------------------1------- FIRST Row (Only!)
nFieldCount = oReader.FieldCount ' Number of columns
Console.WriteLine(" ""FieldCount"": " & nFieldCount & ",")
Console.Write(" ""FieldIDs"":") ' Write field headers...
Console.Write(" {")
For nLooper = 0 To nFieldCount - 1 ' Each field
Console.Write("""" & oReader.GetName(nLooper) & """: " & nLooper)
If nLooper < nFieldCount - 1 Then ' ~~~~~~~~ All EXCEPT Last Field
Console.Write(", ")
End If
Next nLooper
Console.WriteLine("},")
Console.Write(" ""SubTypes"":")
Console.Write(" [")
For nLooper = 0 To nFieldCount - 1 ' Each field
If oReader.IsDBNull(nLooper) Then
Console.Write("""Null""")
Else
Console.Write("""" & Mid(oReader.GetFieldType(nLooper).ToString, 8) & """")
End If
If nLooper < nFieldCount - 1 Then ' ~~~~~~~~ All EXCEPT Last field
Console.Write(", ")
End If
Next nLooper
Console.WriteLine("],")
Console.Write(" ""Types"":")
Console.Write(" [")
For nLooper = 0 To nFieldCount - 1 ' Each field
If oReader.IsDBNull(nLooper) Then
Console.Write("""null""")
Else
Console.Write("""" & cSuperType(Mid(oReader.GetFieldType(nLooper).ToString, 8)) _
.ToString.ToLower() & """")
End If
If nLooper < nFieldCount - 1 Then ' ~~~~~~~~ All EXCEPT Last field
Console.Write(", ")
End If
Next nLooper
Console.WriteLine("],")
Console.Write(" ""Names"":")
Console.Write(" [")
For nLooper = 0 To nFieldCount - 1 ' Each field
Console.Write("""" & oReader.GetName(nLooper) & """")
If nLooper < nFieldCount - 1 Then ' ~~~~~~~~ All EXCEPT Last field
Console.Write(", ")
End If
Next nLooper
Console.WriteLine("],")
Console.WriteLine(" ""Values"":") ' Now start Values 2D Array...
Console.WriteLine(" [")
Console.Write(" [")
Else ' > 1 -------------------------------------------------------------------2,3,4,5- EVERY OTHER Row
Console.WriteLine("],")
Console.Write(" [")
End If
' ---------------------------------------------------------------------------1,2,3,4,5- EVERY Row
For nLooper = 0 To nFieldCount - 1 ' Each field
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Every field
sShortSubType = oReader.GetFieldType(nLooper).ToString.Split(CType(".", Char()))(1) ' Remove prefix: System.
sShortType = cSuperType(sShortSubType).ToString ' Hashtable lookup
sVal_Qt = IIf((sShortType = "String" Or sShortType = "Date"), """", "").ToString()
If oReader.IsDBNull(nLooper) Then
Console.Write("null")
Else
Console.Write(sVal_Qt)
Console.Write(oReader(nLooper).ToString() _
.Replace("\", "\\") _
.Replace("""", "\""") _
.Replace(vbCrLf, " ") _
.Replace(vbCr, " ") _
.Replace(vbLf, " ") _
.Replace(vbTab, " ") _
.Replace("True", "true") _
.Replace("False", "false"))
Console.Write(sVal_Qt)
End If
If nLooper < nFieldCount - 1 Then ' ~~~~~~~~~~~~ All EXCEPT Last field
Console.Write(", ")
End If
Next nLooper
End While
If nRecordCount > 0 Then ' --------------------------------------------------------------5- After LAST Row
Console.WriteLine("]")
Console.WriteLine(" ],")
End If ' -------------------------------------------------------------------------------------------------
End If
Console.Write(" ""RecordCount"": " & nRecordCount & "," & vbCrLf &
" ""EndTime"": " & """" & Now() & """" & vbCrLf)
Console.Write("}")
If oConn.State <> ConnectionState.Closed Then oConn.Close()
oCmd = Nothing
oConn = Nothing
End Sub
End Module