]>
Commit | Line | Data |
---|---|---|
e5993b94 JR |
1 | // Kelsier project - Database interaction code (Database.cs) |
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/>. | |
19 | // | |
20 | ||
21 | using System; | |
22 | using System.Collections.Generic; | |
23 | using System.Linq; | |
24 | using System.Text; | |
25 | using MySql.Data.MySqlClient; | |
26 | ||
27 | namespace Kelsier.Common { | |
28 | public class Database { | |
29 | public MySqlConnection dbh { get; private set; } | |
30 | ||
31 | private string dbinfo; | |
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) { | |
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(); | |
53 | } | |
54 | ||
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]); | |
67 | } | |
68 | ||
69 | return cmdo.ExecuteNonQuery(); | |
70 | } | |
71 | ||
72 | public object queryScalar(string query) { | |
73 | MySqlCommand cmdo = new MySqlCommand(query, dbh); | |
74 | return cmdo.ExecuteScalar(); | |
75 | } | |
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 | } | |
88 | ||
89 | public MySqlDataReader queryReader(string query) { | |
90 | MySqlCommand cmdo = new MySqlCommand(query, dbh); | |
91 | return cmdo.ExecuteReader(); | |
92 | } | |
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 | } | |
107 | } | |
108 | ||
109 | public class PlaceholdersException : Exception { } | |
110 | } |