src/Object/Test-Object.ps1
|
using namespace System.Data using module ../SqlCommandBuilder.psm1 <# .SYNOPSIS Checks whether an entity with the specified primary key exists. .OUTPUTS `$true` if an entity with the specified primary key exists, otherwise `$false`. #> function Test-SqlObject { [CmdletBinding()] [OutputType([bool])] param ( # The connection to the data source. [Parameter(Mandatory, Position = 0)] [IDbConnection] $Connection, # The type of object to check. [Parameter(Mandatory, Position = 1)] [Type] $Class, # The primary key value. [Parameter(Mandatory, Position = 2, ValueFromPipeline)] [object] $Id, # The wait time, in seconds, before terminating the attempt to execute the command and generating an error. [ValidateRange("NonNegative")] [int] $Timeout = 30, # The transaction within which the command executes. [IDbTransaction] $Transaction ) process { $statement = [SqlCommandBuilder]::new($Connection).GetExistsCommand($Class, $Id) $statement[0].Timeout = $Timeout $statement[0].Transaction = $Transaction Get-SqlScalar $Connection -As ([bool]) -Command $statement[0] -Parameters $statement[1] } } |