public inbox for [email protected]  
help / color / mirror / Atom feed
Using Transaction with if-else in prepared statement
4+ messages / 3 participants
[nested] [flat]

* Using Transaction with if-else in prepared statement
@ 2022-05-25 11:12 Hui Jackson <[email protected]>
  2022-05-25 18:03 ` Re: Using Transaction with if-else in prepared statement Steve Midgley <[email protected]>
  2022-05-25 23:35 ` Re: Using Transaction with if-else in prepared statement hubert depesz lubaczewski <[email protected]>
  0 siblings, 2 replies; 4+ messages in thread

From: Hui Jackson @ 2022-05-25 11:12 UTC (permalink / raw)
  To: [email protected]

I am trying to make transaction in nodejs
The logic will be check if sufficient coin in sender's amount, if
sufficient then do transaction.
I am new to postgres, not sure if this is a right way to do so, if you have
a better solution, please let me know, thank you.

const coin = 10
const senderId = 1
const receiverId = 2
await pgPool.query(`
DO $$
    BEGIN
        if (SELECT coin from app_user where id = $1) >= $3 THEN
            UPDATE app_user set coin = coin - $3 where id = $1;
            UPDATE app_user set coin = coin + $3 where id = $2;
            INSERT INTO coin_history(sender_id, receiver_id, amount) VALUES
($1, $2, $3)

        END IF;
    END;
$$`,[senderId, receiverId, coin]);

Error (node:10044) UnhandledPromiseRejectionWarning: error: bind message
supplies 3 parameters, but prepared statement "" requires 0


^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Using Transaction with if-else in prepared statement
  2022-05-25 11:12 Using Transaction with if-else in prepared statement Hui Jackson <[email protected]>
@ 2022-05-25 18:03 ` Steve Midgley <[email protected]>
  1 sibling, 0 replies; 4+ messages in thread

From: Steve Midgley @ 2022-05-25 18:03 UTC (permalink / raw)
  To: Hui Jackson <[email protected]>; +Cc: pgsql-sql <[email protected]>

On Wed, May 25, 2022 at 4:13 AM Hui Jackson <[email protected]> wrote:

> I am trying to make transaction in nodejs
> The logic will be check if sufficient coin in sender's amount, if
> sufficient then do transaction.
> I am new to postgres, not sure if this is a right way to do so, if you
> have a better solution, please let me know, thank you.
>
> const coin = 10
> const senderId = 1
> const receiverId = 2
> await pgPool.query(`
> DO $$
>     BEGIN
>         if (SELECT coin from app_user where id = $1) >= $3 THEN
>             UPDATE app_user set coin = coin - $3 where id = $1;
>             UPDATE app_user set coin = coin + $3 where id = $2;
>             INSERT INTO coin_history(sender_id, receiver_id, amount)
> VALUES ($1, $2, $3)
>
>         END IF;
>     END;
> $$`,[senderId, receiverId, coin]);
>
> Error (node:10044) UnhandledPromiseRejectionWarning: error: bind message
> supplies 3 parameters, but prepared statement "" requires 0
>

For debugging, you might try to sending each select, update, and insert in
separate NodeJS query statements. You can do the IF work in NodeJS via a
select for data from Pg.

But at the core of your problem I believe is that you are not configuring
the postgres statement to receive parameters, but your injecting parameters
from Node. From the Pg docs on prepared statements:
https://www.postgresql.org/docs/current/sql-prepare.html

PREPARE fooplan (int, text, bool, numeric) AS
    INSERT INTO foo VALUES($1, $2, $3, $4);
EXECUTE fooplan(1, 'Hunter Valley', 't', 200.00);


^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Using Transaction with if-else in prepared statement
  2022-05-25 11:12 Using Transaction with if-else in prepared statement Hui Jackson <[email protected]>
@ 2022-05-25 23:35 ` hubert depesz lubaczewski <[email protected]>
  2022-05-26 10:00   ` Re: Using Transaction with if-else in prepared statement Hui Jackson <[email protected]>
  1 sibling, 1 reply; 4+ messages in thread

From: hubert depesz lubaczewski @ 2022-05-25 23:35 UTC (permalink / raw)
  To: Hui Jackson <[email protected]>; +Cc: [email protected]

On Wed, May 25, 2022 at 07:12:49PM +0800, Hui Jackson wrote:
> I am trying to make transaction in nodejs
> The logic will be check if sufficient coin in sender's amount, if
> sufficient then do transaction.
> I am new to postgres, not sure if this is a right way to do so, if you have
> a better solution, please let me know, thank you.

I would do i by issuing *only*
INSERT INTO coin_history(sender_id, receiver_id, amount) VALUES
and then changing coin data with triggers plus adding constraints that
app_user.coin can never be < 0.

depesz





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Using Transaction with if-else in prepared statement
  2022-05-25 11:12 Using Transaction with if-else in prepared statement Hui Jackson <[email protected]>
  2022-05-25 23:35 ` Re: Using Transaction with if-else in prepared statement hubert depesz lubaczewski <[email protected]>
@ 2022-05-26 10:00   ` Hui Jackson <[email protected]>
  0 siblings, 0 replies; 4+ messages in thread

From: Hui Jackson @ 2022-05-26 10:00 UTC (permalink / raw)
  To: [email protected]; [email protected]; +Cc: [email protected]

Concluded both methods shared:
1.  one query per each call for using prepared statement
await pgPool.query('BEGIN;');
const sender = (await pgPool.query('SELECT coin from app_user where id =
$1;',[senderId])).rows[0];
if(sender.coin >= amount){
  await pgPool.query('UPDATE app_user set coin = coin - $1 where id = $2;',[
coin, senderId]);
  await pgPool.query('UPDATE app_user set coin = coin + $1 where id = $2;',[
coin, receiverId]);
  await pgPool.query('INSERT INTO coin_history(sender_id, receiver_id,
amount) VALUES ($1, $2, $3)',[senderId, receiverId, coin]);
  await pgPool.query('COMMIT;');
}else{
  await pgPool.query('END;');
}
2. Use constraint

CREATE TABLE app_user(
    id integer,
    coin numeric CHECK (coin >= 0)
);


hubert depesz lubaczewski <[email protected]> 於 2022年5月26日週四 上午7:35寫道:

> On Wed, May 25, 2022 at 07:12:49PM +0800, Hui Jackson wrote:
> > I am trying to make transaction in nodejs
> > The logic will be check if sufficient coin in sender's amount, if
> > sufficient then do transaction.
> > I am new to postgres, not sure if this is a right way to do so, if you
> have
> > a better solution, please let me know, thank you.
>
> I would do i by issuing *only*
> INSERT INTO coin_history(sender_id, receiver_id, amount) VALUES
> and then changing coin data with triggers plus adding constraints that
> app_user.coin can never be < 0.
>
> depesz
>


^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2022-05-26 10:00 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-05-25 11:12 Using Transaction with if-else in prepared statement Hui Jackson <[email protected]>
2022-05-25 18:03 ` Steve Midgley <[email protected]>
2022-05-25 23:35 ` hubert depesz lubaczewski <[email protected]>
2022-05-26 10:00   ` Hui Jackson <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox