aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data
diff options
context:
space:
mode:
authoronefang2019-08-06 01:07:54 +1000
committeronefang2019-08-06 01:07:54 +1000
commitbc7039d7c4bd13b5000011aaa306688bb29529ea (patch)
tree68eb0678787947cdd74d4dd4a665424e53128ffb /OpenSim/Data
parentMore splash page fiddling. (diff)
downloadopensim-SC-bc7039d7c4bd13b5000011aaa306688bb29529ea.zip
opensim-SC-bc7039d7c4bd13b5000011aaa306688bb29529ea.tar.gz
opensim-SC-bc7039d7c4bd13b5000011aaa306688bb29529ea.tar.bz2
opensim-SC-bc7039d7c4bd13b5000011aaa306688bb29529ea.tar.xz
Add a more direct MySQL handler.
Diffstat (limited to 'OpenSim/Data')
-rw-r--r--OpenSim/Data/MySQL/MySQLGenericHandler.cs353
1 files changed, 353 insertions, 0 deletions
diff --git a/OpenSim/Data/MySQL/MySQLGenericHandler.cs b/OpenSim/Data/MySQL/MySQLGenericHandler.cs
new file mode 100644
index 0000000..4f84641
--- /dev/null
+++ b/OpenSim/Data/MySQL/MySQLGenericHandler.cs
@@ -0,0 +1,353 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28using System;
29using System.Collections.Generic;
30using System.Data;
31using System.Reflection;
32using log4net;
33using MySql.Data.MySqlClient;
34using OpenMetaverse;
35using OpenSim.Framework;
36using OpenSim.Region.Framework.Interfaces;
37
38namespace OpenSim.Data.MySQL
39{
40// public class MySQLGenericHandler<T> : MySqlFramework where T: class, new()
41 public class MySQLGenericHandler : MySqlFramework
42 {
43// private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
44
45 protected virtual Assembly Assembly
46 {
47 get { return GetType().Assembly; }
48 }
49
50 public MySQLGenericHandler(MySqlTransaction trans) : base(trans)
51 {
52 CommonConstruct();
53 }
54
55 public MySQLGenericHandler(string connectionString) : base(connectionString)
56 {
57 CommonConstruct();
58 }
59
60 protected void CommonConstruct()
61 {
62 // We always use a new connection for any Migrations
63 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
64 {
65 dbcon.Open();
66 }
67 }
68/*
69 private void CheckColumnNames(IDataReader reader)
70 {
71 if (m_ColumnNames != null)
72 return;
73
74 List<string> columnNames = new List<string>();
75
76 DataTable schemaTable = reader.GetSchemaTable();
77 foreach (DataRow row in schemaTable.Rows)
78 {
79 if (row["ColumnName"] != null)
80 columnNames.Add(row["ColumnName"].ToString());
81 }
82
83 m_ColumnNames = columnNames;
84 }
85*/
86/*
87 public virtual T[] Get(string table, string field, string key)
88 {
89 return Get(table, new string[] { field }, new string[] { key });
90 }
91
92 public virtual T[] Get(string table, string[] fields, string[] keys)
93 {
94 return Get(table, fields, keys, String.Empty);
95 }
96
97 public virtual T[] Get(string table, string[] fields, string[] keys, string options)
98 {
99 if (fields.Length != keys.Length)
100 return new T[0];
101
102 List<string> terms = new List<string>();
103
104 using (MySqlCommand cmd = new MySqlCommand())
105 {
106 for (int i = 0 ; i < fields.Length ; i++)
107 {
108 cmd.Parameters.AddWithValue(fields[i], keys[i]);
109 terms.Add("`" + fields[i] + "` = ?" + fields[i]);
110 }
111
112 string where = String.Join(" and ", terms.ToArray());
113
114 string query = String.Format("select * from {0} where {1} {2}",
115 table, where, options);
116
117 cmd.CommandText = query;
118
119 return DoQuery(cmd);
120 }
121 }
122
123 protected T[] DoQuery(MySqlCommand cmd)
124 {
125 if (m_trans == null)
126 {
127 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
128 {
129 dbcon.Open();
130 T[] ret = DoQueryWithConnection(cmd, dbcon);
131 dbcon.Close();
132 return ret;
133 }
134 }
135 else
136 {
137 return DoQueryWithTransaction(cmd, m_trans);
138 }
139 }
140
141 protected T[] DoQueryWithTransaction(MySqlCommand cmd, MySqlTransaction trans)
142 {
143 cmd.Transaction = trans;
144
145 return DoQueryWithConnection(cmd, trans.Connection);
146 }
147
148 protected T[] DoQueryWithConnection(MySqlCommand cmd, MySqlConnection dbcon)
149 {
150 List<T> result = new List<T>();
151
152 cmd.Connection = dbcon;
153
154 using (IDataReader reader = cmd.ExecuteReader())
155 {
156 if (reader == null)
157 return new T[0];
158/*
159
160 CheckColumnNames(reader);
161
162 while (reader.Read())
163 {
164 T row = new T();
165
166 foreach (string name in m_Fields.Keys)
167 {
168 if (reader[name] is DBNull)
169 {
170 continue;
171 }
172 if (m_Fields[name].FieldType == typeof(bool))
173 {
174 int v = Convert.ToInt32(reader[name]);
175 m_Fields[name].SetValue(row, v != 0 ? true : false);
176 }
177 else if (m_Fields[name].FieldType == typeof(UUID))
178 {
179 m_Fields[name].SetValue(row, DBGuid.FromDB(reader[name]));
180 }
181 else if (m_Fields[name].FieldType == typeof(int))
182 {
183 int v = Convert.ToInt32(reader[name]);
184 m_Fields[name].SetValue(row, v);
185 }
186 else if (m_Fields[name].FieldType == typeof(uint))
187 {
188 uint v = Convert.ToUInt32(reader[name]);
189 m_Fields[name].SetValue(row, v);
190 }
191 else
192 {
193 m_Fields[name].SetValue(row, reader[name]);
194 }
195 }
196
197 if (m_DataField != null)
198 {
199 Dictionary<string, string> data =
200 new Dictionary<string, string>();
201
202 foreach (string col in m_ColumnNames)
203 {
204 data[col] = reader[col].ToString();
205 if (data[col] == null)
206 data[col] = String.Empty;
207 }
208
209 m_DataField.SetValue(row, data);
210 }
211 result.Add(row);
212 }
213*/
214/*
215 }
216 cmd.Connection = null;
217 return result.ToArray();
218 }
219
220 public virtual T[] Get(string table, string where)
221 {
222 using (MySqlCommand cmd = new MySqlCommand())
223 {
224 string query = String.Format("select * from {0} where {1}",
225 table, where);
226
227 cmd.CommandText = query;
228
229 return DoQuery(cmd);
230 }
231 }
232*/
233 public virtual bool Delete(string table, string field, string key)
234 {
235 return Delete(table, new string[] { field }, new string[] { key });
236 }
237
238 public virtual bool Delete(string table, string[] fields, string[] keys)
239 {
240// m_log.DebugFormat(
241// "[MYSQL GENERIC TABLE HANDLER]: Delete(string[] fields, string[] keys) invoked with {0}:{1}",
242// string.Join(",", fields), string.Join(",", keys));
243
244 if (fields.Length != keys.Length)
245 return false;
246
247 List<string> terms = new List<string>();
248
249 using (MySqlCommand cmd = new MySqlCommand())
250 {
251 for (int i = 0 ; i < fields.Length ; i++)
252 {
253 cmd.Parameters.AddWithValue(fields[i], keys[i]);
254 terms.Add("`" + fields[i] + "` = ?" + fields[i]);
255 }
256
257 string where = String.Join(" and ", terms.ToArray());
258
259 string query = String.Format("delete from {0} where {1}", table, where);
260
261 cmd.CommandText = query;
262
263 return ExecuteNonQuery(cmd) > 0;
264 }
265 }
266
267 public long GetCount(string table, string field, string key)
268 {
269 return GetCount(table, new string[] { field }, new string[] { key });
270 }
271
272 public long GetCount(string table, string[] fields, string[] keys)
273 {
274 if (fields.Length != keys.Length)
275 return 0;
276
277 List<string> terms = new List<string>();
278
279 using (MySqlCommand cmd = new MySqlCommand())
280 {
281 for (int i = 0; i < fields.Length; i++)
282 {
283 cmd.Parameters.AddWithValue(fields[i], keys[i]);
284 terms.Add("`" + fields[i] + "` = ?" + fields[i]);
285 }
286
287 string where = String.Join(" and ", terms.ToArray());
288
289 string query = String.Format("select count(*) from {0} where {1}",
290 table, where);
291
292 cmd.CommandText = query;
293
294 Object result = DoQueryScalar(cmd);
295
296 return Convert.ToInt64(result);
297 }
298 }
299
300 public long GetCount(string table, string where)
301 {
302 using (MySqlCommand cmd = new MySqlCommand())
303 {
304 string query = String.Format("select count(*) from {0} where {1}",
305 table, where);
306
307 cmd.CommandText = query;
308
309 object result = DoQueryScalar(cmd);
310
311 return Convert.ToInt64(result);
312 }
313 }
314
315 public long GetCount(string table)
316 {
317 using (MySqlCommand cmd = new MySqlCommand())
318 {
319 string query = String.Format("select count(*) from {0}", table);
320
321 cmd.CommandText = query;
322
323 object result = DoQueryScalar(cmd);
324
325 return Convert.ToInt64(result);
326 }
327 }
328
329 public object DoQueryScalar(MySqlCommand cmd)
330 {
331 if (m_trans == null)
332 {
333 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
334 {
335 dbcon.Open();
336 cmd.Connection = dbcon;
337
338 Object ret = cmd.ExecuteScalar();
339 cmd.Connection = null;
340 dbcon.Close();
341 return ret;
342 }
343 }
344 else
345 {
346 cmd.Connection = m_trans.Connection;
347 cmd.Transaction = m_trans;
348
349 return cmd.ExecuteScalar();
350 }
351 }
352 }
353}