diff options
Merge branch '0.6.9-post-fixes' into careminster
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Data/SQLiteLegacy/SQLiteEstateData.cs | 389 |
1 files changed, 389 insertions, 0 deletions
diff --git a/OpenSim/Data/SQLiteLegacy/SQLiteEstateData.cs b/OpenSim/Data/SQLiteLegacy/SQLiteEstateData.cs new file mode 100644 index 0000000..5a7fbd7 --- /dev/null +++ b/OpenSim/Data/SQLiteLegacy/SQLiteEstateData.cs | |||
@@ -0,0 +1,389 @@ | |||
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.SQLiteLegacy | ||
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) | ||
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.Add(":RegionID", regionID.ToString()); | ||
91 | |||
92 | return DoLoad(cmd, regionID); | ||
93 | } | ||
94 | |||
95 | private EstateSettings DoLoad(SqliteCommand cmd, UUID regionID) | ||
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 | ||
129 | { | ||
130 | // Migration case | ||
131 | // | ||
132 | r.Close(); | ||
133 | |||
134 | List<string> names = new List<string>(FieldList); | ||
135 | |||
136 | names.Remove("EstateID"); | ||
137 | |||
138 | string sql = "insert into estate_settings ("+String.Join(",", names.ToArray())+") values ( :"+String.Join(", :", names.ToArray())+")"; | ||
139 | |||
140 | cmd.CommandText = sql; | ||
141 | cmd.Parameters.Clear(); | ||
142 | |||
143 | foreach (string name in FieldList) | ||
144 | { | ||
145 | if (m_FieldMap[name].GetValue(es) is bool) | ||
146 | { | ||
147 | if ((bool)m_FieldMap[name].GetValue(es)) | ||
148 | cmd.Parameters.Add(":"+name, "1"); | ||
149 | else | ||
150 | cmd.Parameters.Add(":"+name, "0"); | ||
151 | } | ||
152 | else | ||
153 | { | ||
154 | cmd.Parameters.Add(":"+name, m_FieldMap[name].GetValue(es).ToString()); | ||
155 | } | ||
156 | } | ||
157 | |||
158 | cmd.ExecuteNonQuery(); | ||
159 | |||
160 | cmd.CommandText = "select LAST_INSERT_ROWID() as id"; | ||
161 | cmd.Parameters.Clear(); | ||
162 | |||
163 | r = cmd.ExecuteReader(); | ||
164 | |||
165 | r.Read(); | ||
166 | |||
167 | es.EstateID = Convert.ToUInt32(r["id"]); | ||
168 | |||
169 | r.Close(); | ||
170 | |||
171 | cmd.CommandText = "insert into estate_map values (:RegionID, :EstateID)"; | ||
172 | cmd.Parameters.Add(":RegionID", regionID.ToString()); | ||
173 | cmd.Parameters.Add(":EstateID", es.EstateID.ToString()); | ||
174 | |||
175 | // This will throw on dupe key | ||
176 | try | ||
177 | { | ||
178 | cmd.ExecuteNonQuery(); | ||
179 | } | ||
180 | catch (Exception) | ||
181 | { | ||
182 | } | ||
183 | |||
184 | es.Save(); | ||
185 | } | ||
186 | |||
187 | LoadBanList(es); | ||
188 | |||
189 | es.EstateManagers = LoadUUIDList(es.EstateID, "estate_managers"); | ||
190 | es.EstateAccess = LoadUUIDList(es.EstateID, "estate_users"); | ||
191 | es.EstateGroups = LoadUUIDList(es.EstateID, "estate_groups"); | ||
192 | return es; | ||
193 | } | ||
194 | |||
195 | public void StoreEstateSettings(EstateSettings es) | ||
196 | { | ||
197 | List<string> fields = new List<string>(FieldList); | ||
198 | fields.Remove("EstateID"); | ||
199 | |||
200 | List<string> terms = new List<string>(); | ||
201 | |||
202 | foreach (string f in fields) | ||
203 | terms.Add(f+" = :"+f); | ||
204 | |||
205 | string sql = "update estate_settings set "+String.Join(", ", terms.ToArray())+" where EstateID = :EstateID"; | ||
206 | |||
207 | SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand(); | ||
208 | |||
209 | cmd.CommandText = sql; | ||
210 | |||
211 | foreach (string name in FieldList) | ||
212 | { | ||
213 | if (m_FieldMap[name].GetValue(es) is bool) | ||
214 | { | ||
215 | if ((bool)m_FieldMap[name].GetValue(es)) | ||
216 | cmd.Parameters.Add(":"+name, "1"); | ||
217 | else | ||
218 | cmd.Parameters.Add(":"+name, "0"); | ||
219 | } | ||
220 | else | ||
221 | { | ||
222 | cmd.Parameters.Add(":"+name, m_FieldMap[name].GetValue(es).ToString()); | ||
223 | } | ||
224 | } | ||
225 | |||
226 | cmd.ExecuteNonQuery(); | ||
227 | |||
228 | SaveBanList(es); | ||
229 | SaveUUIDList(es.EstateID, "estate_managers", es.EstateManagers); | ||
230 | SaveUUIDList(es.EstateID, "estate_users", es.EstateAccess); | ||
231 | SaveUUIDList(es.EstateID, "estate_groups", es.EstateGroups); | ||
232 | } | ||
233 | |||
234 | private void LoadBanList(EstateSettings es) | ||
235 | { | ||
236 | es.ClearBans(); | ||
237 | |||
238 | SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand(); | ||
239 | |||
240 | cmd.CommandText = "select bannedUUID from estateban where EstateID = :EstateID"; | ||
241 | cmd.Parameters.Add(":EstateID", es.EstateID); | ||
242 | |||
243 | IDataReader r = cmd.ExecuteReader(); | ||
244 | |||
245 | while (r.Read()) | ||
246 | { | ||
247 | EstateBan eb = new EstateBan(); | ||
248 | |||
249 | UUID uuid = new UUID(); | ||
250 | UUID.TryParse(r["bannedUUID"].ToString(), out uuid); | ||
251 | |||
252 | eb.BannedUserID = uuid; | ||
253 | eb.BannedHostAddress = "0.0.0.0"; | ||
254 | eb.BannedHostIPMask = "0.0.0.0"; | ||
255 | es.AddBan(eb); | ||
256 | } | ||
257 | r.Close(); | ||
258 | } | ||
259 | |||
260 | private void SaveBanList(EstateSettings es) | ||
261 | { | ||
262 | SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand(); | ||
263 | |||
264 | cmd.CommandText = "delete from estateban where EstateID = :EstateID"; | ||
265 | cmd.Parameters.Add(":EstateID", es.EstateID.ToString()); | ||
266 | |||
267 | cmd.ExecuteNonQuery(); | ||
268 | |||
269 | cmd.Parameters.Clear(); | ||
270 | |||
271 | cmd.CommandText = "insert into estateban (EstateID, bannedUUID, bannedIp, bannedIpHostMask, bannedNameMask) values ( :EstateID, :bannedUUID, '', '', '' )"; | ||
272 | |||
273 | foreach (EstateBan b in es.EstateBans) | ||
274 | { | ||
275 | cmd.Parameters.Add(":EstateID", es.EstateID.ToString()); | ||
276 | cmd.Parameters.Add(":bannedUUID", b.BannedUserID.ToString()); | ||
277 | |||
278 | cmd.ExecuteNonQuery(); | ||
279 | cmd.Parameters.Clear(); | ||
280 | } | ||
281 | } | ||
282 | |||
283 | void SaveUUIDList(uint EstateID, string table, UUID[] data) | ||
284 | { | ||
285 | SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand(); | ||
286 | |||
287 | cmd.CommandText = "delete from "+table+" where EstateID = :EstateID"; | ||
288 | cmd.Parameters.Add(":EstateID", EstateID.ToString()); | ||
289 | |||
290 | cmd.ExecuteNonQuery(); | ||
291 | |||
292 | cmd.Parameters.Clear(); | ||
293 | |||
294 | cmd.CommandText = "insert into "+table+" (EstateID, uuid) values ( :EstateID, :uuid )"; | ||
295 | |||
296 | foreach (UUID uuid in data) | ||
297 | { | ||
298 | cmd.Parameters.Add(":EstateID", EstateID.ToString()); | ||
299 | cmd.Parameters.Add(":uuid", uuid.ToString()); | ||
300 | |||
301 | cmd.ExecuteNonQuery(); | ||
302 | cmd.Parameters.Clear(); | ||
303 | } | ||
304 | } | ||
305 | |||
306 | UUID[] LoadUUIDList(uint EstateID, string table) | ||
307 | { | ||
308 | List<UUID> uuids = new List<UUID>(); | ||
309 | |||
310 | SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand(); | ||
311 | |||
312 | cmd.CommandText = "select uuid from "+table+" where EstateID = :EstateID"; | ||
313 | cmd.Parameters.Add(":EstateID", EstateID); | ||
314 | |||
315 | IDataReader r = cmd.ExecuteReader(); | ||
316 | |||
317 | while (r.Read()) | ||
318 | { | ||
319 | // EstateBan eb = new EstateBan(); | ||
320 | |||
321 | UUID uuid = new UUID(); | ||
322 | UUID.TryParse(r["uuid"].ToString(), out uuid); | ||
323 | |||
324 | uuids.Add(uuid); | ||
325 | } | ||
326 | r.Close(); | ||
327 | |||
328 | return uuids.ToArray(); | ||
329 | } | ||
330 | |||
331 | public EstateSettings LoadEstateSettings(int estateID) | ||
332 | { | ||
333 | string sql = "select estate_settings."+String.Join(",estate_settings.", FieldList)+" from estate_settings where estate_settings.EstateID :EstateID"; | ||
334 | |||
335 | SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand(); | ||
336 | |||
337 | cmd.CommandText = sql; | ||
338 | cmd.Parameters.Add(":EstateID", estateID.ToString()); | ||
339 | |||
340 | return DoLoad(cmd, UUID.Zero); | ||
341 | } | ||
342 | |||
343 | public List<int> GetEstates(string search) | ||
344 | { | ||
345 | List<int> result = new List<int>(); | ||
346 | |||
347 | string sql = "select EstateID from estate_settings where estate_settings.EstateName :EstateName"; | ||
348 | |||
349 | SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand(); | ||
350 | |||
351 | cmd.CommandText = sql; | ||
352 | cmd.Parameters.Add(":EstateName", search); | ||
353 | |||
354 | IDataReader r = cmd.ExecuteReader(); | ||
355 | |||
356 | while (r.Read()) | ||
357 | { | ||
358 | result.Add(Convert.ToInt32(r["EstateID"])); | ||
359 | } | ||
360 | r.Close(); | ||
361 | |||
362 | return result; | ||
363 | } | ||
364 | |||
365 | public bool LinkRegion(UUID regionID, int estateID) | ||
366 | { | ||
367 | SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand(); | ||
368 | |||
369 | cmd.CommandText = "insert into estate_map values (:RegionID, :EstateID)"; | ||
370 | cmd.Parameters.Add(":RegionID", regionID.ToString()); | ||
371 | cmd.Parameters.Add(":EstateID", estateID.ToString()); | ||
372 | |||
373 | if (cmd.ExecuteNonQuery() == 0) | ||
374 | return false; | ||
375 | |||
376 | return true; | ||
377 | } | ||
378 | |||
379 | public List<UUID> GetRegions(int estateID) | ||
380 | { | ||
381 | return new List<UUID>(); | ||
382 | } | ||
383 | |||
384 | public bool DeleteEstate(int estateID) | ||
385 | { | ||
386 | return false; | ||
387 | } | ||
388 | } | ||
389 | } | ||