Permalink
Browse files

Add InsertIntoSelectAsync APIs

  • Loading branch information...
mythz committed Dec 5, 2018
1 parent a60b9ea commit ae9f8f27f5dcdaf619df34a10e05fb59bc5b13dc
@@ -881,6 +881,8 @@ public static long LongScalar(this IDbCommand dbCmd)
return ToLong(result);
}
internal static long ToLong(int result) => result;
internal static long ToLong(object result)
{
if (result is DBNull) return default(long);
@@ -93,7 +93,7 @@ public static void InsertUsingDefaults<T>(this IDbConnection dbConn, params T[]
/// <summary>
/// Insert results from SELECT SqlExpression, use selectIdentity to retrieve the last insert AutoIncrement id (if any). E.g:
/// <para>var id = db.InsertIntoSelect&lt;Contact&gt;(db.From&lt;Person&gt;().Select(x => new { x.Id, Surname == x.LastName }))</para>
/// <para>db.InsertIntoSelect&lt;Contact&gt;(db.From&lt;Person&gt;().Select(x => new { x.Id, Surname == x.LastName }))</para>
/// </summary>
public static long InsertIntoSelect<T>(this IDbConnection dbConn, ISqlExpression query)
{
@@ -102,7 +102,7 @@ public static long InsertIntoSelect<T>(this IDbConnection dbConn, ISqlExpression
/// <summary>
/// Insert results from SELECT SqlExpression, use selectIdentity to retrieve the last insert AutoIncrement id (if any). E.g:
/// <para>var id = db.InsertIntoSelect&lt;Contact&gt;(db.From&lt;Person&gt;().Select(x => new { x.Id, Surname == x.LastName }))</para>
/// <para>db.InsertIntoSelect&lt;Contact&gt;(db.From&lt;Person&gt;().Select(x => new { x.Id, Surname == x.LastName }))</para>
/// </summary>
public static long InsertIntoSelect<T>(this IDbConnection dbConn, ISqlExpression query, Action<IDbCommand> commandFilter)
{
@@ -76,6 +76,35 @@ public static Task InsertAsync<T>(this IDbConnection dbConn, Action<IDbCommand>
return dbConn.Exec(dbCmd => dbCmd.InsertAsync(commandFilter:commandFilter, token:token, objs:objs));
}
/// <summary>
/// Insert 1 or more POCOs in a transaction using Table default values when defined. E.g:
/// <para>db.InsertUsingDefaultsAsync(new Person { FirstName = "Tupac", LastName = "Shakur" },</para>
/// <para> new Person { FirstName = "Biggie", LastName = "Smalls" })</para>
/// </summary>
public static Task InsertUsingDefaultsAsync<T>(this IDbConnection dbConn, T[] objs, CancellationToken token=default(CancellationToken))
{
return dbConn.Exec(dbCmd => dbCmd.InsertUsingDefaultsAsync(objs, token));
}
/// <summary>
/// Insert results from SELECT SqlExpression, use selectIdentity to retrieve the last insert AutoIncrement id (if any). E.g:
/// <para>db.InsertIntoSelectAsync&lt;Contact&gt;(db.From&lt;Person&gt;().Select(x => new { x.Id, Surname == x.LastName }))</para>
/// </summary>
public static Task<long> InsertIntoSelectAsync<T>(this IDbConnection dbConn, ISqlExpression query, CancellationToken token=default(CancellationToken))
{
return dbConn.Exec(dbCmd => dbCmd.InsertIntoSelectAsync<T>(query, commandFilter: null, token:token));
}
/// <summary>
/// Insert results from SELECT SqlExpression, use selectIdentity to retrieve the last insert AutoIncrement id (if any). E.g:
/// <para>db.InsertIntoSelectAsync&lt;Contact&gt;(db.From&lt;Person&gt;().Select(x => new { x.Id, Surname == x.LastName }))</para>
/// </summary>
public static Task<long> InsertIntoSelectAsync<T>(this IDbConnection dbConn, ISqlExpression query, Action<IDbCommand> commandFilter, CancellationToken token = default(CancellationToken))
{
return dbConn.Exec(dbCmd => dbCmd.InsertIntoSelectAsync<T>(query, commandFilter: commandFilter, token:token));
}
/// <summary>
/// Insert a collection of POCOs in a transaction. E.g:
/// <para>db.InsertAllAsync(new[] { new Person { Id = 9, FirstName = "Biggie", LastName = "Smalls", Age = 24 } })</para>
@@ -689,7 +689,10 @@ internal static void Insert<T>(this IDbCommand dbCmd, Action<IDbCommand> command
dbCmd.InsertAll(objs: objs, commandFilter: commandFilter);
}
internal static long InsertIntoSelect<T>(this IDbCommand dbCmd, ISqlExpression query, Action<IDbCommand> commandFilter)
internal static long InsertIntoSelect<T>(this IDbCommand dbCmd, ISqlExpression query, Action<IDbCommand> commandFilter) =>
dbCmd.InsertIntoSelectInternal<T>(query, commandFilter).ExecNonQuery();
internal static IDbCommand InsertIntoSelectInternal<T>(this IDbCommand dbCmd, ISqlExpression query, Action<IDbCommand> commandFilter)
{
var dialectProvider = dbCmd.GetDialectProvider();
@@ -701,16 +704,15 @@ internal static long InsertIntoSelect<T>(this IDbCommand dbCmd, ISqlExpression q
var fieldsOrAliases = selectFields
.Map(x => x.Original.ToString().LastRightPart(" AS ").Trim().StripQuotes());
dialectProvider.PrepareParameterizedInsertStatement<T>(dbCmd, insertFields: fieldsOrAliases);
dbCmd.SetParameters(query.Params);
dbCmd.CommandText = dbCmd.CommandText.LeftPart(")") + ")\n" + sql;
commandFilter?.Invoke(dbCmd); //dbCmd.OnConflictInsert() needs to be applied before last insert id
return dbCmd.ExecNonQuery();
return dbCmd;
}
internal static void InsertAll<T>(this IDbCommand dbCmd, IEnumerable<T> objs, Action<IDbCommand> commandFilter)
@@ -157,6 +157,35 @@ public void Does_InsertIntoSelect_LRARichiesta()
Assert.That(result, Is.EqualTo(numeroRichieste));
}
}
[Test]
public async Task Does_InsertIntoSelect_LRARichiesta_Async()
{
using (var db = OpenDbConnection())
{
db.DropAndCreateTable<LRARichiesta>();
db.DropAndCreateTable<LRARisultato>();
long numeroRichieste = await db.CountAsync<LRARichiesta>();
var q = db.From<LRARichiesta>()
.Select(ric => new //LRARisultato
{
AnalisiId = 1,
Commento = ric.Commento,
TipoValore = 1,
Stato = 1,
RisultatoId = 1,
DataOraRicezione = DateTime.UtcNow,
DataModifica = DateTime.UtcNow,
VersioneRecord = 1
});
long result = await db.InsertIntoSelectAsync<LRARisultato>(q);
Assert.That(result, Is.EqualTo(numeroRichieste));
}
}
private static void CreateTables(IDbConnection db)
{
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NUnit.Framework;
using ServiceStack.Common.Tests.Models;
using ServiceStack.DataAnnotations;
@@ -424,6 +425,56 @@ public void Can_InsertIntoSelect_using_Custom_Select()
Assert.That(result.FullName, Is.EqualTo(userAuth.FirstName + " " + userAuth.LastName));
}
}
[Test]
public async Task Can_InsertIntoSelect_using_Custom_Select_Async()
{
using (var db = OpenDbConnection())
{
db.DropAndCreateTable<UserAuth>();
db.DropAndCreateTable<SubUserAuth>();
var userAuth = new UserAuth {
Id = 1,
UserName = "UserName",
Email = "a@b.com",
PrimaryEmail = "c@d.com",
FirstName = "FirstName",
LastName = "LastName",
DisplayName = "DisplayName",
Salt = "Salt",
PasswordHash = "PasswordHash",
CreatedDate = DateTime.Now,
ModifiedDate = DateTime.UtcNow,
};
await db.InsertAsync(userAuth);
// OrmLiteUtils.PrintSql();
var q = db.From<UserAuth>()
.Where(x => x.UserName == "UserName")
.Select(x => new {
FullName = x.FirstName + " " + x.LastName,
GivenName = x.FirstName,
Surname = x.LastName,
x.Email,
x.UserName,
});
var rowsInserted = await db.InsertIntoSelectAsync<SubUserAuth>(q);
Assert.That(rowsInserted, Is.EqualTo(1));
var result = (await db.SelectAsync<SubUserAuth>())[0];
Assert.That(result.Id, Is.GreaterThan(0));
Assert.That(result.UserName, Is.EqualTo(userAuth.UserName));
Assert.That(result.Email, Is.EqualTo(userAuth.Email));
Assert.That(result.GivenName, Is.EqualTo(userAuth.FirstName));
Assert.That(result.Surname, Is.EqualTo(userAuth.LastName));
Assert.That(result.FullName, Is.EqualTo(userAuth.FirstName + " " + userAuth.LastName));
}
}
}
public class Market

0 comments on commit ae9f8f2

Please sign in to comment.