29 using System.Collections.Generic;
31 using System.Reflection;
33 using MySql.Data.MySqlClient;
35 using OpenSim.Framework;
36 using OpenSim.Region.Framework.Interfaces;
38 namespace OpenSim.Data.MySQL
40 public class MySQLGenericTableHandler<T> :
MySqlFramework where T: class, new()
44 protected Dictionary<string, FieldInfo> m_Fields =
45 new Dictionary<string, FieldInfo>();
47 protected List<string> m_ColumnNames = null;
49 protected FieldInfo m_DataField = null;
51 protected virtual Assembly Assembly
53 get {
return GetType().Assembly; }
57 string realm,
string storeName) : base(connectionString)
60 m_connectionString = connectionString;
62 if (storeName !=
String.Empty)
64 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
73 FieldInfo[] fields = t.GetFields(BindingFlags.Public |
74 BindingFlags.Instance |
75 BindingFlags.DeclaredOnly);
77 if (fields.Length == 0)
80 foreach (FieldInfo f
in fields)
89 private void CheckColumnNames(IDataReader reader)
91 if (m_ColumnNames != null)
94 List<string> columnNames =
new List<string>();
96 DataTable schemaTable = reader.GetSchemaTable();
97 foreach (DataRow row
in schemaTable.Rows)
99 if (row[
"ColumnName"] != null &&
100 (!m_Fields.ContainsKey(row[
"ColumnName"].ToString())))
101 columnNames.Add(row[
"ColumnName"].ToString());
104 m_ColumnNames = columnNames;
107 public virtual T[]
Get(
string field,
string key)
109 return Get(
new string[] { field },
new string[] { key });
112 public virtual T[]
Get(
string[] fields,
string[] keys)
114 if (fields.Length != keys.Length)
117 List<string> terms =
new List<string>();
119 using (MySqlCommand cmd =
new MySqlCommand())
121 for (
int i = 0 ; i < fields.Length ; i++)
123 cmd.Parameters.AddWithValue(fields[i], keys[i]);
124 terms.Add(
"`" + fields[i] +
"` = ?" + fields[i]);
127 string where = String.Join(
" and ", terms.ToArray());
129 string query = String.Format(
"select * from {0} where {1}",
132 cmd.CommandText = query;
140 List<T> result =
new List<T>();
142 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
145 cmd.Connection = dbcon;
147 using (IDataReader reader = cmd.ExecuteReader())
152 CheckColumnNames(reader);
154 while (reader.Read())
158 foreach (
string name
in m_Fields.Keys)
160 if (reader[name] is DBNull)
164 if (m_Fields[name].FieldType == typeof(
bool))
166 int v = Convert.ToInt32(reader[name]);
167 m_Fields[name].SetValue(row, v != 0 ?
true :
false);
169 else if (m_Fields[name].FieldType == typeof(
UUID))
171 m_Fields[name].SetValue(row, DBGuid.FromDB(reader[name]));
173 else if (m_Fields[name].FieldType == typeof(
int))
175 int v = Convert.ToInt32(reader[name]);
176 m_Fields[name].SetValue(row, v);
178 else if (m_Fields[name].FieldType == typeof(uint))
180 uint v = Convert.ToUInt32(reader[name]);
181 m_Fields[name].SetValue(row, v);
185 m_Fields[name].SetValue(row, reader[name]);
189 if (m_DataField != null)
191 Dictionary<string, string> data =
192 new Dictionary<string, string>();
194 foreach (
string col
in m_ColumnNames)
196 data[col] = reader[col].ToString();
197 if (data[col] == null)
198 data[col] = String.Empty;
201 m_DataField.SetValue(row, data);
209 return result.ToArray();
212 public virtual T[]
Get(
string where)
214 using (MySqlCommand cmd =
new MySqlCommand())
216 string query = String.Format(
"select * from {0} where {1}",
219 cmd.CommandText = query;
229 using (MySqlCommand cmd =
new MySqlCommand())
232 List<String> names =
new List<String>();
233 List<String> values =
new List<String>();
235 foreach (FieldInfo fi
in m_Fields.Values)
238 values.Add(
"?" + fi.Name);
243 if (fi.GetValue(row) == null)
244 throw new NullReferenceException(
246 "[MYSQL GENERIC TABLE HANDLER]: Trying to store field {0} for {1} which is unexpectedly null",
249 cmd.Parameters.AddWithValue(fi.Name, fi.GetValue(row).ToString());
252 if (m_DataField != null)
254 Dictionary<string, string> data =
255 (Dictionary<string, string>)m_DataField.GetValue(row);
257 foreach (KeyValuePair<string, string> kvp
in data)
260 values.Add(
"?" + kvp.Key);
261 cmd.Parameters.AddWithValue(
"?" + kvp.Key, kvp.Value);
265 query = String.Format(
"replace into {0} (`", m_Realm) +
String.Join(
"`,`", names.ToArray()) +
"`) values (" +
String.Join(
",", values.ToArray()) +
")";
267 cmd.CommandText = query;
269 if (ExecuteNonQuery(cmd) > 0)
278 return Delete(
new string[] { field },
new string[] { key });
281 public virtual bool Delete(
string[] fields,
string[] keys)
287 if (fields.Length != keys.Length)
290 List<string> terms =
new List<string>();
292 using (MySqlCommand cmd =
new MySqlCommand())
294 for (
int i = 0 ; i < fields.Length ; i++)
296 cmd.Parameters.AddWithValue(fields[i], keys[i]);
297 terms.Add(
"`" + fields[i] +
"` = ?" + fields[i]);
300 string where = String.Join(
" and ", terms.ToArray());
302 string query = String.Format(
"delete from {0} where {1}", m_Realm, where);
304 cmd.CommandText = query;
306 return ExecuteNonQuery(cmd) > 0;
312 return GetCount(
new string[] { field },
new string[] { key });
315 public long GetCount(
string[] fields,
string[] keys)
317 if (fields.Length != keys.Length)
320 List<string> terms =
new List<string>();
322 using (MySqlCommand cmd =
new MySqlCommand())
324 for (
int i = 0; i < fields.Length; i++)
326 cmd.Parameters.AddWithValue(fields[i], keys[i]);
327 terms.Add(
"`" + fields[i] +
"` = ?" + fields[i]);
330 string where = String.Join(
" and ", terms.ToArray());
332 string query = String.Format(
"select count(*) from {0} where {1}",
335 cmd.CommandText = query;
337 Object result = DoQueryScalar(cmd);
339 return Convert.ToInt64(result);
345 using (MySqlCommand cmd =
new MySqlCommand())
347 string query = String.Format(
"select count(*) from {0} where {1}",
350 cmd.CommandText = query;
352 object result = DoQueryScalar(cmd);
354 return Convert.ToInt64(result);
360 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
363 cmd.Connection = dbcon;
365 return cmd.ExecuteScalar();
A database interface class to a user profile storage system
virtual bool Delete(string[] fields, string[] keys)
long GetCount(string where)
OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString key
MySQLGenericTableHandler(string connectionString, string realm, string storeName)
virtual T[] Get(string where)
T[] DoQuery(MySqlCommand cmd)
object DoQueryScalar(MySqlCommand cmd)
virtual T[] Get(string[] fields, string[] keys)
long GetCount(string[] fields, string[] keys)
long GetCount(string field, string key)
virtual bool Delete(string field, string key)
virtual bool Store(T row)
virtual T[] Get(string field, string key)