Skip to content

Commit 992d9c3

Browse files
Merge pull request #413 from snalesso/patch-3
Added support for any IEnumerable<KeyValuePair<string, object>> as values parameter
2 parents ab2a303 + 503ca6f commit 992d9c3

17 files changed

+366
-221
lines changed

QueryBuilder.Tests/GeneralTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public void CompilerSpecificLimit()
291291
var engines = new[] { EngineCodes.SqlServer, EngineCodes.MySql, EngineCodes.PostgreSql };
292292
var c = Compilers.Compile(engines, query);
293293

294-
Assert.Equal(2, query.GetComponents("limit").Count());
294+
Assert.Equal(2, query.GetComponents("limit").Count);
295295
Assert.Equal("SELECT TOP (5) * FROM [mytable]", c[EngineCodes.SqlServer].ToString());
296296
Assert.Equal("SELECT * FROM \"mytable\" LIMIT 10", c[EngineCodes.PostgreSql].ToString());
297297
Assert.Equal("SELECT * FROM `mytable`", c[EngineCodes.MySql].ToString());
@@ -319,7 +319,7 @@ public void CompilerSpecificOffset()
319319
var engines = new[] { EngineCodes.SqlServer, EngineCodes.MySql, EngineCodes.PostgreSql };
320320
var c = Compilers.Compile(engines, query);
321321

322-
Assert.Equal(2, query.GetComponents("offset").Count());
322+
Assert.Equal(2, query.GetComponents("offset").Count);
323323
Assert.Equal("SELECT * FROM `mytable` LIMIT 18446744073709551615 OFFSET 5", c[EngineCodes.MySql].ToString());
324324
Assert.Equal("SELECT * FROM \"mytable\" OFFSET 10", c[EngineCodes.PostgreSql].ToString());
325325
Assert.Equal("SELECT * FROM [mytable]", c[EngineCodes.SqlServer].ToString());

QueryBuilder.Tests/InfrastructureTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void ShouldThrowIfInvalidEngineCode()
4747
[Fact]
4848
public void ShouldThrowIfAnyEngineCodesAreInvalid()
4949
{
50-
var codes = new[] {EngineCodes.SqlServer, "123", EngineCodes.MySql, "abc"};
50+
var codes = new[] { EngineCodes.SqlServer, "123", EngineCodes.MySql, "abc" };
5151
Assert.Throws<InvalidOperationException>(() => Compilers.Compile(codes, new Query()));
5252
}
5353
}

QueryBuilder.Tests/InsertTests.cs

Lines changed: 156 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.Dynamic;
25
using System.Linq;
36
using SqlKata.Compilers;
47
using SqlKata.Tests.Infrastructure;
@@ -8,21 +11,44 @@ namespace SqlKata.Tests
811
{
912
public class InsertTests : TestSupport
1013
{
14+
private class Account
15+
{
16+
public Account(string name, string currency = null, string created_at = null, string color = null)
17+
{
18+
this.name = name ?? throw new ArgumentNullException(nameof(name));
19+
this.Currency = currency;
20+
this.color = color;
21+
}
22+
23+
public string name { get; set; }
24+
25+
[Column("currency_id")]
26+
public string Currency { get; set; }
27+
28+
[Ignore]
29+
public string color { get; set; }
30+
}
31+
1132
[Fact]
1233
public void InsertObject()
1334
{
14-
var query = new Query("Table").AsInsert(new
15-
{
16-
Name = "The User",
17-
Age = new DateTime(2018, 1, 1),
18-
});
35+
var query = new Query("Table")
36+
.AsInsert(
37+
new
38+
{
39+
Name = "The User",
40+
Age = new DateTime(2018, 1, 1),
41+
});
1942

2043
var c = Compile(query);
2144

22-
Assert.Equal("INSERT INTO [Table] ([Name], [Age]) VALUES ('The User', '2018-01-01')", c[EngineCodes.SqlServer]);
23-
45+
Assert.Equal(
46+
"INSERT INTO [Table] ([Name], [Age]) VALUES ('The User', '2018-01-01')",
47+
c[EngineCodes.SqlServer]);
2448

25-
Assert.Equal("INSERT INTO \"TABLE\" (\"NAME\", \"AGE\") VALUES ('The User', '2018-01-01')", c[EngineCodes.Firebird]);
49+
Assert.Equal(
50+
"INSERT INTO \"TABLE\" (\"NAME\", \"AGE\") VALUES ('The User', '2018-01-01')",
51+
c[EngineCodes.Firebird]);
2652
}
2753

2854
[Fact]
@@ -32,8 +58,7 @@ public void InsertFromSubQueryWithCte()
3258
.With("old_cards", new Query("all_cars").Where("year", "<", 2000))
3359
.AsInsert(
3460
new[] { "name", "model", "year" },
35-
new Query("old_cars").Where("price", ">", 100).ForPage(2, 10)
36-
);
61+
new Query("old_cars").Where("price", ">", 100).ForPage(2, 10));
3762

3863
var c = Compile(query);
3964

@@ -58,19 +83,17 @@ public void InsertMultiRecords()
5883
new[] { "name", "brand", "year" },
5984
new[]
6085
{
61-
new object[] {"Chiron", "Bugatti", null},
62-
new object[] {"Huayra", "Pagani", 2012},
63-
new object[] {"Reventon roadster", "Lamborghini", 2009}
64-
}
65-
);
86+
new object[] { "Chiron", "Bugatti", null },
87+
new object[] { "Huayra", "Pagani", 2012 },
88+
new object[] { "Reventon roadster", "Lamborghini", 2009 }
89+
});
6690

