Skip to content

Commit

Permalink
Fix issue in Install-SqlSizerSecureViews (now there is no limit on nu…
Browse files Browse the repository at this point in the history
…mber of columns)
  • Loading branch information
marcin-golebiowski committed Oct 29, 2022
1 parent be11a57 commit 01662b0
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions SqlSizer-MSSQL/Private/Install-SqlSizerSecureViews.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}

Expand All @@ -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 (
Expand Down

0 comments on commit 01662b0

Please sign in to comment.