Functions/GenXdev.Data.SQLite/Get-SQLiteTableColumnData.ps1
|
############################################################################### <# .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. .LICENSE Copyright (C) 2026 René Vaessen / GenXdev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. .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\Invoke-SQLiteQuery @PSBoundParameters } end { # log completion of the operation Microsoft.PowerShell.Utility\Write-Verbose 'Column data retrieval completed successfully' } } |