6791
var c = Compile(query);
6892

6993
Assert.Equal(
7094
"INSERT INTO [expensive_cars] ([name], [brand], [year]) VALUES ('Chiron', 'Bugatti', NULL), ('Huayra', 'Pagani', 2012), ('Reventon roadster', 'Lamborghini', 2009)",
7195
c[EngineCodes.SqlServer]);
7296

73-
7497
Assert.Equal(
7598
"INSERT INTO \"EXPENSIVE_CARS\" (\"NAME\", \"BRAND\", \"YEAR\") SELECT 'Chiron', 'Bugatti', NULL FROM RDB$DATABASE UNION ALL SELECT 'Huayra', 'Pagani', 2012 FROM RDB$DATABASE UNION ALL SELECT 'Reventon roadster', 'Lamborghini', 2009 FROM RDB$DATABASE",
7699
c[EngineCodes.Firebird]);
@@ -79,10 +102,10 @@ public void InsertMultiRecords()
79102
[Fact]
80103
public void InsertWithNullValues()
81104
{
82-
var query = new Query("Books").AsInsert(
83-
new[] { "Id", "Author", "ISBN", "Date" },
84-
new object[] { 1, "Author 1", "123456", null }
85-
);
105+
var query = new Query("Books")
106+
.AsInsert(
107+
new[] { "Id", "Author", "ISBN", "Date" },
108+
new object[] { 1, "Author 1", "123456", null });
86109

87110
var c = Compile(query);
88111

@@ -98,10 +121,10 @@ public void InsertWithNullValues()
98121
[Fact]
99122
public void InsertWithEmptyString()
100123
{
101-
var query = new Query("Books").AsInsert(
102-
new[] { "Id", "Author", "ISBN", "Description" },
103-
new object[] { 1, "Author 1", "123456", "" }
104-
);
124+
var query = new Query("Books")
125+
.AsInsert(
126+
new[] { "Id", "Author", "ISBN", "Description" },
127+
new object[] { 1, "Author 1", "123456", "" });
105128

106129
var c = Compile(query);
107130

@@ -120,7 +143,8 @@ public void InsertWithByteArray()
120143
{
121144
var fauxImagebytes = new byte[] { 0x1, 0x3, 0x3, 0x7 };
122145
var query = new Query("Books")
123-
.AsInsert(new[] { "Id", "CoverImageBytes" },
146+
.AsInsert(
147+
new[] { "Id", "CoverImageBytes" },
124148
new object[]
125149
{
126150
1,
@@ -131,27 +155,11 @@ public void InsertWithByteArray()
131155
Assert.All(c.Values, a => Assert.Equal(2, a.NamedBindings.Count));
132156

133157
var exemplar = c[EngineCodes.SqlServer];
158+
134159
Assert.Equal("INSERT INTO [Books] ([Id], [CoverImageBytes]) VALUES (?, ?)", exemplar.RawSql);
135160
Assert.Equal("INSERT INTO [Books] ([Id], [CoverImageBytes]) VALUES (@p0, @p1)", exemplar.Sql);
136161
}
137162

138-
139-
private class Account
140-
{
141-
public Account(string name, string currency = null, string created_at = null, string color = null)
142-
{
143-
this.name = name ?? throw new ArgumentNullException("name must be provided");
144-
this.Currency = currency;
145-
this.color = color;
146-
}
147-
148-
public string name { get; set; }
149-
[Column("currency_id")]
150-
public string Currency { get; set; }
151-
[Ignore]
152-
public string color { get; set; }
153-
}
154-
155163
[Fact]
156164
public void InsertWithIgnoreAndColumnProperties()
157165
{
@@ -172,37 +180,129 @@ public void InsertWithIgnoreAndColumnProperties()
172180
[Fact]
173181
public void InsertFromRaw()
174182
{
175-
var query = new Query().FromRaw("Table.With.Dots").AsInsert(new
176-
{
177-
Name = "The User",
178-
Age = new DateTime(2018, 1, 1),
179-
});
183+
var query = new Query()
184+
.FromRaw("Table.With.Dots")
185+
.AsInsert(
186+
new
187+
{
188+
Name = "The User",
189+
Age = new DateTime(2018, 1, 1),
190+
});
180191

181192
var c = Compile(query);
182193

183194
Assert.Equal(
184195
"INSERT INTO Table.With.Dots ([Name], [Age]) VALUES ('The User', '2018-01-01')",
185-
c[EngineCodes.SqlServer]
186-
);
187-
196+
c[EngineCodes.SqlServer]);
188197
}
189198

190-
191199
[Fact]
192200
public void InsertFromQueryShouldFail()
193201
{
194-
var query = new Query().From(new Query("InnerTable")).AsInsert(new
195-
{
196-
Name = "The User",
197-
Age = new DateTime(2018, 1, 1),
198-
});
202+
var query = new Query()
203+
.From(new Query("InnerTable"))
204+
.AsInsert(
205+
new
206+
{
207+
Name = "The User",
208+
Age = new DateTime(2018, 1, 1),
209+
});
199210

200211
Assert.Throws<InvalidOperationException>(() =>
201212
{
202213
Compile(query);
203214
});
215+
}
204216

217+
[Fact]
218+
public void InsertKeyValuePairs()
219+
{
220+
var dictionaryUser = new Dictionary<string, object>
221+
{
222+
{ "Name", "The User" },
223+
{ "Age", new DateTime(2018, 1, 1) },
224+
}
225+
.ToArray();
226+
227+
var query = new Query("Table")
228+
.AsInsert(dictionaryUser);
229+
230+
var c = Compile(query);
231+
232+
Assert.Equal(
233+
"INSERT INTO [Table] ([Name], [Age]) VALUES ('The User', '2018-01-01')",
234+
c[EngineCodes.SqlServer]);
235+
236+
Assert.Equal(
237+
"INSERT INTO \"TABLE\" (\"NAME\", \"AGE\") VALUES ('The User', '2018-01-01')",
238+
c[EngineCodes.Firebird]);
205239
}
206240

241+
[Fact]
242+
public void InsertDictionary()
243+
{
244+
var dictionaryUser = new Dictionary<string, object> {
245+
{ "Name", "The User" },
246+
{ "Age", new DateTime(2018, 1, 1) },
247+
};
248+
249+
var query = new Query("Table")
250+
.AsInsert(dictionaryUser);
251+
252+
var c = Compile(query);
253+
254+
Assert.Equal(
255+
"INSERT INTO [Table] ([Name], [Age]) VALUES ('The User', '2018-01-01')",
256+
c[EngineCodes.SqlServer]);
257+
258+
Assert.Equal(
259+
"INSERT INTO \"TABLE\" (\"NAME\", \"AGE\") VALUES ('The User', '2018-01-01')",
260+
c[EngineCodes.Firebird]);
261+
}
262+
263+
[Fact]
264+
public void InsertReadOnlyDictionary()
265+
{
266+
var dictionaryUser = new ReadOnlyDictionary<string, object>(
267+
new Dictionary<string, object>
268+
{
269+
{ "Name", "The User" },
270+
{ "Age", new DateTime(2018, 1, 1) },
271+
});
272+
273+
var query = new Query("Table")
274+
.AsInsert(dictionaryUser);
275+
276+
var c = Compile(query);
277+
278+
Assert.Equal(
279+
"INSERT INTO [Table] ([Name], [Age]) VALUES ('The User', '2018-01-01')",
280+
c[EngineCodes.SqlServer]);
281+
282+
Assert.Equal(
283+
"INSERT INTO \"TABLE\" (\"NAME\", \"AGE\") VALUES ('The User', '2018-01-01')",
284+
c[EngineCodes.Firebird]);
285+
}
286+
287+
[Fact]
288+
public void InsertExpandoObject()
289+
{
290+
dynamic expandoUser = new ExpandoObject();
291+
expandoUser.Name = "The User";
292+
expandoUser.Age = new DateTime(2018, 1, 1);
293+
294+
var query = new Query("Table")
295+
.AsInsert(expandoUser);
296+
297+
var c = Compile(query);
298+
299+
Assert.Equal(
300+
"INSERT INTO [Table] ([Name], [Age]) VALUES ('The User', '2018-01-01')",
301+
c[EngineCodes.SqlServer]);
302+
303+
Assert.Equal(
304+
"INSERT INTO \"TABLE\" (\"NAME\", \"AGE\") VALUES ('The User', '2018-01-01')",
305+
c[EngineCodes.Firebird]);
306+
}
207307
}
208308
}

0 commit comments

Comments
 (0)