Skip to content

Commit

Permalink
Handle Num and Rat as MYSQL_TYPE_DOUBLE
Browse files Browse the repository at this point in the history
This matches the behaviour of the perl DBD::mysql driver.

The test confirms that the fields are created as "double", and that strings which look like numbers remain a string.
  • Loading branch information
rbt committed Nov 24, 2022
1 parent 33460bf commit 51b913b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/DBDish/mysql/StatementHandle.rakumod
Expand Up @@ -116,6 +116,13 @@ method execute(*@params --> DBDish::StatementHandle) {
@Bufs[$k] = do {
when Blob { $st = MYSQL_TYPE_BLOB; $_ }
when Str { .encode }
when Rat | Num {
# This may be lossy for very large values.
$st = MYSQL_TYPE_DOUBLE;
my $buf = buf8.new();
$buf.write-num64(0, $_.Num);
$buf;
}
when Int {
# Handle anything larger than int64 as a string.
if -2**63 <= $_ <= 2**63 -1 {
Expand Down
26 changes: 26 additions & 0 deletions t/24-mysql-types.t
@@ -0,0 +1,26 @@
use v6;
use Test;
use DBIish::CommonTesting;

plan 1;
my %con-parms = :database<dbdishtest>, :user<testuser>, :password<testpass>;
%con-parms<host> = %*ENV<MYSQL_HOST> if %*ENV<MYSQL_HOST>;
my $dbh = DBIish::CommonTesting.connect-or-skip('mysql', |%con-parms);

subtest 'Num and Rat are numbers' => {
$dbh.execute('CREATE TEMPORARY TABLE tmp_num_test AS SELECT ? as num, ? as rat, ? as unspecified',
2.0.Num, 2.0.Rat, 2.0);

my $sth = $dbh.execute('describe tmp_num_test');
while my $row = $sth.row {
my ($col-name, $datatype-buf) = $row;
my $datatype = $datatype-buf.decode;
is $datatype, 'double', "$col-name type is double";
}

$sth = $dbh.execute('SELECT * FROM tmp_num_test');
my ($num, $rat, $unspecified, $string) = $sth.row;
is $num, 2.0.Num, 'Num value roundtriped';
is $rat, 2.0.Rat, 'Rat value roundtriped';
is $unspecified, 2.0.Num, 'Unspecified was treated as Num';
}

0 comments on commit 51b913b

Please sign in to comment.