From a564483f1067014edf6e3c5523078ade4403a23e Mon Sep 17 00:00:00 2001 From: al-ign <33247544+al-ign@users.noreply.github.com> Date: Fri, 26 Nov 2021 10:35:59 +0000 Subject: [PATCH] Connector creation and reader were moved to a separate function Separate Invoke-SqlNonQuery function New-SQLConnector function to support session workflows --- Public/Get-SqlRow.ps1 | 2 +- Public/Invoke-SqlNonQuery.ps1 | 78 +++++++++++++++++++++ Public/Invoke-SqlQuery.ps1 | 120 +++++++++------------------------ Public/Invoke-SqlReader.ps1 | 40 +++++++++++ Public/New-SQLConnector.ps1 | 106 +++++++++++++++++++++++++++++ Public/New-SqlRow.ps1 | 2 +- Public/Register-Assemblies.ps1 | 2 +- Public/Set-SqlRow.ps1 | 2 +- 8 files changed, 261 insertions(+), 91 deletions(-) create mode 100644 Public/Invoke-SqlNonQuery.ps1 create mode 100644 Public/Invoke-SqlReader.ps1 create mode 100644 Public/New-SQLConnector.ps1 diff --git a/Public/Get-SqlRow.ps1 b/Public/Get-SqlRow.ps1 index 571f213..1ece6df 100644 --- a/Public/Get-SqlRow.ps1 +++ b/Public/Get-SqlRow.ps1 @@ -1,4 +1,4 @@ -function Get-SqlRow { +function Get-SqlRow { [CmdletBinding()] #[Alias('')] Param ( diff --git a/Public/Invoke-SqlNonQuery.ps1 b/Public/Invoke-SqlNonQuery.ps1 new file mode 100644 index 0000000..2943af5 --- /dev/null +++ b/Public/Invoke-SqlNonQuery.ps1 @@ -0,0 +1,78 @@ +<# + .SYNOPSIS + Execute a non-query on a database + + .DESCRIPTION + Execute SQL queries using ADO.NET. + + .PARAMETER Query + The SQL query to execute which could be any string containing simple valid SQL. Can consist of + multiple SQL commands separated by semicolons which will be processed sequentially by the Cmdlet. + + .PARAMETER Provider + The ADO.NET dataprovider used for accessing the data source to query. Can be any of the following: + Sql - Provides data access for Microsoft SQL Server + OleDb - For data sources exposed by using OLE DB + Odbc - For data sources exposed by using ODBC + Oracle - For Oracle data sources + Entity - Provides data access for Entity Data Model (EDM) applications + SqlCe - Provides data access for Microsoft SQL Server Compact 4.0 + + .PARAMETER ConnectionString + The database connection string. + + .OUTPUTS + Amount of affected rows + + .NOTES + Original work by https://github.com/off-world/PowerADO.NET/blob/master/PowerADO.NET.psm1 + + .COMPONENT + ADO.NET + + .EXAMPLE + $query = "INSERT INTO Customers VALUES ('Doe','John')" + $query | Invoke-SqlQuery -Provider Sql -ConnectionString 'Server=db1.contoso.com;Database=CustomersDb;User Id=Admin;Password=pwd' + + .EXAMPLE + $query1 = "INSERT INTO Customers VALUES ('Doe','John')" + $query1 | query Sql 'Server=db1.contoso.com;Database=CustomersDb;User Id=Admin;Password=pwd' +#> +function Invoke-SqlNonQuery { + [CmdletBinding()] + [Alias('Invoke-DotNetSqlNonQuery')] + [OutputType([int])] + param( + [Parameter(Mandatory=$true, ValueFromPipeline=$true)] + [String] + $Query, + + [Parameter(Mandatory=$true, Position=0)] + [ValidateSet('MySql', 'Sql', 'OleDb', 'Odbc', 'Oracle', 'Entity', 'SqlCe')] + [String] + $Provider, + + [Parameter(Mandatory=$true, Position=1)] + [String] + $ConnectionString + ) + + begin + { + $sql = New-SQLConnector -Provider $Provider -ConnectionString $ConnectionString + } + + process { + ForEach ($thisQuery in $Query) { + if (-not [String]::IsNullOrWhiteSpace($thisQuery)) { + $sql.command.CommandText = $thisQuery.Trim() + $sql.command.ExecuteNonQuery() + } + } + } + + end + { + $sql.connection.Close() + } +} diff --git a/Public/Invoke-SqlQuery.ps1 b/Public/Invoke-SqlQuery.ps1 index a9a218e..d0be40b 100644 --- a/Public/Invoke-SqlQuery.ps1 +++ b/Public/Invoke-SqlQuery.ps1 @@ -1,4 +1,4 @@ -<# +<# .SYNOPSIS Query a database. @@ -48,6 +48,8 @@ function Invoke-SqlQuery { [CmdletBinding()] + [Alias('Invoke-DotNetSqlQuery')] + param( [Parameter(Mandatory=$true, ValueFromPipeline=$true)] [String] @@ -65,105 +67,49 @@ function Invoke-SqlQuery begin { - - try { - - $connection, $command = switch ($Provider) - { - 'MySql' - { - [MySql.Data.MySqlClient.MySqlConnection]::new($ConnectionString) - [mysql.data.mysqlclient.mysqlcommand]::new() - - } - 'Sql' - { - [System.Data.SqlClient.SqlConnection]::new($ConnectionString) - [System.Data.SqlClient.SqlCommand]::new() - } - 'OleDb' - { - [System.Data.OleDb.OleDbConnection]::new($ConnectionString) - [System.Data.OleDb.OleDbCommand]::new() - } - 'Odbc' - { - [System.Data.Odbc.OdbcConnection]::new($ConnectionString) - [System.Data.Odbc.OdbcCommand]::new() - } - 'Oracle' - { - [System.Data.OracleClient.OracleConnection]::new($ConnectionString) - [System.Data.OracleClient.OracleCommand]::new() - } - 'Entity' - { - [System.Data.EntityClient.EntityConnection]::new($ConnectionString) - [System.Data.EntityClient.EntityCommand]::new() - } - 'SqlCe' - { - [System.Data.SqlServerCe.SqlCeConnection]::new($ConnectionString) - [System.Data.SqlServerCe.SqlCeCommand]::new() - } - } - - } - catch { - Write-Error $_ - break - } - - try { - $connection.Open() - $command.Connection = $connection - } - catch { - Write-Error $_ - break - } - } + $sql = New-SQLConnector -Provider $Provider -ConnectionString $ConnectionString + } process { ForEach ($thisQuery in $Query) { - if (-not [String]::IsNullOrWhiteSpace($thisQuery)) + + $sql.command.CommandText = $thisQuery.Trim() + + if ($sql.command.CommandText -match '^\s*(SELECT )|(SHOW )') { - $command.CommandText = $thisQuery.Trim() - if ($command.CommandText -match '(SELECT )|(SHOW )') - { - try { - $reader = $command.ExecuteReader() - #Write-Verbose "Executing reader" - while ($reader.Read()) - { - $row = [ordered]@{} - for ($i=0; $i -lt $reader.FieldCount; $i++) { - $row.Add($reader.GetName($i) , $reader[$i]) - } - [pscustomobject]$row - + Invoke-SqlReader -Command $sql.Command + + <# + try { + $reader = $sql.command.ExecuteReader() + while ($reader.Read()) + { + $row = [ordered]@{} + for ($i=0; $i -lt $reader.FieldCount; $i++) { + $row.Add($reader.GetName($i) , $reader[$i]) } - $reader.Close() - } - catch { - Write-Error $_ + [pscustomobject]$row + } - - - } - else - { - $affectedRows = $command.ExecuteNonQuery() - #Write-Verbose "$affectedRows row(s) affected by $(($command.CommandText -split '\s+')[0]) command" - } + $reader.Close() + } + catch { + Write-Error $_ + } + #> + } + else + { + # returns int value of affected rows + $sql.command.ExecuteNonQuery() } } } end { - $connection.Close() + $sql.connection.Close() } } diff --git a/Public/Invoke-SqlReader.ps1 b/Public/Invoke-SqlReader.ps1 new file mode 100644 index 0000000..6c9f161 --- /dev/null +++ b/Public/Invoke-SqlReader.ps1 @@ -0,0 +1,40 @@ +<# +.Synopsis + Execute SQL reader +.DESCRIPTION + Execute .NET SQL reader method on SQL Command object to retrieve all rows +.EXAMPLE + Example of how to use this cmdlet +.EXAMPLE + Another example of how to use this cmdlet +#> + +function Invoke-SqlReader { + [CmdletBinding()] + [Alias('Invoke-DotNetSqlReader')] + Param + ( + # SQL Command object + [Parameter(Mandatory=$true, + ValueFromPipeline=$true, + Position=0)] + $Command + ) + + try { + $reader = $Command.ExecuteReader() + while ($reader.Read()) + { + $row = [ordered]@{} + for ($i=0; $i -lt $reader.FieldCount; $i++) { + $row.Add($reader.GetName($i) , $reader[$i]) + } + [pscustomobject]$row + + } + $reader.Close() + } + catch { + Write-Error $_ + } +} \ No newline at end of file diff --git a/Public/New-SQLConnector.ps1 b/Public/New-SQLConnector.ps1 new file mode 100644 index 0000000..feeca0d --- /dev/null +++ b/Public/New-SQLConnector.ps1 @@ -0,0 +1,106 @@ +<# +.Synopsis + Creates an instance of SQL connector + +.DESCRIPTION + Creates an instance of SQL connector + +.PARAMETER Provider + The ADO.NET dataprovider used for accessing the data source to query. Can be any of the following: + Sql - Provides data access for Microsoft SQL Server + OleDb - For data sources exposed by using OLE DB + Odbc - For data sources exposed by using ODBC + Oracle - For Oracle data sources + Entity - Provides data access for Entity Data Model (EDM) applications + SqlCe - Provides data access for Microsoft SQL Server Compact 4.0 + +.PARAMETER ConnectionString + The database connection string. + +.OUTPUTS + A hashtable with Connection and Command items. + +.EXAMPLE + $sql = New-SQLConnector -Provider MySql -ConnectionString 'server=localhost;username=root;password=root' + +.EXAMPLE + $sql = New-SQLConnector -Provider MySql -ConnectionString 'server=localhost;username=root;password=root' + $sql.command.CommandText = 'UPDATE database01.mac SET lastseen='2021-11-24 03:58:43' WHERE ID = 11543" + $sql.command.ExecuteNonQuery() +#> +function New-SQLConnector { + [CmdletBinding()] + [Alias('New-DotNetSqlConnector')] + param( + [Parameter(Mandatory=$true, Position=0)] + [ValidateSet('MySql', 'Sql', 'OleDb', 'Odbc', 'Oracle', 'Entity', 'SqlCe')] + [String] + $Provider, + + [Parameter(Mandatory=$true, Position=1)] + [String] + $ConnectionString + ) + + try { + + $connection, $command = switch ($Provider) + { + 'MySql' + { + [MySql.Data.MySqlClient.MySqlConnection]::new($ConnectionString) + [mysql.data.mysqlclient.mysqlcommand]::new() + + } + 'Sql' + { + [System.Data.SqlClient.SqlConnection]::new($ConnectionString) + [System.Data.SqlClient.SqlCommand]::new() + } + 'OleDb' + { + [System.Data.OleDb.OleDbConnection]::new($ConnectionString) + [System.Data.OleDb.OleDbCommand]::new() + } + 'Odbc' + { + [System.Data.Odbc.OdbcConnection]::new($ConnectionString) + [System.Data.Odbc.OdbcCommand]::new() + } + 'Oracle' + { + [System.Data.OracleClient.OracleConnection]::new($ConnectionString) + [System.Data.OracleClient.OracleCommand]::new() + } + 'Entity' + { + [System.Data.EntityClient.EntityConnection]::new($ConnectionString) + [System.Data.EntityClient.EntityCommand]::new() + } + 'SqlCe' + { + [System.Data.SqlServerCe.SqlCeConnection]::new($ConnectionString) + [System.Data.SqlServerCe.SqlCeCommand]::new() + } + } + + } + catch { + Write-Error $_ + break + } + + try { + $connection.Open() + $command.Connection = $connection + } + catch { + Write-Error $_ + break + } + + @{ + Connection = $connection + Command = $command + } +} \ No newline at end of file diff --git a/Public/New-SqlRow.ps1 b/Public/New-SqlRow.ps1 index b1527f9..d6af666 100644 --- a/Public/New-SqlRow.ps1 +++ b/Public/New-SqlRow.ps1 @@ -1,4 +1,4 @@ -function New-SqlRow { +function New-SqlRow { [CmdletBinding()] [Alias('Insert-SqlRow')] Param ( diff --git a/Public/Register-Assemblies.ps1 b/Public/Register-Assemblies.ps1 index c638583..aca2dc2 100644 --- a/Public/Register-Assemblies.ps1 +++ b/Public/Register-Assemblies.ps1 @@ -1,4 +1,4 @@ -function Register-Assemblies { +function Register-Assemblies { param ( #Path to *.dll diff --git a/Public/Set-SqlRow.ps1 b/Public/Set-SqlRow.ps1 index 1578d1a..b73c36a 100644 --- a/Public/Set-SqlRow.ps1 +++ b/Public/Set-SqlRow.ps1 @@ -1,4 +1,4 @@ -function Set-SqlRow { +function Set-SqlRow { [CmdletBinding()] [Alias('Update-SqlRow')] Param (