Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding and testing InsertOrReplaceAll Query #1226

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/SQLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ CreateTablesResult CreateTables<T, T2, T3, T4, T5> (CreateFlags createFlags = Cr
int InsertAll (IEnumerable objects, Type objType, bool runInTransaction = true);
int InsertOrReplace (object obj);
int InsertOrReplace (object obj, Type objType);
int InsertOrReplaceAll(IEnumerable objects, bool runInTransaction = true);
int InsertOrReplaceAll(IEnumerable objects, Type objType, bool runInTransaction = true);
List<T> Query<T> (string query, params object[] args) where T : new();
List<object> Query (TableMapping map, string query, params object[] args);
List<T> QueryScalars<T> (string query, params object[] args);
Expand Down Expand Up @@ -1731,6 +1733,57 @@ public int InsertAll (System.Collections.IEnumerable objects, Type objType, bool
return c;
}

/// <summary>
/// Inserts all specified objects.
/// </summary>
/// <param name="objects">
/// An <see cref="IEnumerable"/> of the objects to insert.
/// <param name="runInTransaction"/>
/// A boolean indicating if the inserts should be wrapped in a transaction.
/// </param>
/// <param name="runInTransaction"></param>
/// <returns>
/// The number of rows added to the table.
/// </returns>
public int InsertOrReplaceAll(IEnumerable objects, bool runInTransaction = true)
{
var c = 0;
if (runInTransaction) {
RunInTransaction(() => { c += objects.Cast<object>().Sum(InsertOrReplace); });
}
else {
c += objects.Cast<object>().Sum(InsertOrReplace);
}
return c;
}

/// <summary>
/// Inserts all specified objects.
/// </summary>
/// <param name="objects">
/// An <see cref="IEnumerable"/> of the objects to insert.
/// </param>
/// <param name="objType">
/// The type of object to insert.
/// </param>
/// <param name="runInTransaction">
/// A boolean indicating if the inserts should be wrapped in a transaction.
/// </param>
/// <returns>
/// The number of rows added to the table.
/// </returns>
public int InsertOrReplaceAll(IEnumerable objects, Type objType, bool runInTransaction = true)
{
var c = 0;
if (runInTransaction) {
RunInTransaction(() => { c += objects.Cast<object>().Sum(r => InsertOrReplace(r, objType)); });
}
else {
c += objects.Cast<object>().Sum(r => InsertOrReplace(r, objType));
}
return c;
}

/// <summary>
/// Inserts the given object (and updates its
/// auto incremented primary key if it has one).
Expand Down
46 changes: 46 additions & 0 deletions tests/SQLite.Tests/InsertTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,5 +279,51 @@ public void InsertOrReplace ()
Assert.AreEqual (20, r.Count);
Assert.AreEqual ("Foo", r[4].Text);
}

[Test]
public void InsertOrReplaceAllSuccessOutsideTransaction()
{
var testObjects = Enumerable.Range(1, 20).Select(i => new UniqueObj { Id = i }).ToList();

_db.InsertOrReplaceAll(testObjects);

Assert.AreEqual(testObjects.Count, _db.Table<UniqueObj>().Count());
}

[Test]
public void InsertOrReplaceAllFailureOutsideTransaction()
{
var testObjects = Enumerable.Range(1, 20).Select(i => new UniqueObj { Id = i }).ToList();
testObjects[^1].Id = 1; // causes the insert to fail because of duplicate key

ExceptionAssert.Throws<SQLiteException>(() => _db.InsertAll(testObjects));

Assert.AreEqual(0, _db.Table<UniqueObj>().Count());
}

[Test]
public void InsertOrReplaceAllSuccessInsideTransaction()
{
var testObjects = Enumerable.Range(1, 20).Select(i => new UniqueObj { Id = i }).ToList();

_db.RunInTransaction(() => {
_db.InsertOrReplaceAll(testObjects);
});

Assert.AreEqual(testObjects.Count, _db.Table<UniqueObj>().Count());
}

[Test]
public void InsertOrReplaceAllFailureInsideTransaction()
{
var testObjects = Enumerable.Range(1, 20).Select(i => new UniqueObj { Id = i }).ToList();
testObjects[^1].Id = 1; // causes the insert to fail because of duplicate key

ExceptionAssert.Throws<SQLiteException>(() => _db.RunInTransaction(() => {
_db.InsertOrReplaceAll(testObjects);
}));

Assert.AreEqual(0, _db.Table<UniqueObj>().Count());
}
}
}