Perl 6 - the future is here, just unevenly distributed

IRC log for #askriba, 2017-01-18

| Channels | #askriba index | Today | | Search | Google Search | Plain-Text | summary

All times shown according to UTC.

Time Nick Message
05:04 dboehmer_ joined #askriba
09:10 Relequestual joined #askriba
10:11 ilbot2 joined #askriba
10:11 Topic for #askriba is now Where random Perl5-related questions get definitive answers. Currently no preset business hours due to low demand: just highlight my name and I'll answer ASAP \o/
14:38 tcohen joined #askriba
14:39 tcohen Hi, I'm facing a weird issue when trying to convert a row into JSON using JSON::encode_json.
14:40 tcohen The symptom is that numeric values are being considered strings
14:40 tcohen This is a MySQL-backed schema
14:40 tcohen with DBIx::Class 0.082840
14:41 tcohen A colleague tested on a project he has, but against Postgres and wasn't able to reproduce the issue
14:41 tcohen so maybe is a configuration issue of my project, or a problem with DBI
14:41 tcohen Mysql driver
14:43 ribasushi tcohen: hmmmm is it by chance bigint *in the rdbms* ?
14:43 tcohen `borrowernumber` int(11) NOT NULL auto_increment
14:44 tcohen to reproduce, do Dumper(encode_json($rs->next));
14:45 ribasushi tcohen: there are at least 4 ( possibly up to 7 ) layers between the rdbms and $rs->next
14:46 ribasushi yesterday I asked you to try both $rs->cursor->next and $rs->search (... hri ... )->next
14:46 ribasushi how are these ending up encoded?
14:46 tcohen http://paste.koha-community.org/267?hl=on&submit=Format+it%21
14:47 tcohen they were all encoded the same
14:47 ribasushi tcohen: i.e. "the wrong way" ?
14:48 ribasushi tcohen: hmmm there's a bunch of mistakes in that paste: e.g. my $patron = Koha::Database->schema->resultset('Borrower')->cursor->next; could never work
14:48 ribasushi tcohen: let's try something else to limit things even more
14:49 ribasushi ( typing ... )
14:50 ribasushi p( encode_json( Koha::Database->schema->dbh_do( sub { $_[1]->selectrow_arrayref("SELECT borrowernumber FROM ... whatever the table is ... ") }) ) )
14:51 ribasushi tcohen: ^^ do *JUST* that, do not factor it into pieces via return_a_numbr etc: this is making things much harder to reason about
14:51 ribasushi small atomic pieces while debugging
14:53 tcohen "["1"]"
14:53 tcohen :-D
14:54 ribasushi tcohen: so - this rules out DBIC *almost* entirely at this point ( the only part would be what kind of connection options are you passing )
14:54 ribasushi tcohen: retry with plain DBI->connect( ... ) ->selectrow_arrayref to rule out werid on-connect magic
14:55 tcohen http://paste.koha-community.org/268
14:55 ribasushi tcohen: right - try just with the DSN, and none of the on_connect do stuff
14:55 ribasushi see how that fare
14:55 ribasushi *fares
14:57 tcohen I only left             dsn => "dbi:$db_driver:database=$db_name;host=$db_host;port=$db_port",
14:57 tcohen user => $db_user,
14:57 tcohen password => $db_passwd,
14:57 tcohen same results
14:58 ribasushi tcohen: did you try with direct DBI though, or did you go through DBIC again?
14:58 ribasushi remember - layers ;)
14:58 tcohen I missed that message, trying just now
15:04 ribasushi tcohen: I'll have to step away in a bit - were you able to isolate it down to DBI alone?
15:04 tcohen I ran this
15:04 tcohen 13 my $dbh = DBI->connect( "dbi:mysql:database=koha_kohadev;host=localhost;port=3306", "koha_kohadev", "password" );
15:04 tcohen 14 p(encode_json($dbh->selectrow_arrayref("SELECT borrowernumber FROM borrowers LIMIT 1")) );
15:04 tcohen same problem
15:05 ribasushi right - that's good news... now why this is the case I am not sure: IIRC mysql DTRT in this case
15:05 tcohen i.e., the result is "["1"]"
15:06 ribasushi the one last step before reporting a bug against DBD::mysql
15:06 ribasushi would be to prepare a statement
15:06 ribasushi i.e. $sth = $dbh->prepare('SELECT ... ')
15:06 tcohen and explicitly ask for an integer?
15:07 ribasushi and checking what does https://metacpan.org/pod/DBD::mysql#mysql_is_num show on that handle
15:07 ribasushi no - this attribute should be set to "true" on the columns that are... well numeric
15:07 tcohen ok
15:08 ribasushi tcohen: all of this aside, to get you going again you can use this on the DBIC side: https://metacpan.org/pod/DBIx::Class::Helper::Row::NumifyGet#SYNOPSIS
15:08 ribasushi it will work around your problem by re-nummifying stuff back on the perl side
15:09 ribasushi tcohen: but of course it is a workaround with a small perf-penalty, so if possible: find the real cause on the DBD:: side and fix that
15:11 tcohen thanks ribasushi I'll try the mysql_is_num
15:11 ribasushi tcohen: mysql_is_num is an information-attr in your case, you can't set it to get what you want
15:12 ribasushi i.e. it will be an indication of where the problem might be, but not your solution
15:12 tcohen yes, it will just tell me if the driver considers the column numeric
15:12 tcohen right?
15:13 ribasushi precisely
15:18 tcohen ribasushi: I created the statement, but cannot figure how to use mysql_is_num
15:19 tcohen my $sth = $dbh->prepare("SELECT borrowernumber FROM borrowers LIMIT 1");
15:19 ribasushi tcohen: confusion is warranted: the API is a bit bizarre
15:19 ribasushi https://metacpan.org/pod/DBD::mysql#STATEMENT-HANDLES
15:19 ribasushi ^^ this is how you access attributes
15:19 * tcohen feels relieved
15:19 ribasushi i.e. $sth->{whatyouwannaknow}
15:20 tcohen holy s*t!
15:20 ribasushi brb
15:21 tcohen hm, 1
15:25 ribasushi tcohen: hm... this is really bizarre, then next step
15:25 ribasushi use Devel::Peek 'Dump'; $sth->execute; Dump( $sth->fetchrow_arrayref->[0] );
15:26 ribasushi tcohen: ^^ what does that say? ( it'll be a lot of output it's ok )
15:26 tcohen SV = PV(0x2d96240) at 0x2d70e08
15:26 tcohen REFCNT = 1
15:26 tcohen FLAGS = (POK,pPOK)
15:26 tcohen PV = 0x2d81330 "1"\0
15:26 tcohen CUR = 1
15:27 ribasushi tcohen: try to do p( $sth->{mysql_is_num} ) *after*  the execute+fetch
15:27 ribasushi is ti still "1" ?
15:28 tcohen yes
15:29 ribasushi tcohen: this... is where my ideas end: I'd file the above as a bug and ask for advice
15:29 ribasushi make sure to include DBD and RDBMS versions
15:30 tcohen thanks ribasushi!
15:32 ribasushi tcohen: btw for educational purposes do ` Dump ( [ 1, "1" ]) ` to see what is the most low-level difference ( this is how JSON decides how to encode )
15:33 ribasushi IV ( integer value ) vs PV ( string value, the P doesn't stand for anything obvious ) is what is tripping you up
15:44 tcohen thanks ribasushi, that was really educational!
17:08 tcohen joined #askriba
21:38 tcohen joined #askriba

| Channels | #askriba index | Today | | Search | Google Search | Plain-Text | summary