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

SQLITE: EXPLAIN requires adding values for all query's parameters #16647

Closed
lsoft opened this issue Jul 18, 2019 · 11 comments · Fixed by #19630
Closed

SQLITE: EXPLAIN requires adding values for all query's parameters #16647

lsoft opened this issue Jul 18, 2019 · 11 comments · Fixed by #19630
Assignees
Labels
area-adonet-sqlite closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported good first issue This issue should be relatively straightforward to fix. type-bug
Milestone

Comments

@lsoft
Copy link

lsoft commented Jul 18, 2019

I am not very familiar with SQLite and Microsoft.Data.SQLite in particular, but it's unclear for me why Microsoft.Data.SQLite requires a values for a query's parameters for EXPLAIN queries.

Please take a look at Visual Studio unit test screenshot
image
it fails with the exception 'Must add values for the following parameters: @A'.

At the same time 'DB browser for SQLite' process this query with no problem:
image
Looks like SQLite allows such things.

My task is to validate query syntax. My way is to add an 'explain' term in the head of the query and call ExecuteNonQuery.
How can I execute EXPLAIN-queries without parameter's values?

Thanks!

@lsoft lsoft changed the title EXPLAIN requires adding values for all query's parameters SQLITE: EXPLAIN requires adding values for all query's parameters Jul 18, 2019
@ajcvickers
Copy link
Member

@bricelam Backlog/enhancement?

@bricelam
Copy link
Contributor

👍 We can use sqlite3_stmt_isexplain to special case these statements.

@bricelam bricelam added the good first issue This issue should be relatively straightforward to fix. label Jul 19, 2019
@ajcvickers ajcvickers added this to the Backlog milestone Jul 19, 2019
@lsoft
Copy link
Author

lsoft commented Jul 22, 2019

any workaround exists?

@bricelam
Copy link
Contributor

Provide dummy parameter values

@lsoft
Copy link
Author

lsoft commented Jul 22, 2019

Validation class works like a service, it does not know the list of parameters for each checked query. I can split statement to term (word) list, and take those that begin with @, but it does not look good enough... anyway thanks for the answer! :)

@bricelam
Copy link
Contributor

Written in notepad, so it may not compile, but this should get you started in the right direction:

static void AddDummyParameters(this SqliteCommand command)
{
    var db = command.Connection.Handle;
    
    var remainingSql = command.CommandText;
    while (!string.IsNullOrEmpty(remainingSql))
    {
        var rc = raw.sqlite3_prepare_v2(db, remainingSql, out var stmt, out remainingSql);
        SqliteException.ThrowExceptionForRC(rc, db);
        
        if (stmt.ptr == IntPtr.Zero)
        {
            // Statement was empty, white space, or a comment
            continue;
        }
        
        using (stmt)
        {        
            var count = raw.sqlite3_bind_parameter_count(stmt);
            for (var i = 1; i <= count; i++)
            {
                var name = raw.sqlite3_bind_parameter_name(stmt, i);
                
                // Add a dummy value
                command.Parameters.AddWithValue(name, "dummy");
            }
        }
    }
}

@lsoft
Copy link
Author

lsoft commented Jul 23, 2019

@bricelam it works, thank you!

@bricelam
Copy link
Contributor

@AlexanderTaeschner Are you bored? lol, here's a good one if you're interested.

@AlexanderTaeschner
Copy link
Contributor

Not really bored, but interested. Unfortunately the sqlite3_stmt_isexplain is not exposed by SQLitePCL yet (opened ericsink/SQLitePCL.raw#326), so that the current PR is a bit hacky.

@bricelam
Copy link
Contributor

Ah, I didn’t realize that. Maybe I’ll prepare a few PRs for SQLitePCLRaw. I think we’re blocked on a few missing APIs in there.

@AlexanderTaeschner
Copy link
Contributor

AlexanderTaeschner commented Jan 19, 2020

I just added PR ericsink/SQLitePCL.raw#328 to add sqlite3_stmt_isexplain to SQLitePCLRaw.

@bricelam bricelam removed the blocked label May 1, 2020
@bricelam bricelam added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label May 1, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0, 5.0.0-preview5 May 11, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0-preview5, 5.0.0 Nov 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-adonet-sqlite closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported good first issue This issue should be relatively straightforward to fix. type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants