Fixed the query helper functions
This commit is contained in:
parent
5cb8e019bf
commit
6f26cc0dc5
3 changed files with 59 additions and 18 deletions
|
|
@ -73,7 +73,7 @@ namespace TShockAPI.DB
|
||||||
using (var reader = database.QueryReader("SELECT * FROM Bans WHERE IP=@0", ip))
|
using (var reader = database.QueryReader("SELECT * FROM Bans WHERE IP=@0", ip))
|
||||||
{
|
{
|
||||||
if (reader.Read())
|
if (reader.Read())
|
||||||
return new Ban((string)reader["IP"], (string)reader["Name"], (string)reader["Reason"]);
|
return new Ban(reader.Get<string>("IP"), reader.Get<string>("Name"), reader.Get<string>("Reason"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|
@ -97,7 +97,7 @@ namespace TShockAPI.DB
|
||||||
using (var reader = database.QueryReader("SELECT * FROM Bans WHERE " + namecol + "=@0", name))
|
using (var reader = database.QueryReader("SELECT * FROM Bans WHERE " + namecol + "=@0", name))
|
||||||
{
|
{
|
||||||
if (reader.Read())
|
if (reader.Read())
|
||||||
return new Ban((string)reader["IP"], (string)reader["Name"], (string)reader["Reason"]);
|
return new Ban(reader.Get<string>("IP"), reader.Get<string>("Name"), reader.Get<string>("Reason"));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,42 +5,48 @@ using System.Data;
|
||||||
namespace TShockAPI.DB
|
namespace TShockAPI.DB
|
||||||
{
|
{
|
||||||
public static class DbExt
|
public static class DbExt
|
||||||
{
|
{
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Executes a query on a database.
|
/// Executes a query on a database.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="db">Database to query</param>
|
/// <param name="olddb">Database to query</param>
|
||||||
/// <param name="query">Query string with parameters as @0, @1, etc.</param>
|
/// <param name="query">Query string with parameters as @0, @1, etc.</param>
|
||||||
/// <param name="args">Parameters to be put in the query</param>
|
/// <param name="args">Parameters to be put in the query</param>
|
||||||
/// <returns>Rows affected by query</returns>
|
/// <returns>Rows affected by query</returns>
|
||||||
public static int Query(this IDbConnection db, string query, params object[] args)
|
public static int Query(this IDbConnection olddb, string query, params object[] args)
|
||||||
{
|
{
|
||||||
using (var com = db.CreateCommand())
|
using (var db = olddb.CloneEx())
|
||||||
{
|
{
|
||||||
com.CommandText = query;
|
db.Open();
|
||||||
for (int i = 0; i < args.Length; i++)
|
using (var com = db.CreateCommand())
|
||||||
com.AddParameter("@" + i, args[i]);
|
{
|
||||||
|
com.CommandText = query;
|
||||||
|
for (int i = 0; i < args.Length; i++)
|
||||||
|
com.AddParameter("@" + i, args[i]);
|
||||||
|
|
||||||
return com.ExecuteNonQuery();
|
return com.ExecuteNonQuery();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Executes a query on a database.
|
/// Executes a query on a database.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="db">Database to query</param>
|
/// <param name="olddb">Database to query</param>
|
||||||
/// <param name="query">Query string with parameters as @0, @1, etc.</param>
|
/// <param name="query">Query string with parameters as @0, @1, etc.</param>
|
||||||
/// <param name="args">Parameters to be put in the query</param>
|
/// <param name="args">Parameters to be put in the query</param>
|
||||||
/// <returns>Query result as IDataReader</returns>
|
/// <returns>Query result as IDataReader</returns>
|
||||||
public static IDataReader QueryReader(this IDbConnection db, string query, params object[] args)
|
public static QueryResult QueryReader(this IDbConnection olddb, string query, params object[] args)
|
||||||
{
|
{
|
||||||
|
var db = olddb.CloneEx();
|
||||||
|
db.Open();
|
||||||
using (var com = db.CreateCommand())
|
using (var com = db.CreateCommand())
|
||||||
{
|
{
|
||||||
com.CommandText = query;
|
com.CommandText = query;
|
||||||
for (int i = 0; i < args.Length; i++)
|
for (int i = 0; i < args.Length; i++)
|
||||||
com.AddParameter("@" + i, args[i]);
|
com.AddParameter("@" + i, args[i]);
|
||||||
|
|
||||||
return com.ExecuteReader();
|
return new QueryResult(db, com.ExecuteReader());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -53,8 +59,15 @@ namespace TShockAPI.DB
|
||||||
return parm;
|
return parm;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Dictionary<Type, Func<IDataReader, int, object>> ReadFuncs = new Dictionary<Type, Func<IDataReader, int, object>>
|
public static IDbConnection CloneEx(this IDbConnection conn)
|
||||||
{
|
{
|
||||||
|
var clone = (IDbConnection)Activator.CreateInstance(conn.GetType());
|
||||||
|
clone.ConnectionString = conn.ConnectionString;
|
||||||
|
return clone;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Dictionary<Type, Func<IDataReader, int, object>> ReadFuncs = new Dictionary<Type, Func<IDataReader, int, object>>()
|
||||||
|
{
|
||||||
{typeof(bool), (s, i) => s.GetBoolean(i)},
|
{typeof(bool), (s, i) => s.GetBoolean(i)},
|
||||||
{typeof(byte), (s, i) => s.GetByte(i)},
|
{typeof(byte), (s, i) => s.GetByte(i)},
|
||||||
{typeof(Int16), (s, i) => s.GetInt16(i)},
|
{typeof(Int16), (s, i) => s.GetInt16(i)},
|
||||||
|
|
@ -64,7 +77,6 @@ namespace TShockAPI.DB
|
||||||
{typeof(decimal), (s, i) => s.GetDecimal(i)},
|
{typeof(decimal), (s, i) => s.GetDecimal(i)},
|
||||||
{typeof(float), (s, i) => s.GetFloat(i)},
|
{typeof(float), (s, i) => s.GetFloat(i)},
|
||||||
{typeof(double), (s, i) => s.GetDouble(i)},
|
{typeof(double), (s, i) => s.GetDouble(i)},
|
||||||
{typeof(object), (s, i) => s.GetValue(i)},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
public static T Get<T>(this IDataReader reader, string column)
|
public static T Get<T>(this IDataReader reader, string column)
|
||||||
|
|
@ -80,4 +92,34 @@ namespace TShockAPI.DB
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public class QueryResult : IDisposable
|
||||||
|
{
|
||||||
|
public IDbConnection Connection { get; protected set; }
|
||||||
|
public IDataReader Reader { get; protected set; }
|
||||||
|
|
||||||
|
public QueryResult(IDbConnection conn, IDataReader reader)
|
||||||
|
{
|
||||||
|
Connection = conn;
|
||||||
|
Reader = reader;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
Reader.Dispose();
|
||||||
|
Connection.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Read()
|
||||||
|
{
|
||||||
|
return Reader.Read();
|
||||||
|
}
|
||||||
|
|
||||||
|
public T Get<T>(string column)
|
||||||
|
{
|
||||||
|
return Reader.Get<T>(Reader.GetOrdinal(column));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -193,7 +193,6 @@ namespace TShockAPI.DB
|
||||||
returndata[1] = reader.Get<string>("UserGroup");
|
returndata[1] = reader.Get<string>("UserGroup");
|
||||||
return returndata;
|
return returndata;
|
||||||
}
|
}
|
||||||
reader.Close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|
@ -295,7 +294,7 @@ namespace TShockAPI.DB
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
IDataReader result;
|
QueryResult result;
|
||||||
if (string.IsNullOrEmpty(user.Address))
|
if (string.IsNullOrEmpty(user.Address))
|
||||||
{
|
{
|
||||||
result = database.QueryReader("SELECT * FROM Users WHERE Username=@0", user.Name);
|
result = database.QueryReader("SELECT * FROM Users WHERE Username=@0", user.Name);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue