Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions Dapper.Contrib NET45/SqlMapperExtensionsAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,21 @@ public static async Task<T> GetAsync<T>(this IDbConnection connection, dynamic i
if (!GetQueries.TryGetValue(type.TypeHandle, out sql))
{
var keys = KeyPropertiesCache(type);
var explicitKeys = ExplicitKeyPropertiesCache(type);
if (keys.Count() > 1)
throw new DataException("Get<T> only supports an entity with a single [Key] property");
if (!keys.Any())
throw new DataException("Get<T> only supports en entity with a [Key] property");

var onlyKey = keys.First();

var key = keys.Any() ? keys.First() : explicitKeys.First();
var name = GetTableName(type);
var colsList = ColumnListForSelect(connection, type);

var keyColumn = ColumnNamesCache(type, key.Name);

// TODO: query information schema and only select fields that are both in information schema and underlying class / interface
sql = "select * from " + name + " where " + onlyKey.Name + " = @id";
sql = "select " + colsList + " from " + name + " where " + keyColumn + " = @id";

GetQueries[type.TypeHandle] = sql;
}

Expand Down Expand Up @@ -96,8 +100,19 @@ public static async Task<IEnumerable<T>> GetAllAsync<T>(this IDbConnection conne

var name = GetTableName(type);

ISqlAdapter adapter = GetFormatter(connection);
var props = TypePropertiesCache(type);
var colsList = new StringBuilder();
foreach (var prop in props)
{
if (colsList.Length > 0) colsList.Append(", ");
adapter.AppendColumnName(colsList, ColumnNamesCache(type, prop.Name));
colsList.Append(" as ");
adapter.AppendColumnName(colsList, prop.Name);
}

// TODO: query information schema and only select fields that are both in information schema and underlying class / interface
sql = "select * from " + name;
sql = "select " + colsList.ToString() + " from " + name;
GetQueries[cacheType.TypeHandle] = sql;
}

Expand Down Expand Up @@ -149,10 +164,13 @@ public static async Task<int> InsertAsync<T>(this IDbConnection connection, T en
var computedProperties = ComputedPropertiesCache(type);
var allPropertiesExceptKeyAndComputed = allProperties.Except(keyProperties.Union(computedProperties)).ToList();

var adapter = GetFormatter(connection);

for (var i = 0; i < allPropertiesExceptKeyAndComputed.Count(); i++)
{
var property = allPropertiesExceptKeyAndComputed.ElementAt(i);
sbColumnList.AppendFormat("[{0}]", property.Name);
var column = ColumnNamesCache(type, property.Name);
adapter.AppendColumnName(sbColumnList, column);
if (i < allPropertiesExceptKeyAndComputed.Count() - 1)
sbColumnList.Append(", ");
}
Expand Down Expand Up @@ -214,18 +232,22 @@ public static async Task<bool> UpdateAsync<T>(this IDbConnection connection, T e
var computedProperties = ComputedPropertiesCache(type);
var nonIdProps = allProperties.Except(keyProperties.Union(computedProperties)).ToList();

var adapter = GetFormatter(connection);

for (var i = 0; i < nonIdProps.Count(); i++)
{
var property = nonIdProps.ElementAt(i);
sb.AppendFormat("{0} = @{1}", property.Name, property.Name);
var column = ColumnNamesCache(type, property.Name);
adapter.AppendColumnNameEqualsValue(sb, column, property.Name);
if (i < nonIdProps.Count() - 1)
sb.AppendFormat(", ");
}
sb.Append(" where ");
for (var i = 0; i < keyProperties.Count(); i++)
{
var property = keyProperties.ElementAt(i);
sb.AppendFormat("{0} = @{1}", property.Name, property.Name);
var column = ColumnNamesCache(type, property.Name);
adapter.AppendColumnNameEqualsValue(sb, column, property.Name);
if (i < keyProperties.Count() - 1)
sb.AppendFormat(" and ");
}
Expand Down Expand Up @@ -261,10 +283,13 @@ public static async Task<bool> DeleteAsync<T>(this IDbConnection connection, T e
var sb = new StringBuilder();
sb.AppendFormat("delete from {0} where ", name);

var adapter = GetFormatter(connection);

for (var i = 0; i < keyProperties.Count(); i++)
{
var property = keyProperties.ElementAt(i);
sb.AppendFormat("{0} = @{1}", property.Name, property.Name);
var column = ColumnNamesCache(type, property.Name);
adapter.AppendColumnNameEqualsValue(sb, column, property.Name); //fix for issue #336
if (i < keyProperties.Count() - 1)
sb.AppendFormat(" and ");
}
Expand Down
2 changes: 1 addition & 1 deletion Dapper.Contrib.Tests NET45/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private static void Setup()
connection.Execute(@" create table People (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null) ");
connection.Execute(@" create table Users (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null, Age int not null) ");
connection.Execute(@" create table Automobiles (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null) ");
connection.Execute(@" create table Results (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null, [Order] int not null) ");
connection.Execute(@" create table Results (ResultId int IDENTITY(1,1) not null, ResultName nvarchar(100) not null, [Order] int not null) ");
connection.Execute(@" create table ObjectX (ObjectXId nvarchar(100) not null, Name nvarchar(100) not null) ");
connection.Execute(@" create table ObjectY (ObjectYId int not null, Name nvarchar(100) not null) ");
}
Expand Down
16 changes: 16 additions & 0 deletions Dapper.Contrib.Tests NET45/TestsAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ public async Task TableNameAsync()
}
}

public async Task ColumnNameAsync()
{
using (var connection = GetOpenConnection())
{
await connection.DeleteAllAsync<Result>();

// tests against "Name" column (set by Column attribute on Result.FullName )
await connection.InsertAsync(new Result { Name = "Mike" });
(await connection.GetAsync<Result>(1)).Name.IsEqualTo("Mike");
(await connection.UpdateAsync(new Result() { Id = 1, Name = "Michael" })).IsEqualTo(true);
(await connection.GetAsync<Result>(1)).Name.IsEqualTo("Michael");
(await connection.DeleteAsync(new Result() { Id = 1 })).IsEqualTo(true);
(await connection.GetAsync<Result>(1)).IsNull();
}
}

public async Task TestSimpleGetAsync()
{
using (var connection = GetOpenConnection())
Expand Down
4 changes: 2 additions & 2 deletions Dapper.Contrib.Tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static void Main(string[] args)
{
Setup();
RunTests();
Console.WriteLine("Press any key...");
Console.WriteLine("Press any key...");
Console.ReadKey();
}

Expand All @@ -32,7 +32,7 @@ private static void Setup()
connection.Execute(@" create table People (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null) ");
connection.Execute(@" create table Users (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null, Age int not null) ");
connection.Execute(@" create table Automobiles (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null) ");
connection.Execute(@" create table Results (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null, [Order] int not null) ");
connection.Execute(@" create table Results (ResultId int IDENTITY(1,1) not null, ResultName nvarchar(100) not null, [Order] int not null) ");
connection.Execute(@" create table ObjectX (ObjectXId nvarchar(100) not null, Name nvarchar(100) not null) ");
connection.Execute(@" create table ObjectY (ObjectYId int not null, Name nvarchar(100) not null) ");
}
Expand Down
18 changes: 17 additions & 1 deletion Dapper.Contrib.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ public class Car
[Table("Results")]
public class Result
{
[Column("ResultId")]
public int Id { get; set; }
[Column("ResultName")]
public string Name { get; set; }
public int Order { get; set; }
}
Expand Down Expand Up @@ -191,6 +193,20 @@ public void TableName()
}
}

public void ColumnName()
{
using (var connection = GetOpenConnection())
{
// tests against "Name" column (set by Column attribute on Result.FullName )
connection.Insert(new Result { Name = "Mike" });
connection.Get<Result>(1).Name.IsEqualTo("Mike");
connection.Update(new Result() { Id = 1, Name = "Michael" }).IsEqualTo(true);
connection.Get<Result>(1).Name.IsEqualTo("Michael");
connection.Delete(new Result() { Id = 1 }).IsEqualTo(true);
connection.Get<Result>(1).IsNull();
}
}

public void TestSimpleGet()
{
using (var connection = GetOpenConnection())
Expand Down Expand Up @@ -342,7 +358,7 @@ public void InsertWithCustomDbType()
{
sqliteCodeCalled = ex.Message.IndexOf("There was an error parsing the query", StringComparison.InvariantCultureIgnoreCase) >= 0;
}
// ReSharper disable once EmptyGeneralCatchClause
// ReSharper disable once EmptyGeneralCatchClause
catch (Exception)
{
}
Expand Down
Loading