29 using System.Collections.Generic;
31 using System.Reflection;
33 using MySql.Data.MySqlClient;
35 using OpenSim.Framework;
36 using OpenSim.Region.Framework.Interfaces;
39 namespace OpenSim.Data.MySQL
43 private static readonly ILog m_log =
44 LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
46 private string m_connectionString;
48 private FieldInfo[] m_Fields;
49 private Dictionary<string, FieldInfo> m_FieldMap =
50 new Dictionary<string, FieldInfo>();
52 protected virtual Assembly Assembly
54 get {
return GetType().Assembly; }
63 Initialise(connectionString);
68 m_connectionString = connectionString;
72 m_log.Info(
"[REGION DB]: MySql - connecting: " + Util.GetDisplayConnectionString(m_connectionString));
76 m_log.Debug(
"Exception: password not found in connection string\n" + e.ToString());
79 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
87 m_Fields = t.GetFields(BindingFlags.NonPublic |
88 BindingFlags.Instance |
89 BindingFlags.DeclaredOnly);
91 foreach (FieldInfo f
in m_Fields)
93 if (f.Name.Substring(0, 2) ==
"m_")
94 m_FieldMap[f.Name.Substring(2)] = f;
99 private string[] FieldList
101 get {
return new List<string>(m_FieldMap.Keys).ToArray(); }
106 string sql =
"select estate_settings." + String.Join(
",estate_settings.", FieldList) +
107 " from estate_map left join estate_settings on estate_map.EstateID = estate_settings.EstateID where estate_settings.EstateID is not null and RegionID = ?RegionID";
109 using (MySqlCommand cmd =
new MySqlCommand())
111 cmd.CommandText = sql;
112 cmd.Parameters.AddWithValue(
"?RegionID", regionID.ToString());
125 es.OnSave += StoreEstateSettings;
131 es.EstateManagers = LoadUUIDList(es.
EstateID,
"estate_managers");
132 es.EstateAccess = LoadUUIDList(es.
EstateID,
"estate_users");
133 es.EstateGroups = LoadUUIDList(es.
EstateID,
"estate_groups");
138 private EstateSettings DoLoad(MySqlCommand cmd, UUID regionID,
bool create)
141 es.OnSave += StoreEstateSettings;
143 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
147 cmd.Connection = dbcon;
151 using (IDataReader r = cmd.ExecuteReader())
157 foreach (
string name
in FieldList)
159 if (m_FieldMap[name].FieldType == typeof(
bool))
161 m_FieldMap[name].SetValue(es, Convert.ToInt32(r[name]) != 0);
163 else if (m_FieldMap[name].FieldType == typeof(UUID))
165 m_FieldMap[name].SetValue(es, DBGuid.FromDB(r[name]));
169 m_FieldMap[name].SetValue(es, r[name]);
175 if (!found && create)
178 LinkRegion(regionID, (
int)es.
EstateID);
183 es.EstateManagers = LoadUUIDList(es.
EstateID,
"estate_managers");
184 es.EstateAccess = LoadUUIDList(es.
EstateID,
"estate_users");
185 es.EstateGroups = LoadUUIDList(es.
EstateID,
"estate_groups");
192 List<string> names =
new List<string>(FieldList);
194 names.Remove(
"EstateID");
196 string sql =
"insert into estate_settings (" + String.Join(
",", names.ToArray()) +
") values ( ?" + String.Join(
", ?", names.ToArray()) +
")";
198 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
201 using (MySqlCommand cmd2 = dbcon.CreateCommand())
203 cmd2.CommandText = sql;
204 cmd2.Parameters.Clear();
206 foreach (
string name
in FieldList)
208 if (m_FieldMap[name].GetValue(es) is bool)
210 if ((
bool)m_FieldMap[name].GetValue(es))
211 cmd2.Parameters.AddWithValue(
"?" + name,
"1");
213 cmd2.Parameters.AddWithValue(
"?" + name,
"0");
217 cmd2.Parameters.AddWithValue(
"?" + name, m_FieldMap[name].GetValue(es).ToString());
221 cmd2.ExecuteNonQuery();
223 cmd2.CommandText =
"select LAST_INSERT_ID() as id";
224 cmd2.Parameters.Clear();
226 using (IDataReader r = cmd2.ExecuteReader())
229 es.EstateID = Convert.ToUInt32(r[
"id"]);
239 string sql =
"replace into estate_settings (" + String.Join(
",", FieldList) +
") values ( ?" +
String.Join(
", ?", FieldList) +
")";
241 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
245 using (MySqlCommand cmd = dbcon.CreateCommand())
247 cmd.CommandText = sql;
249 foreach (
string name
in FieldList)
251 if (m_FieldMap[name].GetValue(es) is bool)
253 if ((
bool)m_FieldMap[name].GetValue(es))
254 cmd.Parameters.AddWithValue(
"?" + name,
"1");
256 cmd.Parameters.AddWithValue(
"?" + name,
"0");
260 cmd.Parameters.AddWithValue(
"?" + name, m_FieldMap[name].GetValue(es).ToString());
264 cmd.ExecuteNonQuery();
278 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
282 using (MySqlCommand cmd = dbcon.CreateCommand())
284 cmd.CommandText =
"select bannedUUID from estateban where EstateID = ?EstateID";
285 cmd.Parameters.AddWithValue(
"?EstateID", es.EstateID);
287 using (IDataReader r = cmd.ExecuteReader())
294 UUID.TryParse(r[
"bannedUUID"].ToString(), out uuid);
296 eb.BannedUserID = uuid;
297 eb.BannedHostAddress =
"0.0.0.0";
298 eb.BannedHostIPMask =
"0.0.0.0";
308 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
312 using (MySqlCommand cmd = dbcon.CreateCommand())
314 cmd.CommandText =
"delete from estateban where EstateID = ?EstateID";
315 cmd.Parameters.AddWithValue(
"?EstateID", es.EstateID.ToString());
317 cmd.ExecuteNonQuery();
319 cmd.Parameters.Clear();
321 cmd.CommandText =
"insert into estateban (EstateID, bannedUUID, bannedIp, bannedIpHostMask, bannedNameMask) values ( ?EstateID, ?bannedUUID, '', '', '' )";
325 cmd.Parameters.AddWithValue(
"?EstateID", es.EstateID.ToString());
326 cmd.Parameters.AddWithValue(
"?bannedUUID", b.BannedUserID.ToString());
328 cmd.ExecuteNonQuery();
329 cmd.Parameters.Clear();
335 void SaveUUIDList(uint EstateID,
string table, UUID[] data)
337 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
341 using (MySqlCommand cmd = dbcon.CreateCommand())
343 cmd.CommandText =
"delete from " + table +
" where EstateID = ?EstateID";
344 cmd.Parameters.AddWithValue(
"?EstateID", EstateID.ToString());
346 cmd.ExecuteNonQuery();
348 cmd.Parameters.Clear();
350 cmd.CommandText =
"insert into " + table +
" (EstateID, uuid) values ( ?EstateID, ?uuid )";
352 foreach (UUID uuid
in data)
354 cmd.Parameters.AddWithValue(
"?EstateID", EstateID.ToString());
355 cmd.Parameters.AddWithValue(
"?uuid", uuid.ToString());
357 cmd.ExecuteNonQuery();
358 cmd.Parameters.Clear();
364 UUID[] LoadUUIDList(uint EstateID,
string table)
366 List<UUID> uuids =
new List<UUID>();
368 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
372 using (MySqlCommand cmd = dbcon.CreateCommand())
374 cmd.CommandText =
"select uuid from " + table +
" where EstateID = ?EstateID";
375 cmd.Parameters.AddWithValue(
"?EstateID", EstateID);
377 using (IDataReader r = cmd.ExecuteReader())
382 uuids.Add(DBGuid.FromDB(r[
"uuid"]));
388 return uuids.ToArray();
393 using (MySqlCommand cmd =
new MySqlCommand())
395 string sql =
"select estate_settings." + String.Join(
",estate_settings.", FieldList) +
" from estate_settings where EstateID = ?EstateID";
397 cmd.CommandText = sql;
398 cmd.Parameters.AddWithValue(
"?EstateID", estateID);
401 if (e.EstateID != estateID)
409 List<EstateSettings> allEstateSettings =
new List<EstateSettings>();
411 List<int> allEstateIds = GetEstatesAll();
413 foreach (
int estateId
in allEstateIds)
414 allEstateSettings.Add(LoadEstateSettings(estateId));
416 return allEstateSettings;
421 List<int> result =
new List<int>();
423 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
427 using (MySqlCommand cmd = dbcon.CreateCommand())
429 cmd.CommandText =
"select estateID from estate_settings";
431 using (IDataReader reader = cmd.ExecuteReader())
433 while (reader.Read())
435 result.Add(Convert.ToInt32(reader[
"EstateID"]));
449 List<int> result =
new List<int>();
451 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
455 using (MySqlCommand cmd = dbcon.CreateCommand())
457 cmd.CommandText =
"select estateID from estate_settings where EstateName = ?EstateName";
458 cmd.Parameters.AddWithValue(
"?EstateName", search);
460 using (IDataReader reader = cmd.ExecuteReader())
462 while (reader.Read())
464 result.Add(Convert.ToInt32(reader[
"EstateID"]));
478 List<int> result =
new List<int>();
480 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
484 using (MySqlCommand cmd = dbcon.CreateCommand())
486 cmd.CommandText =
"select estateID from estate_settings where EstateOwner = ?EstateOwner";
487 cmd.Parameters.AddWithValue(
"?EstateOwner", ownerID);
489 using (IDataReader reader = cmd.ExecuteReader())
491 while (reader.Read())
493 result.Add(Convert.ToInt32(reader[
"EstateID"]));
507 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
510 MySqlTransaction transaction = dbcon.BeginTransaction();
515 using (MySqlCommand cmd = dbcon.CreateCommand())
517 cmd.Transaction = transaction;
518 cmd.CommandText =
"delete from estate_map where RegionID = ?RegionID";
519 cmd.Parameters.AddWithValue(
"?RegionID", regionID);
521 cmd.ExecuteNonQuery();
524 using (MySqlCommand cmd = dbcon.CreateCommand())
526 cmd.Transaction = transaction;
527 cmd.CommandText =
"insert into estate_map values (?RegionID, ?EstateID)";
528 cmd.Parameters.AddWithValue(
"?RegionID", regionID);
529 cmd.Parameters.AddWithValue(
"?EstateID", estateID);
531 int ret = cmd.ExecuteNonQuery();
534 transaction.Commit();
536 transaction.Rollback();
543 catch (MySqlException ex)
545 m_log.Error(
"[REGION DB]: LinkRegion failed: " + ex.Message);
546 transaction.Rollback();
557 List<UUID> result =
new List<UUID>();
559 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
565 using (MySqlCommand cmd = dbcon.CreateCommand())
567 cmd.CommandText =
"select RegionID from estate_map where EstateID = ?EstateID";
568 cmd.Parameters.AddWithValue(
"?EstateID", estateID.ToString());
570 using (IDataReader reader = cmd.ExecuteReader())
573 result.Add(DBGuid.FromDB(reader[
"RegionID"]));
580 m_log.Error(
"[REGION DB]: Error reading estate map. " + e.ToString());
MySQLEstateStore(string connectionString)
List< int > GetEstatesByOwner(UUID ownerID)
Get the IDs of all estates owned by the given user.
List< int > GetEstatesAll()
Get the IDs of all estates.
EstateSettings CreateNewEstate()
Create a new estate.
EstateSettings LoadEstateSettings(int estateID)
Load estate settings for an estate ID.
List< EstateSettings > LoadEstateSettingsAll()
Load/Get all estate settings.
bool LinkRegion(UUID regionID, int estateID)
Link a region to an estate.
List< UUID > GetRegions(int estateID)
Get the UUIDs of all the regions in an estate.
void StoreEstateSettings(EstateSettings es)
Store estate settings.
void Initialise(string connectionString)
Initialise the data store.
EstateSettings LoadEstateSettings(UUID regionID, bool create)
Load estate settings for a region.
List< int > GetEstates(string search)
Get estate IDs.
bool DeleteEstate(int estateID)
Delete an estate