diff --git a/Examples/Join-Object.Examples.ps1 b/Examples/Join-Object.Examples.ps1 index 9e5a771..a26724f 100644 --- a/Examples/Join-Object.Examples.ps1 +++ b/Examples/Join-Object.Examples.ps1 @@ -21,24 +21,24 @@ static System.Collections.Generic.IEnumerable[TResult] #> $TestData1 = { - $PSCustomObjects= @( - [PSCustomObject]@{ID = 1 ; Sub = 'S1'} - [PSCustomObject]@{ID = 2 ; Sub = 'S2'} - [PSCustomObject]@{ID = 3 ; Sub = 'S3'} + $PSCustomObjects = @( + [PSCustomObject]@{ ID = 1 ; Sub = 'S1' } + [PSCustomObject]@{ ID = 2 ; Sub = 'S2' } + [PSCustomObject]@{ ID = 3 ; Sub = 'S3' } ) $DataTable = [Data.DataTable]::new('Test') $null = $DataTable.Columns.Add('IDD') $null = $DataTable.Columns.Add('Name') $null = $DataTable.Columns.Add('Junk') - $null = $DataTable.Rows.Add(1,'A','AAA') - $null = $DataTable.Rows.Add(3,'C',$null) + $null = $DataTable.Rows.Add(1, 'A', 'AAA') + $null = $DataTable.Rows.Add(3, 'C', $null) } . $TestData1 -Join-Object -Left $PSCustomObjects -Right $DataTable -LeftJoinProperty ID -RightJoinProperty IDD -LeftProperties @{ID= 'ID' ; Sub = 'Subscription'} -ExcludeRightProperties Junk -Prefix 'R_' | Format-Table -Join-Object -Left $DataTable -Right $PSCustomObjects -LeftJoinProperty IDD -RightJoinProperty ID -RightProperties @{ID= 'ID' ; Sub = 'Subscription'} -ExcludeLeftProperties Junk -Suffix '_R' | Format-Table -Join-Object -Left $PSCustomObjects -Right $DataTable -LeftJoinProperty ID -RightJoinProperty IDD -LeftProperties ([ordered]@{Sub = 'Subscription' ; ID= 'ID'}) -ExcludeRightProperties Junk -Prefix 'R_' | Format-Table +Join-Object -Left $PSCustomObjects -Right $DataTable -LeftJoinProperty ID -RightJoinProperty IDD -LeftProperties @{ ID = 'ID' ; Sub = 'Subscription' } -ExcludeRightProperties Junk -Prefix 'R_' | Format-Table +Join-Object -Left $DataTable -Right $PSCustomObjects -LeftJoinProperty IDD -RightJoinProperty ID -RightProperties @{ ID = 'ID' ; Sub = 'Subscription' } -ExcludeLeftProperties Junk -Suffix '_R' | Format-Table +Join-Object -Left $PSCustomObjects -Right $DataTable -LeftJoinProperty ID -RightJoinProperty IDD -LeftProperties ([ordered]@{ Sub = 'Subscription' ; ID = 'ID' }) -ExcludeRightProperties Junk -Prefix 'R_' | Format-Table <# Output ID Subscription R_Name -- ------------ ------ @@ -59,7 +59,7 @@ S3 3 C #> . $TestData1 -$null = Join-Object -Left $PSCustomObjects -Right $DataTable -LeftJoinProperty ID -RightJoinProperty IDD -LeftProperties @{ID= 'ID' ; Sub = 'Subscription'} -ExcludeRightProperties Junk -Prefix 'R_' -PassThru +$null = Join-Object -Left $PSCustomObjects -Right $DataTable -LeftJoinProperty ID -RightJoinProperty IDD -LeftProperties @{ ID = 'ID' ; Sub = 'Subscription' } -ExcludeRightProperties Junk -Prefix 'R_' -PassThru $PSCustomObjects <# $PSCustomObjects changed to: ID Subscription R_Name @@ -70,7 +70,7 @@ ID Subscription R_Name #> . $TestData1 -$null = Join-Object -Left $DataTable -Right $PSCustomObjects -LeftJoinProperty IDD -RightJoinProperty ID -LeftProperties @{IDD = 'IDD' ; Name = 'NewName'}-RightProperties @{ID= 'ID' ; Sub = 'Subscription'} -ExcludeLeftProperties Junk -Suffix '_R' -PassThru +$null = Join-Object -Left $DataTable -Right $PSCustomObjects -LeftJoinProperty IDD -RightJoinProperty ID -LeftProperties @{ IDD = 'IDD' ; Name = 'NewName' }-RightProperties @{ ID = 'ID' ; Sub = 'Subscription' } -ExcludeLeftProperties Junk -Suffix '_R' -PassThru $DataTable <# $DataTable changed to: IDD NewName Subscription_R @@ -81,10 +81,10 @@ IDD NewName Subscription_R 'DBNull to $null test' . $TestData1 -Join-Object -Left $PSCustomObjects -Right $DataTable -LeftJoinProperty ID -RightJoinProperty IDD | Where-Object {$_.Junk} | Format-Table +Join-Object -Left $PSCustomObjects -Right $DataTable -LeftJoinProperty ID -RightJoinProperty IDD | Where-Object { $_.Junk } | Format-Table . $TestData1 $null = Join-Object -Left $PSCustomObjects -Right $DataTable -LeftJoinProperty ID -RightJoinProperty IDD -PassThru -$PSCustomObjects | Where-Object {$_.Junk} | Format-Table +$PSCustomObjects | Where-Object { $_.Junk } | Format-Table <# Output ID Sub Name Junk -- --- ---- ---- diff --git a/Join-Object.ps1 b/Join-Object.ps1 index 58f3aaf..63d6dce 100644 --- a/Join-Object.ps1 +++ b/Join-Object.ps1 @@ -1,7 +1,6 @@ using namespace System.Data Add-Type -AssemblyName System.Data.DataSetExtensions -function Join-Object -{ +function Join-Object { <# .SYNOPSIS Join data from two sets of objects based on a common value @@ -213,9 +212,9 @@ function Join-Object [System.Func[System.Object, string]]$LeftJoinScript, [System.Func[System.Object, string]]$RightJoinScript, - [ValidateScript( {$_ -is [Collections.Hashtable] -or $_ -is [string] -or $_ -is [Collections.Specialized.OrderedDictionary]})] + [ValidateScript( { $_ -is [Collections.Hashtable] -or $_ -is [string] -or $_ -is [Collections.Specialized.OrderedDictionary] } )] $LeftProperties = '*', - [ValidateScript( {$_ -is [Collections.Hashtable] -or $_ -is [string] -or $_ -is [Collections.Specialized.OrderedDictionary]})] + [ValidateScript( { $_ -is [Collections.Hashtable] -or $_ -is [string] -or $_ -is [Collections.Specialized.OrderedDictionary] } )] $RightProperties = '*', [string[]]$ExcludeLeftProperties, @@ -247,8 +246,7 @@ function Join-Object [switch]$AllowColumnsMerging ) #region Validate Params - if ($PassThru -and $Type -eq 'AllInBoth') - { + if ($PassThru -and $Type -eq 'AllInBoth') { $PSCmdlet.ThrowTerminatingError( [Management.Automation.ErrorRecord]::new( [ArgumentException]::new('"-PassThru" and "-Type AllInBoth" are not compatible'), @@ -259,8 +257,7 @@ function Join-Object ) } - if ($AddKey -and $Type -ne 'AllInBoth') - { + if ($AddKey -and $Type -ne 'AllInBoth') { $PSCmdlet.ThrowTerminatingError( [Management.Automation.ErrorRecord]::new( [ArgumentException]::new('"-AddKey" support only "-Type AllInBoth"'), @@ -271,10 +268,8 @@ function Join-Object ) } - if ($Type -in 'AllInLeft', 'OnlyIfInBoth') - { - if ($PSBoundParameters['LeftMultiMode'] -ne 'DuplicateLines' -and $null -ne $PSBoundParameters['LeftMultiMode']) - { + if ($Type -in 'AllInLeft', 'OnlyIfInBoth') { + if ($PSBoundParameters['LeftMultiMode'] -ne 'DuplicateLines' -and $null -ne $PSBoundParameters['LeftMultiMode']) { $PSCmdlet.ThrowTerminatingError( [Management.Automation.ErrorRecord]::new( [ArgumentException]::new('"-Type AllInLeft" and "-Type OnlyIfInBoth" support only "-LeftMultiMode DuplicateLines"'), @@ -285,15 +280,13 @@ function Join-Object ) } $Attributes = (Get-Variable 'LeftMultiMode').Attributes - $null = $Attributes.Remove($Attributes.Where( {$_.TypeId.Name -eq 'ValidateSetAttribute'})[0]) + $null = $Attributes.Remove($Attributes.Where( { $_.TypeId.Name -eq 'ValidateSetAttribute' } )[0]) $ValidateSetAttribute = [System.Management.Automation.ValidateSetAttribute]::new('SingleOnly', 'DuplicateLines', 'SubGroups', $null) $Attributes.Add($ValidateSetAttribute) $LeftMultiMode = $null } - if ($Type -in 'OnlyIfInBoth') - { - if ($PSBoundParameters['RightMultiMode'] -ne 'DuplicateLines' -and $null -ne $PSBoundParameters['RightMultiMode']) - { + if ($Type -in 'OnlyIfInBoth') { + if ($PSBoundParameters['RightMultiMode'] -ne 'DuplicateLines' -and $null -ne $PSBoundParameters['RightMultiMode']) { $PSCmdlet.ThrowTerminatingError( [Management.Automation.ErrorRecord]::new( [ArgumentException]::new('"-Type OnlyIfInBoth" support only "-RightMultiMode DuplicateLines"'), @@ -304,14 +297,13 @@ function Join-Object ) } $Attributes = (Get-Variable 'RightMultiMode').Attributes - $null = $Attributes.Remove($Attributes.Where( {$_.TypeId.Name -eq 'ValidateSetAttribute'})[0]) + $null = $Attributes.Remove($Attributes.Where( { $_.TypeId.Name -eq 'ValidateSetAttribute' } )[0]) $ValidateSetAttribute = [System.Management.Automation.ValidateSetAttribute]::new('SingleOnly', 'DuplicateLines', 'SubGroups', $null) $Attributes.Add($ValidateSetAttribute) $RightMultiMode = $null } - if ($AllowColumnsMerging -and !$DataTable -and !($PassThru -and $Left -is [DataTable])) - { + if ($AllowColumnsMerging -and !$DataTable -and !($PassThru -and $Left -is [DataTable])) { $PSCmdlet.ThrowTerminatingError( [Management.Automation.ErrorRecord]::new( [ArgumentException]::new('"-AllowColumnsMerging" support only on DataTable output'), @@ -323,8 +315,7 @@ function Join-Object } #endregion Validate Params #region Set $SelectedLeftProperties and $SelectedRightProperties - function Get-Properties - { + function Get-Properties { [CmdletBinding()] param ( @@ -334,65 +325,51 @@ function Join-Object $Prefix, $Suffix ) - $Properties = [ordered]@{} - if ($Object -is [System.Data.DataTable]) - { + $Properties = [ordered]@{ } + if ($Object -is [System.Data.DataTable]) { $ObjectProperties = $Object.Columns.ColumnName } - else - { + else { $ObjectProperties = $Object[0].PSObject.Properties.Name } - if ($SelectProperties -is [hashtable] -or $SelectProperties -is [Collections.Specialized.OrderedDictionary]) - { - $SelectProperties.GetEnumerator() | Where-Object {$_.Key -notin $ExcludeProperties} | ForEach-Object {$Properties.Add($_.Key, $Prefix + $_.Value + $Suffix)} + if ($SelectProperties -is [hashtable] -or $SelectProperties -is [Collections.Specialized.OrderedDictionary]) { + $SelectProperties.GetEnumerator() | Where-Object { $_.Key -notin $ExcludeProperties } | ForEach-Object { $Properties.Add($_.Key, $Prefix + $_.Value + $Suffix) } } - elseif ($SelectProperties -eq '*') - { - $ObjectProperties | Where-Object {$_ -notin $ExcludeProperties} | ForEach-Object {$Properties.Add($_, $Prefix + $_ + $Suffix)} + elseif ($SelectProperties -eq '*') { + $ObjectProperties | Where-Object { $_ -notin $ExcludeProperties } | ForEach-Object { $Properties.Add($_, $Prefix + $_ + $Suffix) } } - else - { - $SelectProperties | Where-Object {$_ -notin $ExcludeProperties} | ForEach-Object {$Properties.Add($_, $Prefix + $_ + $Suffix)} + else { + $SelectProperties | Where-Object { $_ -notin $ExcludeProperties } | ForEach-Object { $Properties.Add($_, $Prefix + $_ + $Suffix) } } $Properties } $SelectedLeftProperties = Get-Properties -Object $Left -SelectProperties $LeftProperties -ExcludeProperties $ExcludeLeftProperties - if (!$KeepRightJoinProperty) - { + if (!$KeepRightJoinProperty) { $ExcludeRightProperties = @($ExcludeRightProperties) + @($RightJoinProperty) -ne $null } $SelectedRightProperties = Get-Properties -Object $Right -SelectProperties $RightProperties -ExcludeProperties $ExcludeRightProperties -Prefix $Prefix -Suffix $Suffix #endregion Set $SelectedLeftProperties and $SelectedRightProperties #region Importing package MoreLinq - if ($Type -eq 'AllInBoth') - { - try - { - if ($PSScriptRoot) - { + if ($Type -eq 'AllInBoth') { + try { + if ($PSScriptRoot) { $ScriptRoot = $PSScriptRoot } - elseif ($psISE.CurrentFile.IsUntitled -eq $false) - { + elseif ($psISE.CurrentFile.IsUntitled -eq $false) { $ScriptRoot = Split-Path -Path $psISE.CurrentFile.FullPath } - elseif ($null -ne $psEditor.GetEditorContext().CurrentFile.Path -and $psEditor.GetEditorContext().CurrentFile.Path -notlike 'untitled:*') - { + elseif ($null -ne $psEditor.GetEditorContext().CurrentFile.Path -and $psEditor.GetEditorContext().CurrentFile.Path -notlike 'untitled:*') { $ScriptRoot = Split-Path -Path $psEditor.GetEditorContext().CurrentFile.Path } - else - { + else { $ScriptRoot = '.' } - if (!('MoreLinq.MoreEnumerable' -as [type])) - { + if (!('MoreLinq.MoreEnumerable' -as [type])) { Add-Type -Path (Resolve-Path -Path "$ScriptRoot\morelinq.*\lib\net451\MoreLinq.dll") } } - catch - { + catch { $PSCmdlet.ThrowTerminatingError( [Management.Automation.ErrorRecord]::new( [TypeLoadException]::new('Importing package MoreLinq failed: {0}' -f $_.Exception.Message, $_.Exception), @@ -405,8 +382,7 @@ function Join-Object } #endregion Importing package MoreLinq #region Set $RightJoinScript and $LeftJoinScript - function Get-JoinScript - { + function Get-JoinScript { [CmdletBinding()] param ( @@ -415,30 +391,24 @@ function Join-Object $Side, $Object ) - if ($JoinScript) - { + if ($JoinScript) { $JoinScript #.GetNewClosure() } - else - { - $JoinScript = if ($JoinProperty.Count -gt 1) - { + else { + $JoinScript = if ($JoinProperty.Count -gt 1) { { param ($_Side_Line) ($_Side_Line | Select-Object -Property $_Side_JoinProperty).PSObject.Properties.Value } } - else - { - if ($Object -is [Data.DataTable]) - { + else { + if ($Object -is [Data.DataTable]) { { param ($_Side_Line) $_Side_Line[$_Side_JoinProperty] } } - else - { + else { { param ($_Side_Line) $_Side_Line.$_Side_JoinProperty @@ -452,8 +422,7 @@ function Join-Object $RightJoinScript = Get-JoinScript -JoinScript $RightJoinScript -JoinProperty $RightJoinProperty -Side 'Right' -Object $Right #endregion Set $RightJoinScript and $LeftJoinScript #region Prepare Data - function Set-OutDataTable - { + function Set-OutDataTable { param ( $OutDataTable, @@ -462,99 +431,75 @@ function Join-Object $AllowColumnsMerging ) # Create Columns - foreach ($item in $SelectedProperties.GetEnumerator()) - { - if (!$AllowColumnsMerging -or !$OutDataTable.Columns.Item($item.Value)) - { - if ($Object -is [Data.DataTable]) - { + foreach ($item in $SelectedProperties.GetEnumerator()) { + if (!$AllowColumnsMerging -or !$OutDataTable.Columns.Item($item.Value)) { + if ($Object -is [Data.DataTable]) { $null = $OutDataTable.Columns.Add($item.Value, $Object.Columns.Item($item.Name).DataType) } - else - { - if ($null -ne $DataTableTypes.($item.Value)) - { + else { + if ($null -ne $DataTableTypes.($item.Value)) { $null = $OutDataTable.Columns.Add($item.Value, $DataTableTypes.($item.Value)) } - else - { + else { $null = $OutDataTable.Columns.Add($item.Value) } } } } } - if ($DataTable) - { + if ($DataTable) { $OutDataTable = [Data.DataTable]::new('Joined') - if ($AddKey) - { + if ($AddKey) { $null = $OutDataTable.Columns.Add('Key') } - if ($LeftMultiMode -eq 'SubGroups') - { + if ($LeftMultiMode -eq 'SubGroups') { $OutDataTableSubGroupTemplateLeft = [Data.DataTable]::new('LeftGroup') Set-OutDataTable -OutDataTable $OutDataTableSubGroupTemplateLeft -Object $Left -SelectedProperties $SelectedLeftProperties $null = $OutDataTable.Columns.Add('LeftGroup', [Object]) } - else - { + else { Set-OutDataTable -OutDataTable $OutDataTable -Object $Left -SelectedProperties $SelectedLeftProperties } - if ($RightMultiMode -eq 'SubGroups') - { + if ($RightMultiMode -eq 'SubGroups') { $OutDataTableSubGroupTemplateRight = [Data.DataTable]::new('RightGroup') Set-OutDataTable -OutDataTable $OutDataTableSubGroupTemplateRight -Object $Right -SelectedProperties $SelectedRightProperties $null = $OutDataTable.Columns.Add('RightGroup', [Object]) } - else - { + else { Set-OutDataTable -OutDataTable $OutDataTable -Object $Right -SelectedProperties $SelectedRightProperties -AllowColumnsMerging $AllowColumnsMerging } } - elseif ($PassThru -and $Left -is [Data.DataTable]) - { + elseif ($PassThru -and $Left -is [Data.DataTable]) { # Remove LeftLine - foreach ($ColumnName in $Left.Columns.ColumnName) - { - if ($ColumnName -notin $SelectedLeftProperties.Keys) - { + foreach ($ColumnName in $Left.Columns.ColumnName) { + if ($ColumnName -notin $SelectedLeftProperties.Keys) { $Left.Columns.Remove($ColumnName) } } # Rename LeftLine - foreach ($item in $SelectedLeftProperties.GetEnumerator()) - { - if ($item.Key -ne $item.value -and ($Column = $Left.Columns.Item($item.Key))) - { + foreach ($item in $SelectedLeftProperties.GetEnumerator()) { + if ($item.Key -ne $item.value -and ($Column = $Left.Columns.Item($item.Key))) { $Column.ColumnName = $item.value } } - if ($RightMultiMode -eq 'SubGroups') - { + if ($RightMultiMode -eq 'SubGroups') { $null = $Left.Columns.Add('RightGroup', [Object]) } - else - { + else { # Add RightLine to LeftLine - foreach ($item in $SelectedRightProperties.GetEnumerator()) - { - if (!$AllowColumnsMerging -or !$Left.Columns.Item($item.Value)) - { - if ($null -ne $DataTableTypes.($item.Value)) - { + foreach ($item in $SelectedRightProperties.GetEnumerator()) { + if (!$AllowColumnsMerging -or !$Left.Columns.Item($item.Value)) { + if ($null -ne $DataTableTypes.($item.Value)) { $null = $Left.Columns.Add($item.Value, $DataTableTypes.($item.Value)) } - else - { + else { $null = $Left.Columns.Add($item.Value) } } } } } - if ($PassThru -and $Right -is [Data.DataTable]) - { + if ($PassThru -and $Right -is [Data.DataTable]) { $OutDataTableSubGroupTemplateRight = [Data.DataTable]::new('RightGroup') Set-OutDataTable -OutDataTable $OutDataTableSubGroupTemplateRight -Object $Right -SelectedProperties $SelectedRightProperties } @@ -563,167 +508,142 @@ function Join-Object #region Main: Set $QueryParts $QueryParts = @{ 'IfSideLine' = { - if ($_Side_Line) - { + if ($_Side_Line) { _SideScript_ } } 'DataTableFromAny' = { - foreach ($item in $Selected_Side_Properties.GetEnumerator()) - { - if ($null -ne ($Value = $_Side_Line.($item.Key))) - { + foreach ($item in $Selected_Side_Properties.GetEnumerator()) { + if ($null -ne ($Value = $_Side_Line.($item.Key))) { $_Row_[$item.Value] = $Value } } } 'DataTableFromDataTable' = { - foreach ($item in $Selected_Side_Properties.GetEnumerator()) - { - if (($Value = $_Side_Line[$item.Key]) -isnot [DBNull]) - { + foreach ($item in $Selected_Side_Properties.GetEnumerator()) { + if (($Value = $_Side_Line[$item.Key]) -isnot [DBNull]) { $_Row_[$item.Value] = $Value } } } 'DataTableFromSubGroup' = { - if ($_Side_Lines) - { + if ($_Side_Lines) { _SubGroup_ $_Row_['_Side_Group'] = $OutSubGroup_Side_ } } 'SubGroupFromDataTable' = { $OutSubGroup_Side_ = $OutDataTableSubGroupTemplate_Side_.Clone() - foreach ($_Side_Line in $_Side_Lines) - { + foreach ($_Side_Line in $_Side_Lines) { $RowSubGroup = $OutSubGroup_Side_.Rows.Add() _DataTable_ } } 'SubGroupFromPSCustomObject' = { $OutSubGroup_Side_ = @() - foreach ($_Side_Line in $_Side_Lines) - { - $RowSubGroup = [ordered]@{} + foreach ($_Side_Line in $_Side_Lines) { + $RowSubGroup = [ordered]@{ } _PSCustomObject_ $OutSubGroup_Side_ += [PSCustomObject]$RowSubGroup } } 'PSCustomObjectFromPSCustomObject' = { - foreach ($item in $Selected_Side_Properties.GetEnumerator()) - { + foreach ($item in $Selected_Side_Properties.GetEnumerator()) { $_Row_.Add($item.Value, $_Side_Line.($item.Key)) } } 'PSCustomObjectFromAny' = { - foreach ($item in $Selected_Side_Properties.GetEnumerator()) - { - if (($Value = $_Side_Line.($item.Key)) -is [DBNull]) - { + foreach ($item in $Selected_Side_Properties.GetEnumerator()) { + if (($Value = $_Side_Line.($item.Key)) -is [DBNull]) { $Value = $null } $_Row_.Add($item.Value, $Value) } } 'PSCustomObjectFromSubGroup' = { - if ($_Side_Lines) - { + if ($_Side_Lines) { _SubGroup_ $_Row_.Add('_Side_Group', $OutSubGroup_Side_) } - else - { + else { $_Row_.Add('_Side_Group', $null) } } } - foreach ($Item in @($QueryParts.GetEnumerator())) - { + foreach ($Item in @($QueryParts.GetEnumerator())) { $QueryParts[$Item.Key] = $Item.Value.ToString() } #endregion Main: Set $QueryParts #region Main: Set $Query - $Query = if ($PassThru) - { - if ($Left -is [Data.DataTable]) - { + $Query = if ($PassThru) { + if ($Left -is [Data.DataTable]) { $QueryTemp = @{ - Main = {_SidesScript_} - Left = {} - Side = {_DataTable_} + Main = { _SidesScript_ } + Left = { } + Side = { _DataTable_ } SideReplace = '_Row_', 'LeftLine' } - if ($Right -is [Data.DataTable]) - { + if ($Right -is [Data.DataTable]) { $QueryTemp['SideSubGroup'] = $QueryParts['DataTableFromSubGroup'].Replace('_Row_', 'LeftLine').Replace('_SubGroup_', $QueryParts['SubGroupFromDataTable']) } - else - { + else { $QueryTemp['SideSubGroup'] = $QueryParts['DataTableFromSubGroup'].Replace('_Row_', 'LeftLine').Replace('_SubGroup_', $QueryParts['SubGroupFromPSCustomObject']) } $QueryTemp } - else # Left is PSCustomObject - { + else { + # Left is PSCustomObject $QueryTemp = @{ # Edit PSCustomObject Main = { # Add to LeftLine (Rename) - foreach ($item in $SelectedLeftProperties.GetEnumerator()) - { - if ($item.Value -notin $LeftLine.PSObject.Properties.Name) - { + foreach ($item in $SelectedLeftProperties.GetEnumerator()) { + if ($item.Value -notin $LeftLine.PSObject.Properties.Name) { $LeftLine.PSObject.Properties.Add([Management.Automation.PSNoteProperty]::new($item.Value, $LeftLine.($item.Key))) } } # Remove from LeftLine - foreach ($item in $LeftLine.PSObject.Properties.Name) - { - if ($item -notin $SelectedLeftProperties.Values) - { + foreach ($item in $LeftLine.PSObject.Properties.Name) { + if ($item -notin $SelectedLeftProperties.Values) { $LeftLine.PSObject.Properties.Remove($item) } } _SidesScript_ } - Left = {} - Side = {_PSCustomObject_} + Left = { } + Side = { _PSCustomObject_ } SideReplace = '_Row_\.Add([^\r\n]*)', 'LeftLine.PSObject.Properties.Add([Management.Automation.PSNoteProperty]::new$1)' } - if ($Right -is [Data.DataTable]) - { + if ($Right -is [Data.DataTable]) { $QueryTemp['SideSubGroup'] = ($QueryParts['PSCustomObjectFromSubGroup'] -Replace $QueryTemp['SideReplace']).Replace('_SubGroup_', $QueryParts['SubGroupFromDataTable']) } - else - { + else { $QueryTemp['SideSubGroup'] = ($QueryParts['PSCustomObjectFromSubGroup'] -Replace $QueryTemp['SideReplace']).Replace('_SubGroup_', $QueryParts['SubGroupFromPSCustomObject']) } $QueryTemp } } - elseif ($DataTable) - { + elseif ($DataTable) { @{ Main = { $RowMain = $OutDataTable.Rows.Add() _SidesScript_ } - Side = {_DataTable_} + Side = { _DataTable_ } SideReplace = '_Row_', 'RowMain' SideSubGroup = $QueryParts['DataTableFromSubGroup'].Replace('_Row_', 'RowMain').Replace('_SubGroup_', $QueryParts['SubGroupFromDataTable']) } } - else # PSCustomObject - { + else { + # PSCustomObject @{ Main = { - $RowMain = [ordered]@{} + $RowMain = [ordered]@{ } _SidesScript_ [PSCustomObject]$RowMain } - Side = {_PSCustomObject_} + Side = { _PSCustomObject_ } SideReplace = '_Row_', 'RowMain' SideSubGroup = $QueryParts['PSCustomObjectFromSubGroup'].Replace('_Row_', 'RowMain').Replace('_SubGroup_', $QueryParts['SubGroupFromPSCustomObject']) } @@ -737,80 +657,64 @@ function Join-Object ) } - foreach ($Item in @($Query.GetEnumerator())) - { - if ($Item.Value -is [scriptblock]) - { + foreach ($Item in @($Query.GetEnumerator())) { + if ($Item.Value -is [scriptblock]) { $Query[$Item.Key] = $Item.Value.ToString() } } #endregion Main: Set $Query #region Main: Assemble $Query - function Invoke-AssembledQuery - { + function Invoke-AssembledQuery { param ( $MultiMode, $Side, $Object ) - if ($MultiMode -eq 'SingleOnly') - { - $Query[$Side + 'Enumerable'] = {$_Side_Line = [System.Linq.Enumerable]::SingleOrDefault($_Side_Line)} + if ($MultiMode -eq 'SingleOnly') { + $Query[$Side + 'Enumerable'] = { $_Side_Line = [System.Linq.Enumerable]::SingleOrDefault($_Side_Line) } } - elseif ($MultiMode -eq 'DuplicateLines') - { - $Query[$Side + 'Enumerable'] = {$_Side_Lines = [System.Linq.Enumerable]::DefaultIfEmpty($_Side_Line)} + elseif ($MultiMode -eq 'DuplicateLines') { + $Query[$Side + 'Enumerable'] = { $_Side_Lines = [System.Linq.Enumerable]::DefaultIfEmpty($_Side_Line) } $Query['Main'] = { - foreach ($_Side_Line in $_Side_Lines) - { + foreach ($_Side_Line in $_Side_Lines) { _MainScript_ } }.ToString().Replace('_Side_', $Side).Replace('_MainScript_', $Query['Main']) } - if ($MultiMode -eq 'SubGroups') - { - $Query[$Side + 'Enumerable'] = {$_Side_Lines = if ($_Side_Line.Count) {$_Side_Line}} - $Query[$Side] = if ($Object -is [Data.DataTable]) - { + if ($MultiMode -eq 'SubGroups') { + $Query[$Side + 'Enumerable'] = { $_Side_Lines = if ($_Side_Line.Count) { $_Side_Line } } + $Query[$Side] = if ($Object -is [Data.DataTable]) { $Query['SideSubGroup'].Replace('_DataTable_', $QueryParts['DataTableFromDataTable']).Replace('_PSCustomObject_', $QueryParts['PSCustomObjectFromAny']) } - else - { + else { $Query['SideSubGroup'].Replace('_DataTable_', $QueryParts['DataTableFromAny']).Replace('_PSCustomObject_', $QueryParts['PSCustomObjectFromPSCustomObject']) } $Query[$Side] = $Query[$Side].Replace('_Row_', 'RowSubGroup') } - else - { - if ($null -eq $Query[$Side]) - { + else { + if ($null -eq $Query[$Side]) { $Query[$Side] = $Query['Side'] } - if ($null -ne $MultiMode -and $Query[$Side] -like '*_DataTable_*') - { + if ($null -ne $MultiMode -and $Query[$Side] -like '*_DataTable_*') { $Query[$Side] = $QueryParts['IfSideLine'].Replace('_SideScript_', $Query[$Side]) } - $Query[$Side] = if ($Object -is [Data.DataTable]) - { + $Query[$Side] = if ($Object -is [Data.DataTable]) { $Query[$Side].Replace('_DataTable_', $QueryParts['DataTableFromDataTable']).Replace('_PSCustomObject_', $QueryParts['PSCustomObjectFromAny']) } - else - { + else { $Query[$Side].Replace('_DataTable_', $QueryParts['DataTableFromAny']).Replace('_PSCustomObject_', $QueryParts['PSCustomObjectFromPSCustomObject']) } $Query[$Side] = $Query[$Side] -Replace $Query['SideReplace'] } $Query[$Side] = $Query[$Side].Replace('_Side_', $Side) - if ($Query[$Side + 'Enumerable']) - { + if ($Query[$Side + 'Enumerable']) { $Query[$Side + 'Enumerable'] = $Query[$Side + 'Enumerable'].ToString().Replace('_Side_', $Side) } } Invoke-AssembledQuery -MultiMode $LeftMultiMode -Side 'Left' -Object $Left Invoke-AssembledQuery -MultiMode $RightMultiMode -Side 'Right' -Object $Right - if ($AddKey) - { + if ($AddKey) { $KeyScript = { $RowMain.Key = $Key _SidesScript_ @@ -820,82 +724,65 @@ function Join-Object $Query['Main'] = $Query['Main'].Replace('_SidesScript_', $Query['Left'] + $Query['Right']) - if ($Type -eq 'OnlyIfInBoth') - { + if ($Type -eq 'OnlyIfInBoth') { [System.Func[System.Object, System.Object, System.Object]]$Query = [Scriptblock]::Create($Query['Base'] + $Query['Main']) } - elseif ($Type -eq 'AllInLeft') - { + elseif ($Type -eq 'AllInLeft') { [System.Func[System.Object, [Collections.Generic.IEnumerable[System.Object]], System.Object]]$Query = [Scriptblock]::Create($Query['Base'] + $Query['RightEnumerable'] + $Query['Main']) } - elseif ($Type -eq 'AllInBoth') - { + elseif ($Type -eq 'AllInBoth') { [System.Func[System.Object, [Collections.Generic.IEnumerable[System.Object]], [Collections.Generic.IEnumerable[System.Object]], System.Object]]$Query = [Scriptblock]::Create($Query['Base'].Replace('#_$Key_', '$Key') + $Query['LeftEnumerable'] + "`n" + $Query['RightEnumerable'] + $Query['Main']) } #endregion Main: Assemble $Query #endregion Main #region Execute - if ($Left -is [Data.DataTable]) - { + if ($Left -is [Data.DataTable]) { $LeftNew = [DataTableExtensions]::AsEnumerable($Left) } - elseif ($Left -is [PSCustomObject]) - { + elseif ($Left -is [PSCustomObject]) { $LeftNew = @($Left) } - else - { + else { $LeftNew = $Left } - if ($Right -is [Data.DataTable]) - { + if ($Right -is [Data.DataTable]) { $RightNew = [DataTableExtensions]::AsEnumerable($Right) } - elseif ($Right -is [PSCustomObject]) - { + elseif ($Right -is [PSCustomObject]) { $RightNew = @($Right) } - else - { + else { $RightNew = $Right } - try - { - $Result = if ($Type -eq 'OnlyIfInBoth') - { + try { + $Result = if ($Type -eq 'OnlyIfInBoth') { [System.Linq.Enumerable]::ToArray( [System.Linq.Enumerable]::Join($LeftNew, $RightNew, $LeftJoinScript, $RightJoinScript, $query) ) } - elseif ($Type -eq 'AllInBoth') - { + elseif ($Type -eq 'AllInBoth') { [System.Linq.Enumerable]::ToArray( [MoreLinq.MoreEnumerable]::FullGroupJoin($LeftNew, $RightNew, $LeftJoinScript, $RightJoinScript, $query) ) } - else - { + else { [System.Linq.Enumerable]::ToArray( [System.Linq.Enumerable]::GroupJoin($LeftNew, $RightNew, $LeftJoinScript, $RightJoinScript, $query) ) } - if ($PassThru) - { + if ($PassThru) { , $Left } - elseif ($DataTable) - { + elseif ($DataTable) { , $OutDataTable } - else - { + else { $Result } } - catch - { + catch { $PSCmdlet.ThrowTerminatingError($_) } #endregion Execute diff --git a/Join-Object.psm1 b/Join-Object.psm1 index 6b02b91..63d0fd8 100644 --- a/Join-Object.psm1 +++ b/Join-Object.psm1 @@ -1,2 +1,2 @@ #Get-ChildItem -Path $PSScriptRoot | Unblock-File -Get-ChildItem -Path "$PSScriptRoot\*.ps1" -Exclude 'Class.ps1', 'Install.ps1' | Foreach-Object {. $_.FullName} \ No newline at end of file +Get-ChildItem -Path "$PSScriptRoot\*.ps1" -Exclude 'Class.ps1', 'Install.ps1' | ForEach-Object { . $_.FullName } \ No newline at end of file diff --git a/Tests/Join-Object.Tests.Big.ps1 b/Tests/Join-Object.Tests.Big.ps1 index 202a020..c0aa6ff 100644 --- a/Tests/Join-Object.Tests.Big.ps1 +++ b/Tests/Join-Object.Tests.Big.ps1 @@ -1,40 +1,34 @@ #Requires -Modules Pester -#Requires -Modules @{ModuleName = 'Assert' ; ModuleVersion = '999.9.2'} +#Requires -Modules @{ ModuleName = 'Assert' ; ModuleVersion = '999.9.2' } #Requires -Modules PSSQLite -$Verbose = @{Verbose = $false} -#$Verbose = @{Verbose = $true} +$Verbose = @{ Verbose = $false } +#$Verbose = @{ Verbose = $true } -if ($PSScriptRoot) -{ +if ($PSScriptRoot) { $ScriptRoot = $PSScriptRoot } -elseif ($psISE.CurrentFile.IsUntitled -eq $false) -{ +elseif ($psISE.CurrentFile.IsUntitled -eq $false) { $ScriptRoot = Split-Path -Path $psISE.CurrentFile.FullPath } -elseif ($null -ne $psEditor.GetEditorContext().CurrentFile.Path -and $psEditor.GetEditorContext().CurrentFile.Path -notlike 'untitled:*') -{ +elseif ($null -ne $psEditor.GetEditorContext().CurrentFile.Path -and $psEditor.GetEditorContext().CurrentFile.Path -notlike 'untitled:*') { $ScriptRoot = Split-Path -Path $psEditor.GetEditorContext().CurrentFile.Path } -else -{ +else { $ScriptRoot = '.' } $TestDataSetBig10k = "$ScriptRoot\TestDataSetBig10k.db" $TestDataSetBig100k = "$ScriptRoot\TestDataSetBig100k.db" -function Format-Test -{ +function Format-Test { [CmdletBinding()] param ( [Parameter(Mandatory)] [HashTable]$Test ) - if ($TestDataSetName, $Test.Params.Left, $Test.Params.Right, $Test.Description -contains $null) - { + if ($TestDataSetName, $Test.Params.Left, $Test.Params.Right, $Test.Description -contains $null) { Throw 'Missing param' } @@ -42,8 +36,7 @@ function Format-Test $Test } -function Get-Params -{ +function Get-Params { [CmdletBinding()] param ( @@ -61,8 +54,8 @@ Describe -Name 'Join-Object' -Fixture { Format-Test @{ Description = 'Basic' Params = @{ - Left = @{Table = 'authors' ; As = 'DataTable'} - Right = @{Table = 'posts' ; As = 'DataTable'} + Left = @{ Table = 'authors' ; As = 'DataTable' } + Right = @{ Table = 'posts' ; As = 'DataTable' } LeftJoinProperty = 'author_id' RightJoinProperty = 'author_id' #DataTable = $true @@ -77,11 +70,10 @@ Describe -Name 'Join-Object' -Fixture { $Description, $RunScript ) - #if ($TestName -ne 'Small: PSCustomObjects - DataTable, DataTable') {Continue} + #if ($TestName -ne 'Small: PSCustomObjects - DataTable, DataTable') { Continue } # Load Data - if ($RunScript) - { + if ($RunScript) { . $RunScript } $Params.Left = Get-Params -Param $Params.Left diff --git a/Tests/Join-Object.Tests.ps1 b/Tests/Join-Object.Tests.ps1 index b6355e6..832e9d3 100644 --- a/Tests/Join-Object.Tests.ps1 +++ b/Tests/Join-Object.Tests.ps1 @@ -1,37 +1,33 @@ #Requires -Modules Pester -#Requires -Modules @{ModuleName = 'Assert' ; ModuleVersion = '999.9.2'} +#Requires -Modules @{ ModuleName = 'Assert' ; ModuleVersion = '999.9.2' } -$Verbose = @{Verbose = $false} -#$Verbose = @{Verbose = $true} +$Verbose = @{ Verbose = $false } +#$Verbose = @{ Verbose = $true } -if ($PSScriptRoot) -{ +if ($PSScriptRoot) { $ScriptRoot = $PSScriptRoot } -elseif ($psISE.CurrentFile.IsUntitled -eq $false) -{ +elseif ($psISE.CurrentFile.IsUntitled -eq $false) { $ScriptRoot = Split-Path -Path $psISE.CurrentFile.FullPath } -elseif ($null -ne $psEditor.GetEditorContext().CurrentFile.Path -and $psEditor.GetEditorContext().CurrentFile.Path -notlike 'untitled:*') -{ +elseif ($null -ne $psEditor.GetEditorContext().CurrentFile.Path -and $psEditor.GetEditorContext().CurrentFile.Path -notlike 'untitled:*') { $ScriptRoot = Split-Path -Path $psEditor.GetEditorContext().CurrentFile.Path } -else -{ +else { $ScriptRoot = '.' } $TestDataSetSmall = { $PSCustomObject = @( - [PSCustomObject]@{ID = 1 ; Sub = 'S1' ; IntO = 6} - [PSCustomObject]@{ID = 2 ; Sub = 'S2' ; IntO = 7} - [PSCustomObject]@{ID = 3 ; Sub = 'S3' ; IntO = $null} + [PSCustomObject]@{ ID = 1 ; Sub = 'S1' ; IntO = 6 } + [PSCustomObject]@{ ID = 2 ; Sub = 'S2' ; IntO = 7 } + [PSCustomObject]@{ ID = 3 ; Sub = 'S3' ; IntO = $null } ) $PSCustomObjectJunk = @( - [PSCustomObject]@{ID = 1 ; Sub = 'S1' ; IntO = 6 ; Junk = 'New1'} - [PSCustomObject]@{ID = 2 ; Sub = 'S2' ; IntO = 7 ; Junk = $null} - [PSCustomObject]@{ID = 3 ; Sub = 'S3' ; IntO = $null ; Junk = $null} + [PSCustomObject]@{ ID = 1 ; Sub = 'S1' ; IntO = 6 ; Junk = 'New1' } + [PSCustomObject]@{ ID = 2 ; Sub = 'S2' ; IntO = 7 ; Junk = $null } + [PSCustomObject]@{ ID = 3 ; Sub = 'S3' ; IntO = $null ; Junk = $null } ) $DataTable = [Data.DataTable]::new('Test') @@ -47,10 +43,10 @@ $TestDataSetSmall = { $TestDataSetSmallMulti = { $PSCustomObject = @( - [PSCustomObject]@{ID = 1 ; Sub = 'S1' ; IntO = 6} - [PSCustomObject]@{ID = 1 ; Sub = 'S12' ; IntO = 62} - [PSCustomObject]@{ID = 2 ; Sub = 'S2' ; IntO = 7} - [PSCustomObject]@{ID = 2 ; Sub = 'S22' ; IntO = 72} + [PSCustomObject]@{ ID = 1 ; Sub = 'S1' ; IntO = 6 } + [PSCustomObject]@{ ID = 1 ; Sub = 'S12' ; IntO = 62 } + [PSCustomObject]@{ ID = 2 ; Sub = 'S2' ; IntO = 7 } + [PSCustomObject]@{ ID = 2 ; Sub = 'S22' ; IntO = 72 } ) $DataTable = [Data.DataTable]::new('Test') @@ -65,16 +61,14 @@ $TestDataSetSmallMulti = { } #. $TestDataSetSmallMulti -function Format-Test -{ +function Format-Test { [CmdletBinding()] param ( [Parameter(Mandatory)] [HashTable]$Test ) - if ($TestDataSetName, $Test.Params.Left, $Test.Params.Right, $Test.Description -contains $null) - { + if ($TestDataSetName, $Test.Params.Left, $Test.Params.Right, $Test.Description -contains $null) { Throw 'Missing param' } @@ -83,44 +77,34 @@ function Format-Test $Test } -function ConvertFrom-DataTable -{ +function ConvertFrom-DataTable { [CmdletBinding()] param ( [Parameter(Mandatory, Position = 0, ValueFromPipeline)] - [ValidateScript( {$_ -is [System.Data.DataRow]})] + [ValidateScript( { $_ -is [System.Data.DataRow] } )] $InputObject ) - begin - { + begin { $First = $true } - process - { - if ($First) - { + process { + if ($First) { $First = $false - $DataSetProperties = if ($InputObject -is [System.Data.DataTable]) - { + $DataSetProperties = if ($InputObject -is [System.Data.DataTable]) { $InputObject.Columns.ColumnName } - else - { - $InputObject.psobject.Properties.Name | Where-Object {$_ -notin ('RowError', 'RowState', 'Table', 'ItemArray', 'HasErrors')} + else { + $InputObject.psobject.Properties.Name | Where-Object { $_ -notin ('RowError', 'RowState', 'Table', 'ItemArray', 'HasErrors') } } } - foreach ($DataRow in $InputObject) - { - $RowHash = [ordered]@{} - foreach ($DataSetProperty in $DataSetProperties) - { - if ($DataRow.$DataSetProperty -is [DBNull]) - { + foreach ($DataRow in $InputObject) { + $RowHash = [ordered]@{ } + foreach ($DataSetProperty in $DataSetProperties) { + if ($DataRow.$DataSetProperty -is [DBNull]) { $RowHash.$DataSetProperty = $null } - else - { + else { $RowHash.$DataSetProperty = $DataRow.$DataSetProperty } } @@ -129,41 +113,33 @@ function ConvertFrom-DataTable } } -function ConvertTo-DataTable -{ +function ConvertTo-DataTable { [CmdletBinding()] param( [Parameter(Position = 0, Mandatory, ValueFromPipeline)] $InputObject ) - begin - { + begin { $OutputDataTable = [Data.DataTable]::new() $First = $true } - process - { - foreach ($PSCustomObject in $InputObject) - { - if ($First) - { + process { + foreach ($PSCustomObject in $InputObject) { + if ($First) { $First = $false - foreach ($Property in $PSCustomObject.PSObject.Properties) - { + foreach ($Property in $PSCustomObject.PSObject.Properties) { $null = $OutputDataTable.Columns.Add($Property.Name, $Property.TypeNameOfValue) } } $null = $OutputDataTable.Rows.Add($PSCustomObject.PSObject.Properties.Value) } } - end - { + end { , $OutputDataTable } } -function Get-Params -{ +function Get-Params { [CmdletBinding()] param ( @@ -171,24 +147,19 @@ function Get-Params [String]$Param ) $ParamSplit = $Param -split '(?<=\])(?