Functions/GenXdev.Data.SQLite/Get-SQLiteTableData.ps1
|
############################################################################### <# .SYNOPSIS Retrieves data from a SQLite database table with optional record limiting. .DESCRIPTION This function queries data from a SQLite database table using either a connection string or database file path. It provides flexibility in connecting to the database and controlling the amount of data retrieved through the Count parameter. .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 Specifies the SQLite connection string in the format: "Data Source=path_to_database_file" .PARAMETER DatabaseFilePath Specifies the full file system path to the SQLite database file. .PARAMETER TableName Specifies the name of the table to query data from. The table must exist in the database. .PARAMETER Count Specifies the maximum number of records to return. Default is 100. Use -1 to return all records. Must be -1 or a positive integer. .EXAMPLE Get-SQLiteTableData -DatabaseFilePath "C:\data\users.db" -TableName "Employees" -Count 50 .EXAMPLE Get-SQLiteTableData "C:\data\users.db" "Employees" #> function Get-SQLiteTableData { [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()] [Alias('dbpath', 'indexpath')] [string]$DatabaseFilePath, ########################################################################### [Parameter( Position = 1, Mandatory = $true, HelpMessage = 'The name of the table to query data from.' )] [ValidateNotNullOrEmpty()] [string]$TableName, ########################################################################### [Parameter( Position = 2, Mandatory = $false, HelpMessage = 'The maximum number of records to return. -1 for all.' )] [ValidateRange(-1, [int]::MaxValue)] [int]$Count = 100 ) begin { Microsoft.PowerShell.Utility\Write-Verbose "Starting Get-SQLiteTableData for table: $TableName" Microsoft.PowerShell.Utility\Write-Verbose "Record limit set to: $(if($Count -eq -1){'unlimited'}else{$Count})" } process { # construct query based on whether we want all records or a limited set $query = if ($Count -eq -1) { "SELECT * FROM $TableName" } else { "SELECT * FROM $TableName LIMIT $Count" } Microsoft.PowerShell.Utility\Write-Verbose "Executing query: $query" # add the constructed query to the parameter set for Invoke-SQLiteQuery $PSBoundParameters['Queries'] = $query # execute the query and return results through the SQLite provider GenXdev\Invoke-SQLiteQuery @PSBoundParameters } end { Microsoft.PowerShell.Utility\Write-Verbose "Completed querying table: $TableName" } } |