diff options
Diffstat (limited to 'OpenSim/Data/MySQL/MySQLRaw.cs')
-rw-r--r-- | OpenSim/Data/MySQL/MySQLRaw.cs | 197 |
1 files changed, 197 insertions, 0 deletions
diff --git a/OpenSim/Data/MySQL/MySQLRaw.cs b/OpenSim/Data/MySQL/MySQLRaw.cs new file mode 100644 index 0000000..bb8c96c --- /dev/null +++ b/OpenSim/Data/MySQL/MySQLRaw.cs | |||
@@ -0,0 +1,197 @@ | |||
1 | // https://dev.mysql.com/doc/connector-net/en/ | ||
2 | |||
3 | |||
4 | using System; | ||
5 | using System.Collections; | ||
6 | using System.Collections.Generic; | ||
7 | using System.Data; | ||
8 | using System.Reflection; | ||
9 | using System.Text; | ||
10 | using log4net; | ||
11 | using MySql.Data.MySqlClient; | ||
12 | |||
13 | namespace OpenSim.Data.MySQL | ||
14 | { | ||
15 | public class MySQLRaw | ||
16 | { | ||
17 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
18 | private string m_connectString; | ||
19 | |||
20 | public MySQLRaw(string connect) | ||
21 | { | ||
22 | m_connectString = connect; | ||
23 | } | ||
24 | |||
25 | public int Count(string table) | ||
26 | { | ||
27 | return Count(table, ""); | ||
28 | } | ||
29 | public int Count(string table, string wher) | ||
30 | { | ||
31 | string query = "SELECT Count(*) FROM " + table; | ||
32 | if ("" != wher) | ||
33 | query = query + " WHERE " + wher; | ||
34 | int result = -1; | ||
35 | |||
36 | object r = doScalarQuery(query); | ||
37 | if (r != null) | ||
38 | result = Convert.ToInt32(r); | ||
39 | |||
40 | return result; | ||
41 | } | ||
42 | |||
43 | public List< Hashtable > Join(string table, string select, string join, string wher, string order) | ||
44 | { | ||
45 | if ("" == select) | ||
46 | select = "*"; | ||
47 | string query = "SELECT " + select + " FROM " + table; | ||
48 | if ("" != join) | ||
49 | query = query + " " + join; | ||
50 | if ("" != wher) | ||
51 | query = query + " WHERE " + wher; | ||
52 | if ("" != order) | ||
53 | query = query + " ORDER BY " + order; | ||
54 | |||
55 | using (MySqlConnection dbcon = new MySqlConnection(m_connectString)) | ||
56 | { | ||
57 | dbcon.Open(); | ||
58 | MySqlCommand cmd = new MySqlCommand(query, dbcon); | ||
59 | MySqlDataReader rdr = cmd.ExecuteReader(); | ||
60 | List<string> names = new List<string>(); | ||
61 | DataTable schema = rdr.GetSchemaTable(); | ||
62 | List< Hashtable > list = new List< Hashtable >(); | ||
63 | |||
64 | foreach (DataRow row in schema.Rows) | ||
65 | { | ||
66 | string tbl = ""; | ||
67 | string nm = ""; | ||
68 | string tp = ""; | ||
69 | foreach (DataColumn col in schema.Columns) | ||
70 | { | ||
71 | if ("BaseTableName" == col.ColumnName) tbl = row[col].ToString(); | ||
72 | if ("ColumnName" == col.ColumnName) nm = row[col].ToString(); | ||
73 | if ("DataType" == col.ColumnName) tp = row[col].ToString(); | ||
74 | } | ||
75 | names.Add(nm); | ||
76 | } | ||
77 | |||
78 | while (rdr.Read()) | ||
79 | { | ||
80 | Hashtable r = new Hashtable(); | ||
81 | foreach (string name in names) | ||
82 | { | ||
83 | r[name] = rdr[name]; | ||
84 | } | ||
85 | list.Add(r); | ||
86 | } | ||
87 | |||
88 | rdr.Close(); | ||
89 | dbcon.Close(); | ||
90 | return list; | ||
91 | } | ||
92 | } | ||
93 | |||
94 | public List< Hashtable > Select(string table, string select, string wher, string order) | ||
95 | { | ||
96 | if ("" == select) | ||
97 | select = "*"; | ||
98 | string query = "SELECT " + select + " FROM " + table; | ||
99 | if ("" != wher) | ||
100 | query = query + " WHERE " + wher; | ||
101 | if ("" != order) | ||
102 | query = query + " ORDER BY " + order; | ||
103 | |||
104 | using (MySqlConnection dbcon = new MySqlConnection(m_connectString)) | ||
105 | { | ||
106 | dbcon.Open(); | ||
107 | MySqlCommand cmd = new MySqlCommand(query, dbcon); | ||
108 | MySqlDataReader rdr = cmd.ExecuteReader(); | ||
109 | List<string> names = new List<string>(); | ||
110 | DataTable schema = rdr.GetSchemaTable(); | ||
111 | List< Hashtable > list = new List< Hashtable >(); | ||
112 | |||
113 | foreach (DataRow row in schema.Rows) | ||
114 | { | ||
115 | string tbl = ""; | ||
116 | string nm = ""; | ||
117 | string tp = ""; | ||
118 | foreach (DataColumn col in schema.Columns) | ||
119 | { | ||
120 | if ("BaseTableName" == col.ColumnName) tbl = row[col].ToString(); | ||
121 | if ("ColumnName" == col.ColumnName) nm = row[col].ToString(); | ||
122 | if ("DataType" == col.ColumnName) tp = row[col].ToString(); | ||
123 | } | ||
124 | names.Add(nm); | ||
125 | } | ||
126 | |||
127 | while (rdr.Read()) | ||
128 | { | ||
129 | Hashtable r = new Hashtable(); | ||
130 | foreach (string name in names) | ||
131 | { | ||
132 | r[name] = rdr[name]; | ||
133 | } | ||
134 | list.Add(r); | ||
135 | } | ||
136 | |||
137 | rdr.Close(); | ||
138 | dbcon.Close(); | ||
139 | return list; | ||
140 | } | ||
141 | } | ||
142 | |||
143 | private object doScalarQuery(string query) | ||
144 | { | ||
145 | try | ||
146 | { | ||
147 | using (MySqlConnection dbcon = new MySqlConnection(m_connectString)) | ||
148 | { | ||
149 | dbcon.Open(); | ||
150 | MySqlCommand cmd = new MySqlCommand(query, dbcon); | ||
151 | Object ret = cmd.ExecuteScalar(); | ||
152 | dbcon.Close(); | ||
153 | return ret; | ||
154 | } | ||
155 | } | ||
156 | catch (MySqlException e) | ||
157 | { | ||
158 | m_log.ErrorFormat("[MYSQL RAW]: Problem connecting to the database {0}", e.Message); | ||
159 | return null; | ||
160 | } | ||
161 | } | ||
162 | |||
163 | private void doNonQuery(string query) | ||
164 | { | ||
165 | using (MySqlConnection dbcon = new MySqlConnection(m_connectString)) | ||
166 | { | ||
167 | dbcon.Open(); | ||
168 | MySqlCommand cmd = new MySqlCommand(query, dbcon); | ||
169 | cmd.ExecuteNonQuery(); | ||
170 | dbcon.Close(); | ||
171 | } | ||
172 | } | ||
173 | |||
174 | public void Insert(string table) | ||
175 | { | ||
176 | string query = "INSERT INTO " + table + " (name, age) VALUES('John Smith', '33')"; | ||
177 | doNonQuery(query); | ||
178 | } | ||
179 | |||
180 | public void Update(string table, string wher) | ||
181 | { | ||
182 | string query = "UPDATE " + table + " SET name='Joe', age='22'"; | ||
183 | if ("" != wher) | ||
184 | query = query + " WHERE " + wher; | ||
185 | doNonQuery(query); | ||
186 | } | ||
187 | |||
188 | public void Delete(string table, string wher) | ||
189 | { | ||
190 | string query = "DELETE FROM " + table; | ||
191 | if ("" != wher) | ||
192 | query = query + " WHERE " + wher; | ||
193 | doNonQuery(query); | ||
194 | } | ||
195 | |||
196 | } | ||
197 | } | ||