Commit c8ca11ef authored by johandanforth's avatar johandanforth

Contrib Insert support for short/int16 id/key Issue #196

parent e408faba
...@@ -64,6 +64,13 @@ public static void IsNull(this object obj) ...@@ -64,6 +64,13 @@ public static void IsNull(this object obj)
throw new ApplicationException("Expected null"); throw new ApplicationException("Expected null");
} }
} }
public static void IsNotNull(this object obj)
{
if (obj == null)
{
throw new ApplicationException("Expected not null");
}
}
} }
} }
\ No newline at end of file
...@@ -28,6 +28,7 @@ private static void Setup() ...@@ -28,6 +28,7 @@ private static void Setup()
using (var connection = new SqlCeConnection(connectionString)) using (var connection = new SqlCeConnection(connectionString))
{ {
connection.Open(); 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 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 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 Automobiles (Id int IDENTITY(1,1) not null, Name nvarchar(100) not null) ");
......
...@@ -33,6 +33,15 @@ public class Person ...@@ -33,6 +33,15 @@ public class Person
public string Name { get; set; } 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")] [Table("Automobiles")]
public class Car public class Car
{ {
...@@ -71,6 +80,30 @@ private IDbConnection GetConnection() ...@@ -71,6 +80,30 @@ private IDbConnection GetConnection()
return connection; 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() public void TableName()
{ {
using (var connection = GetOpenConnection()) using (var connection = GetOpenConnection())
...@@ -96,14 +129,9 @@ public void TestSimpleGet() ...@@ -96,14 +129,9 @@ public void TestSimpleGet()
connection.Delete(user); connection.Delete(user);
} }
} }
public void TestClosedConnection() public void TestClosedConnection()
{ {
//using (var connection = GetOpenConnection())
//{
// connection.Insert(new User { Name = "Adama", Age = 10 }).IsMoreThan(0);
//}
using (var connection = GetConnection()) using (var connection = GetConnection())
{ {
connection.Insert(new User { Name = "Adama", Age = 10 }).IsMoreThan(0); connection.Insert(new User { Name = "Adama", Age = 10 }).IsMoreThan(0);
......
...@@ -223,7 +223,7 @@ private static string GetTableName(Type type) ...@@ -223,7 +223,7 @@ private static string GetTableName(Type type)
{ {
string name; string name;
if (TypeTableName.TryGetValue(type.TypeHandle, out name)) return name; if (TypeTableName.TryGetValue(type.TypeHandle, out name)) return name;
if (TableNameMapper != null) if (TableNameMapper != null)
{ {
name = TableNameMapper(type); name = TableNameMapper(type);
...@@ -246,7 +246,7 @@ private static string GetTableName(Type type) ...@@ -246,7 +246,7 @@ private static string GetTableName(Type type)
return name; return name;
} }
/// <summary> /// <summary>
/// Inserts an entity into table "Ts" and returns identity id or number if inserted rows if inserting a list. /// Inserts an entity into table "Ts" and returns identity id or number if inserted rows if inserting a list.
/// </summary> /// </summary>
...@@ -418,7 +418,7 @@ private static string GetTableName(Type type) ...@@ -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. /// Please note that this callback is global and will be used by all the calls that require a database specific adapter.
/// </summary> /// </summary>
public static GetDatabaseTypeDelegate GetDatabaseType; public static GetDatabaseTypeDelegate GetDatabaseType;
private static ISqlAdapter GetFormatter(IDbConnection connection) private static ISqlAdapter GetFormatter(IDbConnection connection)
{ {
...@@ -634,21 +634,29 @@ public partial class SqlServerAdapter : ISqlAdapter ...@@ -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) 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: 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); var cmd = String.Format("insert into {0} ({1}) values ({2})", tableName, columnList, parameterList);
connection.Execute(cmd, entityToInsert, transaction, commandTimeout); 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 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(); var propertyInfos = keyProperties as PropertyInfo[] ?? keyProperties.ToArray();
if (propertyInfos.Any()) 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 partial class PostgresAdapter : ISqlAdapter
{ {
public int Insert(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, String tableName, string columnList, string parameterList, IEnumerable<PropertyInfo> keyProperties, object entityToInsert) public int Insert(IDbConnection connection, IDbTransaction transaction, int? commandTimeout, String tableName, string columnList, string parameterList, IEnumerable<PropertyInfo> keyProperties, object 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