From c26867633a796967ee386390d6150b864d14483a Mon Sep 17 00:00:00 2001 From: high Date: Tue, 5 Jul 2011 05:44:58 -0400 Subject: [PATCH] Added a nice generic method for things like GetString(GetOrdinal(name)) => Get(name) Moved AddParameter to extension --- TShockAPI/DB/DbExt.cs | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 TShockAPI/DB/DbExt.cs diff --git a/TShockAPI/DB/DbExt.cs b/TShockAPI/DB/DbExt.cs new file mode 100644 index 00000000..a280b1a5 --- /dev/null +++ b/TShockAPI/DB/DbExt.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Text; + +namespace TShockAPI.DB +{ + public static class DbExt + { + public static IDbDataParameter AddParameter(this IDbCommand command, string name, object data) + { + var parm = command.CreateParameter(); + parm.ParameterName = name; + parm.Value = data; + command.Parameters.Add(parm); + return parm; + } + + static Dictionary> ReadFuncs = new Dictionary>() + { + {typeof(bool), (s, i) => s.GetBoolean(i)}, + {typeof(byte), (s, i) => s.GetByte(i)}, + {typeof(Int16), (s, i) => s.GetInt16(i)}, + {typeof(Int32), (s, i) => s.GetInt32(i)}, + {typeof(Int64), (s, i) => s.GetInt64(i)}, + {typeof(string), (s, i) => s.GetString(i)}, + {typeof(decimal), (s, i) => s.GetDecimal(i)}, + {typeof(decimal), (s, i) => s.GetFloat(i)}, + {typeof(double), (s, i) => s.GetDouble(i)}, + }; + + public static T Get(this IDataReader reader, string column) + { + return reader.Get(column); + } + public static T Get(this IDataReader reader, int column) + { + if (ReadFuncs.ContainsKey(typeof(T))) + return (T)ReadFuncs[typeof(T)](reader, column); + + throw new NotImplementedException(); + } + } +}