cmoog / vscode-sql-notebook

Open SQL files as VSCode Notebooks.
https://marketplace.visualstudio.com/items?itemName=cmoog.sqlnotebook
MIT License
120 stars 16 forks source link

Support for parameters #78

Open lorefnon opened 8 months ago

lorefnon commented 8 months ago

It would be nice to be able to have cells with parameterized expressions that can be entered by user when running the query.

In db frontends like DBeaver etc. when a SQL parameter is detected in the expression being evaluated the app shows a dialog to prompt for values.

image

My primary use case is to use pgtyped sql files as notebooks, which can contain parameters:

/* @name FindBookById */
SELECT * FROM books WHERE id = :bookId;

This parameter syntax is nonstandard sql but tools often allow some customization around parameter detection that enables usage of such syntax.

image

These values are remembered and auto populated the next time the query is executed.

Would you consider this to be within the scope of this project ?

cmoog commented 8 months ago

Thank you for including all this helpful context. I hadn't seen the DBeaver implementation before, so that's a useful source of potential solutions here. I also learned that some drivers support a version of this out of the box, such as MySQL.

Here are some disjoint thoughts on the problem space

One possible design

Parse the query for standard query substitution identifiers, ? for MySQL and $n for PostgreSQL, then show an input box via the native VSCode text input API for each variable. Pass these values directly to the driver query execution invocation where the driver will handle escaping etc.

lorefnon commented 8 months ago

Hmm, ok. If we avoid any preprocessing in the notebook, I'd likely need a preprocessor at my end to convert pgtyped files to sql files for notebook. Which may not be too difficult - just an extra step.

Notebooks are (more or less) stateless at the moment. In my opinion, persistent parameter state encourages bad usage patterns.

The motivation behind this is somewhat hard for me to understand. I was thinking we could save the variable values as comments in the notebook so that they are available as examples (or sensible defaults) to someone else trying out the notebook. This could be useful esp. because we can't easily reference some external data in a sql notebook.