aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/SQLiteNG/SQLiteGenericTableHandler.cs
diff options
context:
space:
mode:
authorJustin Clark-Casey (justincc)2010-04-23 17:17:15 +0100
committerJustin Clark-Casey (justincc)2010-04-23 17:17:15 +0100
commita23bebdc0f106c19de09e54d88eebc6b086fdaab (patch)
tree5a0141201ae27b796608f0af77f4d9a1e1cd1d19 /OpenSim/Data/SQLiteNG/SQLiteGenericTableHandler.cs
parentminor: change a comment and replace some magic numbers with the AssetType enum (diff)
downloadopensim-SC_OLD-a23bebdc0f106c19de09e54d88eebc6b086fdaab.zip
opensim-SC_OLD-a23bebdc0f106c19de09e54d88eebc6b086fdaab.tar.gz
opensim-SC_OLD-a23bebdc0f106c19de09e54d88eebc6b086fdaab.tar.bz2
opensim-SC_OLD-a23bebdc0f106c19de09e54d88eebc6b086fdaab.tar.xz
Duplicate OpenSim.Data.SQLite into OpenSim.Data.SQLiteNG. SQLiteNG will shortly be changed to work under mono 2.6 and above
Diffstat (limited to 'OpenSim/Data/SQLiteNG/SQLiteGenericTableHandler.cs')
-rw-r--r--OpenSim/Data/SQLiteNG/SQLiteGenericTableHandler.cs268
1 files changed, 268 insertions, 0 deletions
diff --git a/OpenSim/Data/SQLiteNG/SQLiteGenericTableHandler.cs b/OpenSim/Data/SQLiteNG/SQLiteGenericTableHandler.cs
new file mode 100644
index 0000000..b39bb19
--- /dev/null
+++ b/OpenSim/Data/SQLiteNG/SQLiteGenericTableHandler.cs
@@ -0,0 +1,268 @@
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.SqliteClient;
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 m_Connection.Open();
63
64 if (storeName != String.Empty)
65 {
66 Assembly assem = GetType().Assembly;
67 SqliteConnection newConnection =
68 (SqliteConnection)((ICloneable)m_Connection).Clone();
69 newConnection.Open();
70
71 Migration m = new Migration(newConnection, assem, storeName);
72 m.Update();
73 newConnection.Close();
74 newConnection.Dispose();
75 }
76
77 m_initialized = true;
78 }
79
80 Type t = typeof(T);
81 FieldInfo[] fields = t.GetFields(BindingFlags.Public |
82 BindingFlags.Instance |
83 BindingFlags.DeclaredOnly);
84
85 if (fields.Length == 0)
86 return;
87
88 foreach (FieldInfo f in fields)
89 {
90 if (f.Name != "Data")
91 m_Fields[f.Name] = f;
92 else
93 m_DataField = f;
94 }
95 }
96
97 private void CheckColumnNames(IDataReader reader)
98 {
99 if (m_ColumnNames != null)
100 return;
101
102 m_ColumnNames = new List<string>();
103
104 DataTable schemaTable = reader.GetSchemaTable();
105 foreach (DataRow row in schemaTable.Rows)
106 {
107 if (row["ColumnName"] != null &&
108 (!m_Fields.ContainsKey(row["ColumnName"].ToString())))
109 m_ColumnNames.Add(row["ColumnName"].ToString());
110 }
111 }
112
113 public T[] Get(string field, string key)
114 {
115 return Get(new string[] { field }, new string[] { key });
116 }
117
118 public T[] Get(string[] fields, string[] keys)
119 {
120 if (fields.Length != keys.Length)
121 return new T[0];
122
123 List<string> terms = new List<string>();
124
125 SqliteCommand cmd = new SqliteCommand();
126
127 for (int i = 0 ; i < fields.Length ; i++)
128 {
129 cmd.Parameters.Add(new SqliteParameter(":" + fields[i], keys[i]));
130 terms.Add("`" + fields[i] + "` = :" + fields[i]);
131 }
132
133 string where = String.Join(" and ", terms.ToArray());
134
135 string query = String.Format("select * from {0} where {1}",
136 m_Realm, where);
137
138 cmd.CommandText = query;
139
140 return DoQuery(cmd);
141 }
142
143 protected T[] DoQuery(SqliteCommand cmd)
144 {
145 IDataReader reader = ExecuteReader(cmd, m_Connection);
146 if (reader == null)
147 return new T[0];
148
149 CheckColumnNames(reader);
150
151 List<T> result = new List<T>();
152
153 while (reader.Read())
154 {
155 T row = new T();
156
157 foreach (string name in m_Fields.Keys)
158 {
159 if (m_Fields[name].GetValue(row) is bool)
160 {
161 int v = Convert.ToInt32(reader[name]);
162 m_Fields[name].SetValue(row, v != 0 ? true : false);
163 }
164 else if (m_Fields[name].GetValue(row) is UUID)
165 {
166 UUID uuid = UUID.Zero;
167
168 UUID.TryParse(reader[name].ToString(), out uuid);
169 m_Fields[name].SetValue(row, uuid);
170 }
171 else if (m_Fields[name].GetValue(row) is int)
172 {
173 int v = Convert.ToInt32(reader[name]);
174 m_Fields[name].SetValue(row, v);
175 }
176 else
177 {
178 m_Fields[name].SetValue(row, reader[name]);
179 }
180 }
181
182 if (m_DataField != null)
183 {
184 Dictionary<string, string> data =
185 new Dictionary<string, string>();
186
187 foreach (string col in m_ColumnNames)
188 {
189 data[col] = reader[col].ToString();
190 if (data[col] == null)
191 data[col] = String.Empty;
192 }
193
194 m_DataField.SetValue(row, data);
195 }
196
197 result.Add(row);
198 }
199
200 CloseCommand(cmd);
201
202 return result.ToArray();
203 }
204
205 public T[] Get(string where)
206 {
207 SqliteCommand cmd = new SqliteCommand();
208
209 string query = String.Format("select * from {0} where {1}",
210 m_Realm, where);
211
212 cmd.CommandText = query;
213
214 return DoQuery(cmd);
215 }
216
217 public bool Store(T row)
218 {
219 SqliteCommand cmd = new SqliteCommand();
220
221 string query = "";
222 List<String> names = new List<String>();
223 List<String> values = new List<String>();
224
225 foreach (FieldInfo fi in m_Fields.Values)
226 {
227 names.Add(fi.Name);
228 values.Add(":" + fi.Name);
229 cmd.Parameters.Add(new SqliteParameter(":" + fi.Name, fi.GetValue(row).ToString()));
230 }
231
232 if (m_DataField != null)
233 {
234 Dictionary<string, string> data =
235 (Dictionary<string, string>)m_DataField.GetValue(row);
236
237 foreach (KeyValuePair<string, string> kvp in data)
238 {
239 names.Add(kvp.Key);
240 values.Add(":" + kvp.Key);
241 cmd.Parameters.Add(new SqliteParameter(":" + kvp.Key, kvp.Value));
242 }
243 }
244
245 query = String.Format("replace into {0} (`", m_Realm) + String.Join("`,`", names.ToArray()) + "`) values (" + String.Join(",", values.ToArray()) + ")";
246
247 cmd.CommandText = query;
248
249 if (ExecuteNonQuery(cmd, m_Connection) > 0)
250 return true;
251
252 return false;
253 }
254
255 public bool Delete(string field, string val)
256 {
257 SqliteCommand cmd = new SqliteCommand();
258
259 cmd.CommandText = String.Format("delete from {0} where `{1}` = :{1}", m_Realm, field);
260 cmd.Parameters.Add(new SqliteParameter(field, val));
261
262 if (ExecuteNonQuery(cmd, m_Connection) > 0)
263 return true;
264
265 return false;
266 }
267 }
268}