55using System . Threading . Tasks ;
66using ServiceStack . Data ;
77using ServiceStack . Script ;
8- using ServiceStack . Text ;
98
109namespace ServiceStack . OrmLite
1110{
@@ -15,7 +14,7 @@ public class TemplateDbFiltersAsync : DbScriptsAsync {}
1514 public partial class DbScriptsAsync : ScriptMethods
1615 {
1716 private const string DbInfo = "__dbinfo" ; // Keywords.DbInfo
18- private const string DbConnection = "__dbconnection" ; // useDbConnection global
17+ private const string DbConnection = "__dbconnection" ; // useDb global
1918
2019 private IDbConnectionFactory dbFactory ;
2120 public IDbConnectionFactory DbFactory
@@ -42,6 +41,21 @@ public async Task<IDbConnection> OpenDbConnectionAsync(ScriptScopeContext scope,
4241 return await DbFactory . OpenAsync ( ) ;
4342 }
4443
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+
4559 public IgnoreResult useDb ( ScriptScopeContext scope , Dictionary < string , object > dbConnOptions )
4660 {
4761 if ( dbConnOptions == null )
@@ -82,11 +96,9 @@ async Task<object> exec<T>(Func<IDbConnection, Task<T>> fn, ScriptScopeContext s
8296 {
8397 try
8498 {
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 ;
90102 }
91103 catch ( Exception ex )
92104 {
@@ -172,28 +184,34 @@ public Task<object> dbTableNamesWithRowCounts(ScriptScopeContext scope, Dictiona
172184
173185 public Task < object > dbColumnNames ( ScriptScopeContext scope , string tableName ) => dbColumnNames ( scope , tableName , null ) ;
174186 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 ) ;
176188
177189 public Task < object > dbColumns ( ScriptScopeContext scope , string tableName ) => dbColumns ( scope , tableName , null ) ;
178190 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 ) ;
180192
181193 public Task < object > dbDesc ( ScriptScopeContext scope , string sql ) => dbDesc ( scope , sql , null ) ;
182194 public Task < object > dbDesc ( ScriptScopeContext scope , string sql , object options ) => exec ( db => db . GetTableColumnsAsync ( sql ) , scope , options ) ;
183195
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 ) ;
197215
198216 public string sqlVerifyFragment ( string sql ) => sql . SqlVerifyFragment ( ) ;
199217 public bool isUnsafeSql ( string sql ) => OrmLiteUtils . isUnsafeSql ( sql , OrmLiteUtils . VerifySqlRegEx ) ;
0 commit comments