From c233ea7bec8b2f8e0243e38b22f7f8a203fd01ff Mon Sep 17 00:00:00 2001 From: "mattias@amigarulez.se" Date: Tue, 26 Apr 2011 13:44:24 +0000 Subject: [PATCH] Added optional commandTimeout paraneter to query methods --- Dapper/SqlMapper.cs | 113 +++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 59 deletions(-) diff --git a/Dapper/SqlMapper.cs b/Dapper/SqlMapper.cs index 856578c..15035cb 100644 --- a/Dapper/SqlMapper.cs +++ b/Dapper/SqlMapper.cs @@ -1,4 +1,4 @@ -/* +/* License: http://www.apache.org/licenses/LICENSE-2.0 Home page: http://code.google.com/p/dapper-dot-net/ */ @@ -25,8 +25,8 @@ class CacheInfo public Action ParamReader { get; set; } } - static ConcurrentDictionary queryCache = new ConcurrentDictionary(); - static Dictionary typeMap; + static readonly ConcurrentDictionary queryCache = new ConcurrentDictionary(); + static readonly Dictionary typeMap; static SqlMapper() { @@ -74,16 +74,13 @@ private static DbType LookupDbType(Type type) { return dbType; } - else + if (typeof(IEnumerable).IsAssignableFrom(type)) { - if (typeof(IEnumerable).IsAssignableFrom(type)) - { - // use xml to denote its a list, hacky but will work on any DB - return DbType.Xml; - } + // use xml to denote its a list, hacky but will work on any DB + return DbType.Xml; } - throw new NotSupportedException("The type : " + type.ToString() + " is not supported by dapper"); + throw new NotSupportedException(string.Format("The type : {0} is not supported by dapper", type)); } private class Identity : IEquatable @@ -108,9 +105,9 @@ internal Identity(string sql, IDbConnection cnn, Type type, Type parametersType, hashCode = hashCode * 23 + (type == null ? 0 : type.GetHashCode()); if (otherTypes != null) { - for (int i = 0; i < otherTypes.Length; i++) + foreach (var t in otherTypes) { - hashCode = hashCode * 23 + (otherTypes[i] == null ? 0 : otherTypes[i].GetHashCode()); + hashCode = hashCode * 23 + (t == null ? 0 : t.GetHashCode()); } } hashCode = hashCode * 23 + (connectionString == null ? 0 : connectionString.GetHashCode()); @@ -135,7 +132,7 @@ public bool Equals(Identity other) { return other != null && - this.type == other.type && + type == other.type && sql == other.sql && connectionString == other.connectionString && parametersType == other.parametersType; @@ -146,44 +143,37 @@ public bool Equals(Identity other) /// Execute parameterized SQL /// /// Number of rows affected - public static int Execute(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null) + public static int Execute(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null) { var identity = new Identity(sql, cnn, null, param == null ? null : param.GetType()); var info = GetCacheInfo(param, identity); - return ExecuteCommand(cnn, transaction, sql, info.ParamReader, param); + return ExecuteCommand(cnn, transaction, sql, info.ParamReader, param, commandTimeout); } /// /// Return a list of dynamic objects, reader is closed after the call /// - public static IEnumerable Query(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, bool buffered = true) + public static IEnumerable Query(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null) { - return Query(cnn, sql, param, transaction, buffered); + return Query(cnn, sql, param, transaction, buffered, commandTimeout); } - public static IEnumerable Query(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, bool buffered = true) + public static IEnumerable Query(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null) { - var data = QueryInternal(cnn, sql, param, transaction); - if (buffered) - { - return data.ToList(); - } - else - { - return data; - } + var data = QueryInternal(cnn, sql, param, transaction, commandTimeout); + return (buffered) ? data.ToList() : data; } /// /// Return a typed list of objects, reader is closed after the call /// - private static IEnumerable QueryInternal(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null) + private static IEnumerable QueryInternal(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null) { var identity = new Identity(sql, cnn, typeof(T), param == null ? null : param.GetType()); var info = GetCacheInfo(param, identity); - using (var cmd = SetupCommand(cnn, transaction, sql, info.ParamReader, param)) + using (var cmd = SetupCommand(cnn, transaction, sql, info.ParamReader, param, commandTimeout)) { using (var reader = cmd.ExecuteReader()) { @@ -213,35 +203,37 @@ private static IEnumerable QueryInternal(this IDbConnection cnn, string sq /// /// /// + /// /// The Field we should split and read the second object from (default: id) + /// Number of seconds before command execution timeout /// - public static IEnumerable Query(this IDbConnection cnn, string sql, Action map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id") + public static IEnumerable Query(this IDbConnection cnn, string sql, Action map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null) { - return MultiMap(cnn, sql, map, param, transaction, buffered, splitOn); + return MultiMap(cnn, sql, map, param, transaction, buffered, splitOn, commandTimeout); } - public static IEnumerable Query(this IDbConnection cnn, string sql, Action map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id") + public static IEnumerable Query(this IDbConnection cnn, string sql, Action map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null) { - return MultiMap(cnn, sql, map, param, transaction, buffered, splitOn); + return MultiMap(cnn, sql, map, param, transaction, buffered, splitOn, commandTimeout); } - public static IEnumerable Query(this IDbConnection cnn, string sql, Action map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id") + public static IEnumerable Query(this IDbConnection cnn, string sql, Action map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null) { - return MultiMap(cnn, sql, map, param, transaction, buffered, splitOn); + return MultiMap(cnn, sql, map, param, transaction, buffered, splitOn, commandTimeout); } - public static IEnumerable Query(this IDbConnection cnn, string sql, Action map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id") + public static IEnumerable Query(this IDbConnection cnn, string sql, Action map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null) { - return MultiMap(cnn, sql, map, param, transaction, buffered, splitOn); + return MultiMap(cnn, sql, map, param, transaction, buffered, splitOn, commandTimeout); } class DontMap {} - static IEnumerable MultiMap(this IDbConnection cnn, string sql, object map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id") + static IEnumerable MultiMap(this IDbConnection cnn, string sql, object map, object param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null) { - var identity = new Identity(sql, cnn, typeof(T), param == null ? null : param.GetType(), otherTypes: new Type[] {typeof(T), typeof(U), typeof(V), typeof(Z), typeof(X) }); + var identity = new Identity(sql, cnn, typeof(T), param == null ? null : param.GetType(), otherTypes: new[] {typeof(T), typeof(U), typeof(V), typeof(Z), typeof(X) }); var info = GetCacheInfo(param, identity); - using (var cmd = SetupCommand(cnn, transaction, sql, info.ParamReader, param)) + using (var cmd = SetupCommand(cnn, transaction, sql, info.ParamReader, param, commandTimeout)) { using (var reader = cmd.ExecuteReader()) { @@ -264,7 +256,7 @@ class DontMap {} return pos; }; - List otherDeserializer = new List(); + var otherDeserializer = new List(); split = nextSplit(); info.Deserializer = GetDeserializer(identity, reader, 0, split); @@ -353,10 +345,11 @@ class DontMap {} } } - while (reader.Read()) - { - yield return mapIt(reader); - } + if (mapIt!=null) + while (reader.Read()) + { + yield return mapIt(reader); + } } } } @@ -393,20 +386,20 @@ private static CacheInfo GetCacheInfo(object param, Identity identity) oDeserializer = GetStructDeserializer(reader); } - Func deserializer = (Func)oDeserializer; + var deserializer = (Func)oDeserializer; return deserializer; } - private static object GetDynamicDeserializer(IDataReader reader, int startBound = 0, int length = -1, bool returnNullIfFirstMissing = false) + private static object GetDynamicDeserializer(IDataRecord reader, int startBound = 0, int length = -1, bool returnNullIfFirstMissing = false) { - List colNames = new List(); + var colNames = new List(); if (length == -1) { length = reader.FieldCount - startBound; } - for (int i = startBound; i < startBound + length; i++) + for (var i = startBound; i < startBound + length; i++) { colNames.Add(reader.GetName(i)); } @@ -415,8 +408,8 @@ private static object GetDynamicDeserializer(IDataReader reader, int startBound r => { IDictionary row = new ExpandoObject(); - int i = startBound; - bool first = true; + var i = startBound; + var first = true; foreach (var colName in colNames) { var tmp = r.GetValue(i); @@ -451,7 +444,7 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj { count++; var listParam = command.CreateParameter(); - listParam.ParameterName = namePrefix + count.ToString(); + listParam.ParameterName = string.Format("{0}{1}", namePrefix, count); listParam.Value = item ?? DBNull.Value; if (isString) { @@ -480,7 +473,7 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj } private static Action CreateParamInfoGenerator(Type type) { - DynamicMethod dm = new DynamicMethod("ParamInfo" + Guid.NewGuid().ToString(), null, new Type[] { typeof(IDbCommand), typeof(object) }, type, true); + var dm = new DynamicMethod(string.Format("ParamInfo{0}", Guid.NewGuid()), null, new[] { typeof(IDbCommand), typeof(object) }, type, true); var il = dm.GetILGenerator(); @@ -606,12 +599,14 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj return (Action)dm.CreateDelegate(typeof(Action)); } - private static IDbCommand SetupCommand(IDbConnection cnn, IDbTransaction transaction, string sql, Action paramReader, object obj) + private static IDbCommand SetupCommand(IDbConnection cnn, IDbTransaction transaction, string sql, Action paramReader, object obj, int? commandTimeout) { var cmd = cnn.CreateCommand(); cmd.Transaction = transaction; cmd.CommandText = sql; + if (commandTimeout.HasValue) + cmd.CommandTimeout = commandTimeout.Value; if (paramReader != null) { paramReader(cmd, obj); @@ -620,9 +615,9 @@ private static IDbCommand SetupCommand(IDbConnection cnn, IDbTransaction transac } - private static int ExecuteCommand(IDbConnection cnn, IDbTransaction tranaction, string sql, Action paramReader, object obj) + private static int ExecuteCommand(IDbConnection cnn, IDbTransaction tranaction, string sql, Action paramReader, object obj, int? commandTimeout) { - using (var cmd = SetupCommand(cnn, tranaction, sql, paramReader, obj)) + using (var cmd = SetupCommand(cnn, tranaction, sql, paramReader, obj, commandTimeout)) { return cmd.ExecuteNonQuery(); } @@ -646,15 +641,15 @@ private static object GetStructDeserializer(IDataReader reader) public static Func GetClassDeserializer(IDataReader reader, int startBound = 0, int length = -1, bool returnNullIfFirstMissing = false) { - DynamicMethod dm = new DynamicMethod("Deserialize" + Guid.NewGuid().ToString(), typeof(T), new Type[] { typeof(IDataReader) }, true); + var dm = new DynamicMethod(string.Format("Deserialize{0}", Guid.NewGuid()), typeof(T), new[] { typeof(IDataReader) }, true); var il = dm.GetILGenerator(); var properties = typeof(T) .GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) .Select(p => new - { - Name = p.Name, + { + p.Name, Setter = p.DeclaringType == typeof(T) ? p.GetSetMethod(true) : p.DeclaringType.GetProperty(p.Name).GetSetMethod(true), Type = p.PropertyType }) -- GitLab