@@ -12,6 +12,9 @@ public class TemplateDbFiltersAsync : DbScriptsAsync {}
12
12
13
13
public class DbScriptsAsync : ScriptMethods
14
14
{
15
+ private const string DbInfo = "__dbinfo" ; // Keywords.DbInfo
16
+ private const string DbConnection = "__dbconnection" ; // useDbConnection global
17
+
15
18
private IDbConnectionFactory dbFactory ;
16
19
public IDbConnectionFactory DbFactory
17
20
{
@@ -20,22 +23,53 @@ public IDbConnectionFactory DbFactory
20
23
}
21
24
22
25
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 )
23
56
{
24
57
if ( options != null )
25
58
{
26
59
if ( options . TryGetValue ( "connectionString" , out var connectionString ) )
60
+ {
27
61
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
+
31
66
if ( options . TryGetValue ( "namedConnection" , out var namedConnection ) )
32
- return await DbFactory . OpenDbConnectionAsync ( ( string ) namedConnection ) ;
67
+ {
68
+ return await DbFactory . OpenDbConnectionStringAsync ( ( string ) namedConnection ) ;
69
+ }
33
70
}
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 ) ;
37
71
38
- return await DbFactory . OpenAsync ( ) ;
72
+ return null ;
39
73
}
40
74
41
75
async Task < object > exec < T > ( Func < IDbConnection , Task < T > > fn , ScriptScopeContext scope , object options )
0 commit comments