-Force behavior clearer in Send-SQLData

This commit is contained in:
jhoneill
2019-10-31 10:31:54 +00:00
parent 5d92442488
commit 3284f592b9
2 changed files with 34 additions and 19 deletions

View File

@@ -128,7 +128,7 @@
return $paramDictionary return $paramDictionary
} }
process { process {
#Dynamic params mean we can get passed parameter combination Export-Excel will reject, so throw here, rather than get data and then have Export-Excel error. #region Dynamic params mean we can get passed parameter combination Export-Excel will reject, so throw here, rather than get data and then have Export-Excel error.
if ($PSBoundParameters.Path -and $PSBoundParameters.ExcelPackage) { if ($PSBoundParameters.Path -and $PSBoundParameters.ExcelPackage) {
throw 'Parameter error: you cannot specify both a path and an Excel Package.' throw 'Parameter error: you cannot specify both a path and an Excel Package.'
return return
@@ -137,7 +137,8 @@
Write-Warning "Tables are automatically auto-filtered, -AutoFilter will be ignored" Write-Warning "Tables are automatically auto-filtered, -AutoFilter will be ignored"
$null = $PSBoundParameters.Remove('AutoFilter') $null = $PSBoundParameters.Remove('AutoFilter')
} }
#We were either given a session object or a connection string (with, optionally a MSSQLServer parameter) #endregion
#region if we were either given a session object or a connection string (& optionally -MSSQLServer) make sure we can connect
try { try {
#If we got -MSSQLServer, create a SQL connection, if we didn't but we got -Connection create an ODBC connection #If we got -MSSQLServer, create a SQL connection, if we didn't but we got -Connection create an ODBC connection
if ($MsSQLserver -and $Connection) { if ($MsSQLserver -and $Connection) {
@@ -154,10 +155,11 @@
Write-Warning "An Error occured trying to connect to $Connection, the error was $([Environment]::NewLine + $_.Exception.InnerException))" Write-Warning "An Error occured trying to connect to $Connection, the error was $([Environment]::NewLine + $_.Exception.InnerException))"
} }
if ($Session -is [String] -and $Global:DbSessions[$Session]) {$Session = $Global:DbSessions[$Session]} if ($Session -is [String] -and $Global:DbSessions[$Session]) {$Session = $Global:DbSessions[$Session]}
#endregion
#region we may have been given a table, but if there is a db session to connect to, send the query
if ($Session) { if ($Session) {
try { try {
#A session was either passed in or just created. If it's a SQL one make a SQL DataAdapter, otherwise make an ODBC one #If the session a SQL one make a SQL DataAdapter, otherwise make an ODBC one
if ($Session.GetType().name -match "SqlConnection") { if ($Session.GetType().name -match "SqlConnection") {
$dataAdapter = New-Object -TypeName System.Data.SqlClient.SqlDataAdapter -ArgumentList ( $dataAdapter = New-Object -TypeName System.Data.SqlClient.SqlDataAdapter -ArgumentList (
New-Object -TypeName System.Data.SqlClient.SqlCommand -ArgumentList $SQL, $Session) New-Object -TypeName System.Data.SqlClient.SqlCommand -ArgumentList $SQL, $Session)
@@ -177,16 +179,26 @@
Write-Warning "An Error occured trying to run the query, the error was $([Environment]::NewLine + $_.Exception.InnerException))" Write-Warning "An Error occured trying to run the query, the error was $([Environment]::NewLine + $_.Exception.InnerException))"
} }
} }
#if force was specified export even if there are no rows. If there are no columns, the query failed and export "null" if forced #endregion
if ($Force -or $DataTable.Rows.Count) { #region send the table to Excel
#Call export-excel removing parameters which relate to the SQL query, and keeping the rest. #remove parameters which relate to querying SQL, leaving the ones used by Export-Excel
'Connection' , 'Database' , 'Session' , 'MsSQLserver' , 'SQL' , 'DataTable' , 'QueryTimeout' , 'Force' | 'Connection' , 'Database' , 'Session' , 'MsSQLserver' , 'SQL' , 'DataTable' , 'QueryTimeout' , 'Force' |
ForEach-Object {$null = $PSBoundParameters.Remove($_) } ForEach-Object {$null = $PSBoundParameters.Remove($_) }
if ($DataTable.Columns.Count) { Export-Excel @PSBoundParameters -InputObject $DataTable } #if force was specified export even if there are no rows. If there are no columns, the query failed and export "null" if forced
else { Export-Excel @PSBoundParameters -InputObject $null } if ($DataTable.Rows.Count) {
Export-Excel @PSBoundParameters -InputObject $DataTable
} }
else {Write-Warning -Message ' No Data to insert.' } elseif ($Force -and $DataTable.Columns.Count) {
#If we were passed a connection and opened a session, close that session. Write-Warning -Message "Zero rows returned, and -Force was specified, sending empty table to Excel."
Export-Excel @PSBoundParameters -InputObject $DataTable
}
elseif ($Force) {
Write-Warning -Message "-Force was specified but there is no data to send."
Export-Excel @PSBoundParameters -InputObject $null
}
else {Write-Warning -Message 'There is no Data to insert, and -Force was not specified.' }
#endregion
#If we were passed a connection and opened a session, close that session.
if ($Connection) {$Session.close() } if ($Connection) {$Session.close() }
} }
} }

View File

@@ -1,4 +1,4 @@
Describe "Exporting with -Inputobject" { Describe "Exporting with -Inputobject; table handling, Send SQL Data and import as " {
BeforeAll { BeforeAll {
$path = "TestDrive:\Results.xlsx" $path = "TestDrive:\Results.xlsx"
Remove-Item -Path $path -ErrorAction SilentlyContinue Remove-Item -Path $path -ErrorAction SilentlyContinue
@@ -17,8 +17,8 @@ Describe "Exporting with -Inputobject" {
export-excel -Path $path -InputObject $DataTable -WorksheetName Sheet2 -AutoNameRange export-excel -Path $path -InputObject $DataTable -WorksheetName Sheet2 -AutoNameRange
Send-SQLDataToExcel -path $path -DataTable $DataTable -WorkSheetname Sheet3 -TableName "Data" Send-SQLDataToExcel -path $path -DataTable $DataTable -WorkSheetname Sheet3 -TableName "Data"
$DataTable.Rows.Clear() $DataTable.Rows.Clear()
Send-SQLDataToExcel -path $path -DataTable $DataTable -WorkSheetname Sheet4 -force Send-SQLDataToExcel -path $path -DataTable $DataTable -WorkSheetname Sheet4 -force -WarningVariable WVOne -WarningAction SilentlyContinue
Send-SQLDataToExcel -path $path -DataTable ([System.Data.DataTable]::new('Test2')) -WorkSheetname Sheet5 -force Send-SQLDataToExcel -path $path -DataTable ([System.Data.DataTable]::new('Test2')) -WorkSheetname Sheet5 -force -WarningVariable wvTwo -WarningAction SilentlyContinue
$excel = Open-ExcelPackage $path $excel = Open-ExcelPackage $path
$sheet = $excel.Sheet1 $sheet = $excel.Sheet1
} }
@@ -80,20 +80,23 @@ Describe "Exporting with -Inputobject" {
} }
} }
$Sheet = $excel.Sheet4 $Sheet = $excel.Sheet4
Context "Zero row Data Table sent with Send-SQLDataToExcel -Force" { Context "Zero-row Data Table sent with Send-SQLDataToExcel -Force" {
it "Put the correct data headers into the sheet " { it "Raised a warning and put the correct data headers into the sheet " {
$sheet.Dimension.Rows | should be 1 $sheet.Dimension.Rows | should be 1
$sheet.Dimension.Columns | should be 5 $sheet.Dimension.Columns | should be 5
$sheet.cells["A1"].Value | should be "Name" $sheet.cells["A1"].Value | should be "Name"
$sheet.cells["E1"].Value | should be "StartTime" $sheet.cells["E1"].Value | should be "StartTime"
$sheet.cells["A3"].Value | should beNullOrEmpty $sheet.cells["A3"].Value | should beNullOrEmpty
$wvone | should not beNullOrEmpty
} }
} }
$Sheet = $excel.Sheet5 $Sheet = $excel.Sheet5
Context "Zero column data table handled by Send-SQLDataToExcel -Force" { Context "Zero-column Data Table handled by Send-SQLDataToExcel -Force" {
it "Put Created a blank Sheet " { it "Put Created a blank Sheet and raised a warning " {
$sheet.Dimension | should beNullOrEmpty $sheet.Dimension | should beNullOrEmpty
$wvTwo | should not beNullOrEmpty
} }
} }
Close-ExcelPackage $excel Close-ExcelPackage $excel
Context "Import As Text returns text values" { Context "Import As Text returns text values" {