Cleop / payments-db

A payment app that enables you to monitor account activity and make payments
MIT License
1 stars 0 forks source link

Make Transfer function error #1

Closed Cleop closed 7 years ago

Cleop commented 7 years ago

-Command runs in elephant sql without error. Run in node receives the following error: (node:1729) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): error: cannot insert multiple commands into a prepared statement

Cleop commented 7 years ago

Suggestion found for same issue:

https://github.com/brianc/node-postgres/issues/33

pg.connect(function(err, client, done) {
    client.query('BEGIN');
    client.query('SELECT $1::text t1, $2::text t2', ['a', 'b'], function(err, qry){
        console.log(err || qry.rows);
    });
    client.query('SELECT $1::text t3', ['c'], function(err, qry){
        console.log(err || qry.rows);
    });
    client.query('COMMIT', function(err, qry){
        console.log("done");
        done();
    });
});

Separate the fn out into separate queries.

Cleop commented 7 years ago

Information about prepared statements:

https://github.com/brianc/node-postgres/wiki/Parameterized-queries-and-Prepared-Statements

A prepared statement lets postgres parse, analyze, and rewrite a statement just once instead of every time it is used.

Should :

    SELECT transferBalance($1,$2,$3);`,

be outside of the initial query?

Cleop commented 7 years ago

Why is the error statement for a prepared statement when the format resembles a paramterized query?

parameterized, e.g. query( "select name from emp where emp_id=$1", [123] ) NOT prepared, e.g. query( {name:"emp_name", text:"select name from emp where emp_id=$1", values:[123]} )

Cleop commented 7 years ago

It seems to resemble:

Multi-statement parameterized queries

Multi-statement parameterized queries are currently not supported by postgres.

For example, the following statements will throw an error :

client.query('SELECT $1; SELECT $1;', [123], callback); client.query({ text: 'SELECT $1; SELECT $1;' }, [123], callback); error: cannot insert multiple commands into a prepared statement ... A workaround is to do 2 separate queries and using whatever type of async flow control to dispatch them.

Cleop commented 7 years ago

Anonymous blocks http://forums.devart.com/viewtopic.php?t=21348

Cleop commented 7 years ago

Subqueries or CTE (Common Table Expressions) / WITH https://www.postgresql.org/docs/8.4/static/queries-with.html

Cleop commented 7 years ago

Prepared statements using PREPARE https://www.postgresql.org/docs/9.3/static/sql-prepare.html