opensim – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | eva | 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 System.Reflection; |
||
33 | using MySql.Data.MySqlClient; |
||
34 | using OpenMetaverse; |
||
35 | using OpenSim.Framework; |
||
36 | using OpenSim.Data; |
||
37 | using RegionFlags = OpenSim.Framework.RegionFlags; |
||
38 | |||
39 | namespace OpenSim.Data.MySQL |
||
40 | { |
||
41 | public class MySqlRegionData : MySqlFramework, IRegionData |
||
42 | { |
||
43 | private string m_Realm; |
||
44 | private List<string> m_ColumnNames; |
||
45 | //private string m_connectionString; |
||
46 | |||
47 | protected virtual Assembly Assembly |
||
48 | { |
||
49 | get { return GetType().Assembly; } |
||
50 | } |
||
51 | |||
52 | public MySqlRegionData(string connectionString, string realm) |
||
53 | : base(connectionString) |
||
54 | { |
||
55 | m_Realm = realm; |
||
56 | m_connectionString = connectionString; |
||
57 | |||
58 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) |
||
59 | { |
||
60 | dbcon.Open(); |
||
61 | Migration m = new Migration(dbcon, Assembly, "GridStore"); |
||
62 | m.Update(); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | public List<RegionData> Get(string regionName, UUID scopeID) |
||
67 | { |
||
68 | string command = "select * from `"+m_Realm+"` where regionName like ?regionName"; |
||
69 | if (scopeID != UUID.Zero) |
||
70 | command += " and ScopeID = ?scopeID"; |
||
71 | |||
72 | command += " order by regionName"; |
||
73 | |||
74 | using (MySqlCommand cmd = new MySqlCommand(command)) |
||
75 | { |
||
76 | cmd.Parameters.AddWithValue("?regionName", regionName); |
||
77 | cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString()); |
||
78 | |||
79 | return RunCommand(cmd); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | public RegionData Get(int posX, int posY, UUID scopeID) |
||
84 | { |
||
85 | string command = "select * from `"+m_Realm+"` where locX = ?posX and locY = ?posY"; |
||
86 | if (scopeID != UUID.Zero) |
||
87 | command += " and ScopeID = ?scopeID"; |
||
88 | |||
89 | using (MySqlCommand cmd = new MySqlCommand(command)) |
||
90 | { |
||
91 | cmd.Parameters.AddWithValue("?posX", posX.ToString()); |
||
92 | cmd.Parameters.AddWithValue("?posY", posY.ToString()); |
||
93 | cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString()); |
||
94 | |||
95 | List<RegionData> ret = RunCommand(cmd); |
||
96 | if (ret.Count == 0) |
||
97 | return null; |
||
98 | |||
99 | return ret[0]; |
||
100 | } |
||
101 | } |
||
102 | |||
103 | public RegionData Get(UUID regionID, UUID scopeID) |
||
104 | { |
||
105 | string command = "select * from `"+m_Realm+"` where uuid = ?regionID"; |
||
106 | if (scopeID != UUID.Zero) |
||
107 | command += " and ScopeID = ?scopeID"; |
||
108 | |||
109 | using (MySqlCommand cmd = new MySqlCommand(command)) |
||
110 | { |
||
111 | cmd.Parameters.AddWithValue("?regionID", regionID.ToString()); |
||
112 | cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString()); |
||
113 | |||
114 | List<RegionData> ret = RunCommand(cmd); |
||
115 | if (ret.Count == 0) |
||
116 | return null; |
||
117 | |||
118 | return ret[0]; |
||
119 | } |
||
120 | } |
||
121 | |||
122 | public List<RegionData> Get(int startX, int startY, int endX, int endY, UUID scopeID) |
||
123 | { |
||
124 | string command = "select * from `"+m_Realm+"` where locX between ?startX and ?endX and locY between ?startY and ?endY"; |
||
125 | if (scopeID != UUID.Zero) |
||
126 | command += " and ScopeID = ?scopeID"; |
||
127 | |||
128 | using (MySqlCommand cmd = new MySqlCommand(command)) |
||
129 | { |
||
130 | cmd.Parameters.AddWithValue("?startX", startX.ToString()); |
||
131 | cmd.Parameters.AddWithValue("?startY", startY.ToString()); |
||
132 | cmd.Parameters.AddWithValue("?endX", endX.ToString()); |
||
133 | cmd.Parameters.AddWithValue("?endY", endY.ToString()); |
||
134 | cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString()); |
||
135 | |||
136 | return RunCommand(cmd); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | public List<RegionData> RunCommand(MySqlCommand cmd) |
||
141 | { |
||
142 | List<RegionData> retList = new List<RegionData>(); |
||
143 | |||
144 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) |
||
145 | { |
||
146 | dbcon.Open(); |
||
147 | cmd.Connection = dbcon; |
||
148 | |||
149 | using (IDataReader result = cmd.ExecuteReader()) |
||
150 | { |
||
151 | while (result.Read()) |
||
152 | { |
||
153 | RegionData ret = new RegionData(); |
||
154 | ret.Data = new Dictionary<string, object>(); |
||
155 | |||
156 | ret.RegionID = DBGuid.FromDB(result["uuid"]); |
||
157 | ret.ScopeID = DBGuid.FromDB(result["ScopeID"]); |
||
158 | |||
159 | ret.RegionName = result["regionName"].ToString(); |
||
160 | ret.posX = Convert.ToInt32(result["locX"]); |
||
161 | ret.posY = Convert.ToInt32(result["locY"]); |
||
162 | ret.sizeX = Convert.ToInt32(result["sizeX"]); |
||
163 | ret.sizeY = Convert.ToInt32(result["sizeY"]); |
||
164 | |||
165 | CheckColumnNames(result); |
||
166 | |||
167 | foreach (string s in m_ColumnNames) |
||
168 | { |
||
169 | if (s == "uuid") |
||
170 | continue; |
||
171 | if (s == "ScopeID") |
||
172 | continue; |
||
173 | if (s == "regionName") |
||
174 | continue; |
||
175 | if (s == "locX") |
||
176 | continue; |
||
177 | if (s == "locY") |
||
178 | continue; |
||
179 | |||
180 | object value = result[s]; |
||
181 | if (value is DBNull) |
||
182 | ret.Data[s] = null; |
||
183 | else |
||
184 | ret.Data[s] = result[s].ToString(); |
||
185 | } |
||
186 | |||
187 | retList.Add(ret); |
||
188 | } |
||
189 | } |
||
190 | } |
||
191 | |||
192 | return retList; |
||
193 | } |
||
194 | |||
195 | private void CheckColumnNames(IDataReader result) |
||
196 | { |
||
197 | if (m_ColumnNames != null) |
||
198 | return; |
||
199 | |||
200 | List<string> columnNames = new List<string>(); |
||
201 | |||
202 | DataTable schemaTable = result.GetSchemaTable(); |
||
203 | foreach (DataRow row in schemaTable.Rows) |
||
204 | { |
||
205 | if (row["ColumnName"] != null) |
||
206 | columnNames.Add(row["ColumnName"].ToString()); |
||
207 | } |
||
208 | |||
209 | m_ColumnNames = columnNames; |
||
210 | } |
||
211 | |||
212 | public bool Store(RegionData data) |
||
213 | { |
||
214 | if (data.Data.ContainsKey("uuid")) |
||
215 | data.Data.Remove("uuid"); |
||
216 | if (data.Data.ContainsKey("ScopeID")) |
||
217 | data.Data.Remove("ScopeID"); |
||
218 | if (data.Data.ContainsKey("regionName")) |
||
219 | data.Data.Remove("regionName"); |
||
220 | if (data.Data.ContainsKey("posX")) |
||
221 | data.Data.Remove("posX"); |
||
222 | if (data.Data.ContainsKey("posY")) |
||
223 | data.Data.Remove("posY"); |
||
224 | if (data.Data.ContainsKey("sizeX")) |
||
225 | data.Data.Remove("sizeX"); |
||
226 | if (data.Data.ContainsKey("sizeY")) |
||
227 | data.Data.Remove("sizeY"); |
||
228 | if (data.Data.ContainsKey("locX")) |
||
229 | data.Data.Remove("locX"); |
||
230 | if (data.Data.ContainsKey("locY")) |
||
231 | data.Data.Remove("locY"); |
||
232 | |||
233 | if (data.RegionName.Length > 128) |
||
234 | data.RegionName = data.RegionName.Substring(0, 128); |
||
235 | |||
236 | string[] fields = new List<string>(data.Data.Keys).ToArray(); |
||
237 | |||
238 | using (MySqlCommand cmd = new MySqlCommand()) |
||
239 | { |
||
240 | string update = "update `" + m_Realm + "` set locX=?posX, locY=?posY, sizeX=?sizeX, sizeY=?sizeY"; |
||
241 | foreach (string field in fields) |
||
242 | { |
||
243 | update += ", "; |
||
244 | update += "`" + field + "` = ?" + field; |
||
245 | |||
246 | cmd.Parameters.AddWithValue("?" + field, data.Data[field]); |
||
247 | } |
||
248 | |||
249 | update += " where uuid = ?regionID"; |
||
250 | |||
251 | if (data.ScopeID != UUID.Zero) |
||
252 | update += " and ScopeID = ?scopeID"; |
||
253 | |||
254 | cmd.CommandText = update; |
||
255 | cmd.Parameters.AddWithValue("?regionID", data.RegionID.ToString()); |
||
256 | cmd.Parameters.AddWithValue("?regionName", data.RegionName); |
||
257 | cmd.Parameters.AddWithValue("?scopeID", data.ScopeID.ToString()); |
||
258 | cmd.Parameters.AddWithValue("?posX", data.posX.ToString()); |
||
259 | cmd.Parameters.AddWithValue("?posY", data.posY.ToString()); |
||
260 | cmd.Parameters.AddWithValue("?sizeX", data.sizeX.ToString()); |
||
261 | cmd.Parameters.AddWithValue("?sizeY", data.sizeY.ToString()); |
||
262 | |||
263 | if (ExecuteNonQuery(cmd) < 1) |
||
264 | { |
||
265 | string insert = "insert into `" + m_Realm + "` (`uuid`, `ScopeID`, `locX`, `locY`, `sizeX`, `sizeY`, `regionName`, `" + |
||
266 | String.Join("`, `", fields) + |
||
267 | "`) values ( ?regionID, ?scopeID, ?posX, ?posY, ?sizeX, ?sizeY, ?regionName, ?" + String.Join(", ?", fields) + ")"; |
||
268 | |||
269 | cmd.CommandText = insert; |
||
270 | |||
271 | if (ExecuteNonQuery(cmd) < 1) |
||
272 | { |
||
273 | return false; |
||
274 | } |
||
275 | } |
||
276 | } |
||
277 | |||
278 | return true; |
||
279 | } |
||
280 | |||
281 | public bool SetDataItem(UUID regionID, string item, string value) |
||
282 | { |
||
283 | using (MySqlCommand cmd = new MySqlCommand("update `" + m_Realm + "` set `" + item + "` = ?" + item + " where uuid = ?UUID")) |
||
284 | { |
||
285 | cmd.Parameters.AddWithValue("?" + item, value); |
||
286 | cmd.Parameters.AddWithValue("?UUID", regionID.ToString()); |
||
287 | |||
288 | if (ExecuteNonQuery(cmd) > 0) |
||
289 | return true; |
||
290 | } |
||
291 | |||
292 | return false; |
||
293 | } |
||
294 | |||
295 | public bool Delete(UUID regionID) |
||
296 | { |
||
297 | using (MySqlCommand cmd = new MySqlCommand("delete from `" + m_Realm + "` where uuid = ?UUID")) |
||
298 | { |
||
299 | cmd.Parameters.AddWithValue("?UUID", regionID.ToString()); |
||
300 | |||
301 | if (ExecuteNonQuery(cmd) > 0) |
||
302 | return true; |
||
303 | } |
||
304 | |||
305 | return false; |
||
306 | } |
||
307 | |||
308 | public List<RegionData> GetDefaultRegions(UUID scopeID) |
||
309 | { |
||
310 | return Get((int)RegionFlags.DefaultRegion, scopeID); |
||
311 | } |
||
312 | |||
313 | public List<RegionData> GetDefaultHypergridRegions(UUID scopeID) |
||
314 | { |
||
315 | return Get((int)RegionFlags.DefaultHGRegion, scopeID); |
||
316 | } |
||
317 | |||
318 | public List<RegionData> GetFallbackRegions(UUID scopeID, int x, int y) |
||
319 | { |
||
320 | List<RegionData> regions = Get((int)RegionFlags.FallbackRegion, scopeID); |
||
321 | RegionDataDistanceCompare distanceComparer = new RegionDataDistanceCompare(x, y); |
||
322 | regions.Sort(distanceComparer); |
||
323 | return regions; |
||
324 | } |
||
325 | |||
326 | public List<RegionData> GetHyperlinks(UUID scopeID) |
||
327 | { |
||
328 | return Get((int)RegionFlags.Hyperlink, scopeID); |
||
329 | } |
||
330 | |||
331 | private List<RegionData> Get(int regionFlags, UUID scopeID) |
||
332 | { |
||
333 | string command = "select * from `" + m_Realm + "` where (flags & " + regionFlags.ToString() + ") <> 0"; |
||
334 | if (scopeID != UUID.Zero) |
||
335 | command += " and ScopeID = ?scopeID"; |
||
336 | |||
337 | using (MySqlCommand cmd = new MySqlCommand(command)) |
||
338 | { |
||
339 | cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString()); |
||
340 | |||
341 | return RunCommand(cmd); |
||
342 | } |
||
343 | } |
||
344 | } |
||
345 | } |