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 b8e35dd
Show file tree
Hide file tree
Showing 2 changed files with 34 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
27 changes: 27 additions & 0 deletions t/24-mysql-types.t
@@ -0,0 +1,27 @@
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, ? as str',
2.0.Num, 2.0.Rat, 2.0, '2.0');
my $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';
is $string, '2.0', 'Str value roundtriped';

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

0 comments on commit b8e35dd

Please sign in to comment.