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