MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
DatabaseManagement.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Data.SqlClient;
6using MyCaffe.basecode;
7using System.Diagnostics;
8
9namespace MyCaffe.db.image
10{
14 public class DatabaseManagement
15 {
16 ConnectInfo m_ci;
17 string m_strPath;
18
22 protected bool m_bUpdateDatabase = true;
23
29 public DatabaseManagement(ConnectInfo ci, string strPath = "")
30 {
31 m_ci = ci;
32 m_strPath = strPath;
33 }
34
39 {
40 get { return m_ci; }
41 }
42
48 protected string GetConnectionString(string strName)
49 {
50 return "Data Source=" + m_ci.Server + ";Initial Catalog=" + strName + ";Integrated Security=True; MultipleActiveResultSets=True;";
51 }
52
58 public Exception DatabaseExists(out bool bExists)
59 {
60 int nResult = 0;
61
62 bExists = false;
63
64 try
65 {
66 SqlConnection connection = new SqlConnection(GetConnectionString("master"));
67 SqlCommand cmdQuery = new SqlCommand(getQueryDatabaseCmd(m_ci.Database), connection);
68
69 connection.Open();
70 SqlDataReader reader = cmdQuery.ExecuteReader();
71
72 while (reader.Read())
73 {
74 nResult = reader.GetInt32(0);
75 }
76
77 connection.Close();
78 cmdQuery.Dispose();
79 }
80 catch (Exception excpt)
81 {
82 return excpt;
83 }
84
85 bExists = (nResult == 1) ? true : false;
86
87 return null;
88 }
89
94 public Exception PurgeDatabase()
95 {
96 try
97 {
98 SqlConnection connection = new SqlConnection(GetConnectionString(m_ci.Database));
99
100 connection.Open();
101 deleteTables(connection);
102 createTables(connection, false, false);
103 connection.Close();
104 }
105 catch (Exception excpt)
106 {
107 return excpt;
108 }
109
110 return null;
111 }
112
118 public Exception CreateDatabase(bool bUpdateDatabase = false)
119 {
120 try
121 {
122 SqlConnection connection;
123
124 m_bUpdateDatabase = bUpdateDatabase;
125
127 {
128 bool bExists;
129 Exception err = DatabaseExists(out bExists);
130
131 if (err != null)
132 throw err;
133
134 if (bExists)
135 throw new Exception("Database already exists!");
136
137 connection = new SqlConnection(GetConnectionString("master"));
138 SqlCommand cmdCreate;
139 string strCmd = getCreateDatabaseCmd(m_ci.Database, m_strPath);
140
141 connection.Open();
142 cmdCreate = new SqlCommand(strCmd, connection);
143 cmdCreate.CommandTimeout = 120;
144 cmdCreate.ExecuteNonQuery();
145 cmdCreate.Dispose();
146 connection.Close();
147 }
148
149 connection = new SqlConnection(GetConnectionString(m_ci.Database));
150 connection.Open();
151 createTables(connection, true, m_bUpdateDatabase);
152 connection.Close();
153 }
154 catch (Exception excpt)
155 {
156 return excpt;
157 }
158
159 return null;
160 }
161
166 {
167 try
168 {
169 SqlConnection connection;
170
171 connection = new SqlConnection(GetConnectionString(m_ci.Database));
172 connection.Open();
173 createTables(connection, true, true, true);
174 connection.Close();
175 }
176 catch (Exception excpt)
177 {
178 Trace.WriteLine("ERROR: Creating temporal tables - " + excpt.Message);
179 }
180 }
181
189 protected virtual void createTables(SqlConnection connection, bool bFullCreate, bool bUpdateOnly, bool bTemporalOnly = false)
190 {
191 SqlCommand cmdCreate;
192
193 if (!bUpdateOnly)
194 {
195 cmdCreate = new SqlCommand(Properties.Resources.CreateDatasetGroupsTable, connection);
196 cmdCreate.ExecuteNonQuery();
197 cmdCreate.Dispose();
198
199 cmdCreate = new SqlCommand(Properties.Resources.CreateDatasetsTable, connection);
200 cmdCreate.ExecuteNonQuery();
201 cmdCreate.Dispose();
202
203 cmdCreate = new SqlCommand(Properties.Resources.CreateDatasetParametersTable, connection);
204 cmdCreate.ExecuteNonQuery();
205 cmdCreate.Dispose();
206
207 cmdCreate = new SqlCommand(Properties.Resources.CreateLabelsTable, connection);
208 cmdCreate.ExecuteNonQuery();
209 cmdCreate.Dispose();
210
211 cmdCreate = new SqlCommand(Properties.Resources.CreateLabelBoostsTable, connection);
212 cmdCreate.ExecuteNonQuery();
213 cmdCreate.Dispose();
214
215 cmdCreate = new SqlCommand(Properties.Resources.CreateRawImageGroupsTable, connection);
216 cmdCreate.ExecuteNonQuery();
217 cmdCreate.Dispose();
218
219 cmdCreate = new SqlCommand(Properties.Resources.CreateRawImageMeansTable, connection);
220 cmdCreate.ExecuteNonQuery();
221 cmdCreate.Dispose();
222
223 cmdCreate = new SqlCommand(Properties.Resources.CreateRawImageResultsTable, connection);
224 cmdCreate.ExecuteNonQuery();
225 cmdCreate.Dispose();
226
227 cmdCreate = new SqlCommand(Properties.Resources.CreateRawImageParametersTable, connection);
228 cmdCreate.ExecuteNonQuery();
229 cmdCreate.Dispose();
230
231 cmdCreate = new SqlCommand(Properties.Resources.CreateRawImagesTable, connection);
232 cmdCreate.ExecuteNonQuery();
233 cmdCreate.Dispose();
234
235 cmdCreate = new SqlCommand(Properties.Resources.CreateSourcesTable, connection);
236 cmdCreate.ExecuteNonQuery();
237 cmdCreate.Dispose();
238
239 cmdCreate = new SqlCommand(Properties.Resources.CreateSourceParametersTable, connection);
240 cmdCreate.ExecuteNonQuery();
241 cmdCreate.Dispose();
242
243 cmdCreate = new SqlCommand(Properties.Resources.CreateModelGroupsTable, connection);
244 cmdCreate.ExecuteNonQuery();
245 cmdCreate.Dispose();
246
247 if (bFullCreate)
248 {
249 cmdCreate = new SqlCommand(Properties.Resources.CreateDatasetCreatorsTable, connection);
250 cmdCreate.ExecuteNonQuery();
251 cmdCreate.Dispose();
252 }
253 }
254
255 cmdCreate = new SqlCommand(Properties.Resources.CreateRawValuesTable, connection);
256 cmdCreate.ExecuteNonQuery();
257 cmdCreate.Dispose();
258
259 cmdCreate = new SqlCommand(Properties.Resources.CreateValueStreamsTable, connection);
260 cmdCreate.ExecuteNonQuery();
261 cmdCreate.Dispose();
262
263 cmdCreate = new SqlCommand(Properties.Resources.CreateValueItemsTable, connection);
264 cmdCreate.ExecuteNonQuery();
265 cmdCreate.Dispose();
266
267 if (!bTemporalOnly)
268 {
269 cmdCreate = new SqlCommand(Properties.Resources.CreateRawImageIndex, connection);
270 cmdCreate.ExecuteNonQuery();
271 cmdCreate.Dispose();
272
273 cmdCreate = new SqlCommand(Properties.Resources.CreateRawImageIndex2, connection);
274 cmdCreate.ExecuteNonQuery();
275 cmdCreate.Dispose();
276
277 cmdCreate = new SqlCommand(Properties.Resources.CreateRawValuesIndex, connection);
278 cmdCreate.ExecuteNonQuery();
279 cmdCreate.Dispose();
280
281 cmdCreate = new SqlCommand(Properties.Resources.CreateRawValuesIndex2, connection);
282 cmdCreate.ExecuteNonQuery();
283 cmdCreate.Dispose();
284
285 cmdCreate = new SqlCommand(Properties.Resources.UpdateRawImageResultsTable, connection);
286 cmdCreate.ExecuteNonQuery();
287 cmdCreate.Dispose();
288
289 cmdCreate = new SqlCommand(Properties.Resources.UpdateRawImageResultsTable2, connection);
290 cmdCreate.ExecuteNonQuery();
291 cmdCreate.Dispose();
292 }
293 }
294
299 protected virtual void deleteTables(SqlConnection connection)
300 {
301 SqlCommand cmdCreate;
302
303 cmdCreate = new SqlCommand("DROP TABLE DatasetGroups", connection);
304 cmdCreate.ExecuteNonQuery();
305 cmdCreate.Dispose();
306
307 cmdCreate = new SqlCommand("DROP TABLE Datasets", connection);
308 cmdCreate.ExecuteNonQuery();
309 cmdCreate.Dispose();
310
311 cmdCreate = new SqlCommand("DROP TABLE DatasetParameters", connection);
312 cmdCreate.ExecuteNonQuery();
313 cmdCreate.Dispose();
314
315 cmdCreate = new SqlCommand("DROP TABLE Labels", connection);
316 cmdCreate.ExecuteNonQuery();
317 cmdCreate.Dispose();
318
319 cmdCreate = new SqlCommand("DROP TABLE LabelBoosts", connection);
320 cmdCreate.ExecuteNonQuery();
321 cmdCreate.Dispose();
322
323 cmdCreate = new SqlCommand("DROP TABLE RawImageGroups", connection);
324 cmdCreate.ExecuteNonQuery();
325 cmdCreate.Dispose();
326
327 cmdCreate = new SqlCommand("DROP TABLE RawImageMeans", connection);
328 cmdCreate.ExecuteNonQuery();
329 cmdCreate.Dispose();
330
331 cmdCreate = new SqlCommand("DROP TABLE RawImages", connection);
332 cmdCreate.ExecuteNonQuery();
333 cmdCreate.Dispose();
334
335 cmdCreate = new SqlCommand("DROP TABLE RawImageParameters", connection);
336 cmdCreate.ExecuteNonQuery();
337 cmdCreate.Dispose();
338
339 cmdCreate = new SqlCommand("DROP TABLE Sources", connection);
340 cmdCreate.ExecuteNonQuery();
341 cmdCreate.Dispose();
342
343 cmdCreate = new SqlCommand("DROP TABLE SourceParameters", connection);
344 cmdCreate.ExecuteNonQuery();
345 cmdCreate.Dispose();
346
347 cmdCreate = new SqlCommand("DROP TABLE ModelGroups", connection);
348 cmdCreate.ExecuteNonQuery();
349 cmdCreate.Dispose();
350
351 cmdCreate = new SqlCommand("DROP TABLE RawValues", connection);
352 cmdCreate.ExecuteNonQuery();
353 cmdCreate.Dispose();
354
355 cmdCreate = new SqlCommand("DROP TABLE ValueStreams", connection);
356 cmdCreate.ExecuteNonQuery();
357 cmdCreate.Dispose();
358
359 cmdCreate = new SqlCommand("DROP TABLE Items", connection);
360 cmdCreate.ExecuteNonQuery();
361 cmdCreate.Dispose();
362 }
363
369 protected string getQueryDatabaseCmd(string strName)
370 {
371 string strCmd = Properties.Resources.QueryDatabaseExists;
372
373 strCmd = strCmd.Replace("%DBNAME%", strName);
374
375 return strCmd;
376 }
377
384 protected string getCreateDatabaseCmd(string strName, string strPath)
385 {
386 string strCmd = Properties.Resources.CreateDatabase;
387
388 while (strCmd.Contains("%DBNAME%"))
389 {
390 strCmd = strCmd.Replace("%DBNAME%", strName);
391 }
392
393 while (strCmd.Contains("%PATH%"))
394 {
395 strCmd = strCmd.Replace("%PATH%", strPath);
396 }
397
398 return strCmd;
399 }
400 }
401}
The ConnectInfo class specifies the server, database and username/password used to connect to a datab...
Definition: ConnectInfo.cs:14
string Server
Get/set the server.
Definition: ConnectInfo.cs:127
string Database
Get/set the database.
Definition: ConnectInfo.cs:136
The DatabaseManagement class is used to create the image database.
string GetConnectionString(string strName)
Returns the connection string used to connect to the database named 'strName'.
string getQueryDatabaseCmd(string strName)
Returns the query database command.
Exception DatabaseExists(out bool bExists)
Queries whether or not the database exists.
void CreateTemporalTables()
Create the temporal tables.
string getCreateDatabaseCmd(string strName, string strPath)
Returns the create database command.
DatabaseManagement(ConnectInfo ci, string strPath="")
The DatabaseManagement constructor.
virtual void deleteTables(SqlConnection connection)
The deleteTables function deletes all tables except for the DatasetCreators table.
virtual void createTables(SqlConnection connection, bool bFullCreate, bool bUpdateOnly, bool bTemporalOnly=false)
The createTables function creates the tables of the database.
Exception PurgeDatabase()
The PurgeDatabase function delete the data from the database.
bool m_bUpdateDatabase
Specifies whether or not the database is just being updated or not.
Exception CreateDatabase(bool bUpdateDatabase=false)
The CreateDatabae creates a new instance of the database in Microsoft SQL.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
The MyCaffe.db.image namespace contains all image database related classes.
Definition: Database.cs:18
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12