init
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
|
||||
namespace DatabaseExport
|
||||
{
|
||||
public class GlobalSetting : Setting
|
||||
{
|
||||
private const string SYSTEM = "SYSTEM";
|
||||
public GlobalSetting(string id, string defaultPropertyValue)
|
||||
: base(id, PropertyTypes.Global, defaultPropertyValue, SYSTEM)
|
||||
{
|
||||
}
|
||||
protected override void GetPropertyValue(string defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var cmd = DbOperations.GetCommand())
|
||||
{
|
||||
cmd.CommandText = string.Format("SELECT * FROM {0} WHERE {1}=@{1} AND {2}=@{2}", DbOperations.Settings.Table,
|
||||
DbOperations.Settings.UserFields.PropertyId.ToString(), DbOperations.Settings.UserFields.UserId.ToString());
|
||||
DbOperations.CreateParam(cmd, string.Format("@{0}", DbOperations.Settings.UserFields.PropertyId.ToString()), System.Data.SqlDbType.NVarChar,
|
||||
PropertyId);
|
||||
DbOperations.CreateParam(cmd, string.Format("@{0}", DbOperations.Settings.UserFields.UserId.ToString()), System.Data.SqlDbType.NVarChar,
|
||||
SYSTEM);
|
||||
|
||||
using (var ds = DbOperations.Connection.QueryDataSet(cmd))
|
||||
{
|
||||
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
|
||||
{
|
||||
_propertyValue = Convert.ToString(ds.Tables[0].Rows[0][DbOperations.Settings.UserFields.PropertyValue.ToString()]);
|
||||
}
|
||||
else
|
||||
{
|
||||
_propertyValue = defaultValue;
|
||||
//StoreInDB();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.Exception)
|
||||
{
|
||||
_propertyValue = defaultValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
namespace DatabaseExport
|
||||
{
|
||||
/// <summary>
|
||||
/// generic base class for settings
|
||||
/// </summary>
|
||||
public abstract class Setting
|
||||
{
|
||||
protected string _propertyId;
|
||||
/// <summary>
|
||||
/// string id of property
|
||||
/// </summary>
|
||||
public string PropertyId => _propertyId;
|
||||
protected int _propertyType;
|
||||
// /// <summary>
|
||||
// /// type of property (global or user specific)
|
||||
// /// </summary>
|
||||
// public PropertyTypes PropertyType { get { return (PropertyTypes)_propertyType; } }
|
||||
protected string _propertyValue;
|
||||
/// <summary>
|
||||
/// value of property
|
||||
/// does not currently do length checking and there is a max length
|
||||
/// </summary>
|
||||
public string PropertyValue => _propertyValue;
|
||||
protected string _userId;
|
||||
// /// <summary>
|
||||
// /// User for user specific settings
|
||||
// /// </summary>
|
||||
// public string UserId { get { return _userId; } }
|
||||
|
||||
public enum PropertyTypes
|
||||
{
|
||||
User = 1 << 0,
|
||||
Global = 1 << 1
|
||||
}
|
||||
|
||||
public Setting(string id, PropertyTypes type, string defaultPropertyValue, string userId)
|
||||
{
|
||||
_userId = userId;
|
||||
_propertyId = id;
|
||||
_propertyType = (int)type;
|
||||
GetPropertyValue(defaultPropertyValue);
|
||||
}
|
||||
|
||||
protected abstract void GetPropertyValue(string defaultValue);
|
||||
|
||||
public void SetValue(string value)
|
||||
{
|
||||
_propertyValue = value;
|
||||
//StoreInDB();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user