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

Commit be583b6

Browse files
committed
Modify InsertOnlyAsync to match new sync API
1 parent cff91a5 commit be583b6

8 files changed

+30
-8
lines changed

src/ServiceStack.OrmLite/Async/WriteExpressionCommandExtensionsAsync.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,14 @@ internal static Task<int> UpdateAsync<T>(this IDbCommand dbCmd, object updateOnl
9898
return dbCmd.ExecNonQueryAsync(token);
9999
}
100100

101-
internal static Task InsertOnlyAsync<T>(this IDbCommand dbCmd, T obj, SqlExpression<T> onlyFields, CancellationToken token)
101+
internal static Task InsertOnlyAsync<T>(this IDbCommand dbCmd, T obj, Expression<Func<T, object>> onlyFields, CancellationToken token)
102102
{
103103
if (OrmLiteConfig.InsertFilter != null)
104104
OrmLiteConfig.InsertFilter(dbCmd, obj);
105105

106-
var sql = dbCmd.GetDialectProvider().ToInsertRowStatement(dbCmd, obj, onlyFields.InsertFields);
106+
var q = dbCmd.GetDialectProvider().SqlExpression<T>().Insert(onlyFields);
107+
108+
var sql = dbCmd.GetDialectProvider().ToInsertRowStatement(dbCmd, obj, q.InsertFields);
107109
return dbCmd.ExecuteSqlAsync(sql, token);
108110
}
109111

src/ServiceStack.OrmLite/Legacy/OrmLiteWriteExpressionsApiAsyncLegacy.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ public static class OrmLiteWriteExpressionsApiAsyncLegacy
2020
return dbConn.Exec(dbCmd => dbCmd.InsertOnlyAsync(obj, onlyFields, token));
2121
}
2222

23+
/// <summary>
24+
/// Using an SqlExpression to only Insert the fields specified, e.g:
25+
///
26+
/// db.InsertOnly(new Person { FirstName = "Amy" }, q => q.Insert(p => new { p.FirstName }));
27+
/// INSERT INTO "Person" ("FirstName") VALUES ('Amy');
28+
/// </summary>
29+
public static Task InsertOnlyAsync<T>(this IDbConnection dbConn, T obj, SqlExpression<T> onlyFields, CancellationToken token = default(CancellationToken))
30+
{
31+
return dbConn.Exec(dbCmd => dbCmd.InsertOnlyAsync(obj, onlyFields, token));
32+
}
33+
2334
/// <summary>
2435
/// Use an SqlExpression to select which fields to update and construct the where expression, E.g:
2536
///

src/ServiceStack.OrmLite/Legacy/WriteExpressionCommandExtensionsAsyncLegacy.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ internal static Task<int> DeleteFmtAsync(this IDbCommand dbCmd, string table, st
4949
var sql = WriteExpressionCommandExtensionsLegacy.DeleteFmtSql(dbCmd.GetDialectProvider(), table, @where);
5050
return dbCmd.ExecuteSqlAsync(sql, token);
5151
}
52+
53+
internal static Task InsertOnlyAsync<T>(this IDbCommand dbCmd, T obj, SqlExpression<T> onlyFields, CancellationToken token)
54+
{
55+
if (OrmLiteConfig.InsertFilter != null)
56+
OrmLiteConfig.InsertFilter(dbCmd, obj);
57+
58+
var sql = dbCmd.GetDialectProvider().ToInsertRowStatement(dbCmd, obj, onlyFields.InsertFields);
59+
return dbCmd.ExecuteSqlAsync(sql, token);
60+
}
5261
}
5362
}
5463

