]> jfr.im git - z_archive/kelsier.git/blobdiff - Database.cs
Adding auth
[z_archive/kelsier.git] / Database.cs
index 6497ca28f6d4e8956925bccd67dda68e8ff562c9..463e608f924483cb8153948b2a78ea2b91b90c91 100644 (file)
@@ -1,13 +1,34 @@
-using System;
+// Kelsier project - Database interaction code (Database.cs)
+// Written by the Jobbig codeteam. <http://jobbig.eu/code/>
+// Copyright 2013 John Runyon.
+// 
+// This file is part of the Kelsier project.
+// 
+// Kelsier is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+// 
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU Affero General Public License for more details.
+// 
+// You should have received a copy of the GNU Affero General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
+//
+
+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; } }
+namespace Kelsier.Common {
+    public class Database {
+        public MySqlConnection dbh { get; private set; }
+
+        private string dbinfo;
 
 
         /// <summary>
@@ -15,25 +36,75 @@ class Database {
         /// </summary>
         /// <param name="dbinfo">Connector string</param>
         public Database(string dbinfo) {
-            _dbh = new MySqlConnection(dbinfo);
+            this.dbinfo = dbinfo;
+
+            dbh = new MySqlConnection(this.dbinfo);
+            dbh.Open();
         }
+        public Database(Database copy) {
+            this.dbinfo = copy.dbinfo;
 
-        public void Connect() {
-            try {
-                dbh.Open();
-            } catch (MySqlException e) {
-                Root.log.error("Error: {0}", e.ToString());
+            dbh = new MySqlConnection(this.dbinfo);
+            dbh.Open();
+        }
+
+        ~Database() {
+            dbh.Close();
+        }
+
+        public int execute(string query) {
+            MySqlCommand cmdo = new MySqlCommand(query, dbh);
+            return cmdo.ExecuteNonQuery();
+        }
+        public int execute(string query, object[] keys) {
+            MySqlCommand cmdo = new MySqlCommand();
+            cmdo.Connection = dbh;
+            cmdo.CommandText = query;
+            cmdo.Prepare();
+
+            for (int i = 0; i < keys.Length; i += 2) {
+                cmdo.Parameters.AddWithValue(keys[i].ToString(), keys[i + 1]);
             }
+
+            return cmdo.ExecuteNonQuery();
         }
 
         public object queryScalar(string query) {
             MySqlCommand cmdo = new MySqlCommand(query, dbh);
             return cmdo.ExecuteScalar();
         }
+        public object queryScalar(string query, object[] keys) {
+            MySqlCommand cmdo = new MySqlCommand();
+            cmdo.Connection = dbh;
+            cmdo.CommandText = query;
+            cmdo.Prepare();
+
+            for (int i = 0; i < keys.Length; i += 2) {
+                cmdo.Parameters.AddWithValue(keys[i].ToString(), keys[i + 1]);
+            }
+
+            return cmdo.ExecuteScalar();
+        }
 
         public MySqlDataReader queryReader(string query) {
             MySqlCommand cmdo = new MySqlCommand(query, dbh);
             return cmdo.ExecuteReader();
         }
+        public MySqlDataReader queryReader(string query, object[] keys) {
+            if (keys.Length % 2 != 0) {
+                throw new PlaceholdersException();
+            }
+            MySqlCommand cmdo = new MySqlCommand();
+            cmdo.Connection = dbh;
+            cmdo.CommandText = query;
+            cmdo.Prepare();
+
+            for (int i = 0; i < keys.Length; i += 2) {
+                cmdo.Parameters.AddWithValue(keys[i].ToString(), keys[i + 1]);
+            }
+            return cmdo.ExecuteReader();
+        }
     }
+
+    public class PlaceholdersException : Exception { }
 }