aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/SQLiteNG/SQLiteEstateData.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Data/SQLiteNG/SQLiteEstateData.cs387
1 files changed, 387 insertions, 0 deletions
diff --git a/OpenSim/Data/SQLiteNG/SQLiteEstateData.cs b/OpenSim/Data/SQLiteNG/SQLiteEstateData.cs
new file mode 100644
index 0000000..2e2d717
--- /dev/null
+++ b/OpenSim/Data/SQLiteNG/SQLiteEstateData.cs
@@ -0,0 +1,387 @@
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.SQLiteNG
39{
40 public class SQLiteEstateStore : IEstateDataStore
41 {
42 private static readonly ILog m_log =
43 LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
44
45 private SqliteConnection m_connection;
46 private string m_connectionString;
47
48 private FieldInfo[] m_Fields;
49 private Dictionary<string, FieldInfo> m_FieldMap =
50 new Dictionary<string, FieldInfo>();
51
52 public void Initialise(string connectionString)
53 {
54 m_connectionString = connectionString;
55
56 m_log.Info("[ESTATE DB]: Sqlite - connecting: "+m_connectionString);
57
58 m_connection = new SqliteConnection(m_connectionString);
59 m_connection.Open();
60
61 Assembly assem = GetType().Assembly;
62 Migration m = new Migration(m_connection, assem, "EstateStore");
63 m.Update();
64
65 //m_connection.Close();
66 // m_connection.Open();
67
68 Type t = typeof(EstateSettings);
69 m_Fields = t.GetFields(BindingFlags.NonPublic |
70 BindingFlags.Instance |
71 BindingFlags.DeclaredOnly);
72
73 foreach (FieldInfo f in m_Fields)
74 if (f.Name.Substring(0, 2) == "m_")
75 m_FieldMap[f.Name.Substring(2)] = f;
76 }
77
78 private string[] FieldList
79 {
80 get { return new List<string>(m_FieldMap.Keys).ToArray(); }
81 }
82
83 public EstateSettings LoadEstateSettings(UUID regionID, bool create)
84 {
85 string sql = "select estate_settings."+String.Join(",estate_settings.", FieldList)+" from estate_map left join estate_settings on estate_map.EstateID = estate_settings.EstateID where estate_settings.EstateID is not null and RegionID = :RegionID";
86
87 SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
88
89 cmd.CommandText = sql;
90 cmd.Parameters.AddWithValue(":RegionID", regionID.ToString());
91
92 return DoLoad(cmd, regionID, create);
93 }
94
95 private EstateSettings DoLoad(SqliteCommand cmd, UUID regionID, bool create)
96 {
97 EstateSettings es = new EstateSettings();
98 es.OnSave += StoreEstateSettings;
99
100 IDataReader r = cmd.ExecuteReader();
101
102 if (r.Read())
103 {
104 foreach (string name in FieldList)
105 {
106 if (m_FieldMap[name].GetValue(es) is bool)
107 {
108 int v = Convert.ToInt32(r[name]);
109 if (v != 0)
110 m_FieldMap[name].SetValue(es, true);
111 else
112 m_FieldMap[name].SetValue(es, false);
113 }
114 else if (m_FieldMap[name].GetValue(es) is UUID)
115 {
116 UUID uuid = UUID.Zero;
117
118 UUID.TryParse(r[name].ToString(), out uuid);
119 m_FieldMap[name].SetValue(es, uuid);
120 }
121 else
122 {
123 m_FieldMap[name].SetValue(es, Convert.ChangeType(r[name], m_FieldMap[name].FieldType));
124 }
125 }
126 r.Close();
127 }
128 else if (create)
129 {
130 r.Close();
131
132 List<string> names = new List<string>(FieldList);
133
134 names.Remove("EstateID");
135
136 string sql = "insert into estate_settings ("+String.Join(",", names.ToArray())+") values ( :"+String.Join(", :", names.ToArray())+")";
137
138 cmd.CommandText = sql;
139 cmd.Parameters.Clear();
140
141 foreach (string name in FieldList)
142 {
143 if (m_FieldMap[name].GetValue(es) is bool)
144 {
145 if ((bool)m_FieldMap[name].GetValue(es))
146 cmd.Parameters.AddWithValue(":"+name, "1");
147 else
148 cmd.Parameters.AddWithValue(":"+name, "0");
149 }
150 else
151 {
152 cmd.Parameters.AddWithValue(":"+name, m_FieldMap[name].GetValue(es).ToString());
153 }
154 }
155
156 cmd.ExecuteNonQuery();
157
158 cmd.CommandText = "select LAST_INSERT_ROWID() as id";
159 cmd.Parameters.Clear();
160
161 r = cmd.ExecuteReader();
162
163 r.Read();
164
165 es.EstateID = Convert.ToUInt32(r["id"]);
166
167 r.Close();
168
169 cmd.CommandText = "insert into estate_map values (:RegionID, :EstateID)";
170 cmd.Parameters.AddWithValue(":RegionID", regionID.ToString());
171 cmd.Parameters.AddWithValue(":EstateID", es.EstateID.ToString());
172
173 // This will throw on dupe key
174 try
175 {
176 cmd.ExecuteNonQuery();
177 }
178 catch (Exception)
179 {
180 }
181
182 es.Save();
183 }
184
185 LoadBanList(es);
186
187 es.EstateManagers = LoadUUIDList(es.EstateID, "estate_managers");
188 es.EstateAccess = LoadUUIDList(es.EstateID, "estate_users");
189 es.EstateGroups = LoadUUIDList(es.EstateID, "estate_groups");
190 return es;
191 }
192
193 public void StoreEstateSettings(EstateSettings es)
194 {
195 List<string> fields = new List<string>(FieldList);
196 fields.Remove("EstateID");
197
198 List<string> terms = new List<string>();
199
200 foreach (string f in fields)
201 terms.Add(f+" = :"+f);
202
203 string sql = "update estate_settings set "+String.Join(", ", terms.ToArray())+" where EstateID = :EstateID";
204
205 SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
206
207 cmd.CommandText = sql;
208
209 foreach (string name in FieldList)
210 {
211 if (m_FieldMap[name].GetValue(es) is bool)
212 {
213 if ((bool)m_FieldMap[name].GetValue(es))
214 cmd.Parameters.AddWithValue(":"+name, "1");
215 else
216 cmd.Parameters.AddWithValue(":"+name, "0");
217 }
218 else
219 {
220 cmd.Parameters.AddWithValue(":"+name, m_FieldMap[name].GetValue(es).ToString());
221 }
222 }
223
224 cmd.ExecuteNonQuery();
225
226 SaveBanList(es);
227 SaveUUIDList(es.EstateID, "estate_managers", es.EstateManagers);
228 SaveUUIDList(es.EstateID, "estate_users", es.EstateAccess);
229 SaveUUIDList(es.EstateID, "estate_groups", es.EstateGroups);
230 }
231
232 private void LoadBanList(EstateSettings es)
233 {
234 es.ClearBans();
235
236 SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
237
238 cmd.CommandText = "select bannedUUID from estateban where EstateID = :EstateID";
239 cmd.Parameters.AddWithValue(":EstateID", es.EstateID);
240
241 IDataReader r = cmd.ExecuteReader();
242
243 while (r.Read())
244 {
245 EstateBan eb = new EstateBan();
246
247 UUID uuid = new UUID();
248 UUID.TryParse(r["bannedUUID"].ToString(), out uuid);
249
250 eb.BannedUserID = uuid;
251 eb.BannedHostAddress = "0.0.0.0";
252 eb.BannedHostIPMask = "0.0.0.0";
253 es.AddBan(eb);
254 }
255 r.Close();
256 }
257
258 private void SaveBanList(EstateSettings es)
259 {
260 SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
261
262 cmd.CommandText = "delete from estateban where EstateID = :EstateID";
263 cmd.Parameters.AddWithValue(":EstateID", es.EstateID.ToString());
264
265 cmd.ExecuteNonQuery();
266
267 cmd.Parameters.Clear();
268
269 cmd.CommandText = "insert into estateban (EstateID, bannedUUID, bannedIp, bannedIpHostMask, bannedNameMask) values ( :EstateID, :bannedUUID, '', '', '' )";
270
271 foreach (EstateBan b in es.EstateBans)
272 {
273 cmd.Parameters.AddWithValue(":EstateID", es.EstateID.ToString());
274 cmd.Parameters.AddWithValue(":bannedUUID", b.BannedUserID.ToString());
275
276 cmd.ExecuteNonQuery();
277 cmd.Parameters.Clear();
278 }
279 }
280
281 void SaveUUIDList(uint EstateID, string table, UUID[] data)
282 {
283 SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
284
285 cmd.CommandText = "delete from "+table+" where EstateID = :EstateID";
286 cmd.Parameters.AddWithValue(":EstateID", EstateID.ToString());
287
288 cmd.ExecuteNonQuery();
289
290 cmd.Parameters.Clear();
291
292 cmd.CommandText = "insert into "+table+" (EstateID, uuid) values ( :EstateID, :uuid )";
293
294 foreach (UUID uuid in data)
295 {
296 cmd.Parameters.AddWithValue(":EstateID", EstateID.ToString());
297 cmd.Parameters.AddWithValue(":uuid", uuid.ToString());
298
299 cmd.ExecuteNonQuery();
300 cmd.Parameters.Clear();
301 }
302 }
303
304 UUID[] LoadUUIDList(uint EstateID, string table)
305 {
306 List<UUID> uuids = new List<UUID>();
307
308 SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
309
310 cmd.CommandText = "select uuid from "+table+" where EstateID = :EstateID";
311 cmd.Parameters.AddWithValue(":EstateID", EstateID);
312
313 IDataReader r = cmd.ExecuteReader();
314
315 while (r.Read())
316 {
317 // EstateBan eb = new EstateBan();
318
319 UUID uuid = new UUID();
320 UUID.TryParse(r["uuid"].ToString(), out uuid);
321
322 uuids.Add(uuid);
323 }
324 r.Close();
325
326 return uuids.ToArray();
327 }
328
329 public EstateSettings LoadEstateSettings(int estateID)
330 {
331 string sql = "select estate_settings."+String.Join(",estate_settings.", FieldList)+" from estate_settings where estate_settings.EstateID :EstateID";
332
333 SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
334
335 cmd.CommandText = sql;
336 cmd.Parameters.AddWithValue(":EstateID", estateID.ToString());
337
338 return DoLoad(cmd, UUID.Zero, false);
339 }
340
341 public List<int> GetEstates(string search)
342 {
343 List<int> result = new List<int>();
344
345 string sql = "select EstateID from estate_settings where estate_settings.EstateName :EstateName";
346
347 SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
348
349 cmd.CommandText = sql;
350 cmd.Parameters.AddWithValue(":EstateName", search);
351
352 IDataReader r = cmd.ExecuteReader();
353
354 while (r.Read())
355 {
356 result.Add(Convert.ToInt32(r["EstateID"]));
357 }
358 r.Close();
359
360 return result;
361 }
362
363 public bool LinkRegion(UUID regionID, int estateID)
364 {
365 SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand();
366
367 cmd.CommandText = "insert into estate_map values (:RegionID, :EstateID)";
368 cmd.Parameters.AddWithValue(":RegionID", regionID.ToString());
369 cmd.Parameters.AddWithValue(":EstateID", estateID.ToString());
370
371 if (cmd.ExecuteNonQuery() == 0)
372 return false;
373
374 return true;
375 }
376
377 public List<UUID> GetRegions(int estateID)
378 {
379 return new List<UUID>();
380 }
381
382 public bool DeleteEstate(int estateID)
383 {
384 return false;
385 }
386 }
387}