From 01662b037842126233e9b9040363f7d3aa510d39 Mon Sep 17 00:00:00 2001 From: Marcin Golebiowski Date: Sat, 29 Oct 2022 08:54:18 +0200 Subject: [PATCH] Fix issue in Install-SqlSizerSecureViews (now there is no limit on number of columns) --- .../Private/Install-SqlSizerSecureViews.ps1 | 51 ++++++++++++++++++- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/SqlSizer-MSSQL/Private/Install-SqlSizerSecureViews.ps1 b/SqlSizer-MSSQL/Private/Install-SqlSizerSecureViews.ps1 index a91c871..adadc05 100644 --- a/SqlSizer-MSSQL/Private/Install-SqlSizerSecureViews.ps1 +++ b/SqlSizer-MSSQL/Private/Install-SqlSizerSecureViews.ps1 @@ -53,9 +53,12 @@ function Install-SqlSizerSecureViews { continue } - $sql = "CREATE VIEW SqlSizer_$($SessionId).Secure_$($table.SchemaName)_$($table.TableName) AS SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS SqlSizer_RowSequence, $tableSelect, HASHBYTES('SHA2_512', CONCAT($([string]::Join(', ''|'', ', $hashSelect)))) as row_sha2_512 FROM $($table.SchemaName).$($table.TableName) t INNER JOIN $join" - $null = Invoke-SqlcmdEx -Sql $sql -Database $Database -ConnectionInfo $ConnectionInfo + $hashInput = GetHashInput -hashSelect $hashSelect + + # create a view + $sql = "CREATE VIEW SqlSizer_$($SessionId).Secure_$($table.SchemaName)_$($table.TableName) AS SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS SqlSizer_RowSequence, $tableSelect, HASHBYTES('SHA2_512', $hashInput) as row_sha2_512 FROM $($table.SchemaName).$($table.TableName) t INNER JOIN $join" + $null = Invoke-SqlcmdEx -Sql $sql -Database $Database -ConnectionInfo $ConnectionInfo $total += "SELECT '$($table.SchemaName)' as [Schema], '$($table.TableName)' as [Table], CONVERT(VARCHAR(max), HASHBYTES('SHA1', STRING_AGG(CONVERT(VARCHAR(max), row_sha2_512, 2), '|')), 2) as [TableHash_SHA_1], CONVERT(VARCHAR(max), HASHBYTES('SHA2_256', STRING_AGG(CONVERT(VARCHAR(max), row_sha2_512, 2), '|')), 2) as [TableHash_SHA_256], CONVERT(VARCHAR(max), HASHBYTES('SHA2_512', STRING_AGG(CONVERT(VARCHAR(max), row_sha2_512, 2), '|')), 2) as [TableHash_SHA_512] FROM SqlSizer_$($SessionId).Secure_$($table.SchemaName)_$($table.TableName)" } @@ -66,6 +69,50 @@ function Install-SqlSizerSecureViews } } +function GetHashInput +{ + param ( + [string[]]$hashSelect + ) + + # prepare $hashInput + $hashGroups = [System.Collections.ArrayList]@() + $hashGroupSize = 50 + + for ($i = 0; $i -lt ($hashSelect.Length / $hashGroupSize); $i += 1) + { + $group = $hashSelect | Select-Object -First $hashGroupSize -Skip ($hashGroupSize * $i) + $item = @() + $item += $group + + $null = $hashGroups.Add($item) + } + + $hashInputs = @() + foreach ($hashGroup in $hashGroups) + { + if ($hashGroup.Length -gt 1) + { + $hashInputs += "CONCAT($([string]::Join(', ''|'', ', $hashGroup)))" + } + else + { + $hashInputs += $hashGroup[0] + } + } + + if ($hashInputs.Length -gt 1) + { + $hashInput = "CONCAT($([string]::Join(', ''|'', ', $hashInputs)))" + } + else + { + $hashInput = $hashInputs[0] + } + + return $hashInput +} + function GetSecureViewsTableJoin { param (