-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmdlets Create-SqlDatabase; Execute-SqlDatabase; Upgrade-SqlDatabase
- Loading branch information
1 parent
e8355e0
commit fd248d4
Showing
12 changed files
with
168 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,25 @@ | ||
using System; | ||
using System.Management.Automation; | ||
using System.Management.Automation; | ||
using SqlDatabase.Log; | ||
|
||
namespace SqlDatabase.PowerShell | ||
{ | ||
internal sealed class CmdLetLogger : ILogger | ||
internal sealed class CmdLetLogger : LoggerBase | ||
{ | ||
private readonly Cmdlet _owner; | ||
private readonly PSCmdlet _owner; | ||
|
||
public CmdLetLogger(Cmdlet owner) | ||
public CmdLetLogger(PSCmdlet owner) | ||
{ | ||
_owner = owner; | ||
} | ||
|
||
public void Error(string message) | ||
protected override void WriteError(string message) | ||
{ | ||
_owner.Host.UI.WriteErrorLine(message); | ||
} | ||
|
||
public void Info(string message) | ||
protected override void WriteInfo(string message) | ||
{ | ||
throw new NotImplementedException(); | ||
_owner.Host.UI.WriteLine(message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Management.Automation; | ||
using SqlDatabase.Configuration; | ||
|
||
namespace SqlDatabase.PowerShell | ||
{ | ||
[Cmdlet(nameof(Command.Create), "SqlDatabase")] | ||
public sealed class CreateCmdLet : SqlDatabaseCmdLet | ||
{ | ||
public CreateCmdLet() | ||
: base(Command.Create) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,14 @@ | ||
using System.IO; | ||
using System.Management.Automation; | ||
using System.Management.Automation; | ||
using SqlDatabase.Configuration; | ||
|
||
namespace SqlDatabase.PowerShell | ||
{ | ||
[Cmdlet(VerbsLifecycle.Invoke, "SqlDatabase")] | ||
public sealed class ExecuteCmdLet : Cmdlet | ||
[Cmdlet(nameof(Command.Execute), "SqlDatabase")] | ||
public sealed class ExecuteCmdLet : SqlDatabaseCmdLet | ||
{ | ||
[Parameter(Mandatory = true, Position = 1, HelpMessage = "Connection string to target database.")] | ||
[Alias("d")] | ||
public string Database { get; set; } | ||
|
||
[Parameter(Mandatory = true, Position = 2, ValueFromPipeline = true, HelpMessage = "Scripts file.")] | ||
[Alias("f")] | ||
public FileInfo From { get; set; } | ||
|
||
[Parameter(Position = 3, HelpMessage = "Transaction mode. Possible values: none, perStep. Default is none.")] | ||
[Alias("t")] | ||
public TransactionMode Transaction { get; set; } | ||
|
||
[Parameter(ValueFromRemainingArguments = true, HelpMessage = "set a variable in format \"[name of variable]=[value of variable]\".")] | ||
[Alias("v")] | ||
public string[] Var { get; set; } | ||
|
||
// only for tests | ||
internal static ISqlDatabaseProgram Program { get; set; } | ||
|
||
protected override void ProcessRecord() | ||
{ | ||
var cmd = new CommandLineBuilder() | ||
.SetCommand(Command.Execute) | ||
.SetConnection(Database) | ||
.SetTransaction(Transaction) | ||
.SetScripts(From.FullName); | ||
|
||
if (Var != null && Var.Length > 0) | ||
{ | ||
foreach (var value in Var) | ||
{ | ||
cmd.SetVariable(value); | ||
} | ||
} | ||
|
||
ResolveProgram().ExecuteCommand(cmd.Build()); | ||
} | ||
|
||
private static ISqlDatabaseProgram ResolveProgram() | ||
public ExecuteCmdLet() | ||
: base(Command.Execute) | ||
{ | ||
return Program ?? new SqlDatabaseProgram(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System.Management.Automation; | ||
using SqlDatabase.Configuration; | ||
|
||
namespace SqlDatabase.PowerShell | ||
{ | ||
public abstract class SqlDatabaseCmdLet : PSCmdlet | ||
{ | ||
private readonly Command _command; | ||
|
||
protected SqlDatabaseCmdLet(Command command) | ||
{ | ||
_command = command; | ||
} | ||
|
||
[Parameter(Mandatory = true, Position = 1, HelpMessage = "Connection string to target database.")] | ||
[Alias("d")] | ||
public string Database { get; set; } | ||
|
||
[Parameter(Mandatory = true, Position = 2, ValueFromPipeline = true, HelpMessage = "Scripts file.")] | ||
[Alias("f")] | ||
public string[] From { get; set; } | ||
|
||
[Parameter(Position = 3, HelpMessage = "Transaction mode. Possible values: none, perStep. Default is none.")] | ||
[Alias("t")] | ||
public TransactionMode Transaction { get; set; } | ||
|
||
[Parameter(ValueFromRemainingArguments = true, HelpMessage = "set a variable in format \"[name of variable]=[value of variable]\".")] | ||
[Alias("v")] | ||
public string[] Var { get; set; } | ||
|
||
// only for tests | ||
internal static ISqlDatabaseProgram Program { get; set; } | ||
|
||
protected override void ProcessRecord() | ||
{ | ||
var cmd = new CommandLineBuilder() | ||
.SetCommand(_command) | ||
.SetConnection(Database) | ||
.SetTransaction(Transaction); | ||
|
||
foreach (var from in From) | ||
{ | ||
cmd.SetScripts(from); | ||
} | ||
|
||
if (Var != null && Var.Length > 0) | ||
{ | ||
foreach (var value in Var) | ||
{ | ||
cmd.SetVariable(value); | ||
} | ||
} | ||
|
||
ResolveProgram().ExecuteCommand(cmd.Build()); | ||
} | ||
|
||
private ISqlDatabaseProgram ResolveProgram() | ||
{ | ||
return Program ?? new SqlDatabaseProgram(this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,21 @@ | ||
using SqlDatabase.Configuration; | ||
using System.Management.Automation; | ||
using SqlDatabase.Configuration; | ||
|
||
namespace SqlDatabase.PowerShell | ||
{ | ||
internal sealed class SqlDatabaseProgram : ISqlDatabaseProgram | ||
{ | ||
private readonly PSCmdlet _owner; | ||
|
||
public SqlDatabaseProgram(PSCmdlet owner) | ||
{ | ||
_owner = owner; | ||
} | ||
|
||
public void ExecuteCommand(CommandLine command) | ||
{ | ||
Program.ExecuteCommand(command); | ||
var logger = new CmdLetLogger(_owner); | ||
Program.ExecuteCommand(command, logger); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Management.Automation; | ||
using SqlDatabase.Configuration; | ||
|
||
namespace SqlDatabase.PowerShell | ||
{ | ||
[Cmdlet(nameof(Command.Upgrade), "SqlDatabase")] | ||
public sealed class UpgradeCmdLet : SqlDatabaseCmdLet | ||
{ | ||
public UpgradeCmdLet() | ||
: base(Command.Upgrade) | ||
{ | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.