Commit e408faba authored by johandanforth's avatar johandanforth

Contrib Insert now handles closed connection

parent 62cb035d
...@@ -96,6 +96,21 @@ public void TestSimpleGet() ...@@ -96,6 +96,21 @@ public void TestSimpleGet()
connection.Delete(user); 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);
var users = connection.GetAll<User>();
users.Count().IsMoreThan(0);
}
}
public void InsertList() public void InsertList()
{ {
...@@ -208,7 +223,7 @@ public void InsertGetUpdate() ...@@ -208,7 +223,7 @@ public void InsertGetUpdate()
public void InsertWithCustomDbType() public void InsertWithCustomDbType()
{ {
SqlMapperExtensions.GetDatabaseType = connection => "SQLiteConnection"; SqlMapperExtensions.GetDatabaseType = conn => "SQLiteConnection";
bool sqliteCodeCalled = false; bool sqliteCodeCalled = false;
using (var connection = GetOpenConnection()) using (var connection = GetOpenConnection())
......
...@@ -289,16 +289,24 @@ private static string GetTableName(Type type) ...@@ -289,16 +289,24 @@ private static string GetTableName(Type type)
sbParameterList.Append(", "); sbParameterList.Append(", ");
} }
int returnVal;
var wasClosed = connection.State == ConnectionState.Closed;
if (wasClosed) connection.Open();
if (!isList) //single entity if (!isList) //single entity
{ {
var adapter = GetFormatter(connection); var adapter = GetFormatter(connection);
return adapter.Insert(connection, transaction, commandTimeout, name, sbColumnList.ToString(), returnVal = adapter.Insert(connection, transaction, commandTimeout, name, sbColumnList.ToString(),
sbParameterList.ToString(), keyProperties, entityToInsert); sbParameterList.ToString(), keyProperties, entityToInsert);
} }
else
//insert list of entities {
var cmd = String.Format("insert into {0} ({1}) values ({2})", name, sbColumnList, sbParameterList); //insert list of entities
return connection.Execute(cmd, entityToInsert, transaction, commandTimeout); var cmd = String.Format("insert into {0} ({1}) values ({2})", name, sbColumnList, sbParameterList);
returnVal = connection.Execute(cmd, entityToInsert, transaction, commandTimeout);
}
if (wasClosed) connection.Close();
return returnVal;
} }
/// <summary> /// <summary>
...@@ -625,6 +633,8 @@ public partial class SqlServerAdapter : ISqlAdapter ...@@ -625,6 +633,8 @@ 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?
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);
......
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