Commit b3927ce1 authored by Marc Gravell's avatar Marc Gravell

don't rely on First() on a dictionary for default feature support

parent d0f880d1
...@@ -4435,28 +4435,27 @@ public void AddParameter(IDbCommand command, string name) ...@@ -4435,28 +4435,27 @@ public void AddParameter(IDbCommand command, string name)
/// </summary> /// </summary>
partial class FeatureSupport partial class FeatureSupport
{ {
/// <summary> private static readonly FeatureSupport
/// Dictionary of supported features index by connection type name @default = new FeatureSupport(false),
/// </summary> postgres = new FeatureSupport(true);
private static readonly Dictionary<string, FeatureSupport> FeatureList = new Dictionary<string, FeatureSupport>(StringComparer.InvariantCultureIgnoreCase) {
{"sqlserverconnection", new FeatureSupport { Arrays = false}},
{"npgsqlconnection", new FeatureSupport {Arrays = true}}
};
/// <summary> /// <summary>
/// Gets the featureset based on the passed connection /// Gets the featureset based on the passed connection
/// </summary> /// </summary>
public static FeatureSupport Get(IDbConnection connection) public static FeatureSupport Get(IDbConnection connection)
{ {
string name = connection.GetType().Name; string name = connection == null ? null : connection.GetType().Name;
FeatureSupport features; if (string.Equals(name, "npgsqlconnection", StringComparison.InvariantCultureIgnoreCase)) return postgres;
return FeatureList.TryGetValue(name, out features) ? features : FeatureList.Values.First(); return @default;
}
private FeatureSupport(bool arrays)
{
Arrays = arrays;
} }
/// <summary> /// <summary>
/// True if the db supports array columns e.g. Postgresql /// True if the db supports array columns e.g. Postgresql
/// </summary> /// </summary>
public bool Arrays { get; set; } public bool Arrays { get; private set; }
} }
/// <summary> /// <summary>
......
...@@ -1292,10 +1292,10 @@ public void TestMultiMappingWithNonReturnedProperty() ...@@ -1292,10 +1292,10 @@ public void TestMultiMappingWithNonReturnedProperty()
2 as BlogId, 'Blog' as Title"; 2 as BlogId, 'Blog' as Title";
var postWithBlog = connection.Query<Post_DupeProp, Blog_DupeProp, Post_DupeProp>(sql, var postWithBlog = connection.Query<Post_DupeProp, Blog_DupeProp, Post_DupeProp>(sql,
(p, b) => (p, b) =>
{ {
p.Blog = b; p.Blog = b;
return p; return p;
}, splitOn: "BlogId").First(); }, splitOn: "BlogId").First();
postWithBlog.PostId.IsEqualTo(1); postWithBlog.PostId.IsEqualTo(1);
postWithBlog.Title.IsEqualTo("Title"); postWithBlog.Title.IsEqualTo("Title");
...@@ -1308,7 +1308,7 @@ class Post_DupeProp ...@@ -1308,7 +1308,7 @@ class Post_DupeProp
public int PostId { get; set; } public int PostId { get; set; }
public string Title { get; set; } public string Title { get; set; }
public int BlogId { get; set; } public int BlogId { get; set; }
public Blog_DupeProp Blog { get; set; } public Blog_DupeProp Blog { get; set; }
} }
class Blog_DupeProp class Blog_DupeProp
...@@ -2578,7 +2578,7 @@ public void TestNullFromInt_NoRows() ...@@ -2578,7 +2578,7 @@ public void TestNullFromInt_NoRows()
public void TestChangingDefaultStringTypeMappingToAnsiString() public void TestChangingDefaultStringTypeMappingToAnsiString()
{ {
var sql = "SELECT SQL_VARIANT_PROPERTY(CONVERT(sql_variant, @testParam),'BaseType') AS BaseType"; var sql = "SELECT SQL_VARIANT_PROPERTY(CONVERT(sql_variant, @testParam),'BaseType') AS BaseType";
var param = new {testParam = "TestString"}; var param = new { testParam = "TestString" };
var result01 = connection.Query<string>(sql, param).FirstOrDefault(); var result01 = connection.Query<string>(sql, param).FirstOrDefault();
result01.IsEqualTo("nvarchar"); result01.IsEqualTo("nvarchar");
...@@ -2913,7 +2913,7 @@ public void DataTableParameters() ...@@ -2913,7 +2913,7 @@ public void DataTableParameters()
{ {
connection.Query<int>("select count(1) from @ids", new { ids = table.AsTableValuedParameter() }).First(); connection.Query<int>("select count(1) from @ids", new { ids = table.AsTableValuedParameter() }).First();
throw new InvalidOperationException(); throw new InvalidOperationException();
} catch(Exception ex) } catch (Exception ex)
{ {
ex.Message.Equals("The table type parameter 'ids' must have a valid type name."); ex.Message.Equals("The table type parameter 'ids' must have a valid type name.");
} }
...@@ -2922,7 +2922,7 @@ public void DataTableParameters() ...@@ -2922,7 +2922,7 @@ public void DataTableParameters()
public void DataTableParametersWithExtendedProperty() public void DataTableParametersWithExtendedProperty()
{ {
try { connection.Execute("drop proc #DataTableParameters"); } catch { } try { connection.Execute("drop proc #DataTableParameters"); } catch { }
try { connection.Execute("drop table #DataTableParameters"); } catch { } try { connection.Execute("drop table #DataTableParameters"); } catch { }
try { connection.Execute("drop type MyTVPType"); } catch { } try { connection.Execute("drop type MyTVPType"); } catch { }
connection.Execute("create type MyTVPType as table (id int)"); connection.Execute("create type MyTVPType as table (id int)");
connection.Execute("create proc #DataTableParameters @ids MyTVPType readonly as select count(1) from @ids"); connection.Execute("create proc #DataTableParameters @ids MyTVPType readonly as select count(1) from @ids");
...@@ -2982,7 +2982,7 @@ public void GuidIn_SO_24177902() ...@@ -2982,7 +2982,7 @@ public void GuidIn_SO_24177902()
class HazGeo class HazGeo
{ {
public int Id { get;set; } public int Id { get; set; }
public DbGeography Geo { get; set; } public DbGeography Geo { get; set; }
} }
public void DBGeography_SO24405645_SO24402424() public void DBGeography_SO24405645_SO24402424()
...@@ -3031,7 +3031,7 @@ public void TypeBasedViaTypeMulti() ...@@ -3031,7 +3031,7 @@ public void TypeBasedViaTypeMulti()
Type type = GetSomeType(); Type type = GetSomeType();
dynamic first, second; dynamic first, second;
using(var multi = connection.QueryMultiple("select @A as [A], @B as [B]; select @C as [A], @D as [B]", using (var multi = connection.QueryMultiple("select @A as [A], @B as [B]; select @C as [A], @D as [B]",
new { A = 123, B = "abc", C = 456, D = "def" })) new { A = 123, B = "abc", C = 456, D = "def" }))
{ {
first = multi.Read(type).Single(); first = multi.Read(type).Single();
...@@ -3059,14 +3059,14 @@ static Type GetSomeType() ...@@ -3059,14 +3059,14 @@ static Type GetSomeType()
} }
public class SomeType public class SomeType
{ {
public int A { get;set; } public int A { get; set; }
public string B { get;set; } public string B { get; set; }
} }
class WithInit : ISupportInitialize class WithInit : ISupportInitialize
{ {
public string Value { get; set; } public string Value { get; set; }
public int Flags { get;set; } public int Flags { get; set; }
void ISupportInitialize.BeginInit() void ISupportInitialize.BeginInit()
{ {
...@@ -3352,14 +3352,14 @@ public void TestBigIntForEverythingWorks_SqlLite() ...@@ -3352,14 +3352,14 @@ public void TestBigIntForEverythingWorks_SqlLite()
} }
private void TestBigIntForEverythingWorks_SqlLite_ByDataType<T>(string dbType) private void TestBigIntForEverythingWorks_SqlLite_ByDataType<T>(string dbType)
{ {
using(var reader = connection.ExecuteReader("select cast(1 as " + dbType + ")")) using (var reader = connection.ExecuteReader("select cast(1 as " + dbType + ")"))
{ {
reader.Read().IsTrue(); reader.Read().IsTrue();
reader.GetFieldType(0).Equals(typeof(T)); reader.GetFieldType(0).Equals(typeof(T));
reader.Read().IsFalse(); reader.Read().IsFalse();
reader.NextResult().IsFalse(); reader.NextResult().IsFalse();
} }
string sql = "select " + string.Join(",", typeof(LotsOfNumerics).GetProperties().Select( string sql = "select " + string.Join(",", typeof(LotsOfNumerics).GetProperties().Select(
x => "cast (1 as " + dbType + ") as [" + x.Name + "]")); x => "cast (1 as " + dbType + ") as [" + x.Name + "]"));
var row = connection.Query<LotsOfNumerics>(sql).Single(); var row = connection.Query<LotsOfNumerics>(sql).Single();
...@@ -3482,6 +3482,29 @@ public class HazX ...@@ -3482,6 +3482,29 @@ public class HazX
} }
public void SO25297173_DynamicIn()
{
var query = @"
declare @table table(value int not null);
insert @table values(1);
insert @table values(2);
insert @table values(3);
insert @table values(4);
insert @table values(5);
insert @table values(6);
insert @table values(7);
SELECT value FROM @table WHERE value IN @myIds";
var queryParams = new Dictionary<string, object> {
{ "myIds", new [] { 5, 6 } }
};
var dynamicParams = new DynamicParameters(queryParams);
List<int> result = connection.Query<int>(query, dynamicParams).ToList();
result.Count.IsEqualTo(2);
result.Contains(5).IsTrue();
result.Contains(6).IsTrue();
}
#if POSTGRESQL #if POSTGRESQL
class Cat class Cat
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment