Functions/GenXdev.Data.SQLite/Get-SQLiteTableColumnData.ps1
<##############################################################################
Part of PowerShell module : GenXdev.Data.SQLite Original cmdlet filename : Get-SQLiteTableColumnData.ps1 Original author : René Vaessen / GenXdev Version : 1.300.2025 ################################################################################ Copyright (c) René Vaessen / GenXdev Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ################################################################################> ############################################################################### <# .SYNOPSIS Retrieves data from a specific column in a SQLite database table. .DESCRIPTION This function provides a convenient way to extract data from a single column in a SQLite database table. It supports two connection methods: direct database file path or connection string. The function includes options to limit the number of returned records and uses proper SQLite query construction for optimal performance. .PARAMETER ConnectionString The connection string to connect to the SQLite database. This parameter is mutually exclusive with DatabaseFilePath. .PARAMETER DatabaseFilePath The file path to the SQLite database file. This parameter is mutually exclusive with ConnectionString. .PARAMETER TableName The name of the table from which to retrieve the column data. .PARAMETER ColumnName The name of the column whose data should be retrieved. .PARAMETER Count The maximum number of records to return. Default is 100. Use -1 to return all records without limit. .EXAMPLE Get-SQLiteTableColumnData -DatabaseFilePath "C:\MyDb.sqlite" ` -TableName "Employees" ` -ColumnName "Email" ` -Count 10 .EXAMPLE Get-SQLiteTableColumnData "C:\MyDb.sqlite" "Employees" "Email" #> function Get-SQLiteTableColumnData { [CmdletBinding(DefaultParameterSetName = 'Default')] param ( ############################################################################### [Parameter( Position = 0, Mandatory = $true, ParameterSetName = 'ConnectionString', HelpMessage = 'The connection string to the SQLite database' )] [ValidateNotNullOrEmpty()] [string] $ConnectionString, ############################################################################### [Parameter( Position = 0, Mandatory = $true, ParameterSetName = 'DatabaseFilePath', HelpMessage = 'The path to the SQLite database file' )] [ValidateNotNullOrEmpty()] [string] $DatabaseFilePath, ############################################################################### [Parameter( Position = 1, Mandatory = $true, HelpMessage = 'The name of the table to query' )] [ValidateNotNullOrEmpty()] [string] $TableName, ############################################################################### [Parameter( Position = 2, Mandatory = $true, HelpMessage = 'The name of the column to retrieve' )] [ValidateNotNullOrEmpty()] [string] $ColumnName, ############################################################################### [Parameter( Position = 3, Mandatory = $false, HelpMessage = 'Number of records to return. Default 100. Use -1 for all' )] [int] $Count = 100 ) begin { # log the start of the operation with table and column details Microsoft.PowerShell.Utility\Write-Verbose "Starting data retrieval for column '$ColumnName' from table '$TableName'" } process { # construct the appropriate SQL query based on whether a limit is needed $query = if ($Count -eq -1) { "SELECT $ColumnName FROM $TableName" } else { "SELECT $ColumnName FROM $TableName LIMIT $Count" } # log the constructed query for debugging Microsoft.PowerShell.Utility\Write-Verbose "Executing SQL query: $query" # prepare parameters for Invoke-SQLiteQuery and execute the query $PSBoundParameters['Queries'] = $query GenXdev.Data\Invoke-SQLiteQuery @PSBoundParameters } end { # log completion of the operation Microsoft.PowerShell.Utility\Write-Verbose 'Column data retrieval completed successfully' } } |