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