public SQLiteConnectionED(EDDSqlDbSelection? maindb = null, EDDSqlDbSelection selector = EDDSqlDbSelection.None) { bool locktaken = false; try { _schemaLock.EnterReadLock(); locktaken = true; // System.Threading.Monitor.Enter(monitor); //Console.WriteLine("Connection open " + System.Threading.Thread.CurrentThread.Name); DBFile = GetSQLiteDBFile(maindb ?? SQLiteDBClass.DefaultMainDatabase); _cn = SQLiteDBClass.CreateCN(); // Use the database selected by maindb as the 'main' database _cn.ConnectionString = "Data Source=" + DBFile + ";Pooling=true;"; _cn.Open(); // Attach any other requested databases under their appropriate names foreach (var dbflag in new[] { EDDSqlDbSelection.EDDiscovery, EDDSqlDbSelection.EDDUser, EDDSqlDbSelection.EDDSystem }) { if (selector.HasFlag(dbflag)) { AttachDatabase(dbflag, dbflag.ToString()); } } } catch { if (locktaken) { _schemaLock.ExitReadLock(); } throw; } }
public static string GetSQLiteDBFile(EDDSqlDbSelection selector) { if (selector == EDDSqlDbSelection.None) { // Use an in-memory database if no database is selected return ":memory:"; } else if (selector.HasFlag(EDDSqlDbSelection.EDDUser)) { // Get the EDDUser database path return System.IO.Path.Combine(Tools.GetAppDataDirectory(), "EDDUser.sqlite"); } else if (selector.HasFlag(EDDSqlDbSelection.EDDSystem)) { // Get the EDDSystem database path return System.IO.Path.Combine(Tools.GetAppDataDirectory(), "EDDSystem.sqlite"); } else { // Get the old EDDiscovery database path return System.IO.Path.Combine(Tools.GetAppDataDirectory(), "EDDiscovery.sqlite"); } }
private void AttachDatabase(EDDSqlDbSelection dbflag, string name) { // Check if the connection is already connected to the selected database using (DbCommand cmd = _cn.CreateCommand("PRAGMA database_list")) { using (DbDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { var dbname = reader["name"] as string; if (dbname == name) { return; } } } } // Attach to the selected database under the given schema name using (DbCommand cmd = _cn.CreateCommand("ATTACH DATABASE @dbfile AS @dbname")) { cmd.AddParameterWithValue("@dbfile", GetSQLiteDBFile(dbflag)); cmd.AddParameterWithValue("@dbname", name); cmd.ExecuteNonQuery(); } }