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

Invoke-SQLiteBulkCopy fails when object contains properties with non-alphanumeric characters #4

Closed
beatcracker opened this issue Nov 8, 2015 · 1 comment

Comments

@beatcracker
Copy link
Contributor

Here is a repro script:

Import-Module -Name 'PSSQLite' -Force

$DataSource = '.\NonAlphanum.sqlite'

$Query = @"
    CREATE TABLE ALPHANUM (
        'Alfa-Bravo' INTEGER,
        'Charlie=Delta' INTEGER,
        'Echo#Foxtrot' INTEGER,
        'Echo@Foxtrot' INTEGER,
        'Echo👻Foxtrot' INTEGER)
"@

$NonAlphanum = 0..100 | ForEach-Object {
    [pscustomobject]@{
        'Alfa-Bravo' = Get-Random
        'Charlie=Delta' = Get-Random
        'Echo#Foxtrot' = Get-Random
        'Echo@Foxtrot' = Get-Random
        'Echo👻Foxtrot' = Get-Random    
    }
}

Invoke-SqliteQuery -DataSource $DataSource -Query $Query

$DataTable = $NonAlphanum | Out-DataTable

Invoke-SQLiteBulkCopy -DataTable $DataTable -DataSource $DataSource -Table 'ALPHANUM' -Force

It fails with this error:

Rolled back due to error:
Exception calling "ExecuteNonQuery" with "0" argument(s): "SQL logic error or missing database
near "-": syntax error"
At C:\Path\To\PSSQLite\Invoke-SqliteBulkCopy.ps1:280 char:29
+                             Throw "Rolled back due to error:`n$_"
+                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (Rolled back due...: syntax error":String) [], RuntimeException
    + FullyQualifiedErrorId : Rolled back due to error:
Exception calling "ExecuteNonQuery" with "0" argument(s): "SQL logic error or missing database
near "-": syntax error"

I think I've fixed it, by creating parameters, where every non-alphanumeric character in column's name is converted to it's charcode string and then everything is joined with underscore.

For example, the column Echo👻Foxtrot, with "ghost in the PowerShell" (which consists of two surrogate pairs) will have parameter named Echo_55357_56443_Foxtrot.

I've also added some escaping with single quotes (') where necessary.

I'll do a proper pull-request soon.

@beatcracker
Copy link
Contributor Author

Here goes another pull-request, because while I've fixed parameters' names, the column names and table name should have single quote escaped, which was not.

beatcracker added a commit to beatcracker/PSSQLite that referenced this issue Nov 10, 2015
Invoke-SqliteBulkCopy will still fail if column name consists of *only*
non-alphanumeric characters.
beatcracker added a commit to beatcracker/PSSQLite that referenced this issue Nov 10, 2015
Added escaping of single quote (') for column names and table name.
RamblingCookieMonster added a commit that referenced this issue Nov 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant