sql-psi icon indicating copy to clipboard operation
sql-psi copied to clipboard

EmbeddedSQL: Support SET variable = expr

Open hfhbd opened this issue 1 year ago • 3 comments

Before merging: Please merge https://github.com/AlecStrong/sql-psi/pull/433 first because it contains a commit of it

hfhbd avatar Aug 16 '22 15:08 hfhbd

SET ? = 42;
SET ?, ? = ABS(42), 42;

SET ?, ? = SELECT first_name, last_name FROM hockey_player WHERE _id = ?;

just want to make sure I understand the actual SQL going on before reviewing these PRs:

in this case, the ? is not a bound parameter, but a variable being used in future sql statements?

AlecKazakova avatar Sep 07 '22 14:09 AlecKazakova

Yes, they are host variables. For embeddedSQL you need a precompiler which has access to the host variables and bind these variables into the sql query and write the result into the host variables.

What do you think about a new rule hostvariables ::= ':' string? Would be needed in SELECT INTO too.

hfhbd avatar Sep 07 '22 17:09 hfhbd

Added the host variable rule, nice idea! Works also better with sqldelight class generating

SET :FOO, :BAR = ABS(?), :A;

hfhbd avatar Sep 16 '22 05:09 hfhbd

should this still be open as well?

if yes, is sql-psi the right place for it? I assume this isn't ANSI SQL we would want to pass compilation for like SQLite

AlecKazakova avatar Oct 03 '22 21:10 AlecKazakova

good question. It is part of the sql standard, but is optional and only available if you have a pre-compiler. And sqlite does not support this precompiler too. Then we should also revert #420

hfhbd avatar Oct 03 '22 21:10 hfhbd

I see, the fact that its part of the standard but is optional make this difficult. I would opt to not have them in but its because they're very unusual to me which isn't a good reason

AlecKazakova avatar Oct 04 '22 15:10 AlecKazakova

Personally, I think the reason to remove this syntax from sql-psi is okay cause it is very uncommon. Let's create a ticket if somebody else wants this feature too. In the meantime, I will implement this syntax in my db2 dialect.

hfhbd avatar Oct 05 '22 08:10 hfhbd