|
1 | 1 | # Description: Functions for interacting with the Scoop database cache |
2 | 2 |
|
3 | | -if (-not (Get-Command Compare-Version -ErrorAction Ignore)) { |
4 | | - . "$PSScriptRoot\versions.ps1" |
5 | | -} |
6 | | - |
7 | | -<# |
8 | | -.SYNOPSIS |
9 | | - Get the latest row from a set of Scoop database rows. |
10 | | -.DESCRIPTION |
11 | | - Compares the `version` property of each row semantically and returns the |
12 | | - latest row. Returns `$null` when no rows are provided. |
13 | | -#> |
14 | | -function Get-LatestScoopDBRow { |
15 | | - param( |
16 | | - [Parameter(Mandatory)] |
17 | | - [AllowEmptyCollection()] |
18 | | - [object[]] |
19 | | - $Rows |
20 | | - ) |
21 | | - |
22 | | - if (-not $Rows -or $Rows.Count -eq 0) { |
23 | | - return $null |
24 | | - } |
25 | | - |
26 | | - $latest = $Rows[0] |
27 | | - for ($i = 1; $i -lt $Rows.Count; $i++) { |
28 | | - $row = $Rows[$i] |
29 | | - if ((Compare-Version -ReferenceVersion $latest.version -DifferenceVersion $row.version) -gt 0) { |
30 | | - $latest = $row |
31 | | - } |
32 | | - } |
33 | | - |
34 | | - return $latest |
35 | | -} |
36 | | - |
37 | | -<# |
38 | | -.SYNOPSIS |
39 | | - Return the semantically latest row or rows from a Scoop database result set. |
40 | | -.DESCRIPTION |
41 | | - Clones the schema of `Table` and imports the latest row per group when |
42 | | - `GroupBy` is supplied, or the single latest row across the whole table when |
43 | | - it is omitted. Returns an empty cloned table when the source table has no rows. |
44 | | -.PARAMETER Table |
45 | | - The source `System.Data.DataTable` returned from a Scoop database query. |
46 | | -.PARAMETER GroupBy |
47 | | - Optional column names used to group rows before semantic latest-row selection. |
48 | | -.OUTPUTS |
49 | | - System.Data.DataTable |
50 | | - A cloned table containing the latest matching row for each requested scope. |
51 | | -#> |
52 | | -function Select-LatestScoopDBRow { |
53 | | - param( |
54 | | - [Parameter(Mandatory)] |
55 | | - [System.Data.DataTable] |
56 | | - $Table, |
57 | | - [string[]] |
58 | | - $GroupBy |
59 | | - ) |
60 | | - |
61 | | - $latestRows = $Table.Clone() |
62 | | - $rows = @($Table.Rows) |
63 | | - if ($rows.Count -eq 0) { |
64 | | - return $latestRows |
65 | | - } |
66 | | - |
67 | | - if ($GroupBy -and $GroupBy.Count -gt 0) { |
68 | | - foreach ($group in ($rows | Group-Object -Property $GroupBy)) { |
69 | | - $latestRows.ImportRow((Get-LatestScoopDBRow -Rows @($group.Group))) |
70 | | - } |
71 | | - } else { |
72 | | - $latestRows.ImportRow((Get-LatestScoopDBRow -Rows $rows)) |
73 | | - } |
74 | | - |
75 | | - return $latestRows |
76 | | -} |
77 | | - |
78 | 3 | <# |
79 | 4 | .SYNOPSIS |
80 | 5 | Get SQLite .NET driver |
@@ -438,6 +363,91 @@ function Get-ScoopDBItem { |
438 | 363 | } |
439 | 364 | } |
440 | 365 |
|
| 366 | +<# |
| 367 | +.SYNOPSIS |
| 368 | + Get the latest row from a set of Scoop database rows. |
| 369 | +.DESCRIPTION |
| 370 | + Compares the `version` property of each row semantically and returns the |
| 371 | + latest row. Returns `$null` when no rows are provided. |
| 372 | +.PARAMETER Rows |
| 373 | + System.Object[] |
| 374 | + The rows to evaluate for the latest version. Each row must have a `version` property. |
| 375 | +.INPUTS |
| 376 | + System.Object[] |
| 377 | +.OUTPUTS |
| 378 | + System.Object |
| 379 | + The latest row based on semantic versioning, or `$null` if no rows are provided. |
| 380 | +#> |
| 381 | +function Get-LatestScoopDBRow { |
| 382 | + param( |
| 383 | + [Parameter(Mandatory)] |
| 384 | + [AllowEmptyCollection()] |
| 385 | + [object[]] |
| 386 | + $Rows |
| 387 | + ) |
| 388 | + |
| 389 | + if (-not $Rows -or $Rows.Count -eq 0) { |
| 390 | + return $null |
| 391 | + } |
| 392 | + |
| 393 | + if (-not (Get-Command Compare-Version -ErrorAction Ignore)) { |
| 394 | + . "$PSScriptRoot\versions.ps1" |
| 395 | + } |
| 396 | + |
| 397 | + $latest = $Rows[0] |
| 398 | + for ($i = 1; $i -lt $Rows.Count; $i++) { |
| 399 | + $row = $Rows[$i] |
| 400 | + if ((Compare-Version -ReferenceVersion $latest.version -DifferenceVersion $row.version) -gt 0) { |
| 401 | + $latest = $row |
| 402 | + } |
| 403 | + } |
| 404 | + |
| 405 | + return $latest |
| 406 | +} |
| 407 | + |
| 408 | +<# |
| 409 | +.SYNOPSIS |
| 410 | + Return the semantically latest row or rows from a Scoop database result set. |
| 411 | +.DESCRIPTION |
| 412 | + Clones the schema of `Table` and imports the latest row per group when |
| 413 | + `GroupBy` is supplied, or the single latest row across the whole table when |
| 414 | + it is omitted. Returns an empty cloned table when the source table has no rows. |
| 415 | +.PARAMETER Table |
| 416 | + System.Data.DataTable |
| 417 | + The source table returned from a Scoop database query. |
| 418 | +.PARAMETER GroupBy |
| 419 | + System.String[] |
| 420 | + Optional column names used to group rows before semantic latest-row selection. |
| 421 | +.OUTPUTS |
| 422 | + System.Data.DataTable |
| 423 | + A cloned table containing the latest matching row for each requested scope. |
| 424 | +#> |
| 425 | +function Select-LatestScoopDBRow { |
| 426 | + param( |
| 427 | + [Parameter(Mandatory)] |
| 428 | + [System.Data.DataTable] |
| 429 | + $Table, |
| 430 | + [string[]] |
| 431 | + $GroupBy |
| 432 | + ) |
| 433 | + |
| 434 | + $latestRows = $Table.Clone() |
| 435 | + $rows = @($Table.Rows) |
| 436 | + if ($rows.Count -eq 0) { |
| 437 | + return $latestRows |
| 438 | + } |
| 439 | + |
| 440 | + if ($GroupBy -and $GroupBy.Count -gt 0) { |
| 441 | + foreach ($group in ($rows | Group-Object -Property $GroupBy)) { |
| 442 | + $latestRows.ImportRow((Get-LatestScoopDBRow -Rows @($group.Group))) |
| 443 | + } |
| 444 | + } else { |
| 445 | + $latestRows.ImportRow((Get-LatestScoopDBRow -Rows $rows)) |
| 446 | + } |
| 447 | + |
| 448 | + return $latestRows |
| 449 | +} |
| 450 | + |
441 | 451 | <# |
442 | 452 | .SYNOPSIS |
443 | 453 | Remove Scoop database item(s). |
|
0 commit comments