]> jfr.im git - z_archive/kelsier.git/commitdiff
Moving MySQL into new class
authorJohn Runyon <redacted>
Fri, 12 Apr 2013 14:50:34 +0000 (09:50 -0500)
committerJohn Runyon <redacted>
Fri, 12 Apr 2013 14:50:34 +0000 (09:50 -0500)
Database.cs [new file with mode: 0644]
Kelsier.csproj
Logger.cs
Program.cs

diff --git a/Database.cs b/Database.cs
new file mode 100644 (file)
index 0000000..6497ca2
--- /dev/null
@@ -0,0 +1,39 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using MySql.Data.MySqlClient;
+
+namespace Kelsier {
+    class Database {
+        protected MySqlConnection _dbh = null;
+        public MySqlConnection dbh { get { return _dbh; } }
+
+
+        /// <summary>
+        /// Construct a Database, using the MySQL connector string.
+        /// </summary>
+        /// <param name="dbinfo">Connector string</param>
+        public Database(string dbinfo) {
+            _dbh = new MySqlConnection(dbinfo);
+        }
+
+        public void Connect() {
+            try {
+                dbh.Open();
+            } catch (MySqlException e) {
+                Root.log.error("Error: {0}", e.ToString());
+            }
+        }
+
+        public object queryScalar(string query) {
+            MySqlCommand cmdo = new MySqlCommand(query, dbh);
+            return cmdo.ExecuteScalar();
+        }
+
+        public MySqlDataReader queryReader(string query) {
+            MySqlCommand cmdo = new MySqlCommand(query, dbh);
+            return cmdo.ExecuteReader();
+        }
+    }
+}
index 1afe0bfd9e57925cee7592a0ed5fc33334e5494e..40f73ef65331d655d70943e2648126aa813dde30 100644 (file)
@@ -44,6 +44,7 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Database.cs" />
     <Compile Include="Logger.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
index 753b063af872c44dffd2a7b4426ba60528830a8e..bbe14d183cf8b3b5908c54a0044ee2bf680a509f 100644 (file)
--- a/Logger.cs
+++ b/Logger.cs
@@ -4,13 +4,17 @@
 using System.Text;
 using System.IO;
 
-namespace Kelsier
-{
-    class Logger
-    {
+namespace Kelsier {
+    class Logger {
         protected TextWriter[] debugs, infos, errors;
-        public Logger(TextWriter[] debugs, TextWriter[] infos, TextWriter[] errors) // constructor
-        {
+
+        /// <summary>
+        /// Construct a Logger, writing to the specified TextWriter objects. Use an empty array to disable an output.
+        /// </summary>
+        /// <param name="debugs">Array of TextWriter's to use for debug output</param>
+        /// <param name="infos">...for info output</param>
+        /// <param name="errors">...for error output</param>
+        public Logger(TextWriter[] debugs, TextWriter[] infos, TextWriter[] errors) {
             this.debugs = debugs;
             this.infos = infos;
             this.errors = errors;
@@ -20,40 +24,46 @@ class Logger
             this.error("Starting logging (error) here at {0}", DateTime.Now.ToString("F"));
         }
 
-        ~Logger() // destructor
-        {
+        ~Logger() {
             this.debug("Stopping logging (debug) here at {0}", DateTime.Now.ToString("F"));
             this.info("Stopping logging (info) here at {0}", DateTime.Now.ToString("F"));
             this.error("Stopping logging (error) here at {0}", DateTime.Now.ToString("F"));
-            foreach (TextWriter[] group in new TextWriter[][]{this.debugs, this.infos, this.errors})
-            {
-                foreach (TextWriter outw in group)
-                {
+            foreach (TextWriter[] group in new TextWriter[][] { this.debugs, this.infos, this.errors }) {
+                foreach (TextWriter outw in group) {
                     outw.Close();
                 }
             }
         }
 
-        public void debug(string msg, params object[] args)
-        {
-            foreach (TextWriter outw in this.debugs)
-            {
+        /// <summary>
+        /// Debug logging.
+        /// </summary>
+        /// <param name="msg">Log message (with formatting placeholders, if desired)</param>
+        /// <param name="args">Formatting replacements.</param>
+        public void debug(string msg, params object[] args) {
+            foreach (TextWriter outw in this.debugs) {
                 outw.WriteLine("(D@" + DateTime.Now.ToString("HH:mm:ss") + ")[MAIN] " + msg, args);
             }
         }
 
-        public void info(string msg, params object[] args)
-        {
-            foreach (TextWriter outw in this.infos)
-            {
+        /// <summary>
+        /// Info logging.
+        /// </summary>
+        /// <param name="msg">Log message (with formatting placeholders, if desired)</param>
+        /// <param name="args">Formatting replacements.</param>
+        public void info(string msg, params object[] args) {
+            foreach (TextWriter outw in this.infos) {
                 outw.WriteLine("(I@" + DateTime.Now.ToString("HH:mm:ss") + ")[MAIN] " + msg, args);
             }
         }
 
-        public void error(string msg, params object[] args)
-        {
-            foreach (TextWriter outw in this.errors)
-            {
+        /// <summary>
+        /// Error logging.
+        /// </summary>
+        /// <param name="msg">Log message (with formatting placeholders, if desired)</param>
+        /// <param name="args">Formatting replacements.</param>
+        public void error(string msg, params object[] args) {
+            foreach (TextWriter outw in this.errors) {
                 outw.WriteLine("(E@" + DateTime.Now.ToString("HH:mm:ss") + ")[MAIN] " + msg, args);
             }
         }
index b716ab091336b3641bb5ad0c52cc735360e05d79..b174ed9a2db5ee28ff0b496a52ebd25bb4d3cc0c 100644 (file)
@@ -4,46 +4,35 @@
 using System.Text;
 using MySql.Data.MySqlClient;
 
-namespace Kelsier
-{
-    class Program
-    {
-        static protected MySqlConnection _dbh = null;
-        static public MySqlConnection dbh
-        {
-            get { return _dbh; }
-        }
+namespace Kelsier {
+    class Root {
+        static protected Database _db = null;
+        static public Database db { get { return _db; } }
 
         static protected Logger _log;
-        static public Logger log
-        {
-            get { return _log; }
-        }
+        static public Logger log { get { return _log; } }
+
 
-        static void Main(string[] args)
-        {
+        static void Main(string[] args) {
             System.IO.StreamWriter fileout = new System.IO.StreamWriter("kelsier.log", false);
             fileout.AutoFlush = true;
-            System.IO.TextWriter[] outputs = {Console.Out, fileout};
+            System.IO.TextWriter[] outputs = { Console.Out, fileout };
             _log = new Logger(outputs, outputs, outputs);
 
-            _dbh = new MySqlConnection(Properties.Settings.Default.dbstr);
+            _db = new Database(Properties.Settings.Default.dbstr);
 
-            try
-            {
-                dbh.Open();
-            }
-            catch (MySqlException e)
-            {
-                log.error("Error: {0}", e.ToString());
-            }
-
-            MySqlCommand query = new MySqlCommand("SELECT 1+3", dbh);
-            log.debug("1 + 3 = {0}", query.ExecuteScalar().ToString());
+            db.Connect();
+            
+            log.debug("1 + 3 = {0}", db.queryScalar("SELECT 1+3").ToString());
 
             _log = null;
-            Console.Write("Key to quit...");
+            GC.Collect();
+            GC.WaitForPendingFinalizers();
+
+#if DEBUG
+            Console.Write("Key to end...");
             Console.ReadKey();
+#endif
         }
     }