5
5
using System . Threading . Tasks ;
6
6
using ServiceStack . Data ;
7
7
using ServiceStack . Script ;
8
- using ServiceStack . Text ;
9
8
10
9
namespace ServiceStack . OrmLite
11
10
{
@@ -15,7 +14,7 @@ public class TemplateDbFiltersAsync : DbScriptsAsync {}
15
14
public partial class DbScriptsAsync : ScriptMethods
16
15
{
17
16
private const string DbInfo = "__dbinfo" ; // Keywords.DbInfo
18
- private const string DbConnection = "__dbconnection" ; // useDbConnection global
17
+ private const string DbConnection = "__dbconnection" ; // useDb global
19
18
20
19
private IDbConnectionFactory dbFactory ;
21
20
public IDbConnectionFactory DbFactory
@@ -42,6 +41,21 @@ public async Task<IDbConnection> OpenDbConnectionAsync(ScriptScopeContext scope,
42
41
return await DbFactory . OpenAsync ( ) ;
43
42
}
44
43
44
+ T dialect < T > ( ScriptScopeContext scope , Func < IOrmLiteDialectProvider , T > fn )
45
+ {
46
+ if ( scope . PageResult != null )
47
+ {
48
+ if ( scope . PageResult . Args . TryGetValue ( DbInfo , out var oDbInfo ) && oDbInfo is ConnectionInfo dbInfo )
49
+ return fn ( DbFactory . GetDialectProvider ( dbInfo ) ) ;
50
+
51
+ if ( scope . PageResult . Args . TryGetValue ( DbConnection , out var oDbConn ) && oDbConn is Dictionary < string , object > useDb )
52
+ return fn ( DbFactory . GetDialectProvider (
53
+ providerName : useDb . GetValueOrDefault ( "providerName" ) ? . ToString ( ) ,
54
+ namedConnection : useDb . GetValueOrDefault ( "namedConnection" ) ? . ToString ( ) ) ) ;
55
+ }
56
+ return fn ( OrmLiteConfig . DialectProvider ) ;
57
+ }
58
+
45
59
public IgnoreResult useDb ( ScriptScopeContext scope , Dictionary < string , object > dbConnOptions )
46
60
{
47
61
if ( dbConnOptions == null )
@@ -82,11 +96,9 @@ async Task<object> exec<T>(Func<IDbConnection, Task<T>> fn, ScriptScopeContext s
82
96
{
83
97
try
84
98
{
85
- using ( var db = await OpenDbConnectionAsync ( scope , options as Dictionary < string , object > ) )
86
- {
87
- var result = await fn ( db ) ;
88
- return result ;
89
- }
99
+ using var db = await OpenDbConnectionAsync ( scope , options as Dictionary < string , object > ) ;
100
+ var result = await fn ( db ) ;
101
+ return result ;
90
102
}
91
103
catch ( Exception ex )
92
104
{
@@ -172,28 +184,34 @@ public Task<object> dbTableNamesWithRowCounts(ScriptScopeContext scope, Dictiona
172
184
173
185
public Task < object > dbColumnNames ( ScriptScopeContext scope , string tableName ) => dbColumnNames ( scope , tableName , null ) ;
174
186
public Task < object > dbColumnNames ( ScriptScopeContext scope , string tableName , object options ) =>
175
- exec ( async db => ( await db . GetTableColumnsAsync ( $ "SELECT * FROM { sqlQuote ( tableName ) } ") ) . Select ( x => x . ColumnName ) . ToArray ( ) , scope , options ) ;
187
+ exec ( async db => ( await db . GetTableColumnsAsync ( $ "SELECT * FROM { sqlQuote ( scope , tableName ) } ") ) . Select ( x => x . ColumnName ) . ToArray ( ) , scope , options ) ;
176
188
177
189
public Task < object > dbColumns ( ScriptScopeContext scope , string tableName ) => dbColumns ( scope , tableName , null ) ;
178
190
public Task < object > dbColumns ( ScriptScopeContext scope , string tableName , object options ) =>
179
- exec ( db => db . GetTableColumnsAsync ( $ "SELECT * FROM { sqlQuote ( tableName ) } ") , scope , options ) ;
191
+ exec ( db => db . GetTableColumnsAsync ( $ "SELECT * FROM { sqlQuote ( scope , tableName ) } ") , scope , options ) ;
180
192
181
193
public Task < object > dbDesc ( ScriptScopeContext scope , string sql ) => dbDesc ( scope , sql , null ) ;
182
194
public Task < object > dbDesc ( ScriptScopeContext scope , string sql , object options ) => exec ( db => db . GetTableColumnsAsync ( sql ) , scope , options ) ;
183
195
184
- public string sqlQuote ( string name ) => OrmLiteConfig . DialectProvider . GetQuotedName ( name ) ;
185
- public string sqlConcat ( IEnumerable < object > values ) => OrmLiteConfig . DialectProvider . SqlConcat ( values ) ;
186
- public string sqlCurrency ( string fieldOrValue ) => OrmLiteConfig . DialectProvider . SqlCurrency ( fieldOrValue ) ;
187
- public string sqlCurrency ( string fieldOrValue , string symbol ) => OrmLiteConfig . DialectProvider . SqlCurrency ( fieldOrValue , symbol ) ;
188
-
189
- public string sqlBool ( bool value ) => OrmLiteConfig . DialectProvider . SqlBool ( value ) ;
190
- public string sqlTrue ( ) => OrmLiteConfig . DialectProvider . SqlBool ( true ) ;
191
- public string sqlFalse ( ) => OrmLiteConfig . DialectProvider . SqlBool ( false ) ;
192
- public string sqlLimit ( int ? offset , int ? limit ) => padCondition ( OrmLiteConfig . DialectProvider . SqlLimit ( offset , limit ) ) ;
193
- public string sqlLimit ( int ? limit ) => padCondition ( OrmLiteConfig . DialectProvider . SqlLimit ( null , limit ) ) ;
194
- public string sqlSkip ( int ? offset ) => padCondition ( OrmLiteConfig . DialectProvider . SqlLimit ( offset , null ) ) ;
195
- public string sqlTake ( int ? limit ) => padCondition ( OrmLiteConfig . DialectProvider . SqlLimit ( null , limit ) ) ;
196
- public string ormliteVar ( string name ) => OrmLiteConfig . DialectProvider . Variables . TryGetValue ( name , out var value ) ? value : null ;
196
+ public string sqlQuote ( ScriptScopeContext scope , string name ) => dialect ( scope , d => d . GetQuotedName ( name ) ) ;
197
+ public string sqlConcat ( ScriptScopeContext scope , IEnumerable < object > values ) => dialect ( scope , d => d . SqlConcat ( values ) ) ;
198
+ public string sqlCurrency ( ScriptScopeContext scope , string fieldOrValue ) => dialect ( scope , d => d . SqlCurrency ( fieldOrValue ) ) ;
199
+ public string sqlCurrency ( ScriptScopeContext scope , string fieldOrValue , string symbol ) =>
200
+ dialect ( scope , d => d . SqlCurrency ( fieldOrValue , symbol ) ) ;
201
+
202
+ public string sqlBool ( ScriptScopeContext scope , bool value ) => dialect ( scope , d => d . SqlBool ( value ) ) ;
203
+ public string sqlTrue ( ScriptScopeContext scope ) => dialect ( scope , d => d . SqlBool ( true ) ) ;
204
+ public string sqlFalse ( ScriptScopeContext scope ) => dialect ( scope , d => d . SqlBool ( false ) ) ;
205
+ public string sqlLimit ( ScriptScopeContext scope , int ? offset , int ? limit ) =>
206
+ dialect ( scope , d => padCondition ( d . SqlLimit ( offset , limit ) ) ) ;
207
+ public string sqlLimit ( ScriptScopeContext scope , int ? limit ) =>
208
+ dialect ( scope , d => padCondition ( d . SqlLimit ( null , limit ) ) ) ;
209
+ public string sqlSkip ( ScriptScopeContext scope , int ? offset ) =>
210
+ dialect ( scope , d => padCondition ( d . SqlLimit ( offset , null ) ) ) ;
211
+ public string sqlTake ( ScriptScopeContext scope , int ? limit ) =>
212
+ dialect ( scope , d => padCondition ( d . SqlLimit ( null , limit ) ) ) ;
213
+ public string ormliteVar ( ScriptScopeContext scope , string name ) =>
214
+ dialect ( scope , d => d . Variables . TryGetValue ( name , out var value ) ? value : null ) ;
197
215
198
216
public string sqlVerifyFragment ( string sql ) => sql . SqlVerifyFragment ( ) ;
199
217
public bool isUnsafeSql ( string sql ) => OrmLiteUtils . isUnsafeSql ( sql , OrmLiteUtils . VerifySqlRegEx ) ;
0 commit comments