-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclsDB.vb
222 lines (187 loc) · 7.53 KB
/
clsDB.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
Imports System.Data.SQLite
Imports System.IO
Public Class clsDB
Implements IDisposable
Public Sub Dispose() Implements System.IDisposable.Dispose
' Perform any object clean up here.
conSQLiteMain.Close()
conSQLiteGazetteer.Close()
conSQLiteVoice.Close()
conSQLiteTracks.Close()
conSQLiteMedia.Close()
End Sub
Private strErrorMessage As String
Public Property ErrorMessage() As String
Get
Return strErrorMessage
End Get
Set(ByVal value As String)
strErrorMessage = value
End Set
End Property
Private conSQLiteMain As New SQLiteConnection
Public ReadOnly Property conMain() As SQLiteConnection
Get
Return conSQLiteMain
End Get
End Property
Private conSQLiteGazetteer As New SQLiteConnection
Public ReadOnly Property conGazetteer() As SQLiteConnection
Get
Return conSQLiteGazetteer
End Get
End Property
Private conSQLiteVoice As New SQLiteConnection
Public ReadOnly Property conVoice() As SQLiteConnection
Get
Return conSQLiteVoice
End Get
End Property
Private conSQLiteTracks As New SQLiteConnection
Public ReadOnly Property conTracks() As SQLiteConnection
Get
Return conSQLiteTracks
End Get
End Property
Private conSQLiteMedia As New SQLiteConnection
Public ReadOnly Property conMedia() As SQLiteConnection
Get
Return conSQLiteMedia
End Get
End Property
Private conSQLiteExports As New SQLiteConnection
Public ReadOnly Property conExports() As SQLiteConnection
Get
Return conSQLiteExports
End Get
End Property
Public Sub New()
'SQLite DB connections (will create DBs if not already exist)
strErrorMessage = ""
'Check that the database folder and files exist
If frmOptions.txtDBFolder.Text = "" Then
strErrorMessage = "The database folder has not yet been set. Go to options to set the path for the database folder. Learn about this and other important set-up tasks in the quick-start guide from Help."
Exit Sub
End If
If Not File.Exists(Path.Combine(frmOptions.txtDBFolder.Text, "g21main.db3")) Then
strErrorMessage = "There's no database in the database folder. Learn about this and other important set-up tasks in the quick-start guide from Help."
Exit Sub
End If
conSQLiteMain.ConnectionString = "Data Source=" & Path.Combine(frmOptions.txtDBFolder.Text, "g21main.db3") & ";"
Try
conSQLiteMain.Open()
Catch ex As Exception
strErrorMessage = "Failed to connect to Gilbert 21 main DB: " & ex.Message
Exit Sub
End Try
conSQLiteGazetteer.ConnectionString = "Data Source=" & Path.Combine(frmOptions.txtDBFolder.Text, "g21gazetteer.db3") & ";"
Try
conSQLiteGazetteer.Open()
Catch ex As Exception
strErrorMessage = "Failed to connect to Gilbert 21 gazetteer DB: " & ex.Message
Exit Sub
End Try
conSQLiteVoice.ConnectionString = "Data Source=" & Path.Combine(frmOptions.txtDBFolder.Text, "g21voice.db3") & ";"
Try
conSQLiteVoice.Open()
Catch ex As Exception
strErrorMessage = "Failed to connect to Gilbert 21 voice DB: " & ex.Message
Exit Sub
End Try
conSQLiteTracks.ConnectionString = "Data Source=" & Path.Combine(frmOptions.txtDBFolder.Text, "g21tracks.db3") & ";"
Try
conSQLiteTracks.Open()
Catch ex As Exception
strErrorMessage = "Failed to connect to Gilbert 21 tracks DB: " & ex.Message
Exit Sub
End Try
conSQLiteMedia.ConnectionString = "Data Source=" & Path.Combine(frmOptions.txtDBFolder.Text, "g21media.db3") & ";"
Try
conSQLiteMedia.Open()
Catch ex As Exception
strErrorMessage = "Failed to connect to Gilbert 21 media DB: " & ex.Message
Exit Sub
End Try
conSQLiteExports.ConnectionString = "Data Source=" & Path.Combine(frmOptions.txtDBFolder.Text, "g21export.db3") & ";"
Try
conSQLiteExports.Open()
Catch ex As Exception
strErrorMessage = "Failed to connect to Gilbert 21 export DB: " & ex.Message
Exit Sub
End Try
'Additional checks and updates
CheckGUIDField()
CreateExportDB()
End Sub
Private Sub CheckGUIDField()
'Check that the main table has the GUID field (29th Dec 2012) and, if not, then add it in
'and populate it
Dim com As SQLiteCommand = New SQLiteCommand(conSQLiteMain)
com.CommandText = "select GUID from Records limit 1;"
Dim bGUIDPresent As Boolean = False
Try
Dim str As String = com.ExecuteNonQuery()
bGUIDPresent = True
Catch
End Try
If Not bGUIDPresent Then
'GUID field not found, therefore add it in
com.CommandText = "alter table records add column GUID char(36);"
com.ExecuteNonQuery()
End If
Dim SQLtransaction As SQLiteTransaction
SQLtransaction = conSQLiteMain.BeginTransaction()
'Now populate with GUIDs
Dim daRecords As SQLiteDataAdapter = New SQLiteDataAdapter("select * from records where GUID is null", conSQLiteMain)
Dim cbRecords As SQLiteCommandBuilder = New SQLiteCommandBuilder(daRecords)
Dim dsRecords As DataSet = New DataSet
daRecords.Fill(dsRecords, "records")
Dim row As DataRow
For Each row In dsRecords.Tables("records").Rows
row("GUID") = System.Guid.NewGuid.ToString()
Next
daRecords.Update(dsRecords, "records")
SQLtransaction.Commit()
End Sub
Private Sub CreateExportDB()
'Create the SQLiteCommand object
Dim SQLcommand As SQLiteCommand
SQLcommand = conSQLiteExports.CreateCommand()
Dim strSQL As String
'Create the Records table
strSQL = "CREATE TABLE if not exists Exports (" & _
" ExportID INTEGER PRIMARY KEY AUTOINCREMENT," & _
" ShortTitle TEXT," & _
" Type TEXT," & _
" ExportDate DATE," & _
" Notes TEXT," & _
" File TEXT" & _
");"
SQLcommand.CommandText = strSQL
SQLcommand.ExecuteNonQuery()
'Create the RecordExports table
strSQL = "CREATE TABLE if not exists RecordExport (" & _
" RecordID INTEGER," & _
" ExportID INTEGER," & _
" RecDateMod DATE" & _
");"
SQLcommand.CommandText = strSQL
SQLcommand.ExecuteNonQuery()
'Create the RecordExports table
strSQL = "CREATE TABLE if not exists Recipients (" & _
" RecipientID INTEGER PRIMARY KEY AUTOINCREMENT," & _
" Name String," & _
" Notes String" & _
");"
SQLcommand.CommandText = strSQL
SQLcommand.ExecuteNonQuery()
'Create the RecordExports table
strSQL = "CREATE TABLE if not exists ExportRecipient (" & _
" ExportID INTEGER," & _
" RecipientID INTEGER" & _
");"
SQLcommand.CommandText = strSQL
SQLcommand.ExecuteNonQuery()
SQLcommand.Dispose()
End Sub
End Class