1
+ using SqlDatabase . Adapter ;
2
+ using SqlDatabase . CommandLine ;
3
+ using SqlDatabase . Configuration ;
4
+
5
+ namespace SqlDatabase . PowerShell . Internal ;
6
+
7
+ public static class CmdLetExecutor
8
+ {
9
+ // only for tests
10
+ internal static ISqlDatabaseProgram ? Program { get ; set ; }
11
+
12
+ public static void RunCreate ( IDictionary < string , object ? > param , string currentDirectory , Action < string > writeInfo , Action < string > writeError )
13
+ {
14
+ var commandLine = new CreateCommandLine
15
+ {
16
+ Database = ( string ) param [ nameof ( CreateCommandLine . Database ) ] ! ,
17
+ Configuration = ( string ? ) param [ nameof ( CreateCommandLine . Configuration ) ] ,
18
+ Log = ( string ? ) param [ nameof ( CreateCommandLine . Log ) ] ,
19
+ WhatIf = ( bool ) param [ nameof ( CreateCommandLine . WhatIf ) ] !
20
+ } ;
21
+
22
+ CommandLineTools . AppendFrom ( commandLine . From , false , ( string [ ] ? ) param [ nameof ( CreateCommandLine . From ) ] ) ;
23
+ CommandLineTools . AppendVariables ( commandLine . Variables , ( string [ ] ? ) param [ nameof ( CreateCommandLine . Variables ) ] ) ;
24
+
25
+ Run ( commandLine , currentDirectory , writeInfo , writeError ) ;
26
+ }
27
+
28
+ public static void RunExecute ( IDictionary < string , object ? > param , string currentDirectory , Action < string > writeInfo , Action < string > writeError )
29
+ {
30
+ var commandLine = new ExecuteCommandLine
31
+ {
32
+ Database = ( string ) param [ nameof ( ExecuteCommandLine . Database ) ] ! ,
33
+ Transaction = ( TransactionMode ) ( ( int ) param [ nameof ( ExecuteCommandLine . Transaction ) ] ! ) ,
34
+ Configuration = ( string ? ) param [ nameof ( ExecuteCommandLine . Configuration ) ] ,
35
+ Log = ( string ? ) param [ nameof ( ExecuteCommandLine . Log ) ] ,
36
+ WhatIf = ( bool ) param [ nameof ( ExecuteCommandLine . WhatIf ) ] !
37
+ } ;
38
+
39
+ CommandLineTools . AppendFrom ( commandLine . From , false , ( string [ ] ? ) param [ nameof ( ExecuteCommandLine . From ) ] ) ;
40
+ CommandLineTools . AppendFrom ( commandLine . From , true , ( string [ ] ? ) param [ "FromSql" ] ) ;
41
+ CommandLineTools . AppendVariables ( commandLine . Variables , ( string [ ] ? ) param [ nameof ( ExecuteCommandLine . Variables ) ] ) ;
42
+
43
+ Run ( commandLine , currentDirectory , writeInfo , writeError ) ;
44
+ }
45
+
46
+ public static void RunExport ( IDictionary < string , object ? > param , string currentDirectory , Action < string > writeInfo , Action < string > writeError )
47
+ {
48
+ var commandLine = new ExportCommandLine
49
+ {
50
+ Database = ( string ) param [ nameof ( ExportCommandLine . Database ) ] ! ,
51
+ Configuration = ( string ? ) param [ nameof ( ExportCommandLine . Configuration ) ] ,
52
+ DestinationFileName = ( string ? ) param [ nameof ( ExportCommandLine . DestinationFileName ) ] ,
53
+ DestinationTableName = ( string ? ) param [ nameof ( ExportCommandLine . DestinationTableName ) ] ,
54
+ Log = ( string ? ) param [ nameof ( ExecuteCommandLine . Log ) ]
55
+ } ;
56
+
57
+ CommandLineTools . AppendFrom ( commandLine . From , false , ( string [ ] ? ) param [ nameof ( ExportCommandLine . From ) ] ) ;
58
+ CommandLineTools . AppendFrom ( commandLine . From , true , ( string [ ] ? ) param [ "FromSql" ] ) ;
59
+ CommandLineTools . AppendVariables ( commandLine . Variables , ( string [ ] ? ) param [ nameof ( ExportCommandLine . Variables ) ] ) ;
60
+
61
+ Run ( commandLine , currentDirectory , writeInfo , writeError ) ;
62
+ }
63
+
64
+ public static void RunUpdate ( IDictionary < string , object ? > param , string currentDirectory , Action < string > writeInfo , Action < string > writeError )
65
+ {
66
+ var commandLine = new UpgradeCommandLine
67
+ {
68
+ Database = ( string ) param [ nameof ( UpgradeCommandLine . Database ) ] ! ,
69
+ Transaction = ( TransactionMode ) ( ( int ) param [ nameof ( UpgradeCommandLine . Transaction ) ] ! ) ,
70
+ Configuration = ( string ? ) param [ nameof ( UpgradeCommandLine . Configuration ) ] ,
71
+ Log = ( string ? ) param [ nameof ( UpgradeCommandLine . Log ) ] ,
72
+ WhatIf = ( bool ) param [ nameof ( UpgradeCommandLine . WhatIf ) ] ! ,
73
+ FolderAsModuleName = ( bool ) param [ nameof ( UpgradeCommandLine . FolderAsModuleName ) ] !
74
+ } ;
75
+
76
+ CommandLineTools . AppendFrom ( commandLine . From , false , ( string [ ] ? ) param [ nameof ( UpgradeCommandLine . From ) ] ) ;
77
+ CommandLineTools . AppendVariables ( commandLine . Variables , ( string [ ] ? ) param [ nameof ( UpgradeCommandLine . Variables ) ] ) ;
78
+
79
+ Run ( commandLine , currentDirectory , writeInfo , writeError ) ;
80
+ }
81
+
82
+ public static string GetDefaultConfigurationFile ( ) => ConfigurationManager . GetDefaultConfigurationFile ( ) ;
83
+
84
+ private static void Run ( ICommandLine commandLine , string currentDirectory , Action < string > writeInfo , Action < string > writeError )
85
+ {
86
+ var program = Program ?? new SqlDatabaseProgram ( new CmdLetLogger ( writeInfo , writeError ) , currentDirectory ) ;
87
+ program . ExecuteCommand ( commandLine ) ;
88
+ }
89
+ }
0 commit comments