0

Is it possible to send an unprepared statement to a Postgres Connection (using the crystal-db and crystal-pg shards)?

I've tried running the following statements using the .query methods, but they failed because they use a prepared statement, which prevents multiple statements from running. Maybe a unprepared statement would work?:

 SET LOCAL my.val = 'abc';
 SELECT current_setting('my.val') as my_val, 'aa' as now_;
dgo.a
  • 2,634
  • 23
  • 35

1 Answers1

0

The PG shard allows: an "extended query" protocol (which uses a prepared statement and allows parameters), and a "simple query" protocol (multiple statements with no return value).

In order to prevent some problems, the shard author(s) decided to only return results using the "extended query" protocol. So your only other option would be to use "extended queries" within a transaction. This would allow multiple statements (e.g. SET, SELECT, etc.):

DB.open(uri) do |db|
  db.transaction { |tx|
    tx.connection.exec "SET LOCAL my.val = 'abc';"
    puts tx.connection.scalar("SELECT current_setting('my.val') as my_val;").inspect
  }
  db.transaction { |tx|
    puts tx.connection.scalar("SELECT current_setting('my.val') as my_val;").inspect
  }
end

Output:

"abc"
""

Source: https://github.com/will/crystal-pg/issues/139

dgo.a
  • 2,634
  • 23
  • 35