29 using System.Collections.Generic;
31 using System.Reflection;
34 using Community.CsharpSqlite.Sqlite;
36 using Mono.Data.Sqlite;
39 using OpenSim.Framework;
40 using OpenSim.Region.Framework.Interfaces;
42 namespace OpenSim.Data.SQLite
48 protected Dictionary<string, FieldInfo> m_Fields =
49 new Dictionary<string, FieldInfo>();
51 protected List<string> m_ColumnNames = null;
53 protected FieldInfo m_DataField = null;
56 private static bool m_initialized;
58 protected virtual Assembly Assembly
60 get {
return GetType().Assembly; }
64 string realm,
string storeName) : base(connectionString)
70 m_Connection =
new SqliteConnection(connectionString);
74 if (storeName !=
String.Empty)
91 FieldInfo[] fields = t.GetFields(BindingFlags.Public |
92 BindingFlags.Instance |
93 BindingFlags.DeclaredOnly);
95 if (fields.Length == 0)
98 foreach (FieldInfo f
in fields)
100 if (f.Name !=
"Data")
101 m_Fields[f.Name] = f;
107 private void CheckColumnNames(IDataReader reader)
109 if (m_ColumnNames != null)
112 m_ColumnNames =
new List<string>();
114 DataTable schemaTable = reader.GetSchemaTable();
115 foreach (DataRow row
in schemaTable.Rows)
117 if (row[
"ColumnName"] != null &&
118 (!m_Fields.ContainsKey(row[
"ColumnName"].ToString())))
119 m_ColumnNames.Add(row[
"ColumnName"].ToString());
123 public virtual T[]
Get(
string field,
string key)
125 return Get(
new string[] { field },
new string[] { key });
128 public virtual T[]
Get(
string[] fields,
string[] keys)
130 if (fields.Length != keys.Length)
133 List<string> terms =
new List<string>();
135 using (SqliteCommand cmd =
new SqliteCommand())
137 for (
int i = 0 ; i < fields.Length ; i++)
139 cmd.Parameters.Add(
new SqliteParameter(
":" + fields[i], keys[i]));
140 terms.Add(
"`" + fields[i] +
"` = :" + fields[i]);
143 string where = String.Join(
" and ", terms.ToArray());
145 string query = String.Format(
"select * from {0} where {1}",
148 cmd.CommandText = query;
156 IDataReader reader = ExecuteReader(cmd, m_Connection);
160 CheckColumnNames(reader);
162 List<T> result =
new List<T>();
164 while (reader.Read())
168 foreach (
string name
in m_Fields.Keys)
170 if (m_Fields[name].GetValue(row) is
bool)
172 int v = Convert.ToInt32(reader[name]);
173 m_Fields[name].SetValue(row, v != 0 ?
true :
false);
175 else if (m_Fields[name].GetValue(row) is
UUID)
177 UUID uuid = UUID.Zero;
179 UUID.TryParse(reader[name].ToString(), out uuid);
180 m_Fields[name].SetValue(row, uuid);
182 else if (m_Fields[name].GetValue(row) is int)
184 int v = Convert.ToInt32(reader[name]);
185 m_Fields[name].SetValue(row, v);
189 m_Fields[name].SetValue(row, reader[name]);
193 if (m_DataField != null)
195 Dictionary<string, string> data =
196 new Dictionary<string, string>();
198 foreach (
string col
in m_ColumnNames)
200 data[col] = reader[col].ToString();
201 if (data[col] == null)
202 data[col] = String.Empty;
205 m_DataField.SetValue(row, data);
213 return result.ToArray();
216 public virtual T[]
Get(
string where)
218 using (SqliteCommand cmd =
new SqliteCommand())
220 string query = String.Format(
"select * from {0} where {1}",
223 cmd.CommandText = query;
231 using (SqliteCommand cmd =
new SqliteCommand())
234 List<String> names =
new List<String>();
235 List<String> values =
new List<String>();
237 foreach (FieldInfo fi
in m_Fields.Values)
240 values.Add(
":" + fi.Name);
241 cmd.Parameters.Add(
new SqliteParameter(
":" + fi.Name, fi.GetValue(row).ToString()));
244 if (m_DataField != null)
246 Dictionary<string, string> data =
247 (Dictionary<string, string>)m_DataField.GetValue(row);
249 foreach (KeyValuePair<string, string> kvp
in data)
252 values.Add(
":" + kvp.Key);
253 cmd.Parameters.Add(
new SqliteParameter(
":" + kvp.Key, kvp.Value));
257 query = String.Format(
"replace into {0} (`", m_Realm) +
String.Join(
"`,`", names.ToArray()) +
"`) values (" +
String.Join(
",", values.ToArray()) +
")";
259 cmd.CommandText = query;
261 if (ExecuteNonQuery(cmd, m_Connection) > 0)
270 return Delete(
new string[] { field },
new string[] { key });
273 public virtual bool Delete(
string[] fields,
string[] keys)
275 if (fields.Length != keys.Length)
278 List<string> terms =
new List<string>();
280 using (SqliteCommand cmd =
new SqliteCommand())
282 for (
int i = 0 ; i < fields.Length ; i++)
284 cmd.Parameters.Add(
new SqliteParameter(
":" + fields[i], keys[i]));
285 terms.Add(
"`" + fields[i] +
"` = :" + fields[i]);
288 string where = String.Join(
" and ", terms.ToArray());
290 string query = String.Format(
"delete from {0} where {1}", m_Realm, where);
292 cmd.CommandText = query;
294 return ExecuteNonQuery(cmd, m_Connection) > 0;
T[] DoQuery(SqliteCommand cmd)
SQLiteGenericTableHandler(string connectionString, string realm, string storeName)
virtual T[] Get(string where)
virtual bool Delete(string field, string key)
OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString key
virtual bool Store(T row)
virtual T[] Get(string[] fields, string[] keys)
A database interface class to a user profile storage system
virtual T[] Get(string field, string key)
static SqliteConnection m_Connection
virtual bool Delete(string[] fields, string[] keys)