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

Commit 6ccc018

Browse files
committed
Add TemplateDbFiltersAsync
1 parent e46def2 commit 6ccc018

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Threading.Tasks;
5+
using ServiceStack.Data;
6+
using ServiceStack.Templates;
7+
8+
namespace ServiceStack.OrmLite
9+
{
10+
public class TemplateDbFiltersAsync : TemplateFilter
11+
{
12+
private IDbConnectionFactory dbFactory;
13+
public IDbConnectionFactory DbFactory
14+
{
15+
get => dbFactory ?? (dbFactory = Context.Container.Resolve<IDbConnectionFactory>());
16+
set => dbFactory = value;
17+
}
18+
19+
Task<object> exec<T>(Func<IDbConnection, Task<T>> fn, TemplateScopeContext scope, object options)
20+
{
21+
try
22+
{
23+
using (var db = DbFactory.Open())
24+
{
25+
return fn(db).Then(x => (object)x);
26+
}
27+
}
28+
catch (Exception ex)
29+
{
30+
throw new StopFilterExecutionException(scope, options, ex);
31+
}
32+
}
33+
34+
public Task<object> dbSelect(TemplateScopeContext scope, string sql) =>
35+
exec(db => db.SelectAsync<Dictionary<string, object>>(sql), scope, null);
36+
37+
public Task<object> dbSelect(TemplateScopeContext scope, string sql, Dictionary<string, object> args) =>
38+
exec(db => db.SelectAsync<Dictionary<string, object>>(sql, args), scope, null);
39+
40+
public Task<object> dbSelect(TemplateScopeContext scope, string sql, Dictionary<string, object> args, object options) =>
41+
exec(db => db.SelectAsync<Dictionary<string, object>>(sql, args), scope, options);
42+
43+
44+
public Task<object> dbSingle(TemplateScopeContext scope, string sql) =>
45+
exec(db => db.SingleAsync<Dictionary<string, object>>(sql), scope, null);
46+
47+
public Task<object> dbSingle(TemplateScopeContext scope, string sql, Dictionary<string, object> args) =>
48+
exec(db => db.SingleAsync<Dictionary<string, object>>(sql, args), scope, null);
49+
50+
public Task<object> dbSingle(TemplateScopeContext scope, string sql, Dictionary<string, object> args, object options) =>
51+
exec(db => db.SingleAsync<Dictionary<string, object>>(sql, args), scope, options);
52+
53+
54+
public Task<object> dbScalar(TemplateScopeContext scope, string sql) =>
55+
exec(db => db.ScalarAsync<object>(sql), scope, null);
56+
57+
public Task<object> dbScalar(TemplateScopeContext scope, string sql, Dictionary<string, object> args) =>
58+
exec(db => db.ScalarAsync<object>(sql, args), scope, null);
59+
60+
public Task<object> dbScalar(TemplateScopeContext scope, string sql, Dictionary<string, object> args, object options) =>
61+
exec(db => db.ScalarAsync<object>(sql, args), scope, options);
62+
63+
64+
public Task<object> dbExec(TemplateScopeContext scope, string sql) =>
65+
exec(db => db.ExecuteSqlAsync(sql), scope, null);
66+
67+
public Task<object> dbExec(TemplateScopeContext scope, string sql, Dictionary<string, object> args) =>
68+
exec(db => db.ExecuteSqlAsync(sql, args), scope, null);
69+
70+
public Task<object> dbExec(TemplateScopeContext scope, string sql, Dictionary<string, object> args, object options) =>
71+
exec(db => db.ExecuteSqlAsync(sql, args), scope, options);
72+
73+
74+
public string sqlQuote(string name) => OrmLiteConfig.DialectProvider.GetQuotedName(name);
75+
public string sqlConcat(IEnumerable<object> values) => OrmLiteConfig.DialectProvider.SqlConcat(values);
76+
public string sqlCurrency(string fieldOrValue) => OrmLiteConfig.DialectProvider.SqlCurrency(fieldOrValue);
77+
public string sqlCurrency(string fieldOrValue, string symbol) => OrmLiteConfig.DialectProvider.SqlCurrency(fieldOrValue, symbol);
78+
79+
public string sqlBool(bool value) => OrmLiteConfig.DialectProvider.SqlBool(value);
80+
public string sqlTrue() => OrmLiteConfig.DialectProvider.SqlBool(true);
81+
public string sqlFalse() => OrmLiteConfig.DialectProvider.SqlBool(false);
82+
public string sqlLimit(int? offset, int? limit) => padCondition(OrmLiteConfig.DialectProvider.SqlLimit(offset, limit));
83+
public string sqlLimit(int? limit) => padCondition(OrmLiteConfig.DialectProvider.SqlLimit(null, limit));
84+
public string sqlSkip(int? offset) => padCondition(OrmLiteConfig.DialectProvider.SqlLimit(offset, null));
85+
public string sqlTake(int? limit) => padCondition(OrmLiteConfig.DialectProvider.SqlLimit(null, limit));
86+
private string padCondition(string text) => string.IsNullOrEmpty(text) ? "" : " " + text;
87+
}
88+
}

0 commit comments

Comments
 (0)