MySQLGenericTableHandler.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. using System;
  28. using System.Collections.Generic;
  29. using System.Data;
  30. using System.Reflection;
  31. using log4net;
  32. using MySql.Data.MySqlClient;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.Framework.Interfaces;
  36. namespace OpenSim.Data.MySQL
  37. {
  38. public class MySQLGenericTableHandler<T> : MySqlFramework where T: class, new()
  39. {
  40. protected Dictionary<string, FieldInfo> m_Fields =
  41. new Dictionary<string, FieldInfo>();
  42. protected List<string> m_ColumnNames = null;
  43. protected string m_Realm;
  44. protected FieldInfo m_DataField = null;
  45. public MySQLGenericTableHandler(string connectionString,
  46. string realm, string storeName) : base(connectionString)
  47. {
  48. m_Realm = realm;
  49. m_connectionString = connectionString;
  50. if (storeName != String.Empty)
  51. {
  52. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  53. {
  54. dbcon.Open();
  55. Migration m = new Migration(dbcon, GetType().Assembly, storeName);
  56. m.Update();
  57. }
  58. }
  59. Type t = typeof(T);
  60. FieldInfo[] fields = t.GetFields(BindingFlags.Public |
  61. BindingFlags.Instance |
  62. BindingFlags.DeclaredOnly);
  63. if (fields.Length == 0)
  64. return;
  65. foreach (FieldInfo f in fields)
  66. {
  67. if (f.Name != "Data")
  68. m_Fields[f.Name] = f;
  69. else
  70. m_DataField = f;
  71. }
  72. }
  73. private void CheckColumnNames(IDataReader reader)
  74. {
  75. if (m_ColumnNames != null)
  76. return;
  77. m_ColumnNames = new List<string>();
  78. DataTable schemaTable = reader.GetSchemaTable();
  79. foreach (DataRow row in schemaTable.Rows)
  80. {
  81. if (row["ColumnName"] != null &&
  82. (!m_Fields.ContainsKey(row["ColumnName"].ToString())))
  83. m_ColumnNames.Add(row["ColumnName"].ToString());
  84. }
  85. }
  86. public virtual T[] Get(string field, string key)
  87. {
  88. return Get(new string[] { field }, new string[] { key });
  89. }
  90. public virtual T[] Get(string[] fields, string[] keys)
  91. {
  92. if (fields.Length != keys.Length)
  93. return new T[0];
  94. List<string> terms = new List<string>();
  95. using (MySqlCommand cmd = new MySqlCommand())
  96. {
  97. for (int i = 0 ; i < fields.Length ; i++)
  98. {
  99. cmd.Parameters.AddWithValue(fields[i], keys[i]);
  100. terms.Add("`" + fields[i] + "` = ?" + fields[i]);
  101. }
  102. string where = String.Join(" and ", terms.ToArray());
  103. string query = String.Format("select * from {0} where {1}",
  104. m_Realm, where);
  105. cmd.CommandText = query;
  106. return DoQuery(cmd);
  107. }
  108. }
  109. protected T[] DoQuery(MySqlCommand cmd)
  110. {
  111. List<T> result = new List<T>();
  112. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  113. {
  114. dbcon.Open();
  115. cmd.Connection = dbcon;
  116. using (IDataReader reader = cmd.ExecuteReader())
  117. {
  118. if (reader == null)
  119. return new T[0];
  120. CheckColumnNames(reader);
  121. while (reader.Read())
  122. {
  123. T row = new T();
  124. foreach (string name in m_Fields.Keys)
  125. {
  126. if (reader[name] is DBNull)
  127. {
  128. continue;
  129. }
  130. if (m_Fields[name].FieldType == typeof(bool))
  131. {
  132. int v = Convert.ToInt32(reader[name]);
  133. m_Fields[name].SetValue(row, v != 0 ? true : false);
  134. }
  135. else if (m_Fields[name].FieldType == typeof(UUID))
  136. {
  137. m_Fields[name].SetValue(row, DBGuid.FromDB(reader[name]));
  138. }
  139. else if (m_Fields[name].FieldType == typeof(int))
  140. {
  141. int v = Convert.ToInt32(reader[name]);
  142. m_Fields[name].SetValue(row, v);
  143. }
  144. else
  145. {
  146. m_Fields[name].SetValue(row, reader[name]);
  147. }
  148. }
  149. if (m_DataField != null)
  150. {
  151. Dictionary<string, string> data =
  152. new Dictionary<string, string>();
  153. foreach (string col in m_ColumnNames)
  154. {
  155. data[col] = reader[col].ToString();
  156. if (data[col] == null)
  157. data[col] = String.Empty;
  158. }
  159. m_DataField.SetValue(row, data);
  160. }
  161. result.Add(row);
  162. }
  163. }
  164. }
  165. return result.ToArray();
  166. }
  167. public virtual T[] Get(string where)
  168. {
  169. using (MySqlCommand cmd = new MySqlCommand())
  170. {
  171. string query = String.Format("select * from {0} where {1}",
  172. m_Realm, where);
  173. cmd.CommandText = query;
  174. return DoQuery(cmd);
  175. }
  176. }
  177. public virtual bool Store(T row)
  178. {
  179. using (MySqlCommand cmd = new MySqlCommand())
  180. {
  181. string query = "";
  182. List<String> names = new List<String>();
  183. List<String> values = new List<String>();
  184. foreach (FieldInfo fi in m_Fields.Values)
  185. {
  186. names.Add(fi.Name);
  187. values.Add("?" + fi.Name);
  188. cmd.Parameters.AddWithValue(fi.Name, fi.GetValue(row).ToString());
  189. }
  190. if (m_DataField != null)
  191. {
  192. Dictionary<string, string> data =
  193. (Dictionary<string, string>)m_DataField.GetValue(row);
  194. foreach (KeyValuePair<string, string> kvp in data)
  195. {
  196. names.Add(kvp.Key);
  197. values.Add("?" + kvp.Key);
  198. cmd.Parameters.AddWithValue("?" + kvp.Key, kvp.Value);
  199. }
  200. }
  201. query = String.Format("replace into {0} (`", m_Realm) + String.Join("`,`", names.ToArray()) + "`) values (" + String.Join(",", values.ToArray()) + ")";
  202. cmd.CommandText = query;
  203. if (ExecuteNonQuery(cmd) > 0)
  204. return true;
  205. return false;
  206. }
  207. }
  208. public virtual bool Delete(string field, string val)
  209. {
  210. using (MySqlCommand cmd = new MySqlCommand())
  211. {
  212. cmd.CommandText = String.Format("delete from {0} where `{1}` = ?{1}", m_Realm, field);
  213. cmd.Parameters.AddWithValue(field, val);
  214. if (ExecuteNonQuery(cmd) > 0)
  215. return true;
  216. return false;
  217. }
  218. }
  219. }
  220. }