--- /dev/null
+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();
+ }
+ }
+}
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="Database.cs" />
<Compile Include="Logger.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
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;
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);
}
}
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
}
}