From 51b913b280a5bf1f3d9bfb74e8735928ab52b633 Mon Sep 17 00:00:00 2001 From: Rod Taylor Date: Thu, 24 Nov 2022 11:42:15 -0500 Subject: [PATCH] Handle Num and Rat as MYSQL_TYPE_DOUBLE 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. --- lib/DBDish/mysql/StatementHandle.rakumod | 7 +++++++ t/24-mysql-types.t | 26 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 t/24-mysql-types.t diff --git a/lib/DBDish/mysql/StatementHandle.rakumod b/lib/DBDish/mysql/StatementHandle.rakumod index ebff38f..2aaedb7 100755 --- a/lib/DBDish/mysql/StatementHandle.rakumod +++ b/lib/DBDish/mysql/StatementHandle.rakumod @@ -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 { diff --git a/t/24-mysql-types.t b/t/24-mysql-types.t new file mode 100644 index 0000000..2160e75 --- /dev/null +++ b/t/24-mysql-types.t @@ -0,0 +1,26 @@ +use v6; +use Test; +use DBIish::CommonTesting; + +plan 1; +my %con-parms = :database, :user, :password; +%con-parms = %*ENV if %*ENV; +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'; +}