mirror of
https://github.com/DapperLib/Dapper.git
synced 2026-10-18 22:31:08 +08:00
Contrib Insert support for short/int16 id/key Issue #196
This commit is contained in:
@@ -64,6 +64,13 @@ namespace Dapper.Contrib.Tests
|
||||
throw new ApplicationException("Expected null");
|
||||
}
|
||||
}
|
||||
public static void IsNotNull(this object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
throw new ApplicationException("Expected not null");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@ namespace Dapper.Contrib.Tests
|
||||
using (var connection = new SqlCeConnection(connectionString))
|
||||
{
|
||||
connection.Open();
|
||||
connection.Execute(@" create table Stuff (TheId int IDENTITY(1,1) not null, Name nvarchar(100) not null, Created DateTime null) ");
|
||||
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) ");
|
||||
|
||||
@@ -33,6 +33,15 @@ namespace Dapper.Contrib.Tests
|
||||
public string Name { get; set; }
|
||||
}
|
||||
|
||||
[Table("Stuff")]
|
||||
public class Stuff
|
||||
{
|
||||
[Key]
|
||||
public short TheId { get; set; }
|
||||
public string Name { get; set; }
|
||||
public DateTime? Created { get; set; }
|
||||
}
|
||||
|
||||
[Table("Automobiles")]
|
||||
public class Car
|
||||
{
|
||||
@@ -71,6 +80,30 @@ namespace Dapper.Contrib.Tests
|
||||
return connection;
|
||||
}
|
||||
|
||||
public void ShortIdentity()
|
||||
{
|
||||
using (var connection = GetOpenConnection())
|
||||
{
|
||||
var id = connection.Insert(new Stuff() { Name = "First item" });
|
||||
id.IsEqualTo(1);
|
||||
var item = connection.Get<Stuff>(1);
|
||||
item.TheId.IsEqualTo((short)1);
|
||||
}
|
||||
}
|
||||
|
||||
public void NullDateTime()
|
||||
{
|
||||
using (var connection = GetOpenConnection())
|
||||
{
|
||||
connection.Insert(new Stuff() { Name = "First item" });
|
||||
connection.Insert(new Stuff() { Name = "Second item", Created = DateTime.Now });
|
||||
var stuff = connection.Query<Stuff>("select * from stuff").ToList();
|
||||
stuff.First().Created.IsNull();
|
||||
stuff.Last().Created.IsNotNull();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void TableName()
|
||||
{
|
||||
using (var connection = GetOpenConnection())
|
||||
@@ -96,14 +129,9 @@ namespace Dapper.Contrib.Tests
|
||||
connection.Delete(user);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void TestClosedConnection()
|
||||
{
|
||||
//using (var connection = GetOpenConnection())
|
||||
//{
|
||||
// connection.Insert(new User { Name = "Adama", Age = 10 }).IsMoreThan(0);
|
||||
//}
|
||||
|
||||
using (var connection = GetConnection())
|
||||
{
|
||||
connection.Insert(new User { Name = "Adama", Age = 10 }).IsMoreThan(0);
|
||||
|
||||
@@ -223,7 +223,7 @@ namespace Dapper.Contrib.Extensions
|
||||
{
|
||||
string name;
|
||||
if (TypeTableName.TryGetValue(type.TypeHandle, out name)) return name;
|
||||
|
||||
|
||||
if (TableNameMapper != null)
|
||||
{
|
||||
name = TableNameMapper(type);
|
||||
@@ -246,7 +246,7 @@ namespace Dapper.Contrib.Extensions
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Inserts an entity into table "Ts" and returns identity id or number if inserted rows if inserting a list.
|
||||
/// </summary>
|
||||
@@ -418,7 +418,7 @@ namespace Dapper.Contrib.Extensions
|
||||
/// Please note that this callback is global and will be used by all the calls that require a database specific adapter.
|
||||
/// </summary>
|
||||
public static GetDatabaseTypeDelegate GetDatabaseType;
|
||||
|
||||
|
||||
|
||||
private static ISqlAdapter GetFormatter(IDbConnection connection)
|
||||
{
|
||||
@@ -634,21 +634,29 @@ public partial class SqlServerAdapter : ISqlAdapter
|
||||
public int Insert(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, String tableName, string columnList, string parameterList, IEnumerable<PropertyInfo> keyProperties, object entityToInsert)
|
||||
{
|
||||
//TODO: Append a select identity at end if command?
|
||||
//TODO: Create specific sqladapter for sqlce
|
||||
|
||||
var cmd = String.Format("insert into {0} ({1}) values ({2})", tableName, columnList, parameterList);
|
||||
|
||||
connection.Execute(cmd, entityToInsert, transaction, commandTimeout);
|
||||
|
||||
//NOTE: would prefer to use IDENT_CURRENT('tablename') or IDENT_SCOPE but these are not available on SQLCE
|
||||
//TODO: use IDENT_CURRENT('tablename') or IDENT_SCOPE or SCOPE_IDENTITY() - create specific sqlce adapter with @@IDENTITY
|
||||
var r = connection.Query("select @@IDENTITY id", transaction: transaction, commandTimeout: commandTimeout);
|
||||
var id = (int)r.First().id;
|
||||
var id = r.First().id;
|
||||
var propertyInfos = keyProperties as PropertyInfo[] ?? keyProperties.ToArray();
|
||||
if (propertyInfos.Any())
|
||||
propertyInfos.First().SetValue(entityToInsert, id, null);
|
||||
return id;
|
||||
{
|
||||
var idProperty = propertyInfos.First();
|
||||
if (idProperty.PropertyType.Name == "Int16") //for short id/key types issue #196
|
||||
idProperty.SetValue(entityToInsert, (Int16)id, null);
|
||||
else
|
||||
idProperty.SetValue(entityToInsert, (int)id, null);
|
||||
}
|
||||
return (int)id;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public partial class PostgresAdapter : ISqlAdapter
|
||||
{
|
||||
public int Insert(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, String tableName, string columnList, string parameterList, IEnumerable<PropertyInfo> keyProperties, object entityToInsert)
|
||||
|
||||
Reference in New Issue
Block a user