MySQLite icon indicating copy to clipboard operation
MySQLite copied to clipboard

[Bug] - `Invoke-MySqliteQuery` Query Exception Delegate Misspelled Object Handling

Open mattcargile opened this issue 1 year ago • 0 comments

Usage like the below throws a few errors and confusing warnings. The table metada is misspelled and should be metadata.

New-MySQLiteDB .\t.db
Invoke-MySqliteQuery .\t.db -Query 'select * from metadat;'

Output looks like the below.

WARNING: SQL logic error
WARNING: select * from metadat;
MethodInvocationException: C:\Users\user\OneDrive\Documents\PowerShell\Modules\mysqlite\0.14.0\functions\Invoke-MySQLiteQuery.ps1:100
Line |
 100 |                              $exceptionDelegate.Invoke($_)
     |                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling "Invoke" with "1" argument(s): "times ('-1') must be a non-negative value. (Parameter 'times') Actual value was -1."
InvalidOperation: C:\Users\user\OneDrive\Documents\PowerShell\Modules\mysqlite\0.14.0\functions\Invoke-MySQLiteQuery.ps1:104
Line |
 104 |                          while ($reader.read()) {
     |                                 ~~~~~~~~~~~~~~
     | You cannot call a method on a null-valued expression.
InvalidOperation: C:\Users\user\OneDrive\Documents\PowerShell\Modules\mysqlite\0.14.0\functions\Invoke-MySQLiteQuery.ps1:121
Line |
 121 |                          $reader.close()
     |                          ~~~~~~~~~~~~~~~
     | You cannot call a method on a null-valued expression.

The $exceptionDelgate could handle not finding a match better.

https://github.com/jdhitsolutions/MySQLite/blob/7ec667dca1f262ece43f8174199a4447c81c5a71/functions/Invoke-MySQLiteQuery.ps1#L42-L58

The easiest and maybe best fix would be to check for $null on $syntaxErr and write out the raw $errTxt[1] if $null. So starting on line 49:

if ( $null -ne $syntaxErr ) {
  $syntaxErr = $syntaxErr.Matches.Value 
  # highlight offending token 
  $query -replace ([regex]::Escape($syntaxErr)), "`e[7m`$0`e[0;93m" | Write-Warning 
  # generate a pointer caret to the offending token 
  ' ' * $query.IndexOf($syntaxErr) + '^' | Write-Warning 
}
else {
  Write-Warning $errTxt[1]

mattcargile avatar Jan 24 '25 01:01 mattcargile