Skip to content

Commit 7ce4f50

Browse files
committed
Add the SqlCommandBuilder.GetFindAllCommand() method
1 parent 8e88f6c commit 7ce4f50

1 file changed

Lines changed: 59 additions & 3 deletions

File tree

src/SqlCommandBuilder.psm1

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ using namespace System.Data
22
using namespace System.Data.Common
33
using namespace System.Diagnostics.CodeAnalysis
44
using module ./DbColumnInfo.psm1
5+
using module ./DbColumnOrderHintCollection.psm1
56
using module ./DbTableInfo.psm1
7+
using module ./SortOrder.psm1
68
using module ./SqlCommand.psm1
79
using module ./SqlMapper.psm1
810
using module ./SqlParameter.psm1
@@ -189,8 +191,8 @@ class SqlCommandBuilder {
189191
$idColumn = $table.IdentityColumn
190192
if (-not $idColumn) { throw [InvalidOperationException] "The identity column could not be found." }
191193

192-
$fields = ($Columns ? $table.Columns.Values.Where{ $Columns.Contains($_.Name) } : $table.Columns.Values).Where{ $_.CanWrite }.ForEach{ $_.Name }
193-
if (-not $fields.Contains($idColumn.Name)) { $fields += $idColumn.Name }
194+
$fields = ($Columns ? $table.Columns.Values.Where{ $_.Name -in $Columns } : $table.Columns.Values).Where{ $_.CanWrite }.ForEach{ $_.Name }
195+
if ($idColumn.Name -notin $fields) { $fields += $idColumn.Name }
194196

195197
$parameter = [SqlParameter]::new($this.UsePositionalParameters ? "?1" : $this.GetParameterName($idColumn), $id)
196198
$text = "
@@ -201,6 +203,60 @@ class SqlCommandBuilder {
201203
return [ValueTuple]::Create[SqlCommand, SqlParameterCollection]($text.Trim(), [SqlParameterCollection]::new($parameter))
202204
}
203205

206+
<#
207+
.SYNOPSIS
208+
Gets the generated command to find all entities.
209+
.PARAMETER Type
210+
The entity type.
211+
.OUTPUTS
212+
The generated command to find all entities.
213+
#>
214+
[ValueTuple[SqlCommand, SqlParameterCollection]] GetFindAllCommand([Type] $Type) {
215+
return $this.GetFindAllCommand($Type, [DbColumnOrderHintCollection]::new(), @())
216+
}
217+
218+
<#
219+
.SYNOPSIS
220+
Gets the generated command to find all entities.
221+
.PARAMETER Type
222+
The entity type.
223+
.PARAMETER OrderHints
224+
The hints describing the sort order of columns.
225+
.OUTPUTS
226+
The generated command to find all entities.
227+
#>
228+
[ValueTuple[SqlCommand, SqlParameterCollection]] GetFindAllCommand([Type] $Type, [DbColumnOrderHintCollection] $OrderHints) {
229+
return $this.GetFindAllCommand($Type, $OrderHints, @())
230+
}
231+
232+
<#
233+
.SYNOPSIS
234+
Gets the generated command to find all entities.
235+
.PARAMETER Type
236+
The entity type.
237+
.PARAMETER OrderHints
238+
The hints describing the sort order of columns.
239+
.PARAMETER Columns
240+
The list of columns to select. By default, all columns.
241+
.OUTPUTS
242+
The generated command to find all entities.
243+
#>
244+
[ValueTuple[SqlCommand, SqlParameterCollection]] GetFindAllCommand([Type] $Type, [DbColumnOrderHintCollection] $OrderHints, [string[]] $Columns) {
245+
$table = [SqlMapper]::Instance.GetTable($Type)
246+
$idColumn = $table.IdentityColumn
247+
if (-not $idColumn) { throw [InvalidOperationException] "The identity column could not be found." }
248+
249+
$fields = ($Columns ? $table.Columns.Values.Where{ $_.Name -in $Columns } : $table.Columns.Values).Where{ $_.CanWrite }.ForEach{ $_.Name }
250+
if ($idColumn.Name -notin $fields) { $fields += $idColumn.Name }
251+
252+
$orderBy = $OrderHints `
253+
? ($OrderHints.ForEach{ "$($this.QuoteIdentifier($_.Column)) $($_.SortOrder -eq [SortOrder]::Descending ? "DESC" : "ASC")" } -join ", ") `
254+
: "$($this.QuoteIdentifier($idColumn.Name)) ASC"
255+
256+
$text = "SELECT $($fields.ForEach{ $this.QuoteIdentifier($_) } -join ", ") FROM $($this.GetTableName($table)) ORDER BY $orderBy"
257+
return [ValueTuple]::Create[SqlCommand, SqlParameterCollection]($text, [SqlParameterCollection]::new())
258+
}
259+
204260
<#
205261
.SYNOPSIS
206262
Gets the generated command to insert an entity.
@@ -256,7 +312,7 @@ class SqlCommandBuilder {
256312
$idColumn = $table.IdentityColumn
257313
if (-not $idColumn) { throw [InvalidOperationException] "The identity column could not be found." }
258314

259-
$fields = ($Columns ? $table.Columns.Values.Where{ $Columns.Contains($_.Name) } : $table.Columns.Values).Where{ $_.CanRead -and (-not $_.IsComputed) }
315+
$fields = ($Columns ? $table.Columns.Values.Where{ $_.Name -in $Columns } : $table.Columns.Values).Where{ $_.CanRead -and (-not $_.IsComputed) }
260316
$text = "
261317
UPDATE $($this.GetTableName($table))
262318
SET $($fields.ForEach{ "$($this.QuoteIdentifier($_.Name)) = $($this.UsePositionalParameters ? "?" : $this.GetParameterName($_))" } -join ", ")

0 commit comments

Comments
 (0)