Skip to content

Commit

Permalink
Merge pull request #617 from mikependon/repodb-push-expand-object
Browse files Browse the repository at this point in the history
#234 #235 #236 #237 #238 #239 - Insert/Merge Identity for ExpandoObject.
  • Loading branch information
mikependon authored Oct 11, 2020
2 parents 522731e + 500a586 commit 79f875f
Show file tree
Hide file tree
Showing 27 changed files with 88 additions and 6 deletions.
12 changes: 12 additions & 0 deletions RepoDb.Core/RepoDb/Extensions/DbConnectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2389,14 +2389,26 @@ internal static QueryGroup CreateQueryGroupForUpsert(object entity,
internal static QueryGroup CreateQueryGroupForUpsert(IDictionary<string, object> dictionary,
IEnumerable<Field> qualifiers = null)
{
if (qualifiers?.Any() != true)
{
throw new MissingFieldsException("No qualifier fields found for the 'Upsert' operation.");
}

var queryFields = new List<QueryField>();

foreach (var field in qualifiers)
{
if (dictionary.ContainsKey(field.Name))
{
queryFields.Add(new QueryField(field, dictionary[field.Name]));
}
}

if (queryFields?.Any() != true)
{
throw new MissingFieldsException("No qualifier fields defined for the 'Upsert' operation. Please check the items defined at the dictionary object.");
}

return new QueryGroup(queryFields);
}

Expand Down
6 changes: 4 additions & 2 deletions RepoDb.Core/RepoDb/Operations/DbConnection/MergeAll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1502,13 +1502,14 @@ internal static int UpsertAllInternalBase<TEntity>(this IDbConnection connection
}

// Iterate the entities
var immutableFields = fields.AsList(); // Fix for the IDictionary<string, object> object
foreach (var entity in entities.AsList())
{
// Call the upsert
var upsertResult = connection.UpsertInternalBase<TEntity, object>(tableName,
entity,
qualifiers,
fields,
immutableFields,
hints,
commandTimeout,
transaction,
Expand Down Expand Up @@ -1903,13 +1904,14 @@ internal static async Task<int> UpsertAllAsyncInternalBase<TEntity>(this IDbConn
}

// Iterate the entities
var immutableFields = fields.AsList(); // Fix for the IDictionary<string, object> object
foreach (var entity in entities.AsList())
{
// Call the upsert
var upsertResult = await connection.UpsertAsyncInternalBase<TEntity, object>(tableName,
entity,
qualifiers,
fields,
immutableFields,
hints,
commandTimeout,
transaction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ public void TestMySqlConnectionInsertAllViaTableNameAsExpandoObjectsForIdentity(
// Assert
Assert.AreEqual(tables.Count, connection.CountAll<CompleteTable>());
Assert.AreEqual(tables.Count, result);
Assert.IsTrue(tables.All(table => ((dynamic)table).Id > 0));

// Act
var queryResult = connection.QueryAll<CompleteTable>();
Expand Down Expand Up @@ -343,6 +344,7 @@ public void TestMySqlConnectionInsertAllAsyncViaTableNameAsExpandoObjectsForIden
// Assert
Assert.AreEqual(tables.Count, connection.CountAll<CompleteTable>());
Assert.AreEqual(tables.Count, result);
Assert.IsTrue(tables.All(table => ((dynamic)table).Id > 0));

// Act
var queryResult = connection.QueryAll<CompleteTable>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public void TestMySqlConnectionInsertViaTableNameAsExpandoObjectForIdentity()
// Assert
Assert.AreEqual(1, connection.CountAll<CompleteTable>());
Assert.IsTrue(Convert.ToInt64(result) > 0);
Assert.IsTrue(((dynamic)table).Id == Convert.ToInt64(result));

// Act
var queryResult = connection.Query<CompleteTable>(result);
Expand Down Expand Up @@ -356,6 +357,7 @@ public void TestMySqlConnectionInsertAsyncViaTableNameAsExpandoObjectForIdentity
// Assert
Assert.AreEqual(1, connection.CountAll<CompleteTable>());
Assert.IsTrue(Convert.ToInt64(result) > 0);
Assert.IsTrue(((dynamic)table).Id == Convert.ToInt64(result));

// Act
var queryResult = connection.Query<CompleteTable>(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ public void TestMySqlConnectionMergeAllAsExpandoObjectViaTableNameForIdentityFor
// Assert
Assert.AreEqual(tables.Count, connection.CountAll<CompleteTable>());
Assert.AreEqual(tables.Count, result);
Assert.IsTrue(tables.All(table => ((dynamic)table).Id > 0));

// Act
var queryResult = connection.QueryAll<CompleteTable>();
Expand Down Expand Up @@ -463,6 +464,7 @@ public void TestMySqlConnectionMergeAllAsExpandoObjectViaTableNameForIdentityFor

// Assert
Assert.AreEqual(entities.Count, connection.CountAll<CompleteTable>());
Assert.IsTrue(tables.All(table => ((dynamic)table).Id > 0));

// Act
var queryResult = connection.QueryAll<CompleteTable>();
Expand Down Expand Up @@ -801,6 +803,7 @@ public void TestMySqlConnectionMergeAllAsyncAsExpandoObjectViaTableNameForIdenti
// Assert
Assert.AreEqual(tables.Count, connection.CountAll<CompleteTable>());
Assert.AreEqual(tables.Count, result);
Assert.IsTrue(tables.All(table => ((dynamic)table).Id > 0));

// Act
var queryResult = connection.QueryAll<CompleteTable>();
Expand Down Expand Up @@ -856,6 +859,7 @@ public void TestMySqlConnectionMergeAllAsyncAsExpandoObjectViaTableNameForIdenti

// Assert
Assert.AreEqual(entities.Count, connection.CountAll<CompleteTable>());
Assert.IsTrue(tables.All(table => ((dynamic)table).Id > 0));

// Act
var queryResult = connection.QueryAll<CompleteTable>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ public void TestMySqlConnectionMergeAsExpandoObjectViaTableNameForIdentityForEmp

// Assert
Assert.AreEqual(1, connection.CountAll<CompleteTable>());
Assert.IsTrue(((dynamic)table).Id == Convert.ToInt64(result));
Helper.AssertMembersEquality(queryResult.First(), table);
}
}
Expand Down Expand Up @@ -278,6 +279,7 @@ public void TestMySqlConnectionMergeAsExpandoObjectViaTableNameForIdentityForNon
// Assert
Assert.AreEqual(1, connection.CountAll<CompleteTable>());
Assert.AreEqual(table.Id, result);
Assert.AreEqual(((dynamic)table).Id, result);

// Act
var queryResult = connection.Query<CompleteTable>(result);
Expand Down Expand Up @@ -442,6 +444,7 @@ public void TestMySqlConnectionMergeAsyncAsExpandoObjectViaTableNameForIdentityF

// Assert
Assert.AreEqual(1, connection.CountAll<CompleteTable>());
Assert.IsTrue(((dynamic)table).Id == Convert.ToInt64(result));
Helper.AssertMembersEquality(queryResult.First(), table);
}
}
Expand Down Expand Up @@ -492,6 +495,7 @@ public void TestMySqlConnectionMergeAsyncAsExpandoObjectViaTableNameForIdentityF
// Assert
Assert.AreEqual(1, connection.CountAll<CompleteTable>());
Assert.AreEqual(table.Id, result);
Assert.AreEqual(((dynamic)table).Id, result);

// Act
var queryResult = connection.Query<CompleteTable>(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ public void TestMySqlConnectionInsertAllViaTableNameAsExpandoObjectsForIdentity(
// Assert
Assert.AreEqual(tables.Count, connection.CountAll<CompleteTable>());
Assert.AreEqual(tables.Count, result);
Assert.IsTrue(tables.All(table => ((dynamic)table).Id > 0));

// Act
var queryResult = connection.QueryAll<CompleteTable>();
Expand Down Expand Up @@ -343,6 +344,7 @@ public void TestMySqlConnectionInsertAllAsyncViaTableNameAsExpandoObjectsForIden
// Assert
Assert.AreEqual(tables.Count, connection.CountAll<CompleteTable>());
Assert.AreEqual(tables.Count, result);
Assert.IsTrue(tables.All(table => ((dynamic)table).Id > 0));

// Act
var queryResult = connection.QueryAll<CompleteTable>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public void TestMySqlConnectionInsertViaTableNameAsExpandoObjectForIdentity()
// Assert
Assert.AreEqual(1, connection.CountAll<CompleteTable>());
Assert.IsTrue(Convert.ToInt64(result) > 0);
Assert.IsTrue(((dynamic)table).Id == Convert.ToInt64(result));

// Act
var queryResult = connection.Query<CompleteTable>(result);
Expand Down Expand Up @@ -356,6 +357,7 @@ public void TestMySqlConnectionInsertAsyncViaTableNameAsExpandoObjectForIdentity
// Assert
Assert.AreEqual(1, connection.CountAll<CompleteTable>());
Assert.IsTrue(Convert.ToInt64(result) > 0);
Assert.IsTrue(((dynamic)table).Id == Convert.ToInt64(result));

// Act
var queryResult = connection.Query<CompleteTable>(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ public void TestMySqlConnectionMergeAllAsExpandoObjectViaTableNameForIdentityFor
// Assert
Assert.AreEqual(tables.Count, connection.CountAll<CompleteTable>());
Assert.AreEqual(tables.Count, result);
Assert.IsTrue(tables.All(table => ((dynamic)table).Id > 0));

// Act
var queryResult = connection.QueryAll<CompleteTable>();
Expand Down Expand Up @@ -463,6 +464,7 @@ public void TestMySqlConnectionMergeAllAsExpandoObjectViaTableNameForIdentityFor

// Assert
Assert.AreEqual(entities.Count, connection.CountAll<CompleteTable>());
Assert.IsTrue(tables.All(table => ((dynamic)table).Id > 0));

// Act
var queryResult = connection.QueryAll<CompleteTable>();
Expand Down Expand Up @@ -801,6 +803,7 @@ public void TestMySqlConnectionMergeAllAsyncAsExpandoObjectViaTableNameForIdenti
// Assert
Assert.AreEqual(tables.Count, connection.CountAll<CompleteTable>());
Assert.AreEqual(tables.Count, result);
Assert.IsTrue(tables.All(table => ((dynamic)table).Id > 0));

// Act
var queryResult = connection.QueryAll<CompleteTable>();
Expand Down Expand Up @@ -856,6 +859,7 @@ public void TestMySqlConnectionMergeAllAsyncAsExpandoObjectViaTableNameForIdenti

// Assert
Assert.AreEqual(entities.Count, connection.CountAll<CompleteTable>());
Assert.IsTrue(tables.All(table => ((dynamic)table).Id > 0));

// Act
var queryResult = connection.QueryAll<CompleteTable>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ public void TestMySqlConnectionMergeAsExpandoObjectViaTableNameForIdentityForEmp

// Assert
Assert.AreEqual(1, connection.CountAll<CompleteTable>());
Assert.IsTrue(((dynamic)table).Id == Convert.ToInt64(result));
Helper.AssertMembersEquality(queryResult.First(), table);
}
}
Expand Down Expand Up @@ -278,6 +279,7 @@ public void TestMySqlConnectionMergeAsExpandoObjectViaTableNameForIdentityForNon
// Assert
Assert.AreEqual(1, connection.CountAll<CompleteTable>());
Assert.AreEqual(table.Id, result);
Assert.IsTrue(((dynamic)table).Id == Convert.ToInt64(result));

// Act
var queryResult = connection.Query<CompleteTable>(result);
Expand Down Expand Up @@ -442,6 +444,7 @@ public void TestMySqlConnectionMergeAsyncAsExpandoObjectViaTableNameForIdentityF

// Assert
Assert.AreEqual(1, connection.CountAll<CompleteTable>());
Assert.IsTrue(((dynamic)table).Id == Convert.ToInt64(result));
Helper.AssertMembersEquality(queryResult.First(), table);
}
}
Expand Down Expand Up @@ -492,6 +495,7 @@ public void TestMySqlConnectionMergeAsyncAsExpandoObjectViaTableNameForIdentityF
// Assert
Assert.AreEqual(1, connection.CountAll<CompleteTable>());
Assert.AreEqual(table.Id, result);
Assert.IsTrue(((dynamic)table).Id == Convert.ToInt64(result));

// Act
var queryResult = connection.Query<CompleteTable>(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public static List<ExpandoObject> CreateCompleteTablesAsExpandoObjects(int count
for (var i = 0; i < count; i++)
{
var item = new ExpandoObject() as IDictionary<string, object>;
item["Id"] = (i + 1);
item["Id"] = (long)(i + 1);
item["ColumnBigInt"] = Convert.ToInt64(i);
item["ColumnBigIntAsArray"] = new long[] { 1, 2, 3, 4, 5 };
item["ColumnBigSerial"] = Convert.ToInt64(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ public void TestPostgreSqlConnectionInsertAllViaTableNameAsExpandoObjectsForIden
// Assert
Assert.AreEqual(tables.Count, connection.CountAll<CompleteTable>());
Assert.AreEqual(tables.Count, result);
Assert.IsTrue(tables.All(table => ((dynamic)table).Id > 0));

// Act
var queryResult = connection.QueryAll<CompleteTable>();
Expand Down Expand Up @@ -267,6 +268,7 @@ public void TestPostgreSqlConnectionInsertAllViaTableNameAsExpandoObjectsForNonI
// Assert
Assert.AreEqual(tables.Count, connection.CountAll<NonIdentityCompleteTable>());
Assert.AreEqual(tables.Count, result);
Assert.IsTrue(tables.All(table => ((dynamic)table).Id > 0));

// Act
var queryResult = connection.QueryAll<NonIdentityCompleteTable>();
Expand Down Expand Up @@ -343,6 +345,7 @@ public void TestPostgreSqlConnectionInsertAllAsyncViaTableNameAsExpandoObjectsFo
// Assert
Assert.AreEqual(tables.Count, connection.CountAll<CompleteTable>());
Assert.AreEqual(tables.Count, result);
Assert.IsTrue(tables.All(table => ((dynamic)table).Id > 0));

// Act
var queryResult = connection.QueryAll<CompleteTable>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public void TestPostgreSqlConnectionInsertViaTableNameAsExpandoObjectForIdentity
// Assert
Assert.AreEqual(1, connection.CountAll<CompleteTable>());
Assert.IsTrue(Convert.ToInt64(result) > 0);
Assert.AreEqual(((dynamic)table).Id, result);

// Act
var queryResult = connection.Query<CompleteTable>(result);
Expand Down Expand Up @@ -356,6 +357,7 @@ public void TestPostgreSqlConnectionInsertAsyncViaTableNameAsExpandoObjectForIde
// Assert
Assert.AreEqual(1, connection.CountAll<CompleteTable>());
Assert.IsTrue(Convert.ToInt64(result) > 0);
Assert.AreEqual(((dynamic)table).Id, result);

// Act
var queryResult = connection.Query<CompleteTable>(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ public void TestPostgreSqlConnectionMergeAllAsExpandoObjectViaTableNameForIdenti
// Assert
Assert.AreEqual(tables.Count, connection.CountAll<CompleteTable>());
Assert.AreEqual(tables.Count, result);
Assert.IsTrue(tables.All(table => ((dynamic)table).Id > 0));

// Act
var queryResult = connection.QueryAll<CompleteTable>();
Expand Down Expand Up @@ -801,6 +802,7 @@ public void TestPostgreSqlConnectionMergeAllAsyncAsExpandoObjectViaTableNameForI
// Assert
Assert.AreEqual(tables.Count, connection.CountAll<CompleteTable>());
Assert.AreEqual(tables.Count, result);
Assert.IsTrue(tables.All(table => ((dynamic)table).Id > 0));

// Act
var queryResult = connection.QueryAll<CompleteTable>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ public void TestPostgreSqlConnectionMergeAsExpandoObjectViaTableNameForIdentityF

// Assert
Assert.AreEqual(1, connection.CountAll<CompleteTable>());
Assert.AreEqual(((dynamic)table).Id, result);
Helper.AssertMembersEquality(queryResult.First(), table);
}
}
Expand Down Expand Up @@ -442,6 +443,7 @@ public void TestPostgreSqlConnectionMergeAsyncAsExpandoObjectViaTableNameForIden

// Assert
Assert.AreEqual(1, connection.CountAll<CompleteTable>());
Assert.AreEqual(((dynamic)table).Id, result);
Helper.AssertMembersEquality(queryResult.First(), table);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ public void TestSqLiteConnectionInsertAllViaTableNameAsExpandoObjectForIdentity(
// Assert
Assert.AreEqual(tables.Count, connection.CountAll<MdsCompleteTable>());
Assert.AreEqual(tables.Count, result);
Assert.IsTrue(tables.All(table => ((dynamic)table).Id > 0));

// Act
var queryResult = connection.QueryAll<MdsCompleteTable>();
Expand Down Expand Up @@ -388,6 +389,7 @@ public void TestSqLiteConnectionInsertAllViaTableNameAsyncAsExpandoObjectForIden
// Assert
Assert.AreEqual(tables.Count, connection.CountAll<MdsCompleteTable>());
Assert.AreEqual(tables.Count, result);
Assert.IsTrue(tables.All(table => ((dynamic)table).Id > 0));

// Act
var queryResult = connection.QueryAll<MdsCompleteTable>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ public void TestSqliteConnectionInsertViaTableNameAsExpandoObjectForIdentity()
// Assert
Assert.AreEqual(1, connection.CountAll<MdsCompleteTable>());
Assert.IsTrue(Convert.ToInt64(result) > 0);
Assert.AreEqual(((dynamic)table).Id, result);

// Act
var queryResult = connection.Query<MdsCompleteTable>(result);
Expand Down Expand Up @@ -367,6 +368,7 @@ public void TestSqLiteConnectionInsertAsyncViaTableNameAsExpandoObjectForIdentit
// Assert
Assert.AreEqual(1, connection.CountAll<MdsCompleteTable>());
Assert.IsTrue(Convert.ToInt64(result) > 0);
Assert.AreEqual(((dynamic)table).Id, result);

// Act
var queryResult = connection.Query<MdsCompleteTable>(result);
Expand Down
Loading

0 comments on commit 79f875f

Please sign in to comment.