[Jifty-commit] r5176 - in Jifty-DBI/trunk: . lib/Jifty/DBI lib/Jifty/DBI/Filter t

Jifty commits jifty-commit at lists.jifty.org
Thu Feb 28 22:48:08 EST 2008


Author: sartak
Date: Thu Feb 28 22:48:08 2008
New Revision: 5176

Added:
   Jifty-DBI/trunk/lib/Jifty/DBI/Filter/Boolean.pm
   Jifty-DBI/trunk/t/06filter_boolean.t
Modified:
   Jifty-DBI/trunk/   (props changed)
   Jifty-DBI/trunk/lib/Jifty/DBI/Handle.pm
   Jifty-DBI/trunk/lib/Jifty/DBI/Handle/Pg.pm
   Jifty-DBI/trunk/lib/Jifty/DBI/Schema.pm

Log:
 r52255 at onn:  sartak | 2008-02-28 22:48:03 -0500
 Add "is boolean" support for not-braindead booleans.


Added: Jifty-DBI/trunk/lib/Jifty/DBI/Filter/Boolean.pm
==============================================================================
--- (empty file)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Filter/Boolean.pm	Thu Feb 28 22:48:08 2008
@@ -0,0 +1,88 @@
+package Jifty::DBI::Filter::Boolean;
+
+use warnings;
+use strict;
+
+use base 'Jifty::DBI::Filter';
+
+use constant TRUE_VALUES  => qw(1 t true y yes TRUE);
+use constant FALSE_VALUES => qw(0 f false n no FALSE);
+
+sub _is_true {
+    my $self = shift;
+    my $value = shift;
+
+    for ($self->TRUE_VALUES) {
+        return 1 if $value eq $_;
+    }
+
+    return 0;
+}
+
+sub _is_false {
+    my $self = shift;
+    my $value = shift;
+
+    for ($self->FALSE_VALUES) {
+        return 1 if $value eq $_;
+    }
+
+    return 0;
+}
+
+=head1 NAME
+
+Jifty::DBI::Filter::Boolean - Encodes booleans
+
+=head1 DESCRIPTION
+
+=head2 encode
+
+Transform the value into 1 or 0 so Perl's concept of the boolean's value agrees
+with the database's concept of the boolean's value. (For example, 't' and 'f'
+might be used -- 'f' is true in Perl)
+
+If the value is C<undef>, then the encoded value will also be C<undef>.
+
+=cut
+
+sub encode {
+    my $self = shift;
+    my $value_ref = $self->value_ref;
+
+    return unless defined $$value_ref;
+
+    $$value_ref = $self->_is_true($$value_ref);
+}
+
+=head2 decode
+
+Transform the value to the canonical true or false value as expected by the
+database.
+
+If the value is C<undef>, then the decoded value will also be C<undef>.
+
+=cut
+
+sub decode {
+    my $self = shift;
+    my $value_ref = $self->value_ref;
+
+    return unless defined $$value_ref;
+
+    if ($self->_is_true($$value_ref)) {
+        $$value_ref = $self->handle->canonical_true;
+    }
+    else {
+        $$value_ref = $self->handle->canonical_false;
+    }
+}
+
+=head1 SEE ALSO
+
+L<Jifty::DBI::Filter>, L<Time::Duration>, L<Time::Duration::Parse>
+
+=cut
+
+1;
+

Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Handle.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Handle.pm	(original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Handle.pm	Thu Feb 28 22:48:08 2008
@@ -1258,6 +1258,28 @@
 
 }
 
+=head2 canonical_true
+
+This returns the canonical true value for this database. For example, in SQLite
+it is 1 but in Postgres it is 't'.
+
+The default is 1.
+
+=cut
+
+sub canonical_true { 1 }
+
+=head2 canonical_false
+
+This returns the canonical false value for this database. For example, in SQLite
+it is 0 but in Postgres it is 'f'.
+
+The default is 0.
+
+=cut
+
+sub canonical_false { 0 }
+
 =head2 DESTROY
 
 When we get rid of the L<Jifty::DBI::Handle>, we need to disconnect

Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Handle/Pg.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Handle/Pg.pm	(original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Handle/Pg.pm	Thu Feb 28 22:48:08 2008
@@ -236,6 +236,22 @@
     }
 }
 
