[Jifty-commit] r4436 - in Jifty-DBI/trunk: . lib/Jifty/DBI
lib/Jifty/DBI/Filter
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Thu Nov 15 12:44:23 EST 2007
Author: alexmv
Date: Thu Nov 15 12:44:22 2007
New Revision: 4436
Modified:
Jifty-DBI/trunk/ (props changed)
Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm
Jifty-DBI/trunk/lib/Jifty/DBI/Filter/DateTime.pm
Jifty-DBI/trunk/lib/Jifty/DBI/Handle.pm
Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm
Jifty-DBI/trunk/lib/Jifty/DBI/Schema.pm
Jifty-DBI/trunk/lib/Jifty/DBI/SchemaGenerator.pm
Jifty-DBI/trunk/t/19reference.t
Log:
r24778 at zoq-fot-pik: chmrr | 2007-11-15 12:43:12 -0500
* Alias columns now count as virtual
* Force chained auto-joins to be marked distinct
* Can now call ->record_class as a class method
* When looking up the column object in a limit, try to find the right
class in the aliases. This only currently works if you specified
the model class as the 'table2' in the join, instead of its table
name.
* Only create the parser and formatter for DateTime objects once, as
they are slow to create.
* r4425 actually changed the way that undef foreign objects were
treated; revert to the old behavior, but make it easier to achieve
the new (aka Jifty's) behavior.
Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm (original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm Thu Nov 15 12:44:22 2007
@@ -555,7 +555,7 @@
my $item = shift;
grep {$_} map {
my $col = $_;
- if ( $col->virtual or $col->aliased_as ) {
+ if ( $col->virtual ) {
undef;
} else {
$col = $col->name;
@@ -767,6 +767,7 @@
column1 => 'id',
alias2 => $right_alias,
column2 => $column->by || 'id',
+ is_distinct => 1,
);
$last_alias = $right_alias;
} elsif ( UNIVERSAL::isa( $classname, 'Jifty::DBI::Record' ) ) {
@@ -778,6 +779,7 @@
column1 => $name,
alias2 => $right_alias,
column2 => $column->by || 'id',
+ is_distinct => 1,
);
$last_alias = $right_alias;
} else {
@@ -1012,10 +1014,11 @@
$self->{record_class} = shift if (@_);
$self->{record_class} = ref $self->{record_class}
if ref $self->{record_class};
- } elsif ( not $self->{record_class} ) {
- my $class = ref($self);
+ } elsif ( not ref $self or not $self->{record_class} ) {
+ my $class = ref($self) || $self;
$class =~ s/(Collection|s)$//
|| die "Can't guess record class from $class";
+ return $class unless ref $self;
$self->{record_class} = $class;
}
return $self->{record_class};
@@ -1261,7 +1264,10 @@
# If it's a new value or we're overwriting this sort of restriction,
# XXX: when is column_obj undefined?
- my $column_obj = $self->new_item()->column( $args{column} );
+ my $class = $self->{joins}{$args{alias}} && $self->{joins}{$args{alias}}{class}
+ ? $self->{joins}{$args{alias}}{class}->new($self->_new_collection_args)
+ : $self;
+ my $column_obj = $class->new_item()->column( $args{column} );
my $case_sensitive = $column_obj ? $column_obj->case_sensitive : 0;
$case_sensitive = $args{'case_sensitive'}
if defined $args{'case_sensitive'};
Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Filter/DateTime.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Filter/DateTime.pm (original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Filter/DateTime.pm Thu Nov 15 12:44:22 2007
@@ -11,7 +11,8 @@
use constant _time_zone => 'UTC';
use constant _strptime => '%Y-%m-%d %H:%M:%S';
-
+use constant _parser => DateTime::Format::ISO8601->new();
+use constant _formatter => DateTime::Format::Strptime->new(pattern => _strptime);
=head1 NAME
@@ -81,7 +82,7 @@
my $str = join('T', split ' ', $$value_ref, 2);
my $dt;
- eval { $dt = DateTime::Format::ISO8601->parse_datetime($str) };
+ eval { $dt = $self->_parser->parse_datetime($str) };
if ($@) { # if datetime can't decode this, scream loudly with a useful error message
Carp::cluck($@);
@@ -92,7 +93,7 @@
my $tz = $self->_time_zone;
$dt->set_time_zone($tz) if $tz;
- $dt->set_formatter(DateTime::Format::Strptime->new(pattern => $self->_strptime));
+ $dt->set_formatter($self->_formatter);
$$value_ref = $dt;
} else {
return;
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 Nov 15 12:44:22 2007
@@ -837,14 +837,15 @@
if ( $args{'table2'}
&& UNIVERSAL::isa( $args{'table2'}, 'Jifty::DBI::Collection' ) )
{
+ my $c = ref $args{'table2'} ? $args{'table2'} : $args{'table2'}->new($args{collection}->_new_collection_args);
if ( $args{'operator'} eq '=' ) {
- my $x = $args{'table2'}->new_item->column( $args{column2} );
+ my $x = $c->new_item->column( $args{column2} );
if ( $x->type eq 'serial' || $x->distinct ) {
$args{'is_distinct'} = 1;
}
}
-
- $args{'table2'} = $args{'table2'}->table;
+ $args{'class2'} = ref $c;
+ $args{'table2'} = $c->table;
}
if ( $args{'alias2'} ) {
@@ -904,9 +905,10 @@
$meta->{'type'} = 'NORMAL';
}
$meta->{'depends_on'} = $args{'alias1'};
+ $meta->{'is_distinct'} = $args{'is_distinct'};
+ $meta->{'class'} = $args{'class2'} if $args{'class2'};
$meta->{'entry_aggregator'} = $args{'entry_aggregator'}
if $args{'entry_aggregator'};
- $meta->{'is_distinct'} = $args{'is_distinct'};
my $criterion = $args{'expression'} || "$args{'alias1'}.$args{'column1'}";
$meta->{'criteria'}{'base_criterion'} = [
@@ -979,7 +981,7 @@
my %processed;
$processed{$_}++
- foreach grep $joins->{$_}{'type'} ne 'LEFT', keys %$joins;
+ foreach grep {lc $joins->{$_}{'type'} ne 'left'} keys %$joins;
$processed{'main'}++;
my @ordered;
Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm (original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm Thu Nov 15 12:44:22 2007
@@ -392,6 +392,24 @@
}
}
+=head2 null_reference
+
+By default, Jifty::DBI::Record will return C<undef> for non-existant
+foreign references which don't exist. That is, if each Employee
+C<refers_to> a Department, but isn't required to,
+C<<$model->department>> will return C<undef> for employees not in a
+department.
+
+Overriding this method to return 0 will cause it to return a record
+with no id. That is, C<<$model->department>> will return a Department
+object, but C<<$model->department->id>> will be C<undef>.
+
+=cut
+
+sub null_reference {
+ return 1;
+}
+
=head2 _to_record COLUMN VALUE
This B<PRIVATE> method takes a column name and a value for that column.
@@ -413,6 +431,7 @@
my $classname = $column->refers_to();
my $remote_column = $column->by() || 'id';
+ return undef if not defined $value and $self->null_reference;
return undef unless $classname;
return unless UNIVERSAL::isa( $classname, 'Jifty::DBI::Record' );
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 Nov 15 12:44:22 2007
@@ -348,6 +348,8 @@
# A one-to-one or one-to-many relationship is requested
if ( UNIVERSAL::isa( $refclass, 'Jifty::DBI::Record' ) ) {
+ # Assume we refer to the ID column unless told otherwise
+ $column->by('id') unless $column->by;
# Handle *_id reference columns specially
if ( $name =~ /(.*)_id$/ ) {
@@ -363,13 +365,12 @@
# Note the original column
$virtual_column->aliased_as($aliased_as);
$virtual_column->alias_for_column($name);
+ $virtual_column->virtual(1);
# Create the helper methods for the virtual column too
$from->_init_methods_for_column($virtual_column);
}
- # Assume we refer to the ID column unless told otherwise
- $column->by('id') unless $column->by;
} elsif ( UNIVERSAL::isa( $refclass, 'Jifty::DBI::Collection' ) ) {
$column->by('id') unless $column->by;
$column->virtual('1');
Modified: Jifty-DBI/trunk/lib/Jifty/DBI/SchemaGenerator.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/SchemaGenerator.pm (original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/SchemaGenerator.pm Thu Nov 15 12:44:22 2007
@@ -258,7 +258,6 @@
# Skip "Virtual" columns - (foreign keys to collections)
next if $column->virtual;
- next if defined $column->alias_for_column;
# If schema_version is defined, make sure columns are for that version
if ($model->can('schema_version') and defined $model->schema_version) {
Modified: Jifty-DBI/trunk/t/19reference.t
==============================================================================
--- Jifty-DBI/trunk/t/19reference.t (original)
+++ Jifty-DBI/trunk/t/19reference.t Thu Nov 15 12:44:22 2007
@@ -6,7 +6,7 @@
BEGIN { require "t/utils.pl" }
our (@available_drivers);
-use constant TESTS_PER_DRIVER => 14;
+use constant TESTS_PER_DRIVER => 13;
my $total = scalar(@available_drivers) * TESTS_PER_DRIVER;
plan tests => $total;
@@ -53,23 +53,12 @@
isa_ok($rec->currency, 'TestApp::Currency');
is($rec->currency->name, 'USD');
- isa_ok($rec->food, 'TestApp::Food', 'referee is null but shuold still return an object');
- is($rec->food->id, undef);
-
-TODO: {
- local $TODO = 'not yet';
- *TestApp::User::null_reference = sub {1};
- $rec->load($id);
is( $rec->food, undef, 'null_reference option in effect' );
- is_deeply(
- { $rec->as_hash },
- { currency => 'USD',
- id => $id,
- food => undef
- }
- );
- }
+ local *TestApp::User::null_reference = sub {0};
+ $rec->load($id);
+ isa_ok($rec->food, 'TestApp::Food', 'referee is null but shuold still return an object');
+ is($rec->food->id, undef);
}
}
@@ -91,7 +80,7 @@
<<EOF;
CREATE TEMPORARY table currencies (
id integer auto_increment primary key,
- name varchar
+ name varchar(50)
)
EOF
@@ -133,7 +122,7 @@
<<EOF;
CREATE TEMPORARY table foods (
id integer auto_increment primary key,
- name varchar
+ name varchar(50)
)
EOF
@@ -166,7 +155,7 @@
<<EOF;
CREATE table users (
id integer primary key,
- food integar,
+ food integer,
currency varchar
)
EOF
@@ -178,8 +167,8 @@
<<EOF;
CREATE TEMPORARY table users (
id integer auto_increment primary key,
- food integar,
- currency varchar
+ food integer,
+ currency varchar(50)
)
EOF
@@ -190,7 +179,7 @@
<<EOF;
CREATE TEMPORARY table users (
id serial primary key,
- food integar,
+ food integer,
currency varchar
)
EOF
More information about the Jifty-commit
mailing list