@@ -12,6 +12,9 @@ public class TemplateDbFiltersAsync : DbScriptsAsync {}
1212
1313 public class DbScriptsAsync : ScriptMethods
1414 {
15+ private const string DbInfo = "__dbinfo" ; // Keywords.DbInfo
16+ private const string DbConnection = "__dbconnection" ; // useDbConnection global
17+
1518 private IDbConnectionFactory dbFactory ;
1619 public IDbConnectionFactory DbFactory
1720 {
@@ -20,22 +23,53 @@ public IDbConnectionFactory DbFactory
2023 }
2124
2225 public async Task < IDbConnection > OpenDbConnectionAsync ( ScriptScopeContext scope , Dictionary < string , object > options )
26+ {
27+ var dbConn = await OpenDbConnectionFromOptionsAsync ( options ) ;
28+ if ( dbConn != null )
29+ return dbConn ;
30+
31+ if ( scope . PageResult != null )
32+ {
33+ if ( scope . PageResult . Args . TryGetValue ( DbInfo , out var oDbInfo ) && oDbInfo is ConnectionInfo dbInfo )
34+ return await DbFactory . OpenDbConnectionAsync ( dbInfo ) ;
35+
36+ if ( scope . PageResult . Args . TryGetValue ( DbConnection , out var oDbConn ) && oDbConn is Dictionary < string , object > globalDbConn )
37+ return await OpenDbConnectionFromOptionsAsync ( globalDbConn ) ;
38+ }
39+
40+ return await DbFactory . OpenAsync ( ) ;
41+ }
42+
43+ public IgnoreResult useDb ( ScriptScopeContext scope , Dictionary < string , object > dbConnOptions )
44+ {
45+ if ( dbConnOptions != null )
46+ {
47+ if ( ! dbConnOptions . ContainsKey ( "connectionString" ) && ! dbConnOptions . ContainsKey ( "namedConnection" ) )
48+ throw new NotSupportedException ( nameof ( useDb ) + " requires either 'connectionString' or 'namedConnection' property" ) ;
49+
50+ scope . PageResult . Args [ DbConnection ] = dbConnOptions ;
51+ }
52+ return IgnoreResult . Value ;
53+ }
54+
55+ private async Task < IDbConnection > OpenDbConnectionFromOptionsAsync ( Dictionary < string , object > options )
2356 {
2457 if ( options != null )
2558 {
2659 if ( options . TryGetValue ( "connectionString" , out var connectionString ) )
60+ {
2761 return options . TryGetValue ( "providerName" , out var providerName )
28- ? await DbFactory . OpenDbConnectionStringAsync ( ( string ) connectionString , ( string ) providerName )
29- : await DbFactory . OpenDbConnectionStringAsync ( ( string ) connectionString ) ;
30-
62+ ? await DbFactory . OpenDbConnectionStringAsync ( ( string ) connectionString , ( string ) providerName )
63+ : await DbFactory . OpenDbConnectionStringAsync ( ( string ) connectionString ) ;
64+ }
65+
3166 if ( options . TryGetValue ( "namedConnection" , out var namedConnection ) )
32- return await DbFactory . OpenDbConnectionAsync ( ( string ) namedConnection ) ;
67+ {
68+ return await DbFactory . OpenDbConnectionStringAsync ( ( string ) namedConnection ) ;
69+ }
3370 }
34-
35- if ( scope . PageResult != null && scope . PageResult . Args . TryGetValue ( "__dbinfo" , out var oDbInfo ) && oDbInfo is ConnectionInfo dbInfo ) // Keywords.DbInfo
36- return await DbFactory . OpenDbConnectionAsync ( dbInfo ) ;
3771
38- return await DbFactory . OpenAsync ( ) ;
72+ return null ;
3973 }
4074
4175 async Task < object > exec < T > ( Func < IDbConnection , Task < T > > fn , ScriptScopeContext scope , object options )
0 commit comments