134 lines
4.7 KiB
C#
134 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace DatabaseExport
|
|
{
|
|
/// <summary>
|
|
/// handles storing/retrieving settings from a db
|
|
/// assumes DB connection is already established or connection information has already been set for db
|
|
/// </summary>
|
|
public class SettingsDB
|
|
{
|
|
private static SettingsDB _instance = null;
|
|
private static object _MyLock = new object();
|
|
|
|
private static SettingsDB Instance
|
|
{
|
|
get
|
|
{
|
|
lock (_MyLock)
|
|
{
|
|
if (null == _instance)
|
|
{
|
|
_instance = new SettingsDB();
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
}
|
|
|
|
private Dictionary<string, Setting> _SettingsLookup = new Dictionary<string, Setting>();
|
|
|
|
private SettingsDB()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// retrieves a global value, creates using default value if property doesn't exist yet
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="defaultValue"></param>
|
|
/// <returns></returns>
|
|
public static string GetGlobalValue(string id, string defaultValue)
|
|
{
|
|
lock (_MyLock)
|
|
{
|
|
if (!Instance._SettingsLookup.ContainsKey(id))
|
|
{
|
|
Instance._SettingsLookup[id] = new GlobalSetting(id, defaultValue);
|
|
}
|
|
return Instance._SettingsLookup[id].PropertyValue;
|
|
}
|
|
}
|
|
public static int GetGlobalValueInt(string id, int defaultValue)
|
|
{
|
|
string sValue = GetGlobalValue(id, defaultValue.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
|
int d = defaultValue;
|
|
if (!int.TryParse(sValue, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out d))
|
|
{
|
|
return defaultValue;
|
|
}
|
|
else { return d; }
|
|
}
|
|
/// <summary>
|
|
/// returns the global value for given property, or the default value if no value currently exists
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="defaultValue"></param>
|
|
/// <returns></returns>
|
|
public static bool GetGlobalValueBool(string id, bool defaultValue)
|
|
{
|
|
string sValue = GetGlobalValue(id, defaultValue.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
|
bool b = defaultValue;
|
|
if (!Boolean.TryParse(sValue, out b))
|
|
{
|
|
return b;
|
|
}
|
|
else { return b; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// stores an application global property in the database
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="value"></param>
|
|
public static void SetGlobalValue(string id, string value)
|
|
{
|
|
lock (_MyLock)
|
|
{
|
|
if (!Instance._SettingsLookup.ContainsKey(id))
|
|
{
|
|
Instance._SettingsLookup[id] = new GlobalSetting(id, value);
|
|
}
|
|
else
|
|
{
|
|
Instance._SettingsLookup[id].SetValue(value);
|
|
}
|
|
}
|
|
}
|
|
public static void SetGlobalValueInt(string id, int value)
|
|
{
|
|
lock (_MyLock)
|
|
{
|
|
if (!Instance._SettingsLookup.ContainsKey(id))
|
|
{
|
|
Instance._SettingsLookup[id] = new GlobalSetting(id, value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
|
}
|
|
else
|
|
{
|
|
Instance._SettingsLookup[id].SetValue(value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// stores an application global property value in the database
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="value"></param>
|
|
public static void SetGlobalValueBoolean(string id, bool value)
|
|
{
|
|
lock (_MyLock)
|
|
{
|
|
if (!Instance._SettingsLookup.ContainsKey(id))
|
|
{
|
|
Instance._SettingsLookup[id] = new GlobalSetting(id, value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
|
}
|
|
else
|
|
{
|
|
Instance._SettingsLookup[id].SetValue(value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|