Permalink
Browse files

Copy collection properties to arrays when creating concrete type

  • Loading branch information...
markrendle committed Mar 28, 2012
1 parent e8f513a commit ec067535a2c320b09218c3d368fec465cb29ee30
@@ -50,16 +50,16 @@ public AdoAdapterInserter(AdoAdapter adapter, IDbTransaction transaction)
public IDictionary<string, object> Insert(string tableName, IEnumerable<KeyValuePair<string, object>> data, bool resultRequired)
{
var table = _adapter.GetSchema().FindTable(tableName);
CheckInsertablePropertiesAreAvailable(table, data);
var dataArray = data.ToArray();
CheckInsertablePropertiesAreAvailable(table, dataArray);
var customInserter = _adapter.ProviderHelper.GetCustomProvider<ICustomInserter>(_adapter.ConnectionProvider);
if (customInserter != null)
{
return customInserter.Insert(_adapter, tableName, data.ToDictionary(), _transaction);
return customInserter.Insert(_adapter, tableName, dataArray.ToDictionary(), _transaction);
}
var dataDictionary = data.Where(kvp => table.HasColumn(kvp.Key) && !table.FindColumn(kvp.Key).IsIdentity)
var dataDictionary = dataArray.Where(kvp => table.HasColumn(kvp.Key) && table.FindColumn(kvp.Key).IsWriteable)
.ToDictionary(kvp => table.FindColumn(kvp.Key), kvp => kvp.Value);
string columnList = dataDictionary.Keys.Select(c => c.QuotedName).Aggregate((agg, next) => agg + "," + next);
@@ -83,11 +83,8 @@ public AdoAdapterInserter(AdoAdapter adapter, IDbTransaction transaction)
insertSql += "; " + selectSql;
return ExecuteSingletonQuery(insertSql, dataDictionary.Keys, dataDictionary.Values);
}
else
{
return ExecuteSingletonQuery(insertSql, selectSql, dataDictionary.Keys,
dataDictionary.Values);
}
return ExecuteSingletonQuery(insertSql, selectSql, dataDictionary.Keys,
dataDictionary.Values);
}
}
}
@@ -11,7 +11,7 @@ public class BulkInserter : IBulkInserter
public IEnumerable<IDictionary<string, object>> Insert(AdoAdapter adapter, string tableName, IEnumerable<IDictionary<string, object>> data, IDbTransaction transaction, Func<IDictionary<string,object>, Exception, bool> onError, bool resultRequired)
{
var table = adapter.GetSchema().FindTable(tableName);
var columns = table.Columns.Where(c => !c.IsIdentity).ToList();
var columns = table.Columns.Where(c => c.IsWriteable).ToList();
string columnList = string.Join(",", columns.Select(c => c.QuotedName));
string valueList = string.Join(",", columns.Select(c => "?"));
@@ -69,6 +69,11 @@ public bool IsIdentity
get { return _isIdentity; }
}
public virtual bool IsWriteable
{
get { return !IsIdentity; }
}
public virtual bool IsBinary
{
get { return _dbType == DbType.Binary; }
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Simple.Data.InMemoryTest
{
using NUnit.Framework;
[TestFixture]
public class CanAssignArray
{
[Test]
public void InsertAndGetWithArrayPropertyShouldWork()
{
var adapter = new InMemoryAdapter();
adapter.SetKeyColumn("Test", "Id");
Database.UseMockAdapter(adapter);
var db = Database.Open();
db.Test.Insert(Id: 1, Names: new List<string> {"Alice", "Bob", "Charlie"});
People record = db.Test.Get(1);
Assert.IsNotNull(record);
Assert.AreEqual(1, record.Id);
Assert.AreEqual("Alice", record.Names[0]);
Assert.AreEqual("Bob", record.Names[1]);
Assert.AreEqual("Charlie", record.Names[2]);
}
}
class People
{
public int Id { get; set; }
public string[] Names { get; set; }
}
}
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -56,7 +56,9 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="CanAssignArray.cs" />
<Compile Include="InMemoryTests.cs" />
<Compile Include="JoinTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
@@ -39,5 +39,10 @@ public override bool IsBinary
SqlDbType == SqlDbType.VarBinary;
}
}
public override bool IsWriteable
{
get { return (!IsIdentity) && SqlDbType != SqlDbType.Timestamp; }
}
}
}
@@ -262,5 +262,14 @@ public void TestInsertWithVarBinaryMaxColumn()
blob = db.Blobs.FindById(1);
Assert.IsTrue(image.SequenceEqual(blob.Data));
}
[Test]
public void TestInsertWithTimestampColumn()
{
var db = DatabaseHelper.Open();
var row = db.TimestampTest.Insert(Description: "Foo");
Assert.IsNotNull(row);
Assert.IsInstanceOf<byte[]>(row.Version);
}
}
}
@@ -70,6 +70,8 @@ BEGIN
DROP TABLE [dbo].[GeometryTest]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[HierarchyIdTest]') AND type in (N'U'))
DROP TABLE [dbo].[HierarchyIdTest]
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TimestampTest]') AND type in (N'U'))
DROP TABLE [dbo].[TimestampTest]
CREATE TABLE [dbo].[Users] (
[Id] INT IDENTITY (1, 1) NOT NULL,
@@ -207,6 +209,15 @@ BEGIN
[Id] ASC
))
CREATE TABLE [dbo].[TimestampTest](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Description] [nvarchar](50),
[Version] [timestamp],
CONSTRAINT [PK_TimestampTest] PRIMARY KEY CLUSTERED
(
[Id] ASC
))
BEGIN TRANSACTION
SET IDENTITY_INSERT [dbo].[Customers] ON
INSERT INTO [dbo].[Customers] ([CustomerId], [Name], [Address]) VALUES (1, N'Test', N'100 Road')
Oops, something went wrong.

0 comments on commit ec06753

Please sign in to comment.