[Bps-public-commit] dbix-searchbuilder branch cast-as-decimal created. 1.79-1-g5a6024d
BPS Git Server
git at git.bestpractical.com
Tue Dec 12 22:08:26 UTC 2023
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "dbix-searchbuilder".
The branch, cast-as-decimal has been created
at 5a6024d410c00a99edccc530c25f5c3883f4d5ca (commit)
- Log -----------------------------------------------------------------
commit 5a6024d410c00a99edccc530c25f5c3883f4d5ca
Author: sunnavy <sunnavy at bestpractical.com>
Date: Tue Dec 5 16:24:35 2023 -0500
Add CastAsDecimal helper method
diff --git a/lib/DBIx/SearchBuilder/Handle.pm b/lib/DBIx/SearchBuilder/Handle.pm
index 747191c..b9f9deb 100755
--- a/lib/DBIx/SearchBuilder/Handle.pm
+++ b/lib/DBIx/SearchBuilder/Handle.pm
@@ -1948,5 +1948,18 @@ sub DESTROY {
delete $DBIHandle{$self};
}
+=head2 CastAsDecimal FIELD
+
+Cast the given field as decimal.
+
+E.g. on Pg, it's C<CAST(Content AS DECIMAL)>.
+
+=cut
+
+sub CastAsDecimal {
+ my $self = shift;
+ my $field = shift or return;
+ return "CAST($field AS DECIMAL)";
+}
1;
diff --git a/lib/DBIx/SearchBuilder/Handle/Oracle.pm b/lib/DBIx/SearchBuilder/Handle/Oracle.pm
index 1bd775f..01e0b05 100755
--- a/lib/DBIx/SearchBuilder/Handle/Oracle.pm
+++ b/lib/DBIx/SearchBuilder/Handle/Oracle.pm
@@ -446,4 +446,10 @@ sub HasSupportForNullsOrder {
return 1;
}
+sub CastAsDecimal {
+ my $self = shift;
+ my $field = shift or return;
+ return "TO_NUMBER($field)";
+}
+
1;
diff --git a/lib/DBIx/SearchBuilder/Handle/mysql.pm b/lib/DBIx/SearchBuilder/Handle/mysql.pm
index 5b850cf..0c3d680 100755
--- a/lib/DBIx/SearchBuilder/Handle/mysql.pm
+++ b/lib/DBIx/SearchBuilder/Handle/mysql.pm
@@ -372,4 +372,14 @@ sub HasSupportForCombineSearchAndCount {
}
}
+sub CastAsDecimal {
+ my $self = shift;
+ my $field = shift or return;
+
+ # CAST($field AS DECIMAL) rounds values to integers by default. It supports
+ # specific precisions like CAST($field AS DECIMAL(5,2)), but we don't know
+ # the precisions in advance. +0 works like other dbs.
+ return "($field+0)";
+}
+
1;
diff --git a/t/02searches_function.t b/t/02searches_function.t
index 4c99655..e9a25a9 100644
--- a/t/02searches_function.t
+++ b/t/02searches_function.t
@@ -6,7 +6,7 @@ use Test::More;
BEGIN { require "./t/utils.pl" }
our (@AvailableDrivers);
-use constant TESTS_PER_DRIVER => 18;
+use constant TESTS_PER_DRIVER => 19;
my $total = scalar(@AvailableDrivers) * TESTS_PER_DRIVER;
plan tests => $total;
@@ -125,6 +125,19 @@ diag "FUNCTION w/0 ? and () in Column" if $ENV{'TEST_VERBOSE'};
);
}
+diag "CAST FUNCTION in Column" if $ENV{'TEST_VERBOSE'};
+{
+ my $users_obj = $clean_obj->Clone;
+ $users_obj->UnLimit;
+ $users_obj->OrderByCols( { FIELD => $handle->CastAsDecimal('DeptNumber') } );
+ $users_obj->Column(FIELD => 'DeptNumber');
+ is_deeply(
+ [ map $_->DeptNumber, @{ $users_obj->ItemsArrayRef } ],
+ [ 2, 5, 30, 100 ],
+ 'correct values',
+ );
+}
+
cleanup_schema( 'TestApp', $handle );
}} # SKIP, foreach blocks
@@ -138,7 +151,8 @@ sub schema_sqlite {
q{
CREATE TABLE Users (
id integer primary key,
- Login varchar(36)
+ Login varchar(36),
+ DeptNumber varchar(36)
) },
q{
CREATE TABLE UsersToGroups (
@@ -159,7 +173,8 @@ sub schema_mysql {
q{
CREATE TEMPORARY TABLE Users (
id integer primary key AUTO_INCREMENT,
- Login varchar(36)
+ Login varchar(36),
+ DeptNumber varchar(36)
) },
q{
CREATE TEMPORARY TABLE UsersToGroups (
@@ -180,7 +195,8 @@ sub schema_pg {
q{
CREATE TEMPORARY TABLE Users (
id serial primary key,
- Login varchar(36)
+ Login varchar(36),
+ DeptNumber varchar(36)
) },
q{
CREATE TEMPORARY TABLE UsersToGroups (
@@ -200,7 +216,8 @@ sub schema_oracle { [
"CREATE SEQUENCE Users_seq",
"CREATE TABLE Users (
id integer CONSTRAINT Users_Key PRIMARY KEY,
- Login varchar(36)
+ Login varchar(36),
+ DeptNumber varchar(36)
)",
"CREATE SEQUENCE UsersToGroups_seq",
"CREATE TABLE UsersToGroups (
@@ -244,18 +261,20 @@ sub _ClassAccessible {
{read => 1, type => 'int(11)'},
Login =>
{read => 1, write => 1, type => 'varchar(36)'},
+ DeptNumber =>
+ {read => 1, write => 1, type => 'varchar(36)'},
}
}
sub init_data {
return (
- [ 'Login' ],
+ [ 'Login', 'DeptNumber' ],
- [ 'Ivan' ],
- [ 'john' ],
- [ 'Bob' ],
- [ 'aurelia' ],
+ [ 'Ivan', '30' ],
+ [ 'john', '100' ],
+ [ 'Bob', '5' ],
+ [ 'aurelia', '2' ],
);
}
-----------------------------------------------------------------------
hooks/post-receive
--
dbix-searchbuilder
More information about the Bps-public-commit
mailing list