+=head2 canonical_true
+
+The canonical true value in Postgres is 't'.
+
+=cut
+
+sub canonical_true { 't' }
+
+=head2 canonical_false
+
+The canonical false value in Postgres is 'f'.
+
+=cut
+
+sub canonical_false { 'f' }
+
 1;
 
 __END__

Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Schema.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Schema.pm	(original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Schema.pm	Thu Feb 28 22:48:08 2008
@@ -403,6 +403,13 @@
     }
 }
 
+__PACKAGE__->register_types(
+    boolean => sub {
+        type is 'boolean',
+        filters are qw(Jifty::DBI::Filter::Boolean),
+    },
+);
+
 1;
 
 __END__

Added: Jifty-DBI/trunk/t/06filter_boolean.t
==============================================================================
--- (empty file)
+++ Jifty-DBI/trunk/t/06filter_boolean.t	Thu Feb 28 22:48:08 2008
@@ -0,0 +1,118 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+use Test::More;
+BEGIN { require "t/utils.pl" }
+our (@available_drivers);
+
+use constant TESTS_PER_DRIVER => 86;
+
+my $total = scalar(@available_drivers) * TESTS_PER_DRIVER;
+plan tests => $total;
+
+my @true  = qw/1 t true y yes TRUE/;
+my @false = qw/0 f false n no FALSE/;
+
+foreach my $d (@available_drivers) {
+SKIP: {
+    unless (has_schema('TestApp::User', $d)) {
+        skip "No schema for '$d' driver", TESTS_PER_DRIVER;
+    }
+
+    unless (should_test($d)) {
+        skip "ENV is not defined for driver '$d'", TESTS_PER_DRIVER;
+    }
+
+    diag("start testing with '$d' handle") if $ENV{TEST_VERBOSE};
+
+    my $handle = get_handle($d);
+    connect_handle($handle);
+    isa_ok($handle->dbh, 'DBI::db');
+
+    {
+        my $ret = init_schema('TestApp::User', $handle);
+        isa_ok($ret, 'DBI::st', 'init schema');
+    }
+
+    my @values = (
+        ( map { [$_, 'true']  } @true  ),
+        ( map { [$_, 'false'] } @false ),
+    );
+
+    for my $value ( @values ) {
+        my ($input, $bool) = @$value;
+
+        my $rec = TestApp::User->new( handle => $handle );
+        isa_ok($rec, 'Jifty::DBI::Record');
+
+        my ($id) = $rec->create( my_data => $input );
+        ok($id, 'created record');
+        ok($rec->load($id), 'loaded record');
+        is($rec->id, $id, 'record id matches');
+
+        ok($bool eq 'true' ? $rec->my_data : !$rec->my_data, 'Perl agrees with the expected boolean value');
+        my $sth = $handle->simple_query("SELECT my_data FROM users WHERE id = $id");
+        my ($got) = $sth->fetchrow_array;
+
+        my $method = "canonical_$bool";
+        is( $got, $handle->$method, 'my_data bool match' );
+
+        # undef/NULL
+        $rec->set_my_data;
+        is($rec->my_data, undef, 'set undef value');
+    }
+
+    cleanup_schema('TestApp', $handle);
+    disconnect_handle($handle);
+}
+}
+
+package TestApp::User;
+use base qw/ Jifty::DBI::Record /;
+
+1;
+
+sub schema_sqlite {
+
+<<EOF;
+CREATE table users (
+    id integer primary key,
+    my_data boolean
+)
+EOF
+
+}
+
+sub schema_mysql {
+
+<<EOF;
+CREATE TEMPORARY table users (
+    id integer auto_increment primary key,
+    my_data boolean
+)
+EOF
+
+}
+
+sub schema_pg {
+
+<<EOF;
+CREATE TEMPORARY table users (
+    id serial primary key,
+    my_data boolean
+)
+EOF
+
+}
+
+BEGIN {
+    use Jifty::DBI::Schema;
+
+    use Jifty::DBI::Record schema {
+    column my_data =>
+        is boolean;
+    }
+}
+
+


More information about the Jifty-commit mailing list