On 26/02/2011 01:50, Bill Moseley wrote:
Columns coming from DBD::Pg seems to all be Perl strings and then when I
encode to json then end up quoted:

music=# \d cd;
                           Table "public.cd"
  Column  |  Type   |                    Modifiers
---------+---------+-------------------------------------------------
  id      | integer | not null default nextval('cd_id_seq'::regclass)
  year    | integer |
  name    | text    |
  artist  | integer | not null
  deleted | boolean | not null default false
Indexes:
     "cd_pkey" PRIMARY KEY, btree (id)


my $x = $dbh->selectrow_hashref( "select * from cd where id = 1" );
$x->{foo} = 0;

print Dumper $x;
print encode_json( $x );

Results in:

$VAR1 = {
           'artist' =>  '1',
           'name' =>  'One first CD',
           'foo' =>  0,
           'deleted' =>  '0',
           'id' =>  '1',
           'year' =>  '2010'
         };
{"artist":"1","name":"One first
CD","foo":0,"deleted":"0","id":"1","year":"2010"}

Notice how the deleted boolean and integer columns are quoted, but the "foo"
I injected is not quoted?

In a javascript library we are using it's seeing the deleted value as true.
  So, I'm curious if I can make DBI (or DBD::Pg) return the non-text columns
looking like numbers to Perl.

I suppose I could do something silly like:

/^\d+$/&&  ($_ += 0) for values %{$x};


which then returns:

{"artist":1,"name":"One first CD","foo":0,"deleted":0,"id":1,"year":2010}

I had the same problem with DBD::Oracle. Tim and I added
http://search.cpan.org/~timb/DBI-1.616/DBI.pm#sql_type_cast,
DBIstcf_STRICT and
DBIstcf_DISCARD_STRING

with some minor changes to DBD::Oracle to solve this problem.

It appears from Greg's response he may have already sorted this in a later release of DBD::Pg but I thought I'd just mention it.

Martin

Reply via email to