mojolicious/mojo-pg

Problems handling JSON

tarkhil opened this issue · 5 comments

  • Mojo::Pg version: 4.22
  • Perl version: v5.32.0
  • Operating system: FreeBSD 12.2-RELEASE-p1

Steps to reproduce the behavior

#!/usr/bin/env perl
use Mojo::Base -base;
use SQL::Abstract::Pg;
use YAML;

my $abstract = SQL::Abstract::Pg->new;
say Dump $abstract->insert( 'some_table', {foo => {-json => [1, 2, 3]}}, {bar => 23} );

Actual behavior

 % ./tmp/insert
[SQL::Abstract::__ANON__] Warning: HASH ref as bind value in insert is not supported at ./tmp/insert line 7.
--- 'INSERT INTO some_table ( foo) VALUES ( ? )'
---
-json:
  - 1
  - 2
  - 3

Attempt to use that insert with Mojo::Pg yields "Cannot bind a reference" error

kraih commented

We can only use the hooks SQL::Abstract provides us. How they are handled we have no control over. You probably want to report the issue upstream.

As of 1.87. SQL::Abstract does not support JSON.

And as far as I understand SQL::Abstract::Pg manual, SQL::Abstract::Pg - should, as it's a Pg extension...

kraih commented

That doesn't mean we can magically change SQL::Abstract internals.

@tarkhil I'm not sure exactly what SQL you are expecting to be generated, but if json() is to be a function call in postgres, and its argument is a hashref, then this should do the trick:

$abstract->insert( 'some_table', {foo => \[ 'json(?)', [{}, [1,2,3] ] ]}, {bar => 23} );