29 using System.Collections;
30 using System.Collections.Generic;
32 using System.Reflection;
33 using MySql.Data.MySqlClient;
35 using OpenSim.Framework;
39 namespace OpenSim.Data.MySQL
43 private string m_Realm;
44 private List<string> m_ColumnNames;
47 protected virtual Assembly Assembly
49 get {
return GetType().Assembly; }
53 : base(connectionString)
56 m_connectionString = connectionString;
58 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
66 public List<RegionData>
Get(
string regionName, UUID scopeID)
68 string command =
"select * from `"+m_Realm+
"` where regionName like ?regionName";
69 if (scopeID != UUID.Zero)
70 command +=
" and ScopeID = ?scopeID";
72 command +=
" order by regionName";
74 using (MySqlCommand cmd =
new MySqlCommand(command))
76 cmd.Parameters.AddWithValue(
"?regionName", regionName);
77 cmd.Parameters.AddWithValue(
"?scopeID", scopeID.ToString());
79 return RunCommand(cmd);
104 string command =
"select * from `" + m_Realm +
"` where locX between ?startX and ?endX and locY between ?startY and ?endY";
105 if (scopeID != UUID.Zero)
106 command +=
" and ScopeID = ?scopeID";
109 int startY = posY - (
int)Constants.MaximumRegionSize;
113 List<RegionData> ret;
114 using (MySqlCommand cmd =
new MySqlCommand(command))
116 cmd.Parameters.AddWithValue(
"?startX", startX.ToString());
117 cmd.Parameters.AddWithValue(
"?startY", startY.ToString());
118 cmd.Parameters.AddWithValue(
"?endX", endX.ToString());
119 cmd.Parameters.AddWithValue(
"?endY", endY.ToString());
120 cmd.Parameters.AddWithValue(
"?scopeID", scopeID.ToString());
122 ret = RunCommand(cmd);
145 string command =
"select * from `"+m_Realm+
"` where uuid = ?regionID";
146 if (scopeID != UUID.Zero)
147 command +=
" and ScopeID = ?scopeID";
149 using (MySqlCommand cmd =
new MySqlCommand(command))
151 cmd.Parameters.AddWithValue(
"?regionID", regionID.ToString());
152 cmd.Parameters.AddWithValue(
"?scopeID", scopeID.ToString());
154 List<RegionData> ret = RunCommand(cmd);
162 public List<RegionData>
Get(
int startX,
int startY,
int endX,
int endY, UUID scopeID)
180 string command =
"select * from `" + m_Realm +
"` where locX between ?startX and ?endX and locY between ?startY and ?endY";
181 if (scopeID != UUID.Zero)
182 command +=
" and ScopeID = ?scopeID";
185 int qstartY = startY - (
int)Constants.MaximumRegionSize;
187 List<RegionData> dbret;
188 using (MySqlCommand cmd =
new MySqlCommand(command))
190 cmd.Parameters.AddWithValue(
"?startX", qstartX.ToString());
191 cmd.Parameters.AddWithValue(
"?startY", qstartY.ToString());
192 cmd.Parameters.AddWithValue(
"?endX", endX.ToString());
193 cmd.Parameters.AddWithValue(
"?endY", endY.ToString());
194 cmd.Parameters.AddWithValue(
"?scopeID", scopeID.ToString());
196 dbret = RunCommand(cmd);
199 List<RegionData> ret =
new List<RegionData>();
201 if (dbret.Count == 0)
215 List<RegionData> retList =
new List<RegionData>();
217 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
220 cmd.Connection = dbcon;
222 using (IDataReader result = cmd.ExecuteReader())
224 while (result.Read())
227 ret.Data =
new Dictionary<string, object>();
229 ret.RegionID = DBGuid.FromDB(result[
"uuid"]);
230 ret.ScopeID = DBGuid.FromDB(result[
"ScopeID"]);
232 ret.RegionName = result[
"regionName"].ToString();
233 ret.posX = Convert.ToInt32(result[
"locX"]);
234 ret.posY = Convert.ToInt32(result[
"locY"]);
235 ret.sizeX = Convert.ToInt32(result[
"sizeX"]);
236 ret.sizeY = Convert.ToInt32(result[
"sizeY"]);
238 CheckColumnNames(result);
240 foreach (
string s
in m_ColumnNames)
246 if (s ==
"regionName")
253 object value = result[s];
257 ret.Data[s] = result[s].ToString();
268 private void CheckColumnNames(IDataReader result)
270 if (m_ColumnNames != null)
273 List<string> columnNames =
new List<string>();
275 DataTable schemaTable = result.GetSchemaTable();
276 foreach (DataRow row
in schemaTable.Rows)
278 if (row[
"ColumnName"] != null)
279 columnNames.Add(row[
"ColumnName"].ToString());
282 m_ColumnNames = columnNames;
287 if (data.
Data.ContainsKey(
"uuid"))
288 data.
Data.Remove(
"uuid");
289 if (data.
Data.ContainsKey(
"ScopeID"))
290 data.
Data.Remove(
"ScopeID");
291 if (data.
Data.ContainsKey(
"regionName"))
292 data.
Data.Remove(
"regionName");
293 if (data.
Data.ContainsKey(
"posX"))
294 data.
Data.Remove(
"posX");
295 if (data.
Data.ContainsKey(
"posY"))
296 data.
Data.Remove(
"posY");
297 if (data.
Data.ContainsKey(
"sizeX"))
298 data.
Data.Remove(
"sizeX");
299 if (data.
Data.ContainsKey(
"sizeY"))
300 data.
Data.Remove(
"sizeY");
301 if (data.
Data.ContainsKey(
"locX"))
302 data.
Data.Remove(
"locX");
303 if (data.
Data.ContainsKey(
"locY"))
304 data.
Data.Remove(
"locY");
307 data.RegionName = data.RegionName.Substring(0, 128);
309 string[] fields =
new List<string>(data.Data.Keys).ToArray();
311 using (MySqlCommand cmd =
new MySqlCommand())
313 string update =
"update `" + m_Realm +
"` set locX=?posX, locY=?posY, sizeX=?sizeX, sizeY=?sizeY";
314 foreach (
string field
in fields)
317 update +=
"`" + field +
"` = ?" + field;
319 cmd.Parameters.AddWithValue(
"?" + field, data.Data[field]);
322 update +=
" where uuid = ?regionID";
325 update +=
" and ScopeID = ?scopeID";
327 cmd.CommandText = update;
328 cmd.Parameters.AddWithValue(
"?regionID", data.RegionID.ToString());
329 cmd.Parameters.AddWithValue(
"?regionName", data.RegionName);
330 cmd.Parameters.AddWithValue(
"?scopeID", data.ScopeID.ToString());
331 cmd.Parameters.AddWithValue(
"?posX", data.posX.ToString());
332 cmd.Parameters.AddWithValue(
"?posY", data.posY.ToString());
333 cmd.Parameters.AddWithValue(
"?sizeX", data.sizeX.ToString());
334 cmd.Parameters.AddWithValue(
"?sizeY", data.sizeY.ToString());
336 if (ExecuteNonQuery(cmd) < 1)
338 string insert =
"insert into `" + m_Realm +
"` (`uuid`, `ScopeID`, `locX`, `locY`, `sizeX`, `sizeY`, `regionName`, `" +
339 String.Join(
"`, `", fields) +
340 "`) values ( ?regionID, ?scopeID, ?posX, ?posY, ?sizeX, ?sizeY, ?regionName, ?" + String.Join(
", ?", fields) +
")";
342 cmd.CommandText = insert;
344 if (ExecuteNonQuery(cmd) < 1)
356 using (MySqlCommand cmd =
new MySqlCommand(
"update `" + m_Realm +
"` set `" + item +
"` = ?" + item +
" where uuid = ?UUID"))
358 cmd.Parameters.AddWithValue(
"?" + item, value);
359 cmd.Parameters.AddWithValue(
"?UUID", regionID.ToString());
361 if (ExecuteNonQuery(cmd) > 0)
370 using (MySqlCommand cmd =
new MySqlCommand(
"delete from `" + m_Realm +
"` where uuid = ?UUID"))
372 cmd.Parameters.AddWithValue(
"?UUID", regionID.ToString());
374 if (ExecuteNonQuery(cmd) > 0)
383 return Get((
int)
RegionFlags.DefaultRegion, scopeID);
388 return Get((
int)
RegionFlags.DefaultHGRegion, scopeID);
393 List<RegionData> regions = Get((
int)
RegionFlags.FallbackRegion, scopeID);
395 regions.Sort(distanceComparer);
404 private List<RegionData> Get(
int regionFlags, UUID scopeID)
406 string command =
"select * from `" + m_Realm +
"` where (flags & " + regionFlags.ToString() +
") <> 0";
407 if (scopeID != UUID.Zero)
408 command +=
" and ScopeID = ?scopeID";
410 using (MySqlCommand cmd =
new MySqlCommand(command))
412 cmd.Parameters.AddWithValue(
"?scopeID", scopeID.ToString());
414 return RunCommand(cmd);
List< RegionData > GetHyperlinks(UUID scopeID)
bool SetDataItem(UUID regionID, string item, string value)
A database interface class to a user profile storage system
int posX
The position in meters of this region.
const uint MaximumRegionSize
RegionData Get(UUID regionID, UUID scopeID)
An interface for connecting to the authentication datastore
List< RegionData > Get(int startX, int startY, int endX, int endY, UUID scopeID)
List< RegionData > GetDefaultRegions(UUID scopeID)
List< RegionData > GetFallbackRegions(UUID scopeID, int x, int y)
bool Store(RegionData data)
MySqlRegionData(string connectionString, string realm)
int posY
The position in meters of this region.
Dictionary< string, object > Data
RegionFlags
Region flags used internally by OpenSimulator to store installation specific information about region...
OpenSim.Framework.RegionFlags RegionFlags
List< RegionData > Get(string regionName, UUID scopeID)
List< RegionData > RunCommand(MySqlCommand cmd)
List< RegionData > GetDefaultHypergridRegions(UUID scopeID)
bool Delete(UUID regionID)
RegionData Get(int posX, int posY, UUID scopeID)