question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PL/pgSQL basic support

See original GitHub issue

Feature discussion / request

  1. 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.

  1. 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:open
  • Created 2 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
OlivierCavadenticommented, Nov 2, 2021

@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.

Maybe can be good to implement function/procedure yeah. As I can see :

0reactions
gpetrovcommented, Oct 29, 2021

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chapter 43. PL/pgSQL — SQL Procedural Language
43.1. Overview · 43.1.1. Advantages of Using PL/pgSQL · 43.3. Declarations · 43.3.1. Declaring Function Parameters · 43.5. Basic Statements · 43.5.1. Assignment ......
Read more >
PostgreSQL PL/pgSQL
PL/pgSQL is easy to learn and simple to use. · PL/pgSQL comes with PostgreSQL by default. · PL/pgSQL inherits all user-defined types, functions,...
Read more >
PL/pgSQL Tutorial - w3resource
Introduction. PL/pgSQL (Procedural Language/PostgreSQL) is a loadable procedural programming language supported by the PostgreSQL. PL/pgSQL ...
Read more >
PL/pgSQL Language | Tanzu Greenplum Docs
PL/pgSQL supports the polymorphic anyelement, anyarray, anyenum, and anynonarray types. Using these types, you can create a single PL/pgSQL function that ...
Read more >
EDB: Open-Source, Enterprise Postgres Database Management
Migrate quickly without rewriting Oracle queries; Our team provides 24/7 support everywhere you use EDB Postgres; Keep your code with Oracle compatibility ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found