Skip to content

Commit

Permalink
Add helpers for concatenating Sql
Browse files Browse the repository at this point in the history
  • Loading branch information
schotime committed Nov 12, 2020
1 parent 33be5b8 commit cf50d31
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/NPoco/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,28 @@ public IQueryProviderWithIncludes<T> Query<T>()
return new QueryProvider<T>(this);
}

public (List<T>, List<T1>, List<T2>, List<T3>) QueryMultiple<T, T1, T2, T3>(
Func<IQueryProviderWithIncludes<T>, IQueryProvider<T>> query1,
Func<IQueryProviderWithIncludes<T1>, IQueryProvider<T1>> query2,
Func<IQueryProviderWithIncludes<T2>, IQueryProvider<T2>> query3,
Func<IQueryProviderWithIncludes<T3>, IQueryProvider<T3>> query4
)
{
var qp1 = new QueryProvider<T>(this);
var qp2 = new QueryProvider<T1>(this);
var qp3 = new QueryProvider<T2>(this);
var qp4 = new QueryProvider<T3>(this);
query1.Invoke(qp1);
query2.Invoke(qp2);
query3.Invoke(qp3);
query4.Invoke(qp4);
var sql1 = ((INeedSql)qp1).GetSql();
var sql2 = ((INeedSql)qp2).GetSql();
var sql3 = ((INeedSql)qp3).GetSql();
var sql4 = ((INeedSql)qp4).GetSql();
return FetchMultiple<T, T1, T2, T3>(sql1.Concat(sql2, ";").Concat(sql3, ";").Concat(sql4, ";"));
}

private IEnumerable<T> Query<T>(T instance, Sql Sql)
{
return QueryImp(instance, null, null, Sql);
Expand Down
12 changes: 11 additions & 1 deletion src/NPoco/Linq/SimpleQueryProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public interface IQueryProviderWithIncludes<T> : IQueryProvider<T>
IQueryProviderWithIncludes<T> UsingAlias(string empty);
}

public class AsyncQueryProvider<T> : IAsyncQueryProviderWithIncludes<T>, ISimpleQueryProviderExpression<T>, INeedDatabase
public class AsyncQueryProvider<T> : IAsyncQueryProviderWithIncludes<T>, ISimpleQueryProviderExpression<T>, INeedDatabase, INeedSql
{
protected readonly Database _database;
protected SqlExpression<T> _sqlExpression;
Expand Down Expand Up @@ -473,6 +473,16 @@ IDatabase INeedDatabase.GetDatabase()
{
return _database;
}

Sql INeedSql.GetSql()
{
return BuildSql();
}
}

public interface INeedSql
{
Sql GetSql();
}

public interface INeedDatabase
Expand Down
7 changes: 7 additions & 0 deletions src/NPoco/Sql.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NPoco
Expand Down Expand Up @@ -90,6 +91,12 @@ public Sql Append(Sql sql)
return this;
}

public Sql Concat(Sql sql, string delimiter)
{
sql._sql = delimiter + sql._sql;
return Append(sql);
}

public Sql Append(string sql, params object[] args)
{
Append(new Sql(sql, args));
Expand Down
11 changes: 11 additions & 0 deletions test/NPoco.Tests/SqlBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,16 @@ public void Test14()

Assert.AreEqual("SELECT 1 , COUNT(*) FROM test ", template.RawSql);
}

[Test]
public void Test15()
{
var sql1 = new Sql("select a from test1 where a = @0", 1);
var sql2 = new Sql("select b from test2 where b = @0", 2);
var sql = sql1.Concat(sql2, ";");
Assert.AreEqual("select a from test1 where a = @0\n;select b from test2 where b = @1", sql.SQL);
Assert.AreEqual(1, sql.Arguments[0]);
Assert.AreEqual(2, sql.Arguments[1]);
}
}
}

0 comments on commit cf50d31

Please sign in to comment.