29 using System.Collections.Generic;
30 using System.Reflection;
33 using OpenSim.Framework;
34 using OpenSim.Region.Framework.Interfaces;
39 namespace OpenSim.Data.PGSQL
43 private const string _migrationStore =
"EstateStore";
45 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
48 private string m_connectionString;
49 private FieldInfo[] _Fields;
50 private Dictionary<string, FieldInfo> _FieldMap =
new Dictionary<string, FieldInfo>();
52 #region Public methods
60 Initialise(connectionString);
63 protected virtual Assembly Assembly
65 get {
return GetType().Assembly; }
74 if (!
string.IsNullOrEmpty(connectionString))
76 m_connectionString = connectionString;
81 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
90 _Fields = t.GetFields(BindingFlags.NonPublic |
91 BindingFlags.Instance |
92 BindingFlags.DeclaredOnly);
94 foreach (FieldInfo f
in _Fields)
96 if (f.Name.Substring(0, 2) ==
"m_")
97 _FieldMap[f.Name.Substring(2)] = f;
110 string sql =
"select estate_settings.\"" + String.Join(
"\",estate_settings.\"", FieldList) +
111 "\" from estate_map left join estate_settings on estate_map.\"EstateID\" = estate_settings.\"EstateID\" " +
112 " where estate_settings.\"EstateID\" is not null and \"RegionID\" = :RegionID";
114 bool insertEstate =
false;
115 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
116 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
118 cmd.Parameters.Add(_Database.CreateParameter(
"RegionID", regionID));
120 using (NpgsqlDataReader reader = cmd.ExecuteReader())
124 foreach (
string name
in FieldList)
126 FieldInfo f = _FieldMap[name];
127 object v = reader[name];
128 if (f.FieldType == typeof(
bool))
132 else if (f.FieldType == typeof(
UUID))
134 UUID estUUID = UUID.Zero;
136 UUID.TryParse(v.ToString(), out estUUID);
138 f.SetValue(es, estUUID);
140 else if (f.FieldType == typeof(
string))
142 f.SetValue(es, v.ToString());
144 else if (f.FieldType == typeof(UInt32))
146 f.SetValue(es, Convert.ToUInt32(v));
148 else if (f.FieldType == typeof(Single))
150 f.SetValue(es, Convert.ToSingle(v));
163 if (insertEstate && create)
166 LinkRegion(regionID, (
int)es.
EstateID);
171 es.EstateManagers = LoadUUIDList(es.
EstateID,
"estate_managers");
172 es.EstateAccess = LoadUUIDList(es.
EstateID,
"estate_users");
173 es.EstateGroups = LoadUUIDList(es.
EstateID,
"estate_groups");
176 es.OnSave += StoreEstateSettings;
183 es.OnSave += StoreEstateSettings;
189 es.EstateManagers = LoadUUIDList(es.
EstateID,
"estate_managers");
190 es.EstateAccess = LoadUUIDList(es.
EstateID,
"estate_users");
191 es.EstateGroups = LoadUUIDList(es.
EstateID,
"estate_groups");
198 List<string> names =
new List<string>(FieldList);
200 names.Remove(
"EstateID");
202 string sql = string.Format(
"insert into estate_settings (\"{0}\") values ( :{1} )", String.Join(
"\",\"", names.ToArray()), String.Join(
", :", names.ToArray()));
204 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
205 using (NpgsqlCommand insertCommand =
new NpgsqlCommand(sql, conn))
207 insertCommand.CommandText = sql;
209 foreach (
string name
in names)
211 insertCommand.Parameters.Add(_Database.CreateParameter(
"" + name, _FieldMap[name].GetValue(es)));
220 if (insertCommand.ExecuteNonQuery() > 0)
222 insertCommand.CommandText =
"Select cast(lastval() as int) as ID ;";
224 using (NpgsqlDataReader result = insertCommand.ExecuteReader())
228 es.EstateID = (uint)result.GetInt32(0);
245 List<string> names =
new List<string>(FieldList);
247 names.Remove(
"EstateID");
249 string sql = string.Format(
"UPDATE estate_settings SET ");
250 foreach (
string name
in names)
252 sql +=
"\"" + name +
"\" = :" + name +
", ";
254 sql = sql.Remove(sql.LastIndexOf(
","));
255 sql +=
" WHERE \"EstateID\" = :EstateID";
257 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
258 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
260 foreach (
string name
in names)
262 cmd.Parameters.Add(_Database.CreateParameter(
"" + name, _FieldMap[name].GetValue(es)));
265 cmd.Parameters.Add(_Database.CreateParameter(
"EstateID", es.EstateID));
267 cmd.ExecuteNonQuery();
271 SaveUUIDList(es.EstateID,
"estate_managers", es.EstateManagers);
272 SaveUUIDList(es.EstateID,
"estate_users", es.EstateAccess);
273 SaveUUIDList(es.EstateID,
"estate_groups", es.EstateGroups);
278 #region Private methods
280 private string[] FieldList
282 get {
return new List<string>(_FieldMap.Keys).ToArray(); }
289 string sql =
"select \"bannedUUID\" from estateban where \"EstateID\" = :EstateID";
291 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
292 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
294 NpgsqlParameter idParameter =
new NpgsqlParameter(
"EstateID", DbType.Int32);
295 idParameter.Value = es.EstateID;
296 cmd.Parameters.Add(idParameter);
298 using (NpgsqlDataReader reader = cmd.ExecuteReader())
300 while (reader.Read())
304 eb.BannedUserID =
new UUID((Guid)reader[
"bannedUUID"]);
305 eb.BannedHostAddress =
"0.0.0.0";
306 eb.BannedHostIPMask =
"0.0.0.0";
313 private UUID[] LoadUUIDList(uint estateID,
string table)
315 List<UUID> uuids =
new List<UUID>();
317 string sql = string.Format(
"select uuid from {0} where \"EstateID\" = :EstateID", table);
319 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
320 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
322 cmd.Parameters.Add(_Database.CreateParameter(
"EstateID", estateID));
324 using (NpgsqlDataReader reader = cmd.ExecuteReader())
326 while (reader.Read())
328 uuids.Add(
new UUID((Guid)reader[
"uuid"]));
333 return uuids.ToArray();
339 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
342 using (NpgsqlCommand cmd = conn.CreateCommand())
344 cmd.CommandText =
"delete from estateban where \"EstateID\" = :EstateID";
345 cmd.Parameters.AddWithValue(
"EstateID", (int)es.
EstateID);
346 cmd.ExecuteNonQuery();
349 cmd.CommandText =
"insert into estateban (\"EstateID\", \"bannedUUID\",\"bannedIp\", \"bannedIpHostMask\", \"bannedNameMask\") values ( :EstateID, :bannedUUID, '','','' )";
350 cmd.Parameters.AddWithValue(
"bannedUUID", Guid.Empty);
353 cmd.Parameters[
"bannedUUID"].Value = b.BannedUserID.Guid;
354 cmd.ExecuteNonQuery();
360 private void SaveUUIDList(uint estateID,
string table, UUID[] data)
362 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
365 using (NpgsqlCommand cmd = conn.CreateCommand())
367 cmd.Parameters.AddWithValue(
"EstateID", (int)estateID);
368 cmd.CommandText = string.Format(
"delete from {0} where \"EstateID\" = :EstateID", table);
369 cmd.ExecuteNonQuery();
371 cmd.CommandText = string.Format(
"insert into {0} (\"EstateID\", uuid) values ( :EstateID, :uuid )", table);
372 cmd.Parameters.AddWithValue(
"uuid", Guid.Empty);
373 foreach (UUID uuid
in data)
375 cmd.Parameters[
"uuid"].Value = uuid.Guid;
376 cmd.ExecuteNonQuery();
385 string sql =
"select estate_settings.\"" + String.Join(
"\",estate_settings.\"", FieldList) +
"\" from estate_settings where \"EstateID\" = :EstateID";
386 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
389 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
391 cmd.Parameters.AddWithValue(
"EstateID", (int)estateID);
392 using (NpgsqlDataReader reader = cmd.ExecuteReader())
396 foreach (
string name
in FieldList)
398 FieldInfo f = _FieldMap[name];
399 object v = reader[name];
400 if (f.FieldType == typeof(
bool))
402 f.SetValue(es, Convert.ToInt32(v) != 0);
404 else if (f.FieldType == typeof(
UUID))
406 f.SetValue(es,
new UUID((Guid)v));
408 else if (f.FieldType == typeof(
string))
410 f.SetValue(es, v.ToString());
412 else if (f.FieldType == typeof(UInt32))
414 f.SetValue(es, Convert.ToUInt32(v));
416 else if (f.FieldType == typeof(Single))
418 f.SetValue(es, Convert.ToSingle(v));
430 es.EstateManagers = LoadUUIDList(es.
EstateID,
"estate_managers");
431 es.EstateAccess = LoadUUIDList(es.
EstateID,
"estate_users");
432 es.EstateGroups = LoadUUIDList(es.
EstateID,
"estate_groups");
435 es.OnSave += StoreEstateSettings;
442 List<EstateSettings> allEstateSettings =
new List<EstateSettings>();
444 List<int> allEstateIds = GetEstatesAll();
446 foreach (
int estateId
in allEstateIds)
447 allEstateSettings.Add(LoadEstateSettings(estateId));
449 return allEstateSettings;
454 List<int> result =
new List<int>();
455 string sql =
"select \"EstateID\" from estate_settings where lower(\"EstateName\") = lower(:EstateName)";
456 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
459 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
461 cmd.Parameters.AddWithValue(
"EstateName", search);
463 using (IDataReader reader = cmd.ExecuteReader())
465 while (reader.Read())
467 result.Add(Convert.ToInt32(reader[
"EstateID"]));
479 List<int> result =
new List<int>();
480 string sql =
"select \"EstateID\" from estate_settings";
481 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
484 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
486 using (IDataReader reader = cmd.ExecuteReader())
488 while (reader.Read())
490 result.Add(Convert.ToInt32(reader[
"EstateID"]));
502 List<int> result =
new List<int>();
503 string sql =
"select \"EstateID\" from estate_settings where \"EstateOwner\" = :EstateOwner";
504 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
507 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
509 cmd.Parameters.AddWithValue(
"EstateOwner", ownerID);
511 using (IDataReader reader = cmd.ExecuteReader())
513 while (reader.Read())
515 result.Add(Convert.ToInt32(reader[
"EstateID"]));
527 string deleteSQL =
"delete from estate_map where \"RegionID\" = :RegionID";
528 string insertSQL =
"insert into estate_map values (:RegionID, :EstateID)";
529 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
533 NpgsqlTransaction transaction = conn.BeginTransaction();
537 using (NpgsqlCommand cmd =
new NpgsqlCommand(deleteSQL, conn))
539 cmd.Transaction = transaction;
540 cmd.Parameters.AddWithValue(
"RegionID", regionID.Guid);
542 cmd.ExecuteNonQuery();
545 using (NpgsqlCommand cmd =
new NpgsqlCommand(insertSQL, conn))
547 cmd.Transaction = transaction;
548 cmd.Parameters.AddWithValue(
"RegionID", regionID.Guid);
549 cmd.Parameters.AddWithValue(
"EstateID", estateID);
551 int ret = cmd.ExecuteNonQuery();
554 transaction.Commit();
556 transaction.Rollback();
563 m_log.Error(
"[REGION DB]: LinkRegion failed: " + ex.Message);
564 transaction.Rollback();
572 List<UUID> result =
new List<UUID>();
573 string sql =
"select \"RegionID\" from estate_map where \"EstateID\" = :EstateID";
574 using (NpgsqlConnection conn =
new NpgsqlConnection(m_connectionString))
577 using (NpgsqlCommand cmd =
new NpgsqlCommand(sql, conn))
579 cmd.Parameters.AddWithValue(
"EstateID", estateID);
581 using (IDataReader reader = cmd.ExecuteReader())
583 while (reader.Read())
585 result.Add(DBGuid.FromDB(reader[
"RegionID"]));
List< EstateSettings > LoadEstateSettingsAll()
Load/Get all estate settings.
PGSQLEstateStore(string connectionString)
List< UUID > GetRegions(int estateID)
Get the UUIDs of all the regions in an estate.
List< int > GetEstates(string search)
Get estate IDs.
void Initialise(string connectionString)
Initialises the estatedata class.
A management class for the MS SQL Storage Engine
bool DeleteEstate(int estateID)
Delete an estate
List< int > GetEstatesAll()
Get the IDs of all estates.
EstateSettings CreateNewEstate()
Create a new estate.
void StoreEstateSettings(EstateSettings es)
Stores the estate settings.
bool LinkRegion(UUID regionID, int estateID)
Link a region to an estate.
EstateSettings LoadEstateSettings(UUID regionID, bool create)
Loads the estate settings.
List< int > GetEstatesByOwner(UUID ownerID)
Get the IDs of all estates owned by the given user.
EstateSettings LoadEstateSettings(int estateID)
Load estate settings for an estate ID.