aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/SQLiteNG/SQLiteGenericTableHandler.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Data/SQLiteNG/SQLiteGenericTableHandler.cs')
-rw-r--r--OpenSim/Data/SQLiteNG/SQLiteGenericTableHandler.cs270
1 files changed, 270 insertions, 0 deletions
diff --git a/OpenSim/Data/SQLiteNG/SQLiteGenericTableHandler.cs b/OpenSim/Data/SQLiteNG/SQLiteGenericTableHandler.cs
new file mode 100644
index 0000000..3c70aef
--- /dev/null
+++ b/OpenSim/Data/SQLiteNG/SQLiteGenericTableHandler.cs
@@ -0,0 +1,270 @@
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 Mono.Data.Sqlite;
34using OpenMetaverse;
35using OpenSim.Framework;
36using OpenSim.Region.Framework.Interfaces;
37
38namespace OpenSim.Data.SQLite
39{
40 public class SQLiteGenericTableHandler<T> : SQLiteFramework where T: class, new()
41 {
42// private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
43
44 protected Dictionary<string, FieldInfo> m_Fields =
45 new Dictionary<string, FieldInfo>();
46
47 protected List<string> m_ColumnNames = null;
48 protected string m_Realm;
49 protected FieldInfo m_DataField = null;
50
51 protected static SqliteConnection m_Connection;
52 private static bool m_initialized;
53
54 public SQLiteGenericTableHandler(string connectionString,
55 string realm, string storeName) : base(connectionString)
56 {
57 m_Realm = realm;
58
59 if (!m_initialized)
60 {
61 m_Connection = new SqliteConnection(connectionString);
62 Console.WriteLine(string.Format("OPENING CONNECTION FOR {0} USING {1}", storeName, connectionString));
63 m_Connection.Open();
64
65 if (storeName != String.Empty)
66 {
67 Assembly assem = GetType().Assembly;
68 //SqliteConnection newConnection =
69 // (SqliteConnection)((ICloneable)m_Connection).Clone();
70 //newConnection.Open();
71
72 //Migration m = new Migration(newConnection, assem, storeName);
73 Migration m = new Migration(m_Connection, assem, storeName);
74 m.Update();
75 //newConnection.Close();
76 //newConnection.Dispose();
77 }
78
79 m_initialized = true;
80 }
81
82 Type t = typeof(T);
83 FieldInfo[] fields = t.GetFields(BindingFlags.Public |
84 BindingFlags.Instance |
85 BindingFlags.DeclaredOnly);
86
87 if (fields.Length == 0)
88 return;
89
90 foreach (FieldInfo f in fields)
91 {
92 if (f.Name != "Data")
93 m_Fields[f.Name] = f;
94 else
95 m_DataField = f;
96 }
97 }
98
99 private void CheckColumnNames(IDataReader reader)
100 {
101 if (m_ColumnNames != null)
102 return;
103
104 m_ColumnNames = new List<string>();
105
106 DataTable schemaTable = reader.GetSchemaTable();
107 foreach (DataRow row in schemaTable.Rows)
108 {
109 if (row["ColumnName"] != null &&
110 (!m_Fields.ContainsKey(row["ColumnName"].ToString())))
111 m_ColumnNames.Add(row["ColumnName"].ToString());
112 }
113 }
114
115 public T[] Get(string field, string key)
116 {
117 return Get(new string[] { field }, new string[] { key });
118 }
119
120 public T[] Get(string[] fields, string[] keys)
121 {
122 if (fields.Length != keys.Length)
123 return new T[0];
124
125 List<string> terms = new List<string>();
126
127 SqliteCommand cmd = new SqliteCommand();
128
129 for (int i = 0 ; i < fields.Length ; i++)
130 {
131 cmd.Parameters.Add(new SqliteParameter(":" + fields[i], keys[i]));
132 terms.Add("`" + fields[i] + "` = :" + fields[i]);
133 }
134
135 string where = String.Join(" and ", terms.ToArray());
136
137 string query = String.Format("select * from {0} where {1}",
138 m_Realm, where);
139
140 cmd.CommandText = query;
141
142 return DoQuery(cmd);
143 }
144
145 protected T[] DoQuery(SqliteCommand cmd)
146 {
147 IDataReader reader = ExecuteReader(cmd, m_Connection);
148 if (reader == null)
149 return new T[0];
150
151 CheckColumnNames(reader);
152
153 List<T> result = new List<T>();
154
155 while (reader.Read())
156 {
157 T row = new T();
158
159 foreach (string name in m_Fields.Keys)
160 {
161 if (m_Fields[name].GetValue(row) is bool)
162 {
163 int v = Convert.ToInt32(reader[name]);
164 m_Fields[name].SetValue(row, v != 0 ? true : false);
165 }
166 else if (m_Fields[name].GetValue(row) is UUID)
167 {
168 UUID uuid = UUID.Zero;
169
170 UUID.TryParse(reader[name].ToString(), out uuid);
171 m_Fields[name].SetValue(row, uuid);
172 }
173 else if (m_Fields[name].GetValue(row) is int)
174 {
175 int v = Convert.ToInt32(reader[name]);
176 m_Fields[name].SetValue(row, v);
177 }
178 else
179 {
180 m_Fields[name].SetValue(row, reader[name]);
181 }
182 }
183
184 if (m_DataField != null)
185 {
186 Dictionary<string, string> data =
187 new Dictionary<string, string>();
188
189 foreach (string col in m_ColumnNames)
190 {
191 data[col] = reader[col].ToString();
192 if (data[col] == null)
193 data[col] = String.Empty;
194 }
195
196 m_DataField.SetValue(row, data);
197 }
198
199 result.Add(row);
200 }
201
202 //CloseCommand(cmd);
203
204 return result.ToArray();
205 }
206
207 public T[] Get(string where)
208 {
209 SqliteCommand cmd = new SqliteCommand();
210
211 string query = String.Format("select * from {0} where {1}",
212 m_Realm, where);
213
214 cmd.CommandText = query;
215
216 return DoQuery(cmd);
217 }
218
219 public bool Store(T row)
220 {
221 SqliteCommand cmd = new SqliteCommand();
222
223 string query = "";
224 List<String> names = new List<String>();
225 List<String> values = new List<String>();
226
227 foreach (FieldInfo fi in m_Fields.Values)
228 {
229 names.Add(fi.Name);
230 values.Add(":" + fi.Name);
231 cmd.Parameters.Add(new SqliteParameter(":" + fi.Name, fi.GetValue(row).ToString()));
232 }
233
234 if (m_DataField != null)
235 {
236 Dictionary<string, string> data =
237 (Dictionary<string, string>)m_DataField.GetValue(row);
238
239 foreach (KeyValuePair<string, string> kvp in data)
240 {
241 names.Add(kvp.Key);
242 values.Add(":" + kvp.Key);
243 cmd.Parameters.Add(new SqliteParameter(":" + kvp.Key, kvp.Value));
244 }
245 }
246
247 query = String.Format("replace into {0} (`", m_Realm) + String.Join("`,`", names.ToArray()) + "`) values (" + String.Join(",", values.ToArray()) + ")";
248
249 cmd.CommandText = query;
250
251 if (ExecuteNonQuery(cmd, m_Connection) > 0)
252 return true;
253
254 return false;
255 }
256
257 public bool Delete(string field, string val)
258 {
259 SqliteCommand cmd = new SqliteCommand();
260
261 cmd.CommandText = String.Format("delete from {0} where `{1}` = :{1}", m_Realm, field);
262 cmd.Parameters.Add(new SqliteParameter(field, val));
263
264 if (ExecuteNonQuery(cmd, m_Connection) > 0)
265 return true;
266
267 return false;
268 }
269 }
270}