Skip to content

Commit

Permalink
Contrib Insert support for short/int16 id/key Issue #196
Browse files Browse the repository at this point in the history
  • Loading branch information
johandanforth committed Jun 7, 2015
1 parent e408fab commit c8ca11e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
7 changes: 7 additions & 0 deletions Dapper.Contrib.Tests/Assert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ public static void IsNull(this object obj)
throw new ApplicationException("Expected null");
}
}
public static void IsNotNull(this object obj)
{
if (obj == null)
{
throw new ApplicationException("Expected not null");
}
}

}
}
1 change: 1 addition & 0 deletions Dapper.Contrib.Tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ private static void Setup()
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) ");
Expand Down
40 changes: 34 additions & 6 deletions Dapper.Contrib.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ public class Person
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
{
Expand Down Expand Up @@ -71,6 +80,30 @@ private IDbConnection GetConnection()
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())
Expand All @@ -96,14 +129,9 @@ public void TestSimpleGet()
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);
Expand Down
22 changes: 15 additions & 7 deletions Dapper.Contrib/SqlMapperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private static string GetTableName(Type type)
{
string name;
if (TypeTableName.TryGetValue(type.TypeHandle, out name)) return name;

if (TableNameMapper != null)
{
name = TableNameMapper(type);
Expand All @@ -246,7 +246,7 @@ private static string GetTableName(Type type)
return name;
}


/// <summary>
/// Inserts an entity into table "Ts" and returns identity id or number if inserted rows if inserting a list.
/// </summary>
Expand Down Expand Up @@ -418,7 +418,7 @@ private static string GetTableName(Type type)
/// 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)
{
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c8ca11e

Please sign in to comment.