Permalink
Fetching contributors…
Cannot retrieve contributors at this time
113 lines (87 sloc) 5.88 KB
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using ServiceStack.Data;
using ServiceStack.Templates;
namespace ServiceStack.OrmLite
{
public class TemplateDbFiltersAsync : TemplateFilter
{
private IDbConnectionFactory dbFactory;
public IDbConnectionFactory DbFactory
{
get => dbFactory ?? (dbFactory = Context.Container.Resolve<IDbConnectionFactory>());
set => dbFactory = value;
}
public async Task<IDbConnection> OpenDbConnectionAsync(TemplateScopeContext scope, Dictionary<string, object> options)
{
if (options != null)
{
if (options.TryGetValue("connectionString", out var connectionString))
return options.TryGetValue("providerName", out var providerName)
? await DbFactory.OpenDbConnectionStringAsync((string)connectionString, (string)providerName)
: await DbFactory.OpenDbConnectionStringAsync((string)connectionString);
if (options.TryGetValue("namedConnection", out var namedConnection))
return await DbFactory.OpenDbConnectionAsync((string)namedConnection);
}
if (scope.PageResult.Args.TryGetValue("__dbinfo", out var oDbInfo) && oDbInfo is ConnectionInfo dbInfo) // Keywords,DbInfo
return await DbFactory.OpenDbConnectionAsync(dbInfo);
return await DbFactory.OpenAsync();
}
async Task<object> exec<T>(Func<IDbConnection, Task<T>> fn, TemplateScopeContext scope, object options)
{
try
{
using (var db = await OpenDbConnectionAsync(scope, options as Dictionary<string, object>))
{
var result = await fn(db);
return result;
}
}
catch (Exception ex)
{
throw new StopFilterExecutionException(scope, options, ex);
}
}
public Task<object> dbSelect(TemplateScopeContext scope, string sql) =>
exec(db => db.SqlListAsync<Dictionary<string, object>>(sql), scope, null);
public Task<object> dbSelect(TemplateScopeContext scope, string sql, Dictionary<string, object> args) =>
exec(db => db.SqlListAsync<Dictionary<string, object>>(sql, args), scope, null);
public Task<object> dbSelect(TemplateScopeContext scope, string sql, Dictionary<string, object> args, object options) =>
exec(db => db.SqlListAsync<Dictionary<string, object>>(sql, args), scope, options);
public Task<object> dbSingle(TemplateScopeContext scope, string sql) =>
exec(db => db.SingleAsync<Dictionary<string, object>>(sql), scope, null);
public Task<object> dbSingle(TemplateScopeContext scope, string sql, Dictionary<string, object> args) =>
exec(db => db.SingleAsync<Dictionary<string, object>>(sql, args), scope, null);
public Task<object> dbSingle(TemplateScopeContext scope, string sql, Dictionary<string, object> args, object options) =>
exec(db => db.SingleAsync<Dictionary<string, object>>(sql, args), scope, options);
public Task<object> dbScalar(TemplateScopeContext scope, string sql) =>
exec(db => db.ScalarAsync<object>(sql), scope, null);
public Task<object> dbScalar(TemplateScopeContext scope, string sql, Dictionary<string, object> args) =>
exec(db => db.ScalarAsync<object>(sql, args), scope, null);
public Task<object> dbScalar(TemplateScopeContext scope, string sql, Dictionary<string, object> args, object options) =>
exec(db => db.ScalarAsync<object>(sql, args), scope, options);
public Task<object> dbExec(TemplateScopeContext scope, string sql) =>
exec(db => db.ExecuteSqlAsync(sql), scope, null);
public Task<object> dbExec(TemplateScopeContext scope, string sql, Dictionary<string, object> args) =>
exec(db => db.ExecuteSqlAsync(sql, args), scope, null);
public Task<object> dbExec(TemplateScopeContext scope, string sql, Dictionary<string, object> args, object options) =>
exec(db => db.ExecuteSqlAsync(sql, args), scope, options);
public string sqlQuote(string name) => OrmLiteConfig.DialectProvider.GetQuotedName(name);
public string sqlConcat(IEnumerable<object> values) => OrmLiteConfig.DialectProvider.SqlConcat(values);
public string sqlCurrency(string fieldOrValue) => OrmLiteConfig.DialectProvider.SqlCurrency(fieldOrValue);
public string sqlCurrency(string fieldOrValue, string symbol) => OrmLiteConfig.DialectProvider.SqlCurrency(fieldOrValue, symbol);
public string sqlBool(bool value) => OrmLiteConfig.DialectProvider.SqlBool(value);
public string sqlTrue() => OrmLiteConfig.DialectProvider.SqlBool(true);
public string sqlFalse() => OrmLiteConfig.DialectProvider.SqlBool(false);
public string sqlLimit(int? offset, int? limit) => padCondition(OrmLiteConfig.DialectProvider.SqlLimit(offset, limit));
public string sqlLimit(int? limit) => padCondition(OrmLiteConfig.DialectProvider.SqlLimit(null, limit));
public string sqlSkip(int? offset) => padCondition(OrmLiteConfig.DialectProvider.SqlLimit(offset, null));
public string sqlTake(int? limit) => padCondition(OrmLiteConfig.DialectProvider.SqlLimit(null, limit));
public string ormliteVar(string name) => OrmLiteConfig.DialectProvider.Variables.TryGetValue(name, out var value) ? value : null;
public bool isUnsafeSql(string sql) => OrmLiteUtils.isUnsafeSql(sql, OrmLiteUtils.VerifySqlRegEx);
public bool isUnsafeSqlFragment(string sql) => OrmLiteUtils.isUnsafeSql(sql, OrmLiteUtils.VerifyFragmentRegEx);
private string padCondition(string text) => string.IsNullOrEmpty(text) ? "" : " " + text;
}
}