Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 011d011

Browse files
committed
Add new InsertUsingDefaults API
1 parent 9f277de commit 011d011

File tree

4 files changed

+112
-6
lines changed

4 files changed

+112
-6
lines changed

src/ServiceStack.OrmLite/OrmLiteWriteApi.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ public static long Insert<T>(this IDbConnection dbConn, T obj, bool selectIdenti
6565
}
6666

6767
/// <summary>
68-
/// Insert 1 or more POCOs in a transaction. E.g:
69-
/// <para>db.Insert(new Person { Id = 1, FirstName = "Tupac", LastName = "Shakur", Age = 25 },</para>
70-
/// <para> new Person { Id = 2, FirstName = "Biggie", LastName = "Smalls", Age = 24 })</para>
68+
/// Insert 1 or more POCOs in a transaction using Table default values when defined. E.g:
69+
/// <para>db.InsertUsingDefaults(new Person { FirstName = "Tupac", LastName = "Shakur" },</para>
70+
/// <para> new Person { FirstName = "Biggie", LastName = "Smalls" })</para>
7171
/// </summary>
72-
public static void Insert<T>(this IDbConnection dbConn, params T[] objs)
72+
public static void InsertUsingDefaults<T>(this IDbConnection dbConn, params T[] objs)
7373
{
74-
dbConn.Exec(dbCmd => dbCmd.Insert(objs));
74+
dbConn.Exec(dbCmd => dbCmd.InsertUsingDefaults(objs));
7575
}
7676

7777
/// <summary>
@@ -83,6 +83,16 @@ public static void InsertAll<T>(this IDbConnection dbConn, IEnumerable<T> objs)
8383
dbConn.Exec(dbCmd => dbCmd.InsertAll(objs));
8484
}
8585

86+
/// <summary>
87+
/// Insert 1 or more POCOs in a transaction. E.g:
88+
/// <para>db.Insert(new Person { Id = 1, FirstName = "Tupac", LastName = "Shakur", Age = 25 },</para>
89+
/// <para> new Person { Id = 2, FirstName = "Biggie", LastName = "Smalls", Age = 24 })</para>
90+
/// </summary>
91+
public static void Insert<T>(this IDbConnection dbConn, params T[] objs)
92+
{
93+
dbConn.Exec(dbCmd => dbCmd.Insert(objs));
94+
}
95+
8696
/// <summary>
8797
/// Updates 1 POCO. All fields are updated except for the PrimaryKey which is used as the identity selector. E.g:
8898
/// <para>db.Update(new Person { Id = 1, FirstName = "Jimi", LastName = "Hendrix", Age = 27 })</para>

src/ServiceStack.OrmLite/OrmLiteWriteCommandExtensions.cs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Linq;
1717
using ServiceStack.Data;
1818
using ServiceStack.Logging;
19+
using ServiceStack.Text;
1920

2021
namespace ServiceStack.OrmLite
2122
{
@@ -628,7 +629,6 @@ internal static long Insert<T>(this IDbCommand dbCmd, T obj, bool selectIdentity
628629
return dbCmd.ExecNonQuery();
629630
}
630631

631-
632632
internal static void Insert<T>(this IDbCommand dbCmd, params T[] objs)
633633
{
634634
InsertAll(dbCmd, objs);
@@ -671,6 +671,50 @@ internal static void InsertAll<T>(this IDbCommand dbCmd, IEnumerable<T> objs)
671671
}
672672
}
673673

674+
internal static void InsertUsingDefaults<T>(this IDbCommand dbCmd, params T[] objs)
675+
{
676+
IDbTransaction dbTrans = null;
677+
678+
try
679+
{
680+
if (dbCmd.Transaction == null)
681+
dbCmd.Transaction = dbTrans = dbCmd.Connection.BeginTransaction();
682+
683+
var dialectProvider = dbCmd.GetDialectProvider();
684+
685+
var modelDef = typeof(T).GetModelDefinition();
686+
var fieldsWithoutDefaults = modelDef.FieldDefinitionsArray
687+
.Where(x => x.DefaultValue == null)
688+
.Select(x => x.Name)
689+
.ToHashSet();
690+
691+
dialectProvider.PrepareParameterizedInsertStatement<T>(dbCmd,
692+
insertFields: fieldsWithoutDefaults);
693+
694+
foreach (var obj in objs)
695+
{
696+
OrmLiteConfig.InsertFilter?.Invoke(dbCmd, obj);
697+
dialectProvider.SetParameterValues<T>(dbCmd, obj);
698+
699+
try
700+
{
701+
dbCmd.ExecNonQuery();
702+
}
703+
catch (Exception ex)
704+
{
705+
Log.Error("SQL ERROR: {0}".Fmt(dbCmd.GetLastSqlAndParams()), ex);
706+
throw;
707+
}
708+
}
709+
710+
dbTrans?.Commit();
711+
}
712+
finally
713+
{
714+
dbTrans?.Dispose();
715+
}
716+
}
717+
674718
internal static int Save<T>(this IDbCommand dbCmd, params T[] objs)
675719
{
676720
return SaveAll(dbCmd, objs);

tests/ServiceStack.OrmLite.Tests/DefaultValueTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Data;
3+
using System.Linq;
34
using NUnit.Framework;
45
using ServiceStack.OrmLite.SqlServer;
56
using ServiceStack.OrmLite.SqlServer.Converters;
@@ -303,5 +304,42 @@ public void Can_filter_updateAdd_sqlexpression_to_insert_date()
303304
VerifyUpdateDate(db);
304305
}
305306
}
307+
308+
[Test]
309+
public void Does_use_defaults_for_missing_values()
310+
{
311+
using (var db = OpenDbConnection())
312+
{
313+
db.DropAndCreateTable<ModelWithDefaults>();
314+
315+
db.Insert(new ModelWithDefaults { DefaultInt = 10 });
316+
317+
var row = db.Select<ModelWithDefaults>().FirstOrDefault();
318+
319+
Assert.That(row.Id, Is.GreaterThan(0));
320+
Assert.That(row.DefaultInt, Is.EqualTo(10));
321+
Assert.That(row.DefaultString, Is.EqualTo("String"));
322+
}
323+
}
324+
325+
[Test]
326+
public void Does_only_use_defaults_for_all_default_properties()
327+
{
328+
using (var db = OpenDbConnection())
329+
{
330+
db.DropAndCreateTable<ModelWithDefaults>();
331+
332+
db.InsertUsingDefaults(
333+
new ModelWithDefaults { Name = "foo", DefaultInt = 10 },
334+
new ModelWithDefaults { Name = "bar", DefaultString = "qux" });
335+
336+
var rows = db.Select<ModelWithDefaults>();
337+
rows.PrintDump();
338+
339+
Assert.That(rows.All(x => x.Id > 0));
340+
Assert.That(rows.All(x => x.DefaultInt == 1));
341+
Assert.That(rows.All(x => x.DefaultString == "String"));
342+
}
343+
}
306344
}
307345
}

tests/ServiceStack.OrmLite.Tests/Shared/DefaultValues.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,18 @@ public class DefaultValues
3333
[Default(OrmLiteVariables.SystemUtc)]
3434
public DateTime UpdatedDateUtc { get; set; }
3535
}
36+
37+
public class ModelWithDefaults
38+
{
39+
[AutoIncrement]
40+
public int Id { get; set; }
41+
42+
public string Name { get; set; }
43+
44+
[Default(1)]
45+
public int DefaultInt { get; set; }
46+
47+
[Default("'String'")]
48+
public string DefaultString { get; set; }
49+
}
3650
}

0 commit comments

Comments
 (0)