Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sqlite): Dispose all command to release database file handle #5966

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Features

- **scoop-search:** Use SQLite for caching apps to speed up local search ([#5851](https://github.com/ScoopInstaller/Scoop/issues/5851), [#5918](https://github.com/ScoopInstaller/Scoop/issues/5918), [#5946](https://github.com/ScoopInstaller/Scoop/issues/5946), [#5949](https://github.com/ScoopInstaller/Scoop/issues/5949), [#5955](https://github.com/ScoopInstaller/Scoop/issues/5955))
- **scoop-search:** Use SQLite for caching apps to speed up local search ([#5851](https://github.com/ScoopInstaller/Scoop/issues/5851), [#5918](https://github.com/ScoopInstaller/Scoop/issues/5918), [#5946](https://github.com/ScoopInstaller/Scoop/issues/5946), [#5949](https://github.com/ScoopInstaller/Scoop/issues/5949), [#5955](https://github.com/ScoopInstaller/Scoop/issues/5955), [#5966](https://github.com/ScoopInstaller/Scoop/issues/5966))
- **core:** New cache filename format ([#5929](https://github.com/ScoopInstaller/Scoop/issues/5929))

### Bug Fixes
Expand Down
53 changes: 11 additions & 42 deletions lib/database.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,47 +40,16 @@ function Get-SQLite {

<#
.SYNOPSIS
Close a SQLite database.
Open Scoop SQLite database.
.DESCRIPTION
Close a SQLite database connection.
.PARAMETER InputObject
System.Data.SQLite.SQLiteConnection
The SQLite database connection to close.
.INPUTS
System.Data.SQLite.SQLiteConnection
.OUTPUTS
None
#>
function Close-ScoopDB {
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline)]
[System.Data.SQLite.SQLiteConnection]
$InputObject
)
process {
$InputObject.Dispose()
}
}

<#
.SYNOPSIS
Create a new SQLite database.
.DESCRIPTION
Create a new SQLite database connection and create the necessary tables.
.PARAMETER PassThru
System.Management.Automation.SwitchParameter
Return the SQLite database connection.
Open Scoop SQLite database connection and create the necessary tables if not exists.
.INPUTS
None
.OUTPUTS
None
Default

System.Data.SQLite.SQLiteConnection
The SQLite database connection if **PassThru** is used.
#>
function New-ScoopDB ([switch]$PassThru) {
function Open-ScoopDB {
# Load System.Data.SQLite
if (!('System.Data.SQLite.SQLiteConnection' -as [Type])) {
try {
Expand Down Expand Up @@ -112,11 +81,7 @@ function New-ScoopDB ([switch]$PassThru) {
)"
$tableCommand.ExecuteNonQuery() | Out-Null
$tableCommand.Dispose()
if ($PassThru) {
return $db
} else {
$db.Dispose()
}
return $db
}

<#
Expand All @@ -141,7 +106,7 @@ function Set-ScoopDBItem {
)

begin {
$db = New-ScoopDB -PassThru
$db = Open-ScoopDB
$dbTrans = $db.BeginTransaction()
# TODO Support [hashtable]$InputObject
$colName = @($InputObject | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name)
Expand All @@ -164,6 +129,8 @@ function Set-ScoopDBItem {
$dbTrans.Rollback()
throw $_
} finally {
$dbCommand.Dispose()
$dbTrans.Dispose()
$db.Dispose()
}
}
Expand Down Expand Up @@ -277,7 +244,7 @@ function Select-ScoopDBItem {
)

begin {
$db = New-ScoopDB -PassThru
$db = Open-ScoopDB
$dbAdapter = New-Object -TypeName System.Data.SQLite.SQLiteDataAdapter
$result = New-Object System.Data.DataTable
$dbQuery = "SELECT * FROM app WHERE $(($From -join ' LIKE @Pattern OR ') + ' LIKE @Pattern')"
Expand All @@ -292,6 +259,7 @@ function Select-ScoopDBItem {
[void]$dbAdapter.Fill($result)
}
end {
$dbAdapter.Dispose()
$db.Dispose()
return $result
}
Expand Down Expand Up @@ -332,7 +300,7 @@ function Get-ScoopDBItem {
)

begin {
$db = New-ScoopDB -PassThru
$db = Open-ScoopDB
$dbAdapter = New-Object -TypeName System.Data.SQLite.SQLiteDataAdapter
$result = New-Object System.Data.DataTable
$dbQuery = 'SELECT * FROM app WHERE name = @Name AND bucket = @Bucket'
Expand All @@ -353,6 +321,7 @@ function Get-ScoopDBItem {
[void]$dbAdapter.Fill($result)
}
end {
$dbAdapter.Dispose()
$db.Dispose()
return $result
}
Expand Down