1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Data ;
4
+ using ServiceStack . Data ;
5
+ using ServiceStack . Templates ;
6
+
7
+ namespace ServiceStack . OrmLite
8
+ {
9
+ public class TemplateDbFilters : TemplateFilter
10
+ {
11
+ public IDbConnectionFactory DbFactory { get ; set ; }
12
+ T exec < T > ( Func < IDbConnection , T > fn )
13
+ {
14
+ using ( var db = DbFactory . Open ( ) )
15
+ {
16
+ return fn ( db ) ;
17
+ }
18
+ }
19
+
20
+ public object dbSelect ( string sql ) => exec ( db => db . Select < Dictionary < string , object > > ( sql ) ) ;
21
+ public object dbSelect ( string sql , Dictionary < string , object > args ) => exec ( db => db . Select < Dictionary < string , object > > ( sql , args ) ) ;
22
+ public object dbSingle ( string sql ) => exec ( db => db . Single < Dictionary < string , object > > ( sql ) ) ;
23
+ public object dbSingle ( string sql , Dictionary < string , object > args ) => exec ( db => db . Single < Dictionary < string , object > > ( sql , args ) ) ;
24
+ public object dbScalar ( string sql ) => exec ( db => db . Scalar < object > ( sql ) ) ;
25
+ public object dbScalar ( string sql , Dictionary < string , object > args ) => exec ( db => db . Scalar < object > ( sql , args ) ) ;
26
+ public int dbExec ( string sql , Dictionary < string , object > args ) => exec ( db => db . ExecuteSql ( sql , args ) ) ;
27
+
28
+ public string sqlQuote ( string name ) => OrmLiteConfig . DialectProvider . GetQuotedName ( name ) ;
29
+ public string sqlConcat ( IEnumerable < object > values ) => OrmLiteConfig . DialectProvider . SqlConcat ( values ) ;
30
+ public string sqlCurrency ( string fieldOrValue ) => OrmLiteConfig . DialectProvider . SqlCurrency ( fieldOrValue ) ;
31
+ public string sqlCurrency ( string fieldOrValue , string symbol ) => OrmLiteConfig . DialectProvider . SqlCurrency ( fieldOrValue , symbol ) ;
32
+
33
+ public string sqlBool ( bool value ) => OrmLiteConfig . DialectProvider . SqlBool ( value ) ;
34
+ public string sqlTrue ( ) => OrmLiteConfig . DialectProvider . SqlBool ( true ) ;
35
+ public string sqlFalse ( ) => OrmLiteConfig . DialectProvider . SqlBool ( false ) ;
36
+ public string sqlLimit ( int ? offset , int ? limit ) => padCondition ( OrmLiteConfig . DialectProvider . SqlLimit ( offset , limit ) ) ;
37
+ public string sqlLimit ( int ? limit ) => padCondition ( OrmLiteConfig . DialectProvider . SqlLimit ( null , limit ) ) ;
38
+ public string sqlSkip ( int ? offset ) => padCondition ( OrmLiteConfig . DialectProvider . SqlLimit ( offset , null ) ) ;
39
+ public string sqlTake ( int ? limit ) => padCondition ( OrmLiteConfig . DialectProvider . SqlLimit ( null , limit ) ) ;
40
+ private string padCondition ( string text ) => string . IsNullOrEmpty ( text ) ? "" : " " + text ;
41
+ }
42
+ }
0 commit comments