]> jfr.im git - z_archive/kelsier.git/blame - Database.cs
Adding auth
[z_archive/kelsier.git] / Database.cs
CommitLineData
169f10ae 1// Kelsier project - Database interaction code (Database.cs)
7ed73705
JR
2// Written by the Jobbig codeteam. <http://jobbig.eu/code/>
3// Copyright 2013 John Runyon.
4//
5// This file is part of the Kelsier project.
6//
7// Kelsier is free software: you can redistribute it and/or modify
8// it under the terms of the GNU Affero General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU Affero General Public License for more details.
16//
17// You should have received a copy of the GNU Affero General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
169f10ae 19//
7ed73705
JR
20
21using System;
a3c84b90
JR
22using System.Collections.Generic;
23using System.Linq;
24using System.Text;
25using MySql.Data.MySqlClient;
26
e5993b94
JR
27namespace Kelsier.Common {
28 public class Database {
12e0e204
JR
29 public MySqlConnection dbh { get; private set; }
30
31 private string dbinfo;
a3c84b90
JR
32
33
34 /// <summary>
35 /// Construct a Database, using the MySQL connector string.
36 /// </summary>
37 /// <param name="dbinfo">Connector string</param>
38 public Database(string dbinfo) {
12e0e204
JR
39 this.dbinfo = dbinfo;
40
41 dbh = new MySqlConnection(this.dbinfo);
42 dbh.Open();
43 }
44 public Database(Database copy) {
45 this.dbinfo = copy.dbinfo;
46
47 dbh = new MySqlConnection(this.dbinfo);
48 dbh.Open();
49 }
50
51 ~Database() {
52 dbh.Close();
a3c84b90
JR
53 }
54
12e0e204
JR
55 public int execute(string query) {
56 MySqlCommand cmdo = new MySqlCommand(query, dbh);
57 return cmdo.ExecuteNonQuery();
58 }
59 public int execute(string query, object[] keys) {
60 MySqlCommand cmdo = new MySqlCommand();
61 cmdo.Connection = dbh;
62 cmdo.CommandText = query;
63 cmdo.Prepare();
64
65 for (int i = 0; i < keys.Length; i += 2) {
66 cmdo.Parameters.AddWithValue(keys[i].ToString(), keys[i + 1]);
a3c84b90 67 }
12e0e204
JR
68
69 return cmdo.ExecuteNonQuery();
a3c84b90
JR
70 }
71
72 public object queryScalar(string query) {
73 MySqlCommand cmdo = new MySqlCommand(query, dbh);
74 return cmdo.ExecuteScalar();
75 }
12e0e204
JR
76 public object queryScalar(string query, object[] keys) {
77 MySqlCommand cmdo = new MySqlCommand();
78 cmdo.Connection = dbh;
79 cmdo.CommandText = query;
80 cmdo.Prepare();
81
82 for (int i = 0; i < keys.Length; i += 2) {
83 cmdo.Parameters.AddWithValue(keys[i].ToString(), keys[i + 1]);
84 }
85
86 return cmdo.ExecuteScalar();
87 }
a3c84b90
JR
88
89 public MySqlDataReader queryReader(string query) {
90 MySqlCommand cmdo = new MySqlCommand(query, dbh);
91 return cmdo.ExecuteReader();
92 }
12e0e204
JR
93 public MySqlDataReader queryReader(string query, object[] keys) {
94 if (keys.Length % 2 != 0) {
95 throw new PlaceholdersException();
96 }
97 MySqlCommand cmdo = new MySqlCommand();
98 cmdo.Connection = dbh;
99 cmdo.CommandText = query;
100 cmdo.Prepare();
101
102 for (int i = 0; i < keys.Length; i += 2) {
103 cmdo.Parameters.AddWithValue(keys[i].ToString(), keys[i + 1]);
104 }
105 return cmdo.ExecuteReader();
106 }
a3c84b90 107 }
12e0e204 108
e5993b94 109 public class PlaceholdersException : Exception { }
a3c84b90 110}