Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Caesura.SharperBot
- {
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.IO;
- using System.Xml;
- using System.Xml.Linq;
- public class DatabaseManager
- {
- public string Name { private set; get; }
- public string DBPath { private set; get; }
- public DatabaseManager() : this(null) { }
- public DatabaseManager(string path)
- {
- Name = "Database";
- if (path != null)
- DBPath = path;
- else
- DBPath = Program.DataFolder + @"\Database";
- if (!Directory.Exists(DBPath))
- {
- Directory.CreateDirectory(DBPath);
- Program.Variables.SystemLog.Log(Name, string.Format("Created Database folder at {0}.", DBPath));
- }
- }
- public Context Register(string name)
- {
- return Register(name, true);
- }
- public Context Register(string name, bool makenew)
- {
- return new Context(DBPath, name, makenew);
- }
- public class Context
- {
- //TODO: return an IEnumerable for all files in the context's directory
- public string Name { private set; get; }
- public string CPath { set; get; }
- public Context(string path, string name, bool makenew)
- {
- string fpath = string.Format(@"{0}\{1}", path, name);
- CPath = fpath;
- Name = string.Format("DatabaseContext({0})", name);
- if (!Directory.Exists(fpath) && makenew == true)
- {
- Directory.CreateDirectory(fpath);
- Program.Variables.SystemLog.Log(Name, string.Format("Created folder at {0}.", fpath));
- }
- }
- public FileContext Open(string file)
- {
- if (Directory.Exists(CPath))
- return new FileContext(CPath, file);
- else
- return null;
- }
- public IEnumerable<FileContext> OpenAll {
- get { return GetOpenAll(); }
- }
- public IEnumerable<FileContext> GetOpenAll()
- {
- string[] sa = Directory.GetFiles(CPath);
- foreach (string s in sa)
- {
- yield return new FileContext(s, null);
- }
- }
- }
- public class Element
- {
- public string Key { set; get; }
- public string Value { set; get; }
- public XElement Inner { set; get; }
- public Element() { }
- public Element(string k, string v) : this(k, v, null) { }
- public Element(string k, string v, XElement i)
- {
- Key = k;
- Value = v;
- Inner = i;
- }
- }
- public class FileContext
- {
- private object locker = new object();
- public string Name { private set; get; }
- public string CPath { set; get; }
- public FileContext(string path, string name)
- {
- string fpath = path;
- if (name != null)
- {
- if (!name.All(c => Char.IsLetterOrDigit(c) || c.Equals('_')))
- {
- name = Guid.NewGuid().ToString();
- }
- }
- else
- {
- name = Guid.NewGuid().ToString();
- }
- fpath = string.Format(@"{0}\{1}.xml", path, name);
- CPath = fpath;
- Name = string.Format("DatabaseFileContext({0})", name);
- if (!File.Exists(fpath))
- {
- lock (locker)
- {
- using (FileStream fs = File.Create(fpath))
- {
- string s = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" +
- "<!-- Automatically generated file. Do not modifiy. -->\n" +
- "<DB>\n\t\n</DB>";
- Byte[] info = new UTF8Encoding(true).GetBytes(s);
- fs.Write(info, 0, info.Length);
- }
- }
- Program.Variables.SystemLog.Log(Name, string.Format("Created file at {0}.", fpath));
- }
- }
- public Element FindElement(string pkey)
- {
- Element e = null;
- try
- {
- foreach (var elm in GetAll())
- {
- if (elm.Key == pkey)
- {
- e = elm;
- break;
- }
- }
- }
- catch
- {
- }
- return e;
- }
- public XElement FindInner(string pkey)
- {
- var p = FindElement(pkey);
- if (p != null)
- return p.Inner;
- return null;
- }
- public string Find(string pkey)
- {
- var p = FindElement(pkey);
- if (p != null)
- return p.Value;
- return null;
- }
- public IEnumerable<Element> All {
- get { return GetAll(); }
- }
- public IEnumerable<Element> GetAll()
- {
- lock (locker)
- {
- XDocument xmlFile = XDocument.Load(CPath);
- var query = from c in xmlFile.Elements("DB").Elements("DBReg") select c;
- foreach (XElement elm in query)
- {
- Element e = new Element();
- e.Key = elm.Attribute("key").Value;
- e.Value = elm.Attribute("value").Value;
- foreach (XElement es in elm.Elements())
- {
- e.Inner = es;
- break;
- }
- yield return e;
- }
- }
- }
- public void Persist(Element e)
- {
- Persist(e.Key, e.Value, e.Inner);
- }
- public void Persist(string pkey, string pvalue)
- {
- Persist(pkey, pvalue, null);
- }
- public void Persist(string pkey, string pvalue, XElement content)
- {
- lock (locker)
- {
- XDocument xmlFile = XDocument.Load(CPath);
- if (string.IsNullOrEmpty(Find(pkey)))
- {
- //create
- xmlFile.Element("DB").Add(
- new XElement("DBReg",
- new XAttribute("key", pkey),
- new XAttribute("value", pvalue),
- content
- )
- );
- }
- else
- {
- //update
- var query = from c in xmlFile.Elements("DB").Elements("DBReg") select c;
- foreach (XElement elm in query)
- {
- if (elm.Attribute("key").Value == pkey)
- {
- elm.Attribute("value").Value = pvalue;
- if (content != null)
- {
- elm.RemoveNodes();
- elm.Add(content);
- }
- }
- }
- }
- xmlFile.Save(CPath);
- }
- }
- public bool Delete(string pkey)
- {
- lock (locker)
- {
- XDocument xmlFile = XDocument.Load(CPath);
- //have to duplicate Find's functionality because trying to
- //use it here seems to permanently hold the locker and then it
- //can't delete anyting.
- var query = from c in xmlFile.Elements("DB").Elements("DBReg") select c;
- foreach (XElement elm in query)
- {
- if (elm.Attribute("key").Value == pkey)
- {
- xmlFile.Element("DB")
- .Elements("DBReg")
- .Where(x => (string)x.Attribute("key") == pkey)
- .Remove();
- xmlFile.Save(CPath);
- return true;
- }
- }
- return false;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement