29 using System.Collections.Generic;
33 using System.Reflection;
36 using OpenSim.Framework;
37 using OpenSim.Region.Framework.Interfaces;
38 using OpenSim.Region.Framework.Scenes;
42 namespace OpenSim.Data.PGSQL
49 private string m_Realm;
50 private List<string> m_ColumnNames = null;
51 private string m_ConnectionString;
53 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
55 protected Dictionary<string, string> m_FieldTypes =
new Dictionary<string, string>();
57 protected virtual Assembly Assembly
59 get {
return GetType().Assembly; }
65 m_ConnectionString = connectionString;
68 using (NpgsqlConnection conn =
new NpgsqlConnection(m_ConnectionString))
77 private void LoadFieldTypes()
79 m_FieldTypes =
new Dictionary<string, string>();
81 string query = string.Format(
@"select column_name,data_type
82 from INFORMATION_SCHEMA.COLUMNS
83 where table_name = lower('{0}');
86 using (NpgsqlConnection conn =
new NpgsqlConnection(m_ConnectionString))
87 using (NpgsqlCommand cmd =
new NpgsqlCommand(query, conn))
90 using (NpgsqlDataReader rdr = cmd.ExecuteReader())
95 m_FieldTypes.Add((string)rdr[0], (
string)rdr[1]);
103 string sql =
"select * from "+m_Realm+
" where lower(\"regionName\") like lower(:regionName) ";
104 if (scopeID !=
UUID.Zero)
105 sql +=
" and \"ScopeID\" = :scopeID";
106 sql +=
" order by lower(\"regionName\")";
108 using (NpgsqlConnection conn =
new NpgsqlConnection(m_ConnectionString))
109 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
111 cmd.Parameters.Add(m_database.CreateParameter(
"regionName", regionName));
112 if (scopeID !=
UUID.Zero)
113 cmd.Parameters.Add(m_database.CreateParameter(
"scopeID", scopeID));
115 return RunCommand(cmd);
122 string sql =
"select * from "+m_Realm+
" where \"locX\" between :startX and :endX and \"locY\" between :startY and :endY";
123 if (scopeID !=
UUID.Zero)
124 sql +=
" and \"ScopeID\" = :scopeID";
127 int startY = posY - (
int)Constants.MaximumRegionSize;
131 List<RegionData> ret;
132 using (NpgsqlConnection conn =
new NpgsqlConnection(m_ConnectionString))
133 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
135 cmd.Parameters.Add(m_database.CreateParameter(
"startX", startX));
136 cmd.Parameters.Add(m_database.CreateParameter(
"startY", startY));
137 cmd.Parameters.Add(m_database.CreateParameter(
"endX", endX));
138 cmd.Parameters.Add(m_database.CreateParameter(
"endY", endY));
139 if (scopeID !=
UUID.Zero)
140 cmd.Parameters.Add(m_database.CreateParameter(
"scopeID", scopeID));
142 ret = RunCommand(cmd);
165 string sql =
"select * from "+m_Realm+
" where uuid = :regionID";
166 if (scopeID !=
UUID.Zero)
167 sql +=
" and \"ScopeID\" = :scopeID";
168 using (NpgsqlConnection conn =
new NpgsqlConnection(m_ConnectionString))
169 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
171 cmd.Parameters.Add(m_database.CreateParameter(
"regionID", regionID));
172 if (scopeID !=
UUID.Zero)
173 cmd.Parameters.Add(m_database.CreateParameter(
"scopeID", scopeID));
175 List<RegionData> ret = RunCommand(cmd);
183 public List<RegionData>
Get(
int startX,
int startY,
int endX,
int endY, UUID scopeID)
186 string sql =
"select * from "+m_Realm+
" where \"locX\" between :startX and :endX and \"locY\" between :startY and :endY";
187 if (scopeID !=
UUID.Zero)
188 sql +=
" and \"ScopeID\" = :scopeID";
191 int qstartY = startY - (
int)Constants.MaximumRegionSize;
193 List<RegionData> dbret;
194 using (NpgsqlConnection conn =
new NpgsqlConnection(m_ConnectionString))
195 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
197 cmd.Parameters.Add(m_database.CreateParameter(
"startX", qstartX));
198 cmd.Parameters.Add(m_database.CreateParameter(
"startY", qstartY));
199 cmd.Parameters.Add(m_database.CreateParameter(
"endX", endX));
200 cmd.Parameters.Add(m_database.CreateParameter(
"endY", endY));
201 if (scopeID !=
UUID.Zero)
202 cmd.Parameters.Add(m_database.CreateParameter(
"scopeID", scopeID));
205 dbret = RunCommand(cmd);
208 List<RegionData> ret =
new List<RegionData>();
224 List<RegionData> retList =
new List<RegionData>();
226 NpgsqlDataReader result = cmd.ExecuteReader();
228 while (result.Read())
231 ret.Data =
new Dictionary<string, object>();
234 UUID.TryParse(result[
"uuid"].ToString(), out regionID);
235 ret.RegionID = regionID;
237 UUID.TryParse(result[
"ScopeID"].ToString(), out scope);
239 ret.RegionName = result[
"regionName"].ToString();
240 ret.posX = Convert.ToInt32(result[
"locX"]);
241 ret.posY = Convert.ToInt32(result[
"locY"]);
242 ret.sizeX = Convert.ToInt32(result[
"sizeX"]);
243 ret.sizeY = Convert.ToInt32(result[
"sizeY"]);
245 if (m_ColumnNames == null)
247 m_ColumnNames =
new List<string>();
249 DataTable schemaTable = result.GetSchemaTable();
250 foreach (DataRow row
in schemaTable.Rows)
251 m_ColumnNames.Add(row[
"ColumnName"].ToString());
254 foreach (
string s
in m_ColumnNames)
260 if (s ==
"regionName")
267 ret.Data[s] = result[s].ToString();
277 if (data.
Data.ContainsKey(
"uuid"))
278 data.
Data.Remove(
"uuid");
279 if (data.
Data.ContainsKey(
"ScopeID"))
280 data.
Data.Remove(
"ScopeID");
281 if (data.
Data.ContainsKey(
"regionName"))
282 data.
Data.Remove(
"regionName");
283 if (data.
Data.ContainsKey(
"posX"))
284 data.
Data.Remove(
"posX");
285 if (data.
Data.ContainsKey(
"posY"))
286 data.
Data.Remove(
"posY");
287 if (data.
Data.ContainsKey(
"sizeX"))
288 data.
Data.Remove(
"sizeX");
289 if (data.
Data.ContainsKey(
"sizeY"))
290 data.
Data.Remove(
"sizeY");
291 if (data.
Data.ContainsKey(
"locX"))
292 data.
Data.Remove(
"locX");
293 if (data.
Data.ContainsKey(
"locY"))
294 data.
Data.Remove(
"locY");
296 string[] fields =
new List<string>(data.Data.Keys).ToArray();
298 using (NpgsqlConnection conn =
new NpgsqlConnection(m_ConnectionString))
299 using (NpgsqlCommand cmd =
new NpgsqlCommand())
302 string update =
"update " + m_Realm +
" set \"locX\"=:posX, \"locY\"=:posY, \"sizeX\"=:sizeX, \"sizeY\"=:sizeY ";
304 foreach (
string field
in fields)
308 update +=
" \"" + field +
"\" = :" + field;
310 if (m_FieldTypes.ContainsKey(field))
311 cmd.Parameters.Add(m_database.CreateParameter(field, data.
Data[field], m_FieldTypes[field]));
313 cmd.Parameters.Add(m_database.CreateParameter(field, data.Data[field]));
316 update +=
" where uuid = :regionID";
319 update +=
" and \"ScopeID\" = :scopeID";
321 cmd.CommandText = update;
322 cmd.Connection = conn;
323 cmd.Parameters.Add(m_database.CreateParameter(
"regionID", data.RegionID));
324 cmd.Parameters.Add(m_database.CreateParameter(
"regionName", data.RegionName));
325 cmd.Parameters.Add(m_database.CreateParameter(
"scopeID", data.ScopeID));
326 cmd.Parameters.Add(m_database.CreateParameter(
"posX", data.posX));
327 cmd.Parameters.Add(m_database.CreateParameter(
"posY", data.posY));
328 cmd.Parameters.Add(m_database.CreateParameter(
"sizeX", data.sizeX));
329 cmd.Parameters.Add(m_database.CreateParameter(
"sizeY", data.sizeY));
333 if (cmd.ExecuteNonQuery() < 1)
335 string insert =
"insert into " + m_Realm +
" (uuid, \"ScopeID\", \"locX\", \"locY\", \"sizeX\", \"sizeY\", \"regionName\", \"" +
336 String.Join(
"\", \"", fields) +
337 "\") values (:regionID, :scopeID, :posX, :posY, :sizeX, :sizeY, :regionName, :" +
String.Join(
", :", fields) +
")";
339 cmd.CommandText = insert;
343 if (cmd.ExecuteNonQuery() < 1)
350 m_log.Warn(
"[PGSQL Grid]: Error inserting into Regions table: " + ex.Message +
", INSERT sql: " + insert);
356 m_log.Warn(
"[PGSQL Grid]: Error updating Regions table: " + ex.Message +
", UPDATE sql: " + update);
365 string sql =
"update " + m_Realm +
366 " set \"" + item +
"\" = :" + item +
" where uuid = :UUID";
368 using (NpgsqlConnection conn =
new NpgsqlConnection(m_ConnectionString))
369 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
371 cmd.Parameters.Add(m_database.CreateParameter(
"" + item, value));
372 cmd.Parameters.Add(m_database.CreateParameter(
"UUID", regionID));
374 if (cmd.ExecuteNonQuery() > 0)
382 string sql =
"delete from " + m_Realm +
383 " where uuid = :UUID";
384 using (NpgsqlConnection conn =
new NpgsqlConnection(m_ConnectionString))
385 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
387 cmd.Parameters.Add(m_database.CreateParameter(
"UUID", regionID));
389 if (cmd.ExecuteNonQuery() > 0)
397 return Get((
int)
RegionFlags.DefaultRegion, scopeID);
402 return Get((
int)
RegionFlags.DefaultHGRegion, scopeID);
407 List<RegionData> regions = Get((
int)
RegionFlags.FallbackRegion, scopeID);
409 regions.Sort(distanceComparer);
419 private List<RegionData> Get(
int regionFlags, UUID scopeID)
421 string sql =
"SELECT * FROM " + m_Realm +
" WHERE (\"flags\" & " + regionFlags.ToString() +
") <> 0";
422 if (scopeID !=
UUID.Zero)
423 sql +=
" AND \"ScopeID\" = :scopeID";
425 using (NpgsqlConnection conn =
new NpgsqlConnection(m_ConnectionString))
426 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
428 cmd.Parameters.Add(m_database.CreateParameter(
"scopeID", scopeID));
430 return RunCommand(cmd);
List< RegionData > GetDefaultHypergridRegions(UUID scopeID)
int posX
The position in meters of this region.
const uint MaximumRegionSize
An interface for connecting to the authentication datastore
List< RegionData > Get(int startX, int startY, int endX, int endY, UUID scopeID)
RegionData Get(UUID regionID, UUID scopeID)
RegionData Get(int posX, int posY, UUID scopeID)
List< RegionData > GetDefaultRegions(UUID scopeID)
int posY
The position in meters of this region.
A management class for the MS SQL Storage Engine
Dictionary< string, object > Data
bool Store(RegionData data)
bool SetDataItem(UUID regionID, string item, string value)
RegionFlags
Region flags used internally by OpenSimulator to store installation specific information about region...
List< RegionData > GetFallbackRegions(UUID scopeID, int x, int y)
OpenSim.Framework.RegionFlags RegionFlags
List< RegionData > GetHyperlinks(UUID scopeID)
List< RegionData > RunCommand(NpgsqlCommand cmd)
bool Delete(UUID regionID)
PGSQLRegionData(string connectionString, string realm)
A PGSQL Interface for the Region Server.