Skip to content

Commit d3366a7

Browse files
committed
adding basic handling of unknown datatypes
1 parent 7bc1ea9 commit d3366a7

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

functions/Invoke-DBOQuery.ps1

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,7 @@ function Invoke-DBOQuery {
240240
# Define the queries
241241
if ($Query) {
242242
$queryText = $Query
243-
}
244-
else {
243+
} else {
245244
$fileObjects = @()
246245
try {
247246
if ($InputFile) {
@@ -250,8 +249,7 @@ function Invoke-DBOQuery {
250249
if ($InputObject) {
251250
$fileObjects += $InputObject | Get-Item -ErrorAction Stop
252251
}
253-
}
254-
catch {
252+
} catch {
255253
Stop-PSFFunction -Message 'File not found' -ErrorRecord $_ -EnableException $true
256254
}
257255
$queryText = $fileObjects | Get-Content -Raw
@@ -267,15 +265,13 @@ function Invoke-DBOQuery {
267265
Write-PSFMessage -Level Verbose -Message "Establishing connection with $Type $($config.SqlInstance)"
268266
try {
269267
$dataConnection.Open()
270-
}
271-
catch {
268+
} catch {
272269
Stop-PSFFunction -EnableException $true -Message "Failed to connect to the server" -ErrorRecord $_
273270
return
274271
}
275272
if ($Interactive) {
276273
Write-PSFMessage -Level Host -Message "Running in interactive mode. Finish the query with a $delimiter to execute it immediately. \q, exit, or quit to exit."
277-
}
278-
else {
274+
} else {
279275
Write-PSFMessage -Level Debug -Message "Preparing to run $($queryList.Count) queries"
280276
}
281277
# wrapping everything in a big try {} to support graceful disconnect
@@ -360,6 +356,8 @@ function Invoke-DBOQuery {
360356
}
361357
if ($datatype.FullName -eq 'System.DBNull') {
362358
$datatype = [string]
359+
} elseif (!$datatype.FullName) {
360+
$datatype = [string[]]
363361
}
364362
$null = $table.Columns.Add($name, $datatype)
365363
}
@@ -379,8 +377,7 @@ function Invoke-DBOQuery {
379377
$reader.Close()
380378
$reader.Dispose()
381379
$command.Dispose()
382-
}
383-
catch {
380+
} catch {
384381
$dbUpLog.WriteError($_.Exception.InnerException.Message, $null)
385382
if ($Type -eq 'SqlServer') {
386383
Unregister-Event -SourceIdentifier "DBOpsMessaging" -ErrorAction SilentlyContinue
@@ -395,8 +392,7 @@ function Invoke-DBOQuery {
395392
if ($Interactive) {
396393
# output right to the screen
397394
Write-HostTable -Table $ds.Tables[0]
398-
}
399-
else {
395+
} else {
400396
# output as object
401397
switch ($As) {
402398
'DataSet' {
@@ -425,12 +421,10 @@ function Invoke-DBOQuery {
425421
}
426422
}
427423
} while ($Interactive)
428-
}
429-
catch {
424+
} catch {
430425
# don't really need anything else here
431426
throw $_
432-
}
433-
finally {
427+
} finally {
434428
# close the connection even when interrupted by Ctrl+C
435429
$dataConnection.Close()
436430
$dataConnection.Dispose()

tests/postgresql/Invoke-DBOQuery.Tests.ps1

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ if (!$Batch) {
99
# Is not a part of the global batch => import module
1010
#Explicitly import the module for testing
1111
Import-Module "$here\..\..\dbops.psd1" -Force; Get-DBOModuleFileList -Type internal | ForEach-Object { . $_.FullName }
12-
}
13-
else {
12+
} else {
1413
# Is a part of a batch, output some eye-catching happiness
1514
Write-Host "Running PostgreSQL $commandName tests" -ForegroundColor Cyan
1615
}
@@ -58,10 +57,13 @@ Describe "Invoke-DBOQuery PostgreSQL tests" -Tag $commandName, IntegrationTests
5857
$result.b | Should -Be 2, 4
5958
}
6059
It "should select NULL" {
61-
$query = "SELECT NULL"
60+
$query = "SELECT NULL, NULL::int, NULL::varchar, NULL::timestamp"
6261
$result = Invoke-DBOQuery -Query $query @connParams -As DataTable
63-
$result.Columns.ColumnName | Should -Be @('Column1')
62+
$result.Columns.ColumnName | Should -Be @('Column1', 'int4', 'varchar', 'timestamp')
6463
$result.Column1 | Should -Be ([DBNull]::Value)
64+
$result.int4 | Should -Be ([DBNull]::Value)
65+
$result.varchar | Should -Be ([DBNull]::Value)
66+
$result.timestamp | Should -Be ([DBNull]::Value)
6567
}
6668
It "should run the query with semicolon" {
6769
$query = "SELECT 1 AS A, 2 AS B;
@@ -163,6 +165,13 @@ Describe "Invoke-DBOQuery PostgreSQL tests" -Tag $commandName, IntegrationTests
163165
$result.Column1 | Should Be 2
164166
$result.Column2 | Should Be 3
165167
}
168+
It "should select an array" {
169+
$query = 'select (1,2) as a, ARRAY[3,4] as b'
170+
$result = Invoke-DBOQuery -Query $query @connParams -As DataTable
171+
$result.Columns.ColumnName | Should -Be @('a', 'b')
172+
$result.a | Should -Be 1, 2
173+
$result.b | Should -Be 3, 4
174+
}
166175
}
167176
Context "Negative tests" {
168177
It "should throw an unknown table error" {

0 commit comments

Comments
 (0)