src/ServiceStack.OrmLite/OrmLiteWriteExpressionsApiAsync.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ public static Task<int> UpdateAddAsync<T>(this IDbConnection dbConn,
145145
/// <summary>
146146
/// Using an SqlExpression to only Insert the fields specified, e.g:
147147
///
148-
/// db.InsertOnly(new Person { FirstName = "Amy" }, q => q.Insert(p => new { p.FirstName }));
148+
/// db.InsertOnly(new Person { FirstName = "Amy" }, p => new { p.FirstName });
149149
/// INSERT INTO "Person" ("FirstName") VALUES ('Amy');
150150
/// </summary>
151-
public static Task InsertOnlyAsync<T>(this IDbConnection dbConn, T obj, SqlExpression<T> onlyFields, CancellationToken token = default(CancellationToken))
151+
public static Task InsertOnlyAsync<T>(this IDbConnection dbConn, T obj, Expression<Func<T, object>> onlyFields, CancellationToken token = default(CancellationToken))
152152
{
153153
return dbConn.Exec(dbCmd => dbCmd.InsertOnlyAsync(obj, onlyFields, token));
154154
}

tests/ServiceStack.OrmLiteV45.Tests/ApiMySqlTestsAsync.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ await db.InsertAsync(new Person { Id = 8, FirstName = "Tupac", LastName = "Shaku
231231
await db.InsertAllAsync(new[] { new Person { Id = 10, FirstName = "Biggie", LastName = "Smalls", Age = 24 } });
232232
Assert.That(db.GetLastSql(), Is.EqualTo("INSERT INTO `Person` (`Id`,`FirstName`,`LastName`,`Age`) VALUES (@Id,@FirstName,@LastName,@Age)"));
233233

234-
await db.InsertOnlyAsync(new PersonWithAutoId { FirstName = "Amy", Age = 27 }, db.From<PersonWithAutoId>().Insert(p => new { p.FirstName, p.Age }));
234+
await db.InsertOnlyAsync(new PersonWithAutoId { FirstName = "Amy", Age = 27 }, p => new { p.FirstName, p.Age });
235235
Assert.That(db.GetLastSql(), Is.EqualTo("INSERT INTO `PersonWithAutoId` (`FirstName`,`Age`) VALUES ('Amy',27)"));
236236

237237
await db.UpdateAsync(new Person { Id = 1, FirstName = "Jimi", LastName = "Hendrix", Age = 27 });

tests/ServiceStack.OrmLiteV45.Tests/ApiPostgreSqlTestsAsync.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ await db.InsertAsync(new Person { Id = 8, FirstName = "Tupac", LastName = "Shaku
231231
await db.InsertAllAsync(new[] { new Person { Id = 10, FirstName = "Biggie", LastName = "Smalls", Age = 24 } });
232232
Assert.That(db.GetLastSql(), Is.EqualTo("INSERT INTO \"person\" (\"id\",\"first_name\",\"last_name\",\"age\") VALUES (:Id,:FirstName,:LastName,:Age)"));
233233

234-
await db.InsertOnlyAsync(new PersonWithAutoId { FirstName = "Amy", Age = 27 }, db.From<PersonWithAutoId>().Insert(p => new { p.FirstName, p.Age }));
234+
await db.InsertOnlyAsync(new PersonWithAutoId { FirstName = "Amy", Age = 27 }, p => new { p.FirstName, p.Age });
235235
Assert.That(db.GetLastSql(), Is.EqualTo("INSERT INTO \"person_with_auto_id\" (\"first_name\",\"age\") VALUES ('Amy',27)"));
236236

237237
await db.UpdateAsync(new Person { Id = 1, FirstName = "Jimi", LastName = "Hendrix", Age = 27 });

tests/ServiceStack.OrmLiteV45.Tests/ApiSqlServerTestsAsync.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ await db.InsertAsync(new Person { Id = 8, FirstName = "Tupac", LastName = "Shaku
232232
await db.InsertAllAsync(new[] { new Person { Id = 10, FirstName = "Biggie", LastName = "Smalls", Age = 24 } });
233233
Assert.That(db.GetLastSql(), Is.EqualTo("INSERT INTO \"Person\" (\"Id\",\"FirstName\",\"LastName\",\"Age\") VALUES (@Id,@FirstName,@LastName,@Age)"));
234234

235-
await db.InsertOnlyAsync(new PersonWithAutoId { FirstName = "Amy", Age = 27 }, db.From<PersonWithAutoId>().Insert(p => new { p.FirstName, p.Age }));
235+
await db.InsertOnlyAsync(new PersonWithAutoId { FirstName = "Amy", Age = 27 }, p => new { p.FirstName, p.Age });
236236
Assert.That(db.GetLastSql(), Is.EqualTo("INSERT INTO \"PersonWithAutoId\" (\"FirstName\",\"Age\") VALUES ('Amy',27)"));
237237

238238
await db.UpdateAsync(new Person { Id = 1, FirstName = "Jimi", LastName = "Hendrix", Age = 27 });

tests/ServiceStack.OrmLiteV45.Tests/ApiSqliteTestsAsync.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ await db.InsertAsync(new Person { Id = 8, FirstName = "Tupac", LastName = "Shaku
231231
await db.InsertAllAsync(new[] { new Person { Id = 10, FirstName = "Biggie", LastName = "Smalls", Age = 24 } });
232232
Assert.That(db.GetLastSql(), Is.EqualTo("INSERT INTO \"Person\" (\"Id\",\"FirstName\",\"LastName\",\"Age\") VALUES (@Id,@FirstName,@LastName,@Age)"));
233233

234-
await db.InsertOnlyAsync(new PersonWithAutoId { FirstName = "Amy", Age = 27 }, db.From<PersonWithAutoId>().Insert(p => new { p.FirstName, p.Age }));
234+
await db.InsertOnlyAsync(new PersonWithAutoId { FirstName = "Amy", Age = 27 }, p => new { p.FirstName, p.Age });
235235
Assert.That(db.GetLastSql(), Is.EqualTo("INSERT INTO \"PersonWithAutoId\" (\"FirstName\",\"Age\") VALUES ('Amy',27)"));
236236

237237
await db.UpdateAsync(new Person { Id = 1, FirstName = "Jimi", LastName = "Hendrix", Age = 27 });

0 commit comments

Comments
 (0)