public static GetInstance ( ) : |
||
return |
public DateTimeOffset GetSettingsDate(string key, DateTimeOffset byDefault) { try { var query = "SELECT value FROM " + Settings.SettingsDbTable + " WHERE key=" + Database.GetInstance().Q(key) + ";"; var ret = Database.GetInstance().ExecuteScalar2(query); if (ret == null) { return(byDefault); } var retDt = DateTimeOffset.Parse((string)ret); return(retDt); } catch { return(byDefault); } }
/// <summary> /// Checks if a settings for a given key is stored. /// /// returns false in case of an error /// </summary> /// <param name="key"></param> /// <returns></returns> public bool HasSetting(string key) { try { var query = "SELECT value FROM " + Settings.SettingsDbTable + " WHERE key=" + Database.GetInstance().Q(key) + ";"; var ret = Database.GetInstance().ExecuteScalar2(query); if (ret == null) { return(false); } else { return(true); } } catch { return(false); } }
/// <summary> /// gets the stored setting value for a given key /// </summary> /// <param name="key"></param> /// <param name=""></param> /// <returns></returns> public string GetSettingsString(string key, string byDefault) { try { var query = "SELECT value FROM " + Settings.SettingsDbTable + " WHERE key=" + Database.GetInstance().Q(key) + ";"; var ret = Database.GetInstance().ExecuteScalar2(query); if (ret != null) { return(ret.ToString()); } else { return(byDefault); } } catch { return(byDefault); } }
/// <summary> /// Gets the stored setting value for a given key /// </summary> /// <param name="key"></param> /// <returns>true if 1, false if 0</returns> public bool GetSettingsBool(string key, bool byDefault) { try { var query = "SELECT value FROM " + Settings.SettingsDbTable + " WHERE key=" + Database.GetInstance().Q(key) + ";"; var ret = Database.GetInstance().ExecuteScalar2(query); if (ret == null) { return(byDefault); } else { return((string)ret == "1"); } } catch { return(byDefault); } }
/// <summary> /// Gets the stored setting value for a given key /// </summary> /// <param name="key"></param> /// <returns></returns> public int GetSettingsInt(string key, int byDefault) { try { var query = "SELECT value FROM " + Settings.SettingsDbTable + " WHERE key=" + Database.GetInstance().Q(key) + ";"; var ret = Database.GetInstance().ExecuteScalar(query); if (ret > 0) { return(ret); } else { return(byDefault); } } catch { return(byDefault); } }
/// <summary> /// Opens a connection to the database with the current database save path /// </summary> public void Connect() { var dbJustCreated = (File.Exists(CurrentDatabaseDumpFile)) ? false : true; // Open the Database connection _connection = new SQLiteConnection("Data Source=" + CurrentDatabaseDumpFile); _connection.Open(); // Update database version if db was newly created if (dbJustCreated) { Database.GetInstance().UpdateDbPragmaVersion(Settings.DatabaseVersion); } // Create log table if it doesn't exist CreateLogTable(); // Create a settings table if it doesn't exist CreateSettingsTable(); LogInfo(string.Format(CultureInfo.InvariantCulture, "Opened the connection to the database (File={0}).", CurrentDatabaseDumpFile)); }
/// <summary> /// Saves a setting with a given key or value. Inserts new entry if nothing is yet stored /// or updates the value. /// </summary> /// <param name="key"></param> /// <param name="value"></param> public void SetSettings(string key, string value) { try { var query1 = "SELECT value FROM " + Settings.SettingsDbTable + " WHERE key=" + Database.GetInstance().Q(key) + ";"; var query2 = "INSERT INTO " + Settings.SettingsDbTable + " (key, value) VALUES (" + Database.GetInstance().Q(key) + ", " + Database.GetInstance().Q(value) + ");"; var query3 = "UPDATE " + Settings.SettingsDbTable + " SET value=" + Database.GetInstance().Q(value) + " WHERE key=" + Database.GetInstance().Q(key) + ";"; // if key is not yet stored in settings var keyStored = Database.GetInstance().ExecuteScalar2(query1); if (keyStored == null) { Database.GetInstance().ExecuteDefaultQuery(query2); } // if key is stored in settings else { Database.GetInstance().ExecuteDefaultQuery(query3); } } catch (Exception e) { Logger.WriteToLogFile(e); } }