29 using System.Collections.Generic;
31 using System.Reflection;
34 using Community.CsharpSqlite.Sqlite;
36 using Mono.Data.Sqlite;
39 using OpenSim.Framework;
40 using OpenSim.Region.Framework.Interfaces;
42 namespace OpenSim.Data.SQLite
46 private static readonly ILog m_log =
47 LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
49 private SqliteConnection m_connection;
50 private string m_connectionString;
52 private FieldInfo[] m_Fields;
53 private Dictionary<string, FieldInfo> m_FieldMap =
54 new Dictionary<string, FieldInfo>();
56 protected virtual Assembly Assembly
58 get {
return GetType().Assembly; }
67 Initialise(connectionString);
73 Util.LoadArchSpecificWindowsDll(
"sqlite3.dll");
75 m_connectionString = connectionString;
77 m_log.Info(
"[ESTATE DB]: Sqlite - connecting: "+m_connectionString);
79 m_connection =
new SqliteConnection(m_connectionString);
89 m_Fields = t.GetFields(BindingFlags.NonPublic |
90 BindingFlags.Instance |
91 BindingFlags.DeclaredOnly);
93 foreach (FieldInfo f
in m_Fields)
94 if (f.Name.Substring(0, 2) ==
"m_")
95 m_FieldMap[f.Name.Substring(2)] = f;
98 private string[] FieldList
100 get {
return new List<string>(m_FieldMap.Keys).ToArray(); }
105 string sql =
"select estate_settings."+String.Join(
",estate_settings.", FieldList)+
" from estate_map left join estate_settings on estate_map.EstateID = estate_settings.EstateID where estate_settings.EstateID is not null and RegionID = :RegionID";
107 using (SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
109 cmd.CommandText = sql;
110 cmd.Parameters.AddWithValue(
":RegionID", regionID.ToString());
112 return DoLoad(cmd, regionID, create);
116 private EstateSettings DoLoad(SqliteCommand cmd, UUID regionID,
bool create)
119 es.OnSave += StoreEstateSettings;
120 IDataReader r = null;
123 r = cmd.ExecuteReader();
125 catch (SqliteException)
127 m_log.Error(
"[SQLITE]: There was an issue loading the estate settings. This can happen the first time running OpenSimulator with CSharpSqlite the first time. OpenSimulator will probably crash, restart it and it should be good to go.");
130 if (r != null && r.Read())
132 foreach (
string name
in FieldList)
134 if (m_FieldMap[name].GetValue(es) is bool)
136 int v = Convert.ToInt32(r[name]);
138 m_FieldMap[name].SetValue(es,
true);
140 m_FieldMap[name].SetValue(es,
false);
142 else if (m_FieldMap[name].GetValue(es) is
UUID)
144 UUID uuid = UUID.Zero;
146 UUID.TryParse(r[name].ToString(), out uuid);
147 m_FieldMap[name].SetValue(es, uuid);
151 m_FieldMap[name].SetValue(es, Convert.ChangeType(r[name], m_FieldMap[name].FieldType));
159 LinkRegion(regionID, (
int)es.
EstateID);
164 es.EstateManagers = LoadUUIDList(es.
EstateID,
"estate_managers");
165 es.EstateAccess = LoadUUIDList(es.
EstateID,
"estate_users");
166 es.EstateGroups = LoadUUIDList(es.
EstateID,
"estate_groups");
173 es.OnSave += StoreEstateSettings;
179 es.EstateManagers = LoadUUIDList(es.
EstateID,
"estate_managers");
180 es.EstateAccess = LoadUUIDList(es.
EstateID,
"estate_users");
181 es.EstateGroups = LoadUUIDList(es.
EstateID,
"estate_groups");
188 List<string> names =
new List<string>(FieldList);
190 IDataReader r = null;
192 using (SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
194 names.Remove(
"EstateID");
196 string sql =
"insert into estate_settings ("+String.Join(
",", names.ToArray())+
") values ( :"+String.Join(
", :", names.ToArray())+
")";
198 cmd.CommandText = sql;
199 cmd.Parameters.Clear();
201 foreach (
string name
in FieldList)
203 if (m_FieldMap[name].GetValue(es) is bool)
205 if ((
bool)m_FieldMap[name].GetValue(es))
206 cmd.Parameters.AddWithValue(
":"+name,
"1");
208 cmd.Parameters.AddWithValue(
":"+name,
"0");
212 cmd.Parameters.AddWithValue(
":"+name, m_FieldMap[name].GetValue(es).ToString());
216 cmd.ExecuteNonQuery();
218 cmd.CommandText =
"select LAST_INSERT_ROWID() as id";
219 cmd.Parameters.Clear();
221 r = cmd.ExecuteReader();
226 es.EstateID = Convert.ToUInt32(r[
"id"]);
235 List<string> fields =
new List<string>(FieldList);
236 fields.Remove(
"EstateID");
238 List<string> terms =
new List<string>();
240 foreach (
string f
in fields)
241 terms.Add(f+
" = :"+f);
243 string sql =
"update estate_settings set "+String.Join(
", ", terms.ToArray())+
" where EstateID = :EstateID";
245 using (SqliteCommand cmd = (SqliteCommand)m_connection.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();
279 using (SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
281 cmd.CommandText =
"select bannedUUID from estateban where EstateID = :EstateID";
282 cmd.Parameters.AddWithValue(
":EstateID", es.EstateID);
284 r = cmd.ExecuteReader();
292 UUID.TryParse(r[
"bannedUUID"].ToString(), out uuid);
294 eb.BannedUserID = uuid;
295 eb.BannedHostAddress =
"0.0.0.0";
296 eb.BannedHostIPMask =
"0.0.0.0";
304 using (SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
306 cmd.CommandText =
"delete from estateban where EstateID = :EstateID";
307 cmd.Parameters.AddWithValue(
":EstateID", es.EstateID.ToString());
309 cmd.ExecuteNonQuery();
311 cmd.Parameters.Clear();
313 cmd.CommandText =
"insert into estateban (EstateID, bannedUUID, bannedIp, bannedIpHostMask, bannedNameMask) values ( :EstateID, :bannedUUID, '', '', '' )";
317 cmd.Parameters.AddWithValue(
":EstateID", es.EstateID.ToString());
318 cmd.Parameters.AddWithValue(
":bannedUUID", b.BannedUserID.ToString());
320 cmd.ExecuteNonQuery();
321 cmd.Parameters.Clear();
326 void SaveUUIDList(uint EstateID,
string table, UUID[] data)
328 using (SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
330 cmd.CommandText =
"delete from "+table+
" where EstateID = :EstateID";
331 cmd.Parameters.AddWithValue(
":EstateID", EstateID.ToString());
333 cmd.ExecuteNonQuery();
335 cmd.Parameters.Clear();
337 cmd.CommandText =
"insert into "+table+
" (EstateID, uuid) values ( :EstateID, :uuid )";
339 foreach (UUID uuid
in data)
341 cmd.Parameters.AddWithValue(
":EstateID", EstateID.ToString());
342 cmd.Parameters.AddWithValue(
":uuid", uuid.ToString());
344 cmd.ExecuteNonQuery();
345 cmd.Parameters.Clear();
350 UUID[] LoadUUIDList(uint EstateID,
string table)
352 List<UUID> uuids =
new List<UUID>();
355 using (SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
357 cmd.CommandText =
"select uuid from "+table+
" where EstateID = :EstateID";
358 cmd.Parameters.AddWithValue(
":EstateID", EstateID);
360 r = cmd.ExecuteReader();
368 UUID.TryParse(r[
"uuid"].ToString(), out uuid);
374 return uuids.ToArray();
379 string sql =
"select estate_settings."+String.Join(
",estate_settings.", FieldList)+
" from estate_settings where estate_settings.EstateID = :EstateID";
381 using (SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
383 cmd.CommandText = sql;
384 cmd.Parameters.AddWithValue(
":EstateID", estateID.ToString());
386 return DoLoad(cmd,
UUID.Zero,
false);
392 List<EstateSettings> estateSettings =
new List<EstateSettings>();
394 List<int> estateIds = GetEstatesAll();
395 foreach (
int estateId
in estateIds)
396 estateSettings.Add(LoadEstateSettings(estateId));
398 return estateSettings;
403 List<int> result =
new List<int>();
405 string sql =
"select EstateID from estate_settings where estate_settings.EstateName = :EstateName";
408 using (SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
410 cmd.CommandText = sql;
411 cmd.Parameters.AddWithValue(
":EstateName", search);
413 r = cmd.ExecuteReader();
418 result.Add(Convert.ToInt32(r[
"EstateID"]));
427 List<int> result =
new List<int>();
429 string sql =
"select EstateID from estate_settings";
432 using (SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
434 cmd.CommandText = sql;
436 r = cmd.ExecuteReader();
441 result.Add(Convert.ToInt32(r[
"EstateID"]));
450 List<int> result =
new List<int>();
452 string sql =
"select EstateID from estate_settings where estate_settings.EstateOwner = :EstateOwner";
455 using (SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
457 cmd.CommandText = sql;
458 cmd.Parameters.AddWithValue(
":EstateOwner", ownerID);
460 r = cmd.ExecuteReader();
465 result.Add(Convert.ToInt32(r[
"EstateID"]));
474 SqliteTransaction transaction = m_connection.BeginTransaction();
477 using(SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
479 cmd.CommandText =
"delete from estate_map where RegionID = :RegionID";
480 cmd.Transaction = transaction;
481 cmd.Parameters.AddWithValue(
":RegionID", regionID.ToString());
483 cmd.ExecuteNonQuery();
486 using(SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
488 cmd.CommandText =
"insert into estate_map values (:RegionID, :EstateID)";
489 cmd.Transaction = transaction;
490 cmd.Parameters.AddWithValue(
":RegionID", regionID.ToString());
491 cmd.Parameters.AddWithValue(
":EstateID", estateID.ToString());
493 if (cmd.ExecuteNonQuery() == 0)
495 transaction.Rollback();
500 transaction.Commit();
508 return new List<UUID>();
List< int > GetEstatesByOwner(UUID ownerID)
Get the IDs of all estates owned by the given user.
void Initialise(string connectionString)
Initialise the data store.
EstateSettings LoadEstateSettings(int estateID)
Load estate settings for an estate ID.
bool DeleteEstate(int estateID)
Delete an estate
bool LinkRegion(UUID regionID, int estateID)
Link a region to an estate.
List< int > GetEstatesAll()
Get the IDs of all estates.
SQLiteEstateStore(string connectionString)
void StoreEstateSettings(EstateSettings es)
Store estate settings.
List< EstateSettings > LoadEstateSettingsAll()
Load/Get all estate settings.
List< int > GetEstates(string search)
Get estate IDs.
List< UUID > GetRegions(int estateID)
Get the UUIDs of all the regions in an estate.
EstateSettings LoadEstateSettings(UUID regionID, bool create)
Load estate settings for a region.
EstateSettings CreateNewEstate()
Create a new estate.