a few changes to db generic tables
parent
584df2ab38
commit
ab92dff5be
|
@ -116,8 +116,40 @@ namespace OpenSim.Data.MySQL
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual T[] Get(string field, string key)
|
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)
|
public virtual T[] Get(string[] fields, string[] keys)
|
||||||
|
@ -140,7 +172,7 @@ namespace OpenSim.Data.MySQL
|
||||||
for (int i = 0 ; i < flen ; i++)
|
for (int i = 0 ; i < flen ; i++)
|
||||||
{
|
{
|
||||||
cmd.Parameters.AddWithValue(fields[i], keys[i]);
|
cmd.Parameters.AddWithValue(fields[i], keys[i]);
|
||||||
if(i< flast)
|
if(i < flast)
|
||||||
sb.AppendFormat("`{0}` = ?{0} and ", fields[i]);
|
sb.AppendFormat("`{0}` = ?{0} and ", fields[i]);
|
||||||
else
|
else
|
||||||
sb.AppendFormat("`{0}` = ?{0} ", fields[i]);
|
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}",
|
// "[MYSQL GENERIC TABLE HANDLER]: Delete(string[] fields, string[] keys) invoked with {0}:{1}",
|
||||||
// string.Join(",", fields), string.Join(",", keys));
|
// string.Join(",", fields), string.Join(",", keys));
|
||||||
|
|
||||||
if (fields.Length != keys.Length)
|
int flen = fields.Length;
|
||||||
|
if (flen == 0 || flen != keys.Length)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
List<string> terms = new List<string>();
|
int flast = flen - 1;
|
||||||
|
StringBuilder sb = new StringBuilder(1024);
|
||||||
|
sb.AppendFormat("delete from {0} where ", m_Realm);
|
||||||
|
|
||||||
using (MySqlCommand cmd = new MySqlCommand())
|
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]);
|
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());
|
cmd.CommandText = sb.ToString();
|
||||||
|
|
||||||
string query = String.Format("delete from {0} where {1}", m_Realm, where);
|
|
||||||
|
|
||||||
cmd.CommandText = query;
|
|
||||||
|
|
||||||
return ExecuteNonQuery(cmd) > 0;
|
return ExecuteNonQuery(cmd) > 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -353,27 +386,27 @@ namespace OpenSim.Data.MySQL
|
||||||
|
|
||||||
public long GetCount(string[] fields, string[] keys)
|
public long GetCount(string[] fields, string[] keys)
|
||||||
{
|
{
|
||||||
if (fields.Length != keys.Length)
|
int flen = fields.Length;
|
||||||
|
if (flen == 0 || flen != keys.Length)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
List<string> terms = new List<string>();
|
int flast = flen - 1;
|
||||||
|
StringBuilder sb = new StringBuilder(1024);
|
||||||
|
sb.AppendFormat("select count(*) from {0} where ", m_Realm);
|
||||||
|
|
||||||
using (MySqlCommand cmd = new MySqlCommand())
|
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]);
|
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());
|
cmd.CommandText = sb.ToString();
|
||||||
|
object result = DoQueryScalar(cmd);
|
||||||
string query = String.Format("select count(*) from {0} where {1}",
|
|
||||||
m_Realm, where);
|
|
||||||
|
|
||||||
cmd.CommandText = query;
|
|
||||||
|
|
||||||
Object result = DoQueryScalar(cmd);
|
|
||||||
|
|
||||||
return Convert.ToInt64(result);
|
return Convert.ToInt64(result);
|
||||||
}
|
}
|
||||||
|
@ -403,7 +436,7 @@ namespace OpenSim.Data.MySQL
|
||||||
dbcon.Open();
|
dbcon.Open();
|
||||||
cmd.Connection = dbcon;
|
cmd.Connection = dbcon;
|
||||||
|
|
||||||
Object ret = cmd.ExecuteScalar();
|
object ret = cmd.ExecuteScalar();
|
||||||
cmd.Connection = null;
|
cmd.Connection = null;
|
||||||
dbcon.Close();
|
dbcon.Close();
|
||||||
return ret;
|
return ret;
|
||||||
|
|
|
@ -42,6 +42,47 @@ namespace OpenSim.Data.Null
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class NullGenericDataHandler
|
public class NullGenericDataHandler
|
||||||
{
|
{
|
||||||
|
protected List<T> Get<T>(string field, string val, List<T> inputEntities)
|
||||||
|
{
|
||||||
|
List<T> 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<T> Get<T>(string field, string[] vals, List<T> inputEntities)
|
||||||
|
{
|
||||||
|
List<T> entities = new List<T>();
|
||||||
|
|
||||||
|
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<T> Get<T>(string[] fields, string[] vals, List<T> inputEntities)
|
protected List<T> Get<T>(string[] fields, string[] vals, List<T> inputEntities)
|
||||||
{
|
{
|
||||||
List<T> entities = inputEntities;
|
List<T> entities = inputEntities;
|
||||||
|
|
|
@ -180,7 +180,54 @@ namespace OpenSim.Data.PGSQL
|
||||||
|
|
||||||
public virtual T[] Get(string field, string key)
|
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)
|
public virtual T[] Get(string[] fields, string[] keys)
|
||||||
|
|
Loading…
Reference in New Issue