Skip to content

Commit ec06753

Browse files
committed
Copy collection properties to arrays when creating concrete type
1 parent e8f513a commit ec06753

9 files changed

Lines changed: 221 additions & 64 deletions

File tree

Simple.Data.Ado/AdoAdapterInserter.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@ public IEnumerable<IDictionary<string, object>> InsertMany(string tableName, IEn
5050
public IDictionary<string, object> Insert(string tableName, IEnumerable<KeyValuePair<string, object>> data, bool resultRequired)
5151
{
5252
var table = _adapter.GetSchema().FindTable(tableName);
53-
54-
CheckInsertablePropertiesAreAvailable(table, data);
53+
var dataArray = data.ToArray();
54+
CheckInsertablePropertiesAreAvailable(table, dataArray);
5555

5656
var customInserter = _adapter.ProviderHelper.GetCustomProvider<ICustomInserter>(_adapter.ConnectionProvider);
5757
if (customInserter != null)
5858
{
59-
return customInserter.Insert(_adapter, tableName, data.ToDictionary(), _transaction);
59+
return customInserter.Insert(_adapter, tableName, dataArray.ToDictionary(), _transaction);
6060
}
6161

62-
var dataDictionary = data.Where(kvp => table.HasColumn(kvp.Key) && !table.FindColumn(kvp.Key).IsIdentity)
62+
var dataDictionary = dataArray.Where(kvp => table.HasColumn(kvp.Key) && table.FindColumn(kvp.Key).IsWriteable)
6363
.ToDictionary(kvp => table.FindColumn(kvp.Key), kvp => kvp.Value);
6464

6565
string columnList = dataDictionary.Keys.Select(c => c.QuotedName).Aggregate((agg, next) => agg + "," + next);
@@ -83,11 +83,8 @@ public IDictionary<string, object> Insert(string tableName, IEnumerable<KeyValue
8383
insertSql += "; " + selectSql;
8484
return ExecuteSingletonQuery(insertSql, dataDictionary.Keys, dataDictionary.Values);
8585
}
86-
else
87-
{
88-
return ExecuteSingletonQuery(insertSql, selectSql, dataDictionary.Keys,
89-
dataDictionary.Values);
90-
}
86+
return ExecuteSingletonQuery(insertSql, selectSql, dataDictionary.Keys,
87+
dataDictionary.Values);
9188
}
9289
}
9390
}

Simple.Data.Ado/BulkInserter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class BulkInserter : IBulkInserter
1111
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)
1212
{
1313
var table = adapter.GetSchema().FindTable(tableName);
14-
var columns = table.Columns.Where(c => !c.IsIdentity).ToList();
14+
var columns = table.Columns.Where(c => c.IsWriteable).ToList();
1515

1616
string columnList = string.Join(",", columns.Select(c => c.QuotedName));
1717
string valueList = string.Join(",", columns.Select(c => "?"));

Simple.Data.Ado/Schema/Column.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public bool IsIdentity
6969
get { return _isIdentity; }
7070
}
7171

72+
public virtual bool IsWriteable
73+
{
74+
get { return !IsIdentity; }
75+
}
76+
7277
public virtual bool IsBinary
7378
{
7479
get { return _dbType == DbType.Binary; }
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Simple.Data.InMemoryTest
7+
{
8+
using NUnit.Framework;
9+
10+
[TestFixture]
11+
public class CanAssignArray
12+
{
13+
[Test]
14+
public void InsertAndGetWithArrayPropertyShouldWork()
15+
{
16+
var adapter = new InMemoryAdapter();
17+
adapter.SetKeyColumn("Test", "Id");
18+
Database.UseMockAdapter(adapter);
19+
var db = Database.Open();
20+
db.Test.Insert(Id: 1, Names: new List<string> {"Alice", "Bob", "Charlie"});
21+
People record = db.Test.Get(1);
22+
Assert.IsNotNull(record);
23+
Assert.AreEqual(1, record.Id);
24+
Assert.AreEqual("Alice", record.Names[0]);
25+
Assert.AreEqual("Bob", record.Names[1]);
26+
Assert.AreEqual("Charlie", record.Names[2]);
27+
}
28+
}
29+
30+
class People
31+
{
32+
public int Id { get; set; }
33+
public string[] Names { get; set; }
34+
}
35+
}

Simple.Data.InMemoryTest/Simple.Data.InMemoryTest.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
44
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -56,7 +56,9 @@
5656
</Reference>
5757
</ItemGroup>
5858
<ItemGroup>
59+
<Compile Include="CanAssignArray.cs" />
5960
<Compile Include="InMemoryTests.cs" />
61+
<Compile Include="JoinTests.cs" />
6062
<Compile Include="Properties\AssemblyInfo.cs" />
6163
</ItemGroup>
6264
<ItemGroup>

Simple.Data.SqlServer/SqlColumn.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,10 @@ public override bool IsBinary
3939
SqlDbType == SqlDbType.VarBinary;
4040
}
4141
}
42+
43+
public override bool IsWriteable
44+
{
45+
get { return (!IsIdentity) && SqlDbType != SqlDbType.Timestamp; }
46+
}
4247
}
4348
}

Simple.Data.SqlTest/InsertTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,5 +262,14 @@ public void TestInsertWithVarBinaryMaxColumn()
262262
blob = db.Blobs.FindById(1);
263263
Assert.IsTrue(image.SequenceEqual(blob.Data));
264264
}
265+
266+
[Test]
267+
public void TestInsertWithTimestampColumn()
268+
{
269+
var db = DatabaseHelper.Open();
270+
var row = db.TimestampTest.Insert(Description: "Foo");
271+
Assert.IsNotNull(row);
272+
Assert.IsInstanceOf<byte[]>(row.Version);
273+
}
265274
}
266275
}

Simple.Data.SqlTest/Resources/DatabaseReset.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ BEGIN
7070
DROP TABLE [dbo].[GeometryTest]
7171
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[HierarchyIdTest]') AND type in (N'U'))
7272
DROP TABLE [dbo].[HierarchyIdTest]
73+
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TimestampTest]') AND type in (N'U'))
74+
DROP TABLE [dbo].[TimestampTest]
7375

7476
CREATE TABLE [dbo].[Users] (
7577
[Id] INT IDENTITY (1, 1) NOT NULL,
@@ -207,6 +209,15 @@ BEGIN
207209
[Id] ASC
208210
))
209211

212+
CREATE TABLE [dbo].[TimestampTest](
213+
[Id] [int] IDENTITY(1,1) NOT NULL,
214+
[Description] [nvarchar](50),
215+
[Version] [timestamp],
216+
CONSTRAINT [PK_TimestampTest] PRIMARY KEY CLUSTERED
217+
(
218+
[Id] ASC
219+
))
220+
210221
BEGIN TRANSACTION
211222
SET IDENTITY_INSERT [dbo].[Customers] ON
212223
INSERT INTO [dbo].[Customers] ([CustomerId], [Name], [Address]) VALUES (1, N'Test', N'100 Road')

0 commit comments

Comments
 (0)