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

Commit d2d6fac

Browse files
committed
Throw OptimisticConcurrencyException in StrictMode + add subselect expr example
1 parent 7362dd2 commit d2d6fac

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/ServiceStack.OrmLite/OrmLiteWriteCommandExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using ServiceStack.DataAnnotations;
1919
using ServiceStack.Logging;
2020
using ServiceStack.OrmLite.Converters;
21+
using ServiceStack.Text;
2122

2223
namespace ServiceStack.OrmLite
2324
{
@@ -872,7 +873,7 @@ internal static bool Save<T>(this IDbCommand dbCmd, T obj)
872873
}
873874

874875
var rowsUpdated = dbCmd.Update(obj);
875-
if (rowsUpdated == 0)
876+
if (rowsUpdated == 0 && Env.StrictMode)
876877
throw new OptimisticConcurrencyException("No rows were inserted or updated");
877878

878879
modelDef.RowVersion?.SetValueFn(obj, dbCmd.GetRowVersion(modelDef, id, modelDef.RowVersion.ColumnType));

tests/ServiceStack.OrmLite.Tests/Expression/SqlExpressionTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,5 +653,32 @@ public void Can_select_limit_on_Table_with_References()
653653
Assert.That(results[0].Orders.Count, Is.EqualTo(2));
654654
}
655655
}
656+
657+
[Test]
658+
public void Can_select_subselect()
659+
{
660+
using (var db = OpenDbConnection())
661+
{
662+
db.DropAndCreateTable<LetterFrequency>();
663+
664+
var insertedIds = "A,B,B,C,C,C,D,D,E".Split(',').Map(letter =>
665+
db.Insert(new LetterFrequency { Letter = letter }, selectIdentity: true));
666+
667+
var q = db.From<LetterFrequency>(db.TableAlias("x"));
668+
q.Where(x => x.Letter == Sql.Custom(q.Column<LetterFrequency>(c => c.Letter, true)));
669+
var subSql = q.Select(Sql.Count("*")).ToSelectStatement();
670+
671+
var rows = db.Select<Dictionary<string, object>>(db.From<LetterFrequency>()
672+
.Where(x => x.Letter == "C")
673+
.Select(x => new {
674+
x,
675+
LetterCount = Sql.Custom($"({subSql})"),
676+
}));
677+
678+
// rows.PrintDump();
679+
Assert.That(rows.Count, Is.EqualTo(3));
680+
Assert.That(rows.All(x => x["LetterCount"].ConvertTo<int>() == 3));
681+
}
682+
}
656683
}
657684
}

0 commit comments

Comments
 (0)