Commit 63460c60 authored by johandanforth's avatar johandanforth

specify sqladapter for insert() and insertasync()

parent bb5a16ce
...@@ -129,7 +129,8 @@ public static partial class SqlMapperExtensions ...@@ -129,7 +129,8 @@ public static partial class SqlMapperExtensions
/// <param name="connection">Open SqlConnection</param> /// <param name="connection">Open SqlConnection</param>
/// <param name="entityToInsert">Entity to insert</param> /// <param name="entityToInsert">Entity to insert</param>
/// <returns>Identity of inserted entity</returns> /// <returns>Identity of inserted entity</returns>
public static async Task<int> InsertAsync<T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null, int? commandTimeout = null) where T : class public static async Task<int> InsertAsync<T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null,
int? commandTimeout = null, ISqlAdapter sqlAdapter = null) where T : class
{ {
var isList = false; var isList = false;
...@@ -167,8 +168,9 @@ public static partial class SqlMapperExtensions ...@@ -167,8 +168,9 @@ public static partial class SqlMapperExtensions
if (!isList) //single entity if (!isList) //single entity
{ {
var adapter = GetFormatter(connection); if (sqlAdapter == null)
return await adapter.InsertAsync(connection, transaction, commandTimeout, name, sbColumnList.ToString(), sqlAdapter = GetFormatter(connection);
return await sqlAdapter.InsertAsync(connection, transaction, commandTimeout, name, sbColumnList.ToString(),
sbParameterList.ToString(), keyProperties, entityToInsert); sbParameterList.ToString(), keyProperties, entityToInsert);
} }
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using Dapper;
using Dapper.Contrib.Extensions; using Dapper.Contrib.Extensions;
namespace Dapper.Contrib.Tests namespace Dapper.Contrib.Tests
...@@ -85,9 +86,12 @@ public async Task InsertGetUpdateAsync() ...@@ -85,9 +86,12 @@ public async Task InsertGetUpdateAsync()
(await connection.QueryAsync<User>("select * from Users")).Count().IsEqualTo(0); (await connection.QueryAsync<User>("select * from Users")).Count().IsEqualTo(0);
(await connection.UpdateAsync(notrackedUser)).IsEqualTo(false); //returns false, user not found (await connection.UpdateAsync(notrackedUser)).IsEqualTo(false); //returns false, user not found
(await connection.InsertAsync(new User { Name = "Adam", Age = 10 }, sqlAdapter: new SqlServerAdapter())).IsMoreThan(0);
} }
} }
public async Task InsertCheckKeyAsync() public async Task InsertCheckKeyAsync()
{ {
using (var connection = GetOpenConnection()) using (var connection = GetOpenConnection())
...@@ -271,4 +275,5 @@ public async Task DeleteAllAsync() ...@@ -271,4 +275,5 @@ public async Task DeleteAllAsync()
} }
} }
} }
} }
\ No newline at end of file
...@@ -25,6 +25,14 @@ public static void IsMoreThan(this int obj, int other) ...@@ -25,6 +25,14 @@ public static void IsMoreThan(this int obj, int other)
} }
} }
public static void IsMoreThan(this long obj, int other)
{
if (obj < other)
{
throw new ApplicationException(string.Format("{0} should be larger than {1}", obj, other));
}
}
public static void IsSequenceEqualTo<T>(this IEnumerable<T> obj, IEnumerable<T> other) public static void IsSequenceEqualTo<T>(this IEnumerable<T> obj, IEnumerable<T> other)
{ {
if (!obj.SequenceEqual(other)) if (!obj.SequenceEqual(other))
......
...@@ -2,10 +2,12 @@ ...@@ -2,10 +2,12 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Data.SqlServerCe; using System.Data.SqlServerCe;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Transactions; using System.Transactions;
using Dapper;
using Dapper.Contrib.Extensions; using Dapper.Contrib.Extensions;
namespace Dapper.Contrib.Tests namespace Dapper.Contrib.Tests
...@@ -196,9 +198,13 @@ public void InsertGetUpdate() ...@@ -196,9 +198,13 @@ public void InsertGetUpdate()
connection.Update(notrackedUser).IsEqualTo(false); //returns false, user not found connection.Update(notrackedUser).IsEqualTo(false); //returns false, user not found
//insert with custom sqladapter
connection.Insert(new User { Name = "Adam", Age = 10 }, sqlAdapter: new SqlServerAdapter()).IsMoreThan(0);
} }
} }
public void GetAll() public void GetAll()
{ {
const int numberOfEntities = 100; const int numberOfEntities = 100;
...@@ -341,4 +347,5 @@ public void DeleteAll() ...@@ -341,4 +347,5 @@ public void DeleteAll()
} }
} }
} }
\ No newline at end of file
...@@ -232,7 +232,8 @@ private static string GetTableName(Type type) ...@@ -232,7 +232,8 @@ private static string GetTableName(Type type)
/// <param name="connection">Open SqlConnection</param> /// <param name="connection">Open SqlConnection</param>
/// <param name="entityToInsert">Entity to insert, can be list of entities</param> /// <param name="entityToInsert">Entity to insert, can be list of entities</param>
/// <returns>Identity of inserted entity, or number of inserted rows if inserting a list</returns> /// <returns>Identity of inserted entity, or number of inserted rows if inserting a list</returns>
public static long Insert<T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null, int? commandTimeout = null) where T : class public static long Insert<T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null,
int? commandTimeout = null, ISqlAdapter sqlAdapter = null) where T : class
{ {
var isList = false; var isList = false;
...@@ -270,8 +271,9 @@ private static string GetTableName(Type type) ...@@ -270,8 +271,9 @@ private static string GetTableName(Type type)
if (!isList) //single entity if (!isList) //single entity
{ {
var adapter = GetFormatter(connection); if(sqlAdapter == null)
return adapter.Insert(connection, transaction, commandTimeout, name, sbColumnList.ToString(), sqlAdapter = GetFormatter(connection);
return sqlAdapter.Insert(connection, transaction, commandTimeout, name, sbColumnList.ToString(),
sbParameterList.ToString(), keyProperties, entityToInsert); sbParameterList.ToString(), keyProperties, entityToInsert);
} }
......
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