提交 e414149f 编写于 作者: M Marc Gravell

Implement connection-string equivalence (similar to...

Implement connection-string equivalence (similar to https://github.com/SamSaffron/dapper-dot-net/pull/80)

Fix Issue131
上级 9ac9c401
......@@ -494,7 +494,7 @@ private Identity(string sql, CommandType? commandType, string connectionString,
hashCode = hashCode * 23 + (t == null ? 0 : t.GetHashCode());
}
}
hashCode = hashCode * 23 + (connectionString == null ? 0 : connectionString.GetHashCode());
hashCode = hashCode * 23 + (connectionString == null ? 0 : SqlMapper.connectionStringComparer.GetHashCode(connectionString));
hashCode = hashCode * 23 + (parametersType == null ? 0 : parametersType.GetHashCode());
}
}
......@@ -554,7 +554,7 @@ public bool Equals(Identity other)
type == other.type &&
sql == other.sql &&
commandType == other.commandType &&
connectionString == other.connectionString &&
SqlMapper.connectionStringComparer.Equals(connectionString, other.connectionString) &&
parametersType == other.parametersType;
}
}
......@@ -898,7 +898,7 @@ private static IEnumerable<T> QueryInternal<T>(this IDbConnection cnn, string sq
#if CSHARP30
this IDbConnection cnn, string sql, Func<TFirst, TSecond, TReturn> map, object param, IDbTransaction transaction, bool buffered, string splitOn, int? commandTimeout, CommandType? commandType
#else
this IDbConnection cnn, string sql, Func<TFirst, TSecond, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null
this IDbConnection cnn, string sql, Func<TFirst, TSecond, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null
#endif
)
{
......@@ -1519,7 +1519,7 @@ public object SetValue(string key, object value)
throw new ArgumentException("When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id", "splitOn");
}
var effectiveFieldCount = fieldCount - startBound;
var effectiveFieldCount = Math.Min(fieldCount - startBound, length);
DapperTable table = null;
......@@ -2477,6 +2477,20 @@ private static void EmitInt32(ILGenerator il, int value)
}
}
/// <summary>
/// How should connection strings be compared for equivalence? Defaults to StringComparer.Ordinal.
/// Providing a custom implementation can be useful for allowing multi-tenancy databases with identical
/// schema to share startegies. Note that usual equivalence rules apply: any equivalent connection strings
/// <b>MUST</b> yield the same hash-code.
/// </summary>
public static IEqualityComparer<string> ConnectionStringComparer
{
get { return connectionStringComparer; }
set { connectionStringComparer = value ?? StringComparer.Ordinal; }
}
private static IEqualityComparer<string> connectionStringComparer = StringComparer.Ordinal;
/// <summary>
/// The grid reader provides interfaces for reading multiple result sets from a Dapper query
/// </summary>
......
......@@ -2211,6 +2211,41 @@ public void TestDynamicMutation()
}
}
public void TestIssue131()
{
var results = connection.Query<dynamic, int, dynamic>(
"SELECT 1 Id, 'Mr' Title, 'John' Surname, 4 AddressCount",
(person, addressCount) =>
{
return person;
},
splitOn: "AddressCount"
).FirstOrDefault();
var asDict = (IDictionary<string, object>)results;
asDict.ContainsKey("Id").IsEqualTo(true);
asDict.ContainsKey("Title").IsEqualTo(true);
asDict.ContainsKey("Surname").IsEqualTo(true);
asDict.ContainsKey("AddressCount").IsEqualTo(false);
}
// see http://stackoverflow.com/questions/13127886/dapper-returns-null-for-singleordefaultdatediff
public void TestNullFromInt_NoRows()
{
var result = connection.Query<int>( // case with rows
"select DATEDIFF(day, GETUTCDATE(), @date)", new { date = DateTime.UtcNow.AddDays(20) })
.SingleOrDefault();
result.IsEqualTo(20);
result = connection.Query<int>( // case without rows
"select DATEDIFF(day, GETUTCDATE(), @date) where 1 = 0", new { date = DateTime.UtcNow.AddDays(20) })
.SingleOrDefault();
result.IsEqualTo(0); // zero rows; default of int over zero rows is zero
}
class TransactedConnection : IDbConnection
{
IDbConnection _conn;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册