PL/pgSQL basic support
See original GitHub issueFeature discussion / request
- Explain what is your use case
As I can see, I cant find any PL/pgSQL code generator/executor in any languages. The language is quite powerful but have sometimes a verbose syntax and boilerplate code can be useful. It can be quite natural to use JS to write procedural functionalities wrapped around the query builder. At the first time, only few features can be implemented : declare, set and values of variables, begin end block, for loop, if…else, do…while, dynamic queries. This feature request is quite challenging and focus on only one database (or two if the language is abstracted with PL/PGSQL), so I can understand it’s really not the direction that you want for knex.
EDIT : implements first PL/pgSQL seems more logic, than PL/SQL. I edit this issue.
- Give some API proposal, how the feature should work
Given a table
create table "table" (
i NUMBER
);
I imagine build a PL/SQL like that :
knex.plsql()
// DECLARE section
.declare(function(vars){
// first var number, second size of varchar2
vars.varchar2('insertNumber', 200)
})
.begin(function(block){
// declare section create setter methods in 'block' object in the form : nameVar(<value to set>)
// Here we put a query in the var 'insertNumber' with dynamic parameter 'val1'
block.insertNumber(knex.from('table').insert({i: ':val1'}));
// Classic for loop
block.for(1 ,10, function(forLoop){
// exec = execute immediate with first parameter a var.
// forLoop.idx = each step of the for loop from 1 to 10.
// Without args 'block.insertNumber()' give the value of the var.
block.exec(block.insertNumber(), forLoop.idx);
});
});
to generate :
DO
$$
declare
insertNumber varchar;
begin
insertNumber := 'insert into "table" (i) values ($1)';
for i in 1..10
loop
execute insertNumber using i;
end loop;
end;
$$
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (1 by maintainers)
Top GitHub Comments
Maybe can be good to implement function/procedure yeah. As I can see :
@OlivierCavadenti - I love your example, but maybe try to make it more generic? So that all databases are supported that use stored procedures. Most of them have similar language PL/SQL, T_SQL - it is all very similar.
So it will be really great if we can create stored procedures in all supported databases, in a generic way with KnexJS code.