From ab92dff5be6bd9c6a632b9128f970b4793446bb2 Mon Sep 17 00:00:00 2001 From: UbitUmarov Date: Mon, 26 Aug 2019 03:00:28 +0100 Subject: [PATCH] a few changes to db generic tables --- .../Data/MySQL/MySQLGenericTableHandler.cs | 83 +++++++++++++------ OpenSim/Data/Null/NullGenericDataHandler.cs | 41 +++++++++ .../Data/PGSQL/PGSQLGenericTableHandler.cs | 49 ++++++++++- 3 files changed, 147 insertions(+), 26 deletions(-) diff --git a/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs b/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs index 1564140785..ce228b59a6 100644 --- a/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs +++ b/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs @@ -116,8 +116,40 @@ namespace OpenSim.Data.MySQL } public virtual T[] Get(string field, string key) + { + using (MySqlCommand cmd = new MySqlCommand()) + { + cmd.Parameters.AddWithValue(field, key); + cmd.CommandText = string.Format("select * from {0} where `{1}` = ?{1}", m_Realm, field); + return DoQuery(cmd); + } + } + + public virtual T[] Get(string field, string[] keys) { - return Get(new string[] { field }, new string[] { key }); + int flen = keys.Length; + if(flen == 0) + return new T[0]; + + int flast = flen - 1; + StringBuilder sb = new StringBuilder(1024); + sb.AppendFormat("select * from {0} where {1} IN (?", m_Realm, field); + using (MySqlCommand cmd = new MySqlCommand()) + { + for (int i = 0 ; i < flen ; i++) + { + string fname = field + i.ToString(); + cmd.Parameters.AddWithValue(fname, keys[i]); + + sb.Append(fname); + if(i < flast) + sb.Append(",?"); + else + sb.Append(")"); + } + cmd.CommandText = sb.ToString(); + return DoQuery(cmd); + } } public virtual T[] Get(string[] fields, string[] keys) @@ -140,7 +172,7 @@ namespace OpenSim.Data.MySQL for (int i = 0 ; i < flen ; i++) { cmd.Parameters.AddWithValue(fields[i], keys[i]); - if(i< flast) + if(i < flast) sb.AppendFormat("`{0}` = ?{0} and ", fields[i]); else sb.AppendFormat("`{0}` = ?{0} ", fields[i]); @@ -323,25 +355,26 @@ namespace OpenSim.Data.MySQL // "[MYSQL GENERIC TABLE HANDLER]: Delete(string[] fields, string[] keys) invoked with {0}:{1}", // string.Join(",", fields), string.Join(",", keys)); - if (fields.Length != keys.Length) + int flen = fields.Length; + if (flen == 0 || flen != keys.Length) return false; - List terms = new List(); + int flast = flen - 1; + StringBuilder sb = new StringBuilder(1024); + sb.AppendFormat("delete from {0} where ", m_Realm); using (MySqlCommand cmd = new MySqlCommand()) { - for (int i = 0 ; i < fields.Length ; i++) + for (int i = 0 ; i < flen ; i++) { cmd.Parameters.AddWithValue(fields[i], keys[i]); - terms.Add("`" + fields[i] + "` = ?" + fields[i]); + if(i < flast) + sb.AppendFormat("`{0}` = ?{0} and ", fields[i]); + else + sb.AppendFormat("`{0}` = ?{0}", fields[i]); } - string where = String.Join(" and ", terms.ToArray()); - - string query = String.Format("delete from {0} where {1}", m_Realm, where); - - cmd.CommandText = query; - + cmd.CommandText = sb.ToString(); return ExecuteNonQuery(cmd) > 0; } } @@ -353,27 +386,27 @@ namespace OpenSim.Data.MySQL public long GetCount(string[] fields, string[] keys) { - if (fields.Length != keys.Length) + int flen = fields.Length; + if (flen == 0 || flen != keys.Length) return 0; - List terms = new List(); + int flast = flen - 1; + StringBuilder sb = new StringBuilder(1024); + sb.AppendFormat("select count(*) from {0} where ", m_Realm); using (MySqlCommand cmd = new MySqlCommand()) { - for (int i = 0; i < fields.Length; i++) + for (int i = 0 ; i < flen ; i++) { cmd.Parameters.AddWithValue(fields[i], keys[i]); - terms.Add("`" + fields[i] + "` = ?" + fields[i]); + if(i < flast) + sb.AppendFormat("`{0}` = ?{0} and ", fields[i]); + else + sb.AppendFormat("`{0}` = ?{0}", fields[i]); } - string where = String.Join(" and ", terms.ToArray()); - - string query = String.Format("select count(*) from {0} where {1}", - m_Realm, where); - - cmd.CommandText = query; - - Object result = DoQueryScalar(cmd); + cmd.CommandText = sb.ToString(); + object result = DoQueryScalar(cmd); return Convert.ToInt64(result); } @@ -403,7 +436,7 @@ namespace OpenSim.Data.MySQL dbcon.Open(); cmd.Connection = dbcon; - Object ret = cmd.ExecuteScalar(); + object ret = cmd.ExecuteScalar(); cmd.Connection = null; dbcon.Close(); return ret; diff --git a/OpenSim/Data/Null/NullGenericDataHandler.cs b/OpenSim/Data/Null/NullGenericDataHandler.cs index dd9d190910..0b6e49ba10 100644 --- a/OpenSim/Data/Null/NullGenericDataHandler.cs +++ b/OpenSim/Data/Null/NullGenericDataHandler.cs @@ -42,6 +42,47 @@ namespace OpenSim.Data.Null /// public class NullGenericDataHandler { + protected List Get(string field, string val, List inputEntities) + { + List entities = inputEntities; + + entities + = entities.Where( + e => + { + FieldInfo fi = typeof(T).GetField(field); + if (fi == null) + throw new NotImplementedException(string.Format("No field {0} for val {1}", field, val)); + + return fi.GetValue(e).ToString() == val; + } + ).ToList(); + + return entities; + } + + protected List Get(string field, string[] vals, List inputEntities) + { + List entities = new List(); + + for (int i = 0; i < vals.Length; i++) + { + entities.AddRange (inputEntities.Where( + e => + { + FieldInfo fi = typeof(T).GetField(field); + if (fi == null) + throw new NotImplementedException(string.Format("No field {0} for val {1}", field, vals[i])); + + return fi.GetValue(e).ToString() == vals[i]; + } + ).ToList() + ); + } + return entities; + } + + protected List Get(string[] fields, string[] vals, List inputEntities) { List entities = inputEntities; diff --git a/OpenSim/Data/PGSQL/PGSQLGenericTableHandler.cs b/OpenSim/Data/PGSQL/PGSQLGenericTableHandler.cs index 5b247200d3..a89183b0d4 100644 --- a/OpenSim/Data/PGSQL/PGSQLGenericTableHandler.cs +++ b/OpenSim/Data/PGSQL/PGSQLGenericTableHandler.cs @@ -180,7 +180,54 @@ namespace OpenSim.Data.PGSQL public virtual T[] Get(string field, string key) { - return Get(new string[] { field }, new string[] { key }); + using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString)) + using (NpgsqlCommand cmd = new NpgsqlCommand()) + { + if ( m_FieldTypes.ContainsKey(field) ) + cmd.Parameters.Add(m_database.CreateParameter(field, key, m_FieldTypes[field])); + else + cmd.Parameters.Add(m_database.CreateParameter(field, key)); + + string query = String.Format("SELECT * FROM {0} WHERE \"{1}\" = :{1}", m_Realm, field, field); + + cmd.Connection = conn; + cmd.CommandText = query; + conn.Open(); + return DoQuery(cmd); + } + } + + public virtual T[] Get(string field, string[] keys) + { + + int flen = keys.Length; + if(flen == 0) + return new T[0]; + + int flast = flen - 1; + StringBuilder sb = new StringBuilder(1024); + sb.AppendFormat("select * from {0} where {1} IN ('", m_Realm, field); + + using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString)) + using (NpgsqlCommand cmd = new NpgsqlCommand()) + { + + for (int i = 0 ; i < flen ; i++) + { + sb.Append(keys[i]); + if(i < flast) + sb.Append("','"); + else + sb.Append("')"); + } + + string query = sb.ToString(); + + cmd.Connection = conn; + cmd.CommandText = query; + conn.Open(); + return DoQuery(cmd); + } } public virtual T[] Get(string[] fields, string[] keys)