Skip to content

Commit

Permalink
fix(sqlite): Dispose all command to release database file handle (#5966)
Browse files Browse the repository at this point in the history
  • Loading branch information
niheaven committed Jun 30, 2024
1 parent 438bce5 commit 014449d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 43 deletions.
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), [#5944](https://github.com/ScoopInstaller/Scoop/issues/5944))

### 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

0 comments on commit 014449d

Please sign in to comment.