/
frmInitTracks.vb
184 lines (144 loc) · 8.11 KB
/
frmInitTracks.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
Imports Microsoft.Win32
Imports System.Data.SQLite
Imports System.IO
Public Class frmInitTracks
Private Sub butBrowseDatabaseFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butBrowseDatabaseFolder.Click
Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey("Software", True)
Dim newkey As RegistryKey = key.CreateSubKey("Gilbert21")
FolderBrowserDialog.SelectedPath = txtParentFolder.Text
If FolderBrowserDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
txtParentFolder.Text = FolderBrowserDialog.SelectedPath
End If
newkey.SetValue("ParentTrackFolder", txtParentFolder.Text)
End Sub
Private Sub frmInitTracks_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey("Software", True)
Dim newkey As RegistryKey = key.CreateSubKey("Gilbert21")
txtParentFolder.Text = newkey.GetValue("ParentTrackFolder")
End Sub
Private Sub butProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butProcess.Click
Dim db As clsDB = New clsDB
Dim comTracks As SQLiteCommand = New SQLiteCommand(db.conTracks)
Dim comRecords As SQLiteCommand = New SQLiteCommand(db.conMain)
'Does folder exist
If Not Directory.Exists(txtParentFolder.Text) Then
MessageBox.Show("The parent folder is not set to the path of an existing folder.")
Exit Sub
End If
'Are there any records in the Tracks table already? If so warn and delete.
comTracks.CommandText = "Select count(*) from Tracks;"
If comTracks.ExecuteScalar() > 0 Then
If MessageBox.Show("There are already records in the Tracks table. If you continue, you will completely replace these. Do you want to continue?", "Tracks exist", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
Exit Sub
Else
comTracks.CommandText = "Delete from Tracks;"
comTracks.ExecuteNonQuery()
comTracks.CommandText = "Delete from RecordTracks;"
comTracks.ExecuteNonQuery()
End If
End If
Cursor = Cursors.WaitCursor
'First insert a value in the Tracks table for every unique value of Filename field
'in the Records table.
Dim dtRecords As DataTable = New DataTable
Dim daRecords As SQLiteDataAdapter
Dim strSQL As String = "Select distinct Filename from Records where filename not null;"
daRecords = New SQLiteDataAdapter(strSQL, db.conMain)
daRecords.Fill(dtRecords)
Dim paramFilename As SQLiteParameter = New SQLiteParameter()
comTracks.Parameters.Add(paramFilename)
comTracks.CommandText = "insert into Tracks (Filename) Values (?)"
Dim rowRecord As DataRow
Dim transTracks As SQLiteTransaction = db.conTracks.BeginTransaction()
For Each rowRecord In dtRecords.Rows
paramFilename.Value = rowRecord("Filename")
comTracks.ExecuteNonQuery()
Next
transTracks.Commit()
'Now go find every CSV file under the parent directory and if it matches an
'entry in the Tracks table, then complete the details in the table. If more than
'one file exists with the same name, create entries for both.
transTracks = db.conTracks.BeginTransaction()
UpdateTracksFromFiles(txtParentFolder.Text, comTracks)
transTracks.Commit()
'Now go through all the records in the Records table and which link to a track
'and create a corresponding link entry in the RecordTracks table.
strSQL = "Select ID, Filename from Records where filename not null;"
daRecords = New SQLiteDataAdapter(strSQL, db.conMain)
dtRecords.Clear()
daRecords.Fill(dtRecords)
comTracks.Parameters.Clear()
comTracks.CommandText = "Select FileID from Tracks where filename = ?;"
comTracks.Parameters.Add(paramFilename)
Dim daTracks As SQLiteDataAdapter = New SQLiteDataAdapter(comTracks)
Dim dtTracks As DataTable = New DataTable
Dim rowTracks As DataRow
Dim comRecordTracks As SQLiteCommand = New SQLiteCommand(db.conTracks)
comRecordTracks.CommandText = "insert into RecordTracks (RecordID, FileID) values(?,?);"
Dim paramRecordID As SQLiteParameter = New SQLiteParameter
comRecordTracks.Parameters.Add(paramRecordID)
Dim paramFileID As SQLiteParameter = New SQLiteParameter
comRecordTracks.Parameters.Add(paramFileID)
pbInitiate.Maximum = dtRecords.Rows.Count
transTracks = db.conTracks.BeginTransaction()
For Each rowRecord In dtRecords.Rows
paramFilename.Value = rowRecord("Filename")
dtTracks.Clear()
daTracks.Fill(dtTracks)
For Each rowTracks In dtTracks.Rows
paramRecordID.Value = rowRecord("ID")
paramFileID.Value = rowTracks("FileID")
comRecordTracks.ExecuteNonQuery()
Next
pbInitiate.Value += 1
Next
transTracks.Commit()
Cursor = Cursors.Arrow
MessageBox.Show("Track initialisation is complete.", "Gilbert 21")
Me.Close()
End Sub
Private Sub UpdateTracksFromFiles(ByVal strFolder As String, ByVal comTracks As SQLiteCommand)
'For each file in this folder that is found in the Tracks table,
'update Tracks details. For each sub-folder of this folder, call this
'sub-routine recursively.
Dim strFile As String
Dim strSubFolder As String
Dim strFileName As String
Dim fi As FileInfo
For Each strFile In Directory.GetFiles(strFolder)
strFileName = Path.GetFileName(strFile)
comTracks.CommandText = "Select count(*) from Tracks where FileName = '" & strFileName & "';"
If comTracks.ExecuteScalar() > 0 Then
'The file needs to be recorded in the Tracks table.
'If the existing record's OriginalPath value is null, then update that record,
'else insert a new one.
fi = New FileInfo(strFile)
comTracks.CommandText = "Select OriginalPath from Tracks where FileName = '" & strFileName & "';"
If cfun.HasNoValue(comTracks.ExecuteScalar()) Then
comTracks.CommandText = "Update Tracks set OriginalPath=?, CurrentPath=?, DateCreated=?, FileType=?, GenericFileType=? where FileName = '" & strFileName & "';"
comTracks.Parameters.Clear()
comTracks.Parameters.AddWithValue("OriginalPath", strFile)
comTracks.Parameters.AddWithValue("CurrentPath", strFile)
comTracks.Parameters.AddWithValue("DateCreated", fi.CreationTime)
comTracks.Parameters.AddWithValue("FileType", "Visiontac") 'Ref to visiontac okay
comTracks.Parameters.AddWithValue("GenericFileType", "CSV")
comTracks.ExecuteNonQuery()
Else
'Need to insert a new row into Tracks.
comTracks.CommandText = "Insert into Tracks (Filename, OriginalPath, CurrentPath, DateCreated, FileType, GenericFileType) values(?,?,?,?,?,?);"
comTracks.Parameters.Clear()
comTracks.Parameters.AddWithValue("FileName", strFileName)
comTracks.Parameters.AddWithValue("OriginalPath", strFile)
comTracks.Parameters.AddWithValue("CurrentPath", strFile)
comTracks.Parameters.AddWithValue("DateCreated", fi.CreationTime)
comTracks.Parameters.AddWithValue("FileType", "Visiontac") 'Ref to visiontac okay
comTracks.Parameters.AddWithValue("GenericFileType", "CSV")
comTracks.ExecuteNonQuery()
End If
End If
Next
For Each strSubFolder In Directory.GetDirectories(strFolder)
UpdateTracksFromFiles(strSubFolder, comTracks)
Next
End Sub
End Class