Skip to content

Commit 3c75d82

Browse files
committed
Refinements to LoadQueryFromFile and ExecuteNonQuery
1 parent 78450ca commit 3c75d82

4 files changed

Lines changed: 30 additions & 9 deletions

File tree

project.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"ProjectName": "SqlQueryClass",
33
"Description": "The SqlQueryClass module provides a set of functions and cmdlets for working with SQL databases. It includes functionality for connecting, executing SQL queries, and managing output as DataTable, DataAdapter, DataSet, SqlReader, or NonQuery result objects.",
4-
"Version": "0.1.3",
5-
"ResourceCopyMode": "Content",
4+
"Version": "0.1.4",
5+
"copyResourcesToModuleRoot": true,
66
"Manifest": {
77
"Author": "Brooks Vaughn",
88
"PowerShellHostVersion": "5.1",

src/private/SqlQueryClass.ps1

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,9 @@ class SqlQueryDataSet {
405405
Write-Warning "GetTableFromQuery() TableName from Query is different then TableName in Tables: `$schemaTable.tableName=$($schemaTable.tableName), TableName=$($table.TableName), TableIndex=$($table.TableIndex)"
406406
}
407407
}
408+
If (-not [String]::IsNullOrWhiteSpace($Query) -and $Query -ne $table.Query) {
409+
$table.Query = $Query
410+
}
408411
Write-Host "GetTableFromQuery(): `$schemaTable.tableName=$($schemaTable.tableName), TableName=$($table.TableName), TableIndex=$($table.TableIndex)" -ForegroundColor Green
409412
Return ($table.TableIndex)
410413
}
@@ -455,13 +458,22 @@ class SqlQueryDataSet {
455458

456459
# Method
457460
LoadQueryFromFile([String]$Path) {
461+
LoadQueryFromFile([String]::Empty, $Path)
462+
}
463+
464+
LoadQueryFromFile([String]$TableName, [String]$Path) {
458465
If (Test-Path $Path) {
459466
If ([IO.Path]::GetExtension($Path) -ne ".sql") {
460467
throw [System.IO.FileFormatException] "'$Path' does not have an '.sql' extension'"
461468
} Else {
462469
Try {
463-
[String]$QueryStatement = Get-Content -Path $Path -Raw -ErrorAction Stop
464-
$This.TableIndex = $This.AddQuery($QueryStatement)
470+
# [String]$QueryStatement = Get-Content -Path $Path -ErrorAction Stop
471+
[String]$QueryStatement = [System.IO.File]::ReadAllLines($Path)
472+
If ([string]::IsNullOrEmpty($TableName)) {
473+
$This.TableIndex = $This.AddQuery($QueryStatement)
474+
} Else {
475+
$This.TableIndex = $This.AddQuery($TableName, $QueryStatement)
476+
}
465477
$This.Tables[$This.TableIndex].QueryFile = $Path
466478
} Catch {
467479
Write-host ($_ | Out-String) -ForegroundColor Red
@@ -646,13 +658,22 @@ class SqlQueryDataSet {
646658
}
647659

648660
# Method
661+
[Object] ExecuteNonQuery() {
662+
$table = $This.Tables[$This.TableIndex]
663+
Return ($This.ExecuteNonQuery($table))
664+
}
665+
649666
[Object] ExecuteNonQuery([String]$SqlQuery) {
650667
If ($SqlQuery) {
651-
$This.TableIndex = $This.AddQuery($SqlQuery)
668+
$This.TableIndex = $This.AddQuery('NonQuery',$SqlQuery)
652669
}
653670
$table = $This.Tables[$This.TableIndex]
654-
$table.ResultType = [ResultType]::NonQuery
655-
Return ($This.Execute($table))
671+
Return ($This.ExecuteNonQuery($table))
672+
}
673+
674+
[Object] ExecuteNonQuery([Object]$Table) {
675+
$Table.ResultType = [ResultType]::NonQuery
676+
Return ($This.Execute($Table))
656677
}
657678

658679
# Method
@@ -792,7 +813,7 @@ class SqlQueryDataSet {
792813
[Object] GetDBTableSchema([String]$TableName) {
793814
$SqlQuery = "SELECT * FROM [$($This.Database)].INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '$TableName';"
794815
# $SqlQuery = "SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, IS_NULLABLE FROM [$($This.Database)].INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '$TableName';"
795-
Return ($This.ExecuteQuery($SqlQuery))
816+
Return ($This.ExecuteQuery('COLUMNS', $SqlQuery))
796817
}
797818

798819
# Method to Retrieve Table Indexes from SQL v17 or higher using STRING_AGG
@@ -823,7 +844,7 @@ class SqlQueryDataSet {
823844
FROM [$($This.Database)].sys.indexes AS i
824845
WHERE i.object_id = OBJECT_ID('dbo.$TableName')
825846
GROUP BY i.name, i.is_unique, i.object_id, i.index_id"
826-
Return ($This.ExecuteQuery($SqlQuery))
847+
Return ($This.ExecuteQuery('Indexes', $SqlQuery))
827848
}
828849

829850
# Method to Generate the CREATE TABLE statement

tests/TestDatabase1.mdf

0 Bytes
Binary file not shown.

tests/TestDatabase1_log.ldf

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)