Advertisement
konalisp

Database

Sep 28th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.70 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Caesura.SharperBot
  4. {
  5.     using System.Collections.Generic;
  6.     using System.Linq;
  7.     using System.Text;
  8.     using System.Threading;
  9.     using System.IO;
  10.     using System.Xml;
  11.     using System.Xml.Linq;
  12.    
  13.     public class DatabaseManager
  14.     {
  15.         public string Name { private set; get; }
  16.         public string DBPath { private set; get; }
  17.        
  18.         public DatabaseManager() : this(null) { }
  19.        
  20.         public DatabaseManager(string path)
  21.         {
  22.             Name = "Database";
  23.             if (path != null)
  24.                 DBPath = path;
  25.             else
  26.                 DBPath = Program.DataFolder + @"\Database";
  27.            
  28.             if (!Directory.Exists(DBPath))
  29.             {
  30.                 Directory.CreateDirectory(DBPath);
  31.                 Program.Variables.SystemLog.Log(Name, string.Format("Created Database folder at {0}.", DBPath));
  32.             }
  33.         }
  34.        
  35.         public Context Register(string name)
  36.         {
  37.             return Register(name, true);
  38.         }
  39.        
  40.         public Context Register(string name, bool makenew)
  41.         {
  42.             return new Context(DBPath, name, makenew);
  43.         }
  44.        
  45.         public class Context
  46.         {
  47.             //TODO: return an IEnumerable for all files in the context's directory
  48.             public string Name { private set; get; }
  49.             public string CPath { set; get; }
  50.            
  51.             public Context(string path, string name, bool makenew)
  52.             {
  53.                 string fpath = string.Format(@"{0}\{1}", path, name);
  54.                 CPath = fpath;
  55.                 Name = string.Format("DatabaseContext({0})", name);
  56.                
  57.                 if (!Directory.Exists(fpath) && makenew == true)
  58.                 {
  59.                     Directory.CreateDirectory(fpath);
  60.                     Program.Variables.SystemLog.Log(Name, string.Format("Created folder at {0}.", fpath));
  61.                 }
  62.             }
  63.            
  64.             public FileContext Open(string file)
  65.             {
  66.                 if (Directory.Exists(CPath))
  67.                     return new FileContext(CPath, file);
  68.                 else
  69.                     return null;
  70.             }
  71.            
  72.             public IEnumerable<FileContext> OpenAll {
  73.                     get { return GetOpenAll(); }
  74.                 }
  75.            
  76.             public IEnumerable<FileContext> GetOpenAll()
  77.             {
  78.                 string[] sa = Directory.GetFiles(CPath);
  79.                 foreach (string s in sa)
  80.                 {
  81.                     yield return new FileContext(s, null);
  82.                 }
  83.             }
  84.         }
  85.        
  86.         public class Element
  87.         {
  88.             public string Key { set; get; }
  89.             public string Value { set; get; }
  90.             public XElement Inner { set; get; }
  91.             public Element() { }
  92.             public Element(string k, string v) : this(k, v, null) { }
  93.             public Element(string k, string v, XElement i)
  94.             {
  95.                 Key = k;
  96.                 Value = v;
  97.                 Inner = i;
  98.             }
  99.         }
  100.        
  101.         public class FileContext
  102.         {
  103.             private object locker = new object();
  104.             public string Name { private set; get; }
  105.             public string CPath { set; get; }
  106.            
  107.             public FileContext(string path, string name)
  108.             {
  109.                 string fpath = path;
  110.                
  111.                 if (name != null)
  112.                 {
  113.                     if (!name.All(c => Char.IsLetterOrDigit(c) || c.Equals('_')))
  114.                     {
  115.                         name = Guid.NewGuid().ToString();
  116.                     }
  117.                 }
  118.                 else
  119.                 {
  120.                     name = Guid.NewGuid().ToString();
  121.                 }
  122.                
  123.                 fpath = string.Format(@"{0}\{1}.xml", path, name);
  124.                 CPath = fpath;
  125.                 Name = string.Format("DatabaseFileContext({0})", name);
  126.                
  127.                 if (!File.Exists(fpath))
  128.                 {
  129.                     lock (locker)
  130.                     {
  131.                         using (FileStream fs = File.Create(fpath))
  132.                         {
  133.                             string s = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" +
  134.                                         "<!-- Automatically generated file. Do not modifiy. -->\n" +
  135.                                         "<DB>\n\t\n</DB>";
  136.                             Byte[] info = new UTF8Encoding(true).GetBytes(s);
  137.                             fs.Write(info, 0, info.Length);
  138.                         }
  139.                     }
  140.                     Program.Variables.SystemLog.Log(Name, string.Format("Created file at {0}.", fpath));
  141.                 }
  142.             }
  143.            
  144.             public Element FindElement(string pkey)
  145.             {
  146.                 Element e = null;
  147.                 try
  148.                 {
  149.                     foreach (var elm in GetAll())
  150.                     {
  151.                         if (elm.Key == pkey)
  152.                         {
  153.                             e = elm;
  154.                             break;
  155.                         }
  156.                     }
  157.                 }
  158.                 catch
  159.                 {
  160.                    
  161.                 }
  162.                 return e;
  163.             }
  164.            
  165.             public XElement FindInner(string pkey)
  166.             {
  167.                 var p = FindElement(pkey);
  168.                 if (p != null)
  169.                     return p.Inner;
  170.                 return null;
  171.             }
  172.            
  173.             public string Find(string pkey)
  174.             {
  175.                 var p = FindElement(pkey);
  176.                 if (p != null)
  177.                     return p.Value;
  178.                 return null;
  179.             }
  180.            
  181.             public IEnumerable<Element> All {
  182.                     get { return GetAll(); }
  183.                 }
  184.            
  185.             public IEnumerable<Element> GetAll()
  186.             {
  187.                 lock (locker)
  188.                 {
  189.                     XDocument xmlFile = XDocument.Load(CPath);
  190.                     var query = from c in xmlFile.Elements("DB").Elements("DBReg") select c;
  191.                     foreach (XElement elm in query)
  192.                     {
  193.                         Element e = new Element();
  194.                         e.Key = elm.Attribute("key").Value;
  195.                         e.Value = elm.Attribute("value").Value;
  196.                         foreach (XElement es in elm.Elements())
  197.                         {
  198.                             e.Inner = es;
  199.                             break;
  200.                         }
  201.                         yield return e;
  202.                     }
  203.                 }
  204.             }
  205.            
  206.             public void Persist(Element e)
  207.             {
  208.                 Persist(e.Key, e.Value, e.Inner);
  209.             }
  210.            
  211.             public void Persist(string pkey, string pvalue)
  212.             {
  213.                 Persist(pkey, pvalue, null);
  214.             }
  215.            
  216.             public void Persist(string pkey, string pvalue, XElement content)
  217.             {
  218.                 lock (locker)
  219.                 {
  220.                     XDocument xmlFile = XDocument.Load(CPath);
  221.                    
  222.                     if (string.IsNullOrEmpty(Find(pkey)))
  223.                     {
  224.                         //create
  225.                         xmlFile.Element("DB").Add(
  226.                             new XElement("DBReg",
  227.                                 new XAttribute("key", pkey),
  228.                                 new XAttribute("value", pvalue),
  229.                                 content
  230.                             )
  231.                         );
  232.                     }
  233.                     else
  234.                     {
  235.                         //update
  236.                         var query = from c in xmlFile.Elements("DB").Elements("DBReg") select c;
  237.                         foreach (XElement elm in query)
  238.                         {
  239.                             if (elm.Attribute("key").Value == pkey)
  240.                             {
  241.                                 elm.Attribute("value").Value = pvalue;
  242.                                 if (content != null)
  243.                                 {
  244.                                     elm.RemoveNodes();
  245.                                     elm.Add(content);
  246.                                 }
  247.                             }
  248.                         }
  249.                     }
  250.                    
  251.                     xmlFile.Save(CPath);
  252.                 }
  253.             }
  254.            
  255.             public bool Delete(string pkey)
  256.             {
  257.                 lock (locker)
  258.                 {
  259.                     XDocument xmlFile = XDocument.Load(CPath);
  260.                     //have to duplicate Find's functionality because trying to
  261.                     //use it here seems to permanently hold the locker and then it
  262.                     //can't delete anyting.
  263.                     var query = from c in xmlFile.Elements("DB").Elements("DBReg") select c;
  264.                     foreach (XElement elm in query)
  265.                     {
  266.                         if (elm.Attribute("key").Value == pkey)
  267.                         {
  268.                             xmlFile.Element("DB")
  269.                             .Elements("DBReg")
  270.                             .Where(x => (string)x.Attribute("key") == pkey)
  271.                             .Remove();
  272.                        
  273.                             xmlFile.Save(CPath);
  274.                             return true;
  275.                         }
  276.                     }
  277.                     return false;
  278.                 }
  279.             }
  280.         }
  281.     }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement