[Bps-public-commit] dbix-searchbuilder branch cast-as-decimal created. 1.79-1-gd60c922

BPS Git Server git at git.bestpractical.com
Tue Dec 12 22:06:54 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  d60c9227d60a3c93ddaea88e63409d55d801480e (commit)

- Log -----------------------------------------------------------------
commit d60c9227d60a3c93ddaea88e63409d55d801480e
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..3d13286 100755
--- a/lib/DBIx/SearchBuilder/Handle/mysql.pm
+++ b/lib/DBIx/SearchBuilder/Handle/mysql.pm
@@ -372,4 +372,10 @@ sub HasSupportForCombineSearchAndCount {
     }
 }
 
+sub CastAsDecimal {
+    my $self  = shift;
+    my $field = shift or return;
+    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