Revision history for DBIx::Class
+ - Allow a scalarref to be supplied to the 'from' resultset attribute
+ - Classes submitted as result_class for a resultsource are now
+ automatically loaded via ensure_loaded()
+ - 'result_class' resultset attribute, identical to result_class()
+ - add 'undef_on_null_fk' option for relationship accessors of type 'single'.
+ This will prevent DBIC from querying the database if one or more of
+ the key columns IS NULL. Tests + docs (groditi)
+ - for 'belongs_to' rels, 'null_on_fk' defaults to true.
0.08099_05 2008-10-30 21:30:00 (UTC)
- Rewritte of Storage::DBI::connect_info(), extended with an
starts_when => { data_type => 'datetime' }
);
+NOTE: You B<must> load C<InflateColumn::DateTime> B<before> C<Core>. See
+L<DBIx::Class::Manual::Component> for details.
+
Then you can treat the specified column as a L<DateTime> object.
print "This event starts the month of ".
SQL
# Finally, register your new ResultSource with your Schema
- My::Schema->register_source( 'UserFriendsComplex' => $new_source );
+ My::Schema->register_extra_source( 'UserFriendsComplex' => $new_source );
Next, you can execute your complex query using bind parameters like this:
relationship. To turn them on, pass C<< cascade_delete => 1 >>
in the $attr hashref.
+By default, DBIC will return undef and avoid querying the database if a
+C<belongs_to> accessor is called when any part of the foreign key IS NULL. To
+disable this behavior, pass C<< undef_on_null_fk => 0 >> in the C<$attr>
+hashref.
+
NOTE: If you are used to L<Class::DBI> relationships, this is the equivalent
of C<has_a>.
my ($class, $rel, $acc_type) = @_;
my %meth;
if ($acc_type eq 'single') {
+ my $rel_info = $class->relationship_info($rel);
$meth{$rel} = sub {
my $self = shift;
if (@_) {
} elsif (exists $self->{_relationship_data}{$rel}) {
return $self->{_relationship_data}{$rel};
} else {
+ my $cond = $self->result_source->resolve_condition(
+ $rel_info->{cond}, $rel, $self
+ );
+ if ($rel_info->{attrs}->{undef_on_null_fk}){
+ return if grep { not defined } values %$cond;
+ }
my $val = $self->find_related($rel, {}, {});
return unless $val;
return $self->{_relationship_data}{$rel} = $val;
# assume a foreign key contraint unless defined otherwise
$attrs->{is_foreign_key_constraint} = 1
if not exists $attrs->{is_foreign_key_constraint};
+ $attrs->{undef_on_null_fk} = 1
+ if not exists $attrs->{undef_on_null_fk};
# no join condition or just a column name
if (!ref $cond) {
use strict;
use warnings;
+use warnings::register;
use Sub::Name ();
sub many_to_many {
my $rs_meth = "${meth}_rs";
for ($add_meth, $remove_meth, $set_meth, $rs_meth) {
- warn "***************************************************************************\n".
- "The many-to-many relationship $meth is trying to create a utility method called $_. This will overwrite the existing method on $class. You almost certainly want to rename your method or the many-to-many relationship, as your method will not be callable (it will use the one from the relationship instead.) YOU HAVE BEEN WARNED\n".
- "***************************************************************************\n"
- if $class->can($_);
+ if ( $class->can ($_) ) {
+ warnings::warnif(<<"EOW")
+***************************************************************************
+The many-to-many relationship $meth is trying to create a utility method called
+$_. This will overwrite the existing method on $class. You almost certainly
+want to rename your method or the many-to-many relationship, as your method
+will not be callable (it will use the one from the relationship instead.)
+
+To disable this warning add the following to $class
+
+ no warnings 'DBIx::Class::Relationship::ManyToMany';
+
+***************************************************************************
+EOW
+ }
}
$rel_attrs->{alias} ||= $f_rel;
use Scalar::Util ();
use base qw/DBIx::Class/;
-__PACKAGE__->mk_group_accessors('simple' => qw/result_class _source_handle/);
+__PACKAGE__->mk_group_accessors('simple' => qw/_result_class _source_handle/);
=head1 NAME
# see https://bugzilla.redhat.com/show_bug.cgi?id=196836
my $self = {
_source_handle => $source,
- result_class => $attrs->{result_class} || $source->resolve->result_class,
cond => $attrs->{where},
count => undef,
pager => undef,
bless $self, $class;
+ $self->result_class(
+ $attrs->{result_class} || $source->resolve->result_class
+ );
+
return $self;
}
=cut
+sub result_class {
+ my ($self, $result_class) = @_;
+ if ($result_class) {
+ $self->ensure_class_loaded($result_class);
+ $self->_result_class($result_class);
+ }
+ $self->_result_class;
+}
=head2 count
=back
-Pass an arrayref of hashrefs. Each hashref should be a structure suitable for
-submitting to a $resultset->create(...) method.
+Accepts either an arrayref of hashrefs or alternatively an arrayref of arrayrefs.
+For the arrayref of hashrefs style each hashref should be a structure suitable
+forsubmitting to a $resultset->create(...) method.
In void context, C<insert_bulk> in L<DBIx::Class::Storage::DBI> is used
to insert the data, as this is a faster method.
print $ArtistOne->name; ## response is 'Artist One'
print $ArtistThree->cds->count ## reponse is '2'
-
+
+For the arrayref of arrayrefs style, the first element should be a list of the
+fieldsnames to which the remaining elements are rows being inserted. For
+example:
+
+ $Arstist_rs->populate([
+ [qw/artistid name/],
+ [100, 'A Formally Unknown Singer'],
+ [101, 'A singer that jumped the shark two albums ago'],
+ [102, 'An actually cool singer.'],
+ ]);
+
Please note an important effect on your data when choosing between void and
wantarray context. Since void context goes straight to C<insert_bulk> in
L<DBIx::Class::Storage::DBI> this will skip any component that is overriding
=cut
sub populate {
- my ($self, $data) = @_;
+ my $self = shift @_;
+ my $data = ref $_[0][0] eq 'HASH'
+ ? $_[0] : ref $_[0][0] eq 'ARRAY' ? $self->_normalize_populate_args($_[0]) :
+ $self->throw_exception('Populate expects an arrayref of hashes or arrayref of arrayrefs');
if(defined wantarray) {
my @created;
}
}
+=head2 _normalize_populate_args ($args)
+
+Private method used by L</populate> to normalize it's incoming arguments. Factored
+out in case you want to subclass and accept new argument structures to the
+L</populate> method.
+
+=cut
+
+sub _normalize_populate_args {
+ my ($self, $data) = @_;
+ my @names = @{shift(@$data)};
+ my @results_to_create;
+ foreach my $datum (@$data) {
+ my %result_to_create;
+ foreach my $index (0..$#names) {
+ $result_to_create{$names[$index]} = $$datum[$index];
+ }
+ push @results_to_create, \%result_to_create;
+ }
+ return \@results_to_create;
+}
+
=head2 pager
=over 4
# SELECT child.* FROM person child
# INNER JOIN person father ON child.father_id = father.id
+If you need to express really complex joins or you need a subselect, you
+can supply literal SQL to C<from> via a scalar reference. In this case
+the contents of the scalar will replace the table name asscoiated with the
+resultsource.
+
+WARNING: This technique might very well not work as expected on chained
+searches - you have been warned.
+
+ # Assuming the Event resultsource is defined as:
+
+ MySchema::Event->add_columns (
+ sequence => {
+ data_type => 'INT',
+ is_auto_increment => 1,
+ },
+ location => {
+ data_type => 'INT',
+ },
+ type => {
+ data_type => 'INT',
+ },
+ );
+ MySchema::Event->set_primary_key ('sequence');
+
+ # This will get back the latest event for every location. The column
+ # selector is still provided by DBIC, all we do is add a JOIN/WHERE
+ # combo to limit the resultset
+
+ $rs = $schema->resultset('Event');
+ $table = $rs->result_source->name;
+ $latest = $rs->search (
+ undef,
+ { from => \ "
+ (SELECT e1.* FROM $table e1
+ JOIN $table e2
+ ON e1.location = e2.location
+ AND e1.sequence < e2.sequence
+ WHERE e2.sequence is NULL
+ ) me",
+ },
+ );
+
+ # Equivalent SQL (with the DBIC chunks added):
+
+ SELECT me.sequence, me.location, me.type FROM
+ (SELECT e1.* FROM events e1
+ JOIN events e2
+ ON e1.location = e2.location
+ AND e1.sequence < e2.sequence
+ WHERE e2.sequence is NULL
+ ) me;
+
=head2 for
=over 4
use strict;
use warnings;
use base 'DBIx::Class';
+use List::Util;
=head1 NAME
my ($class, $rs, $column) = @_;
$class = ref $class if ref $class;
my $new_parent_rs = $rs->search_rs; # we don't want to mess up the original, so clone it
- $new_parent_rs->{attrs}->{prefetch} = undef; # prefetch causes additional columns to be fetched
- my $new = bless { _column => $column, _parent_resultset => $new_parent_rs }, $class;
+ my $attrs = $new_parent_rs->_resolved_attrs;
+ $new_parent_rs->{attrs}->{$_} = undef for qw(prefetch include_columns +select +as); # prefetch, include_columns, +select, +as cause additional columns to be fetched
+
+ # If $column can be found in the 'as' list of the parent resultset, use the
+ # corresponding element of its 'select' list (to keep any custom column
+ # definition set up with 'select' or '+select' attrs), otherwise use $column
+ # (to create a new column definition on-the-fly).
+ my $as_list = $attrs->{as} || [];
+ my $select_list = $attrs->{select} || [];
+ my $as_index = List::Util::first { ($as_list->[$_] || "") eq $column } 0..$#$as_list;
+ my $select = defined $as_index ? $select_list->[$as_index] : $column;
+
+ my $new = bless { _select => $select, _as => $column, _parent_resultset => $new_parent_rs }, $class;
$new->throw_exception("column must be supplied") unless $column;
return $new;
}
sub next {
my $self = shift;
- $self->{_resultset} = $self->{_parent_resultset}->search(undef, {select => [$self->{_column}], as => [$self->{_column}]}) unless ($self->{_resultset});
- my ($row) = $self->{_resultset}->cursor->next;
+ my ($row) = $self->_resultset->cursor->next;
return $row;
}
sub all {
my $self = shift;
- return map {$_->[0]} $self->{_parent_resultset}->search(undef, {select => [$self->{_column}], as => [$self->{_column}]})->cursor->all;
+ return map { $_->[0] } $self->_resultset->cursor->all;
+}
+
+=head2 reset
+
+=over 4
+
+=item Arguments: none
+
+=item Return Value: $self
+
+=back
+
+Resets the underlying resultset's cursor, so you can iterate through the
+elements of the column again.
+
+Much like L<DBIx::Class::ResultSet/reset>.
+
+=cut
+
+sub reset {
+ my $self = shift;
+ $self->_resultset->cursor->reset;
+ return $self;
+}
+
+=head2 first
+
+=over 4
+
+=item Arguments: none
+
+=item Return Value: $value
+
+=back
+
+Resets the underlying resultset and returns the next value of the column in the
+resultset (or C<undef> if there is none).
+
+Much like L<DBIx::Class::ResultSet/first> but just returning the one value.
+
+=cut
+
+sub first {
+ my $self = shift;
+ my ($row) = $self->_resultset->cursor->reset->next;
+ return $row;
}
=head2 min
sub func {
my ($self,$function) = @_;
- my $cursor = $self->{_parent_resultset}->search(undef, {select => {$function => $self->{_column}}, as => [$self->{_column}]})->cursor;
+ my $cursor = $self->{_parent_resultset}->search(undef, {select => {$function => $self->{_select}}, as => [$self->{_as}]})->cursor;
if( wantarray ) {
return map { $_->[ 0 ] } $cursor->all;
return ( $cursor->next )[ 0 ];
}
+=head2 throw_exception
+
+See L<DBIx::Class::Schema/throw_exception> for details.
+
+=cut
+
+sub throw_exception {
+ my $self=shift;
+ if (ref $self && $self->{_parent_resultset}) {
+ $self->{_parent_resultset}->throw_exception(@_)
+ } else {
+ croak(@_);
+ }
+}
+
+# _resultset
+#
+# Arguments: none
+#
+# Return Value: $resultset
+#
+# $year_col->_resultset->next
+#
+# Returns the underlying resultset. Creates it from the parent resultset if
+# necessary.
+#
+sub _resultset {
+ my $self = shift;
+
+ return $self->{_resultset} ||= $self->{_parent_resultset}->search(undef,
+ {
+ select => [$self->{_select}],
+ as => [$self->{_as}]
+ }
+ );
+}
+
+
1;
=head1 AUTHORS
$class->result_source_instance($table);
- if ($class->can('schema_instance')) {
+ if ($class->can('schema_instance') && $class->schema_instance) {
$class =~ m/([^:]+)$/;
$class->schema_instance->register_class($class, $class);
}
{
my $rel = delete $upd->{$key};
$self->set_from_related($key => $rel);
- $self->{_relationship_data}{$key} = $rel;
+ $self->{_relationship_data}{$key} = $rel;
} elsif ($info && $info->{attrs}{accessor}
- && $info->{attrs}{accessor} eq 'multi'
- && ref $upd->{$key} eq 'ARRAY') {
- my $others = delete $upd->{$key};
- foreach my $rel_obj (@$others) {
- if(!Scalar::Util::blessed($rel_obj)) {
- $rel_obj = $self->create_related($key, $rel_obj);
- }
- }
- $self->{_relationship_data}{$key} = $others;
-# $related->{$key} = $others;
- next;
+ && $info->{attrs}{accessor} eq 'multi') {
+ $self->throw_exception(
+ "Recursive update is not supported over relationships of type multi ($key)"
+ );
}
elsif ($self->has_column($key)
&& exists $self->column_info($key)->{_inflate_info})
{
- $self->set_inflated_column($key, delete $upd->{$key});
+ $self->set_inflated_column($key, delete $upd->{$key});
}
}
}
sub populate {
my ($self, $name, $data) = @_;
- my $rs = $self->resultset($name);
- my @names = @{shift(@$data)};
- if(defined wantarray) {
- my @created;
- foreach my $item (@$data) {
- my %create;
- @create{@names} = @$item;
- push(@created, $rs->create(\%create));
+ if(my $rs = $self->resultset($name)) {
+ if(defined wantarray) {
+ return $rs->populate($data);
+ } else {
+ $rs->populate($data);
}
- return @created;
- }
- my @results_to_create;
- foreach my $datum (@$data) {
- my %result_to_create;
- foreach my $index (0..$#names) {
- $result_to_create{$names[$index]} = $$datum[$index];
- }
- push @results_to_create, \%result_to_create;
+ } else {
+ $self->throw_exception("$name is not a resultset");
}
- $rs->populate(\@results_to_create);
}
=head2 connection
sub select {
my ($self, $table, $fields, $where, $order, @rest) = @_;
- $table = $self->_quote($table) unless ref($table);
+ if (ref $table eq 'SCALAR') {
+ $table = $$table;
+ }
+ elsif (not ref $table) {
+ $table = $self->_quote($table);
+ }
local $self->{rownum_hack_count} = 1
if (defined $rest[0] && $self->{limit_dialect} eq 'RowNum');
@rest = (-1) unless defined $rest[0];
\r
=head1 NAME\r
\r
-DBIx::Class::Storage::ODBC::ACCESS - Support specific to MS Access over ODBC\r
+DBIx::Class::Storage::DBI::ODBC::ACCESS - Support specific to MS Access over ODBC\r
\r
=head1 WARNING\r
\r
=head1 NAME
-DBIx::Class::Storage::DBI::Oracle - Automatic primary key class for Oracle
+DBIx::Class::Storage::DBI::Oracle::Generic - Automatic primary key class for Oracle
=head1 SYNOPSIS
around 'next_storage' => sub {
my ($next_storage, $self, @args) = @_;
my $now = time;
-
+
## Do we need to validate the replicants?
if(
$self->has_auto_validate_every &&
($self->auto_validate_every + $self->pool->last_validated) <= $now
- ) {
+ ) {
$self->pool->validate_replicants;
}
-
+
## Get a replicant, or the master if none
if(my $next = $self->$next_storage(@args)) {
return $next;
} else {
+ $self->master->debugobj->print("No Replicants validate, falling back to master reads. ");
return $self->master;
}
};
sub connect_replicant {
my ($self, $schema, $connect_info) = @_;
my $replicant = $self->create_replicant($schema);
-
- $replicant->connect_info($connect_info);
- $replicant->ensure_connected;
- DBIx::Class::Storage::DBI::Replicated::Replicant->meta->apply($replicant);
-
+ $replicant->connect_info($connect_info);
+ $self->_safely_ensure_connected($replicant);
+ DBIx::Class::Storage::DBI::Replicated::Replicant->meta->apply($replicant);
return $replicant;
}
+=head2 _safely_ensure_connected ($replicant)
+
+The standard ensure_connected method with throw an exception should it fail to
+connect. For the master database this is desirable, but since replicants are
+allowed to fail, this behavior is not desirable. This method wraps the call
+to ensure_connected in an eval in order to catch any generated errors. That
+way a slave to go completely offline (ie, the box itself can die) without
+bringing down your entire pool of databases.
+
+=cut
+
+sub _safely_ensure_connected {
+ my ($self, $replicant, @args) = @_;
+ my $return; eval {
+ $return = $replicant->ensure_connected(@args);
+ }; if ($@) {
+ $replicant
+ ->debugobj
+ ->print(
+ sprintf( "Exception trying to ->ensure_connected for replicant %s, error is %s",
+ $self->_dbi_connect_info->[0], $@)
+ );
+ }
+ return $return;
+}
+
=head2 connected_replicants
Returns true if there are connected replicants. Actually is overloaded to
Calling this method will generate queries on the replicant databases so it is
not recommended that you run them very often.
+This method requires that your underlying storage engine supports some sort of
+native replication mechanism. Currently only MySQL native replication is
+supported. Your patches to make other replication types work are welcomed.
+
=cut
sub validate_replicants {
my $self = shift @_;
foreach my $replicant($self->all_replicants) {
- if(
- $replicant->is_replicating &&
- $replicant->lag_behind_master <= $self->maximum_lag &&
- $replicant->ensure_connected
- ) {
- $replicant->active(1)
+ if($self->_safely_ensure_connected($replicant)) {
+ my $is_replicating = $replicant->is_replicating;
+ unless(defined $is_replicating) {
+ $replicant->debugobj->print("Storage Driver ".ref $self." Does not support the 'is_replicating' method. Assuming you are manually managing.");
+ next;
+ } else {
+ if($is_replicating) {
+ my $lag_behind_master = $replicant->lag_behind_master;
+ unless(defined $lag_behind_master) {
+ $replicant->debugobj->print("Storage Driver ".ref $self." Does not support the 'lag_behind_master' method. Assuming you are manually managing.");
+ next;
+ } else {
+ if($lag_behind_master <= $self->maximum_lag) {
+ $replicant->active(1);
+ } else {
+ $replicant->active(0);
+ }
+ }
+ } else {
+ $replicant->active(0);
+ }
+ }
} else {
$replicant->active(0);
}
}
## Mark that we completed this validation.
- $self->_last_validated(time);
+ $self->_last_validated(time);
}
=head1 AUTHOR
This class defines the following methods.
-=head2 after: _query_start
+=head2 around: _query_start
advice iof the _query_start method to add more debuggin
=head1 NAME
-DBIx::Class::Storage::DBI::Role::QueryCounter; Role to add a query counter
+DBIx::Class::Storage::DBI::Role::QueryCounter - Role to add a query counter
=head1 SYNOPSIS
=cut
-1;
\ No newline at end of file
+1;
use lib qw(t/lib);
use DBICTest;
-plan tests => 134;
+plan tests => 142;
## ----------------------------------------------------------------------------
ok( $cd2->title eq "VOID_Yet More Tweeny-Pop crap", "Got Expected CD Title");
}
+}
+
+ARRAYREF_OF_ARRAYREF_STYLE: {
+ $art_rs->populate([
+ [qw/artistid name/],
+ [1000, 'A Formally Unknown Singer'],
+ [1001, 'A singer that jumped the shark two albums ago'],
+ [1002, 'An actually cool singer.'],
+ ]);
+
+ ok my $unknown = $art_rs->find(1000), "got Unknown";
+ ok my $jumped = $art_rs->find(1001), "got Jumped";
+ ok my $cool = $art_rs->find(1002), "got Cool";
+
+ is $unknown->name, 'A Formally Unknown Singer', 'Correct Name';
+ is $jumped->name, 'A singer that jumped the shark two albums ago', 'Correct Name';
+ is $cool->name, 'An actually cool singer.', 'Correct Name';
+
+ my ($cooler, $lamer) = $art_rs->populate([
+ [qw/artistid name/],
+ [1003, 'Cooler'],
+ [1004, 'Lamer'],
+ ]);
+
+ is $cooler->name, 'Cooler', 'Correct Name';
+ is $lamer->name, 'Lamer', 'Correct Name';
}
\ No newline at end of file
--- /dev/null
+use strict;
+use warnings;
+use Test::More;
+
+use lib qw(t/lib);
+use Data::Dumper;
+
+plan tests => 2;
+
+{
+ my @w;
+ local $SIG{__WARN__} = sub { push @w, @_ };
+
+ my $code = gen_code ( suffix => 1 );
+ eval "$code";
+
+ ok ( (grep { $_ =~ /The many-to-many relationship bars is trying to create/ } @w), "Warning triggered without relevant 'no warnings'");
+}
+
+{
+ my @w;
+ local $SIG{__WARN__} = sub { push @w, @_ };
+
+ my $code = gen_code ( suffix => 2, no_warn => 1 );
+ eval "$code";
+
+diag Dumper \@w;
+
+ ok ( (not grep { $_ =~ /The many-to-many relationship bars is trying to create/ } @w), "No warning triggered with relevant 'no warnings'");
+}
+
+sub gen_code {
+
+ my $args = { @_ };
+ my $suffix = $args->{suffix};
+ my $no_warn = ( $args->{no_warn}
+ ? "no warnings 'DBIx::Class::Relationship::ManyToMany';"
+ : '',
+ );
+
+ return <<EOF;
+use strict;
+use warnings;
+
+{
+ package #
+ DBICTest::Schema::Foo${suffix};
+ use base 'DBIx::Class::Core';
+ __PACKAGE__->table('foo');
+ __PACKAGE__->add_columns(
+ 'fooid' => {
+ data_type => 'integer',
+ is_auto_increment => 1,
+ },
+ );
+ __PACKAGE__->set_primary_key('fooid');
+
+
+ __PACKAGE__->has_many('foo_to_bar' => 'DBICTest::Schema::FooToBar${suffix}' => 'bar');
+ __PACKAGE__->many_to_many( foos => foo_to_bar => 'bar' );
+}
+{
+ package #
+ DBICTest::Schema::FooToBar${suffix};
+
+ use base 'DBIx::Class::Core';
+ __PACKAGE__->table('foo_to_bar');
+ __PACKAGE__->add_columns(
+ 'foo' => {
+ data_type => 'integer',
+ },
+ 'bar' => {
+ data_type => 'integer',
+ },
+ );
+ __PACKAGE__->belongs_to('foo' => 'DBICTest::Schema::Foo${suffix}');
+ __PACKAGE__->belongs_to('bar' => 'DBICTest::Schema::Foo${suffix}');
+}
+{
+ package #
+ DBICTest::Schema::Bar${suffix};
+
+ use base 'DBIx::Class::Core';
+ __PACKAGE__->table('bar');
+ __PACKAGE__->add_columns(
+ 'barid' => {
+ data_type => 'integer',
+ is_auto_increment => 1,
+ },
+ );
+
+ ${no_warn}
+ __PACKAGE__->set_primary_key('barid');
+ __PACKAGE__->has_many('foo_to_bar' => 'DBICTest::Schema::FooToBar${suffix}' => 'foo');
+
+ __PACKAGE__->many_to_many( bars => foo_to_bar => 'foo' );
+
+ sub add_to_bars {}
+}
+EOF
+
+}
{
local $SIG{__WARN__} = sub {};
eval { $dbh->do("DROP TABLE cd") };
- $dbh->do("CREATE TABLE cd (cdid serial PRIMARY KEY, artist INTEGER NOT NULL UNIQUE, title VARCHAR(100) NOT NULL UNIQUE, year VARCHAR(100) NOT NULL, genreid INTEGER);");
+ $dbh->do("CREATE TABLE cd (cdid serial PRIMARY KEY, artist INTEGER NOT NULL UNIQUE, title VARCHAR(100) NOT NULL UNIQUE, year VARCHAR(100) NOT NULL, genreid INTEGER, single_track INTEGER);");
}
$schema->resultset('CD')->create({ title => 'vacation in antarctica', artist => 123, year => 1901 });
{
local $SIG{__WARN__} = sub {};
eval { $dbh->do("DROP TABLE cd") };
- $dbh->do("CREATE TABLE cd (cdid serial PRIMARY KEY, artist INTEGER NOT NULL UNIQUE, title VARCHAR(100) NOT NULL UNIQUE, year VARCHAR(100) NOT NULL, genreid INTEGER);");
+ $dbh->do("CREATE TABLE cd (cdid serial PRIMARY KEY, artist INTEGER NOT NULL UNIQUE, title VARCHAR(100) NOT NULL UNIQUE, year VARCHAR(100) NOT NULL, genreid INTEGER, single_track INTEGER);");
}
$schema->resultset('CD')->create({ title => 'vacation in antarctica', artist => 123, year => 1901 });
{
local $SIG{__WARN__} = sub {};
eval { $dbh->do("DROP TABLE cd") };
- $dbh->do("CREATE TABLE cd (cdid serial PRIMARY KEY, artist INTEGER NOT NULL UNIQUE, title VARCHAR(100) NOT NULL UNIQUE, year VARCHAR(100) NOT NULL, genreid INTEGER);");
+ $dbh->do("CREATE TABLE cd (cdid serial PRIMARY KEY, artist INTEGER NOT NULL UNIQUE, title VARCHAR(100) NOT NULL UNIQUE, year VARCHAR(100) NOT NULL, genreid INTEGER, single_track INTEGER);");
}
$schema->resultset('CD')->create({ title => 'vacation in antarctica', artist => 123, year => 1901 });
my $cd = $schema->resultset("CD")->find(1);
my %cols = $cd->get_columns;
-cmp_ok(keys %cols, '==', 5, 'get_columns number of columns ok');
+cmp_ok(keys %cols, '==', 6, 'get_columns number of columns ok');
is($cols{title}, 'Spoonful of bees', 'get_columns values ok');
# check whether ResultSource->columns returns columns in order originally supplied
my @cd = $schema->source("CD")->columns;
-is_deeply( \@cd, [qw/cdid artist title year genreid/], 'column order');
+is_deeply( \@cd, [qw/cdid artist title year genreid single_track/], 'column order');
$cd = $schema->resultset("CD")->search({ title => 'Spoonful of bees' }, { columns => ['title'] })->next;
is($cd->title, 'Spoonful of bees', 'subset of columns returned correctly');
# test remove_columns
{
- is_deeply([$schema->source('CD')->columns], [qw/cdid artist title year genreid/]);
+ is_deeply([$schema->source('CD')->columns], [qw/cdid artist title year genreid single_track/]);
$schema->source('CD')->remove_columns('year');
- is_deeply([$schema->source('CD')->columns], [qw/cdid artist title genreid/]);
+ is_deeply([$schema->source('CD')->columns], [qw/cdid artist title genreid single_track/]);
ok(! exists $schema->source('CD')->_columns->{'year'}, 'year still exists in _columns');
}
use strict;
-use warnings;
+use warnings;
use Test::More;
+use Test::Exception;
use lib qw(t/lib);
use DBICTest;
my $schema = DBICTest->init_schema();
-plan tests => 69;
+plan tests => 74;
# has_a test
my $cd = $schema->resultset("CD")->find(4);
} );
}
-is( ($artist->search_related('cds'))[3]->title, 'Big Flop', 'create_related ok' );
+my $big_flop_cd = ($artist->search_related('cds'))[3];
+is( $big_flop_cd->title, 'Big Flop', 'create_related ok' );
+
+{ # make sure we are not making pointless select queries when a FK IS NULL
+ my $queries = 0;
+ $schema->storage->debugcb(sub { $queries++; });
+ $schema->storage->debug(1);
+ $big_flop_cd->genre; #should not trigger a select query
+ is($queries, 0, 'No SELECT made for belongs_to if key IS NULL');
+ $big_flop_cd->genre_inefficient; #should trigger a select query
+ is($queries, 1, 'SELECT made for belongs_to if key IS NULL when undef_on_null_fk disabled');
+ $schema->storage->debug(0);
+ $schema->storage->debugcb(undef);
+}
my( $rs_from_list ) = $artist->search_related_rs('cds');
is( ref($rs_from_list), 'DBIx::Class::ResultSet', 'search_related_rs in list context returns rs' );
ok($new_artist->in_storage, 'artist inserted');
ok($new_related_cd->in_storage, 'new_related_cd inserted');
+TODO: {
+local $TODO = "TODOify for multicreate branch";
+my $new_cd = $schema->resultset("CD")->new_result({});
+my $new_related_artist = $new_cd->new_related('artist', { 'name' => 'Marillion',});
+lives_ok (
+ sub {
+ $new_related_artist->insert;
+ $new_cd->title( 'Misplaced Childhood' );
+ $new_cd->year ( 1985 );
+# $new_cd->artist( $new_related_artist ); # For exact backward compatibility # not sure what this means
+ $new_cd->insert;
+ },
+ 'Reversed staged insertion successful'
+);
+ok($new_related_artist->in_storage, 'related artist inserted');
+ok($new_cd->in_storage, 'cd inserted');
+
# check if is_foreign_key_constraint attr is set
my $rs_normal = $schema->source('Track');
my $relinfo = $rs_normal->relationship_info ('cd');
my $rs_overridden = $schema->source('ForceForeign');
my $relinfo_with_attr = $rs_overridden->relationship_info ('cd_3');
cmp_ok($relinfo_with_attr->{attrs}{is_foreign_key_constraint}, '==', 0, "is_foreign_key_constraint defined for belongs_to relationships with attr.");
+}
use Test::More qw(no_plan);
use lib qw(t/lib);
use DBICTest;
-use DBIx::Class::ResultClass::HashRefInflator;
my $schema = DBICTest->init_schema();
$schema->resultset('CD')->create({ title => 'Silence is golden', artist => 3, year => 2006 });
# order_by to ensure both resultsets have the rows in the same order
+# also check result_class-as-an-attribute syntax
my $rs_dbic = $schema->resultset('CD')->search(undef,
{
prefetch => [ qw/ artist tracks / ],
{
prefetch => [ qw/ artist tracks / ],
order_by => [ 'me.cdid', 'tracks.position' ],
+ result_class => 'DBIx::Class::ResultClass::HashRefInflator',
}
);
-$rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator');
my @dbic = $rs_dbic->all;
my @hashrefinf = $rs_hashrefinf->all;
select => [qw/name tracks.title tracks.cd /],
as => [qw/name cds.tracks.title cds.tracks.cd /],
order_by => [qw/cds.cdid tracks.trackid/],
+ result_class => 'DBIx::Class::ResultClass::HashRefInflator',
});
-$rs_hashrefinf->result_class('DBIx::Class::ResultClass::HashRefInflator');
@dbic = map { $_->tracks->all } ($rs_dbic->first->cds->all);
@hashrefinf = $rs_hashrefinf->all;
DROP TABLE cd");
$dbh->do("CREATE TABLE artist (artistid INT IDENTITY PRIMARY KEY, name VARCHAR(100), rank INT DEFAULT '13', charfield CHAR(10) NULL);");
-$dbh->do("CREATE TABLE cd (cdid INT IDENTITY PRIMARY KEY, artist INT, title VARCHAR(100), year VARCHAR(100), genreid INT NULL);");
+$dbh->do("CREATE TABLE cd (cdid INT IDENTITY PRIMARY KEY, artist INT, title VARCHAR(100), year VARCHAR(100), genreid INT NULL, single_track INT NULL);");
# Just to test compat shim, Auto is in Core
$schema->class('Artist')->load_components('PK::Auto::MSSQL');
my $schema = DBICTest->init_schema();
-plan tests => 7;
+plan tests => 11;
my $rs = $schema->resultset('CD')->search({},
{
lives_ok(sub { $rs->first->get_column('count') }, '+select/+as chained search 1st rscolumn present');
lives_ok(sub { $rs->first->get_column('addedtitle') }, '+select/+as chained search 1st rscolumn present');
lives_ok(sub { $rs->first->get_column('addedtitle2') }, '+select/+as chained search 3rd rscolumn present');
+
+
+# test the from search attribute (gets between the FROM and WHERE keywords, allows arbitrary subselects)
+# also shows that outer select attributes are ok (i.e. order_by)
+#
+# from doesn't seem to be useful without using a scalarref - there were no initial tests >:(
+#
+my $cds = $schema->resultset ('CD')->search ({}, { order_by => 'me.cdid'}); # make sure order is consistent
+cmp_ok ($cds->count, '>', 2, 'Initially populated with more than 2 CDs');
+
+my $table = $cds->result_source->name;
+my $subsel = $cds->search ({}, {
+ columns => [qw/cdid title/],
+ from => \ "(SELECT cdid, title FROM $table LIMIT 2) me",
+});
+
+is ($subsel->count, 2, 'Subselect correctly limited the rs to 2 cds');
+is ($subsel->next->title, $cds->next->title, 'First CD title match');
+is ($subsel->next->title, $cds->next->title, 'Second CD title match');
# (the TODO block itself contains tests ensuring that the warns are removed)
TODO: {
local $TODO = 'Prefetch of multiple has_many rels at the same level (currently warn to protect the clueless git)';
- use DBIx::Class::ResultClass::HashRefInflator;
#( 1 -> M + M )
my $cd_rs = $schema->resultset('CD')->search ({ 'me.title' => 'Forkful of bees' });
use warnings;
use Test::More;
+use Test::Exception;
use lib qw(t/lib);
use DBICTest;
my $schema = DBICTest->init_schema();
-plan tests => 14;
+plan tests => 18;
my $cd;
-my $rs = $cd = $schema->resultset("CD")->search({});
+my $rs = $cd = $schema->resultset("CD")->search({}, { order_by => 'cdid' });
my $rs_title = $rs->get_column('title');
my $rs_year = $rs->get_column('year');
cmp_ok($rs_year->sum, '==', 9996, "three artists returned");
+$rs_year->reset;
+is($rs_year->next, 1999, "reset okay");
+
+is($rs_year->first, 1999, "first okay");
+
+# test +select/+as for single column
my $psrs = $schema->resultset('CD')->search({},
{
'+select' => \'COUNT(*)',
'+as' => 'count'
}
);
-ok(defined($psrs->get_column('count')), '+select/+as count');
+lives_ok(sub { $psrs->get_column('count')->next }, '+select/+as additional column "count" present (scalar)');
+dies_ok(sub { $psrs->get_column('noSuchColumn')->next }, '+select/+as nonexistent column throws exception');
+# test +select/+as for multiple columns
$psrs = $schema->resultset('CD')->search({},
{
'+select' => [ \'COUNT(*)', 'title' ],
'+as' => [ 'count', 'addedtitle' ]
}
);
-ok(defined($psrs->get_column('count')), '+select/+as arrayref count');
-ok(defined($psrs->get_column('addedtitle')), '+select/+as title');
+lives_ok(sub { $psrs->get_column('count')->next }, '+select/+as multiple additional columns, "count" column present');
+lives_ok(sub { $psrs->get_column('addedtitle')->next }, '+select/+as multiple additional columns, "addedtitle" column present');
+
+# test +select/+as for overriding a column
+$psrs = $schema->resultset('CD')->search({},
+ {
+ 'select' => \"'The Final Countdown'",
+ 'as' => 'title'
+ }
+);
+is($psrs->get_column('title')->next, 'The Final Countdown', '+select/+as overridden column "title"');
{
my $rs = $schema->resultset("CD")->search({}, { prefetch => 'artist' });
my @cds = $schema->resultset('CD')->search( { artist => 1, cdid => { -between => [ 1, 3 ] }, } );
is_same_sql_bind(
$sql, [],
- "SELECT me.cdid, me.artist, me.title, me.year, me.genreid FROM cd me WHERE ( artist = ? AND cdid BETWEEN ? AND ? ): '1', '1', '3'", [],
+ "SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE ( artist = ? AND cdid BETWEEN ? AND ? ): '1', '1', '3'", [],
'got correct SQL with all bind parameters (debugcb)'
);
@cds = $schema->resultset('CD')->search( { artist => 1, cdid => { -between => [ 1, 3 ] }, } );
is_same_sql_bind(
$sql, \@bind,
- "SELECT me.cdid, me.artist, me.title, me.year, me.genreid FROM cd me WHERE ( artist = ? AND cdid BETWEEN ? AND ? )", ["'1'", "'1'", "'3'"],
+ "SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE ( artist = ? AND cdid BETWEEN ? AND ? )", ["'1'", "'1'", "'3'"],
'got correct SQL with all bind parameters (debugobj)'
);
}
$replicated->replicate;
$replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
$replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
+$replicated->schema->storage->pool->validate_replicants;
## Make sure we can read the data.
=> 'Found expected name for first result';
is $replicated->schema->storage->pool->connected_replicants => 1
- => "One replicant reconnected to handle the job";
+ => "At Least One replicant reconnected to handle the job";
## What happens when we try to select something that doesn't exist?
$replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
$replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
+$replicated->schema->storage->pool->validate_replicants;
ok $replicated->schema->resultset('Artist')->find(2)
=> 'Returned to replicates';
## Delete the old database files
$replicated->cleanup;
+use Data::Dump qw/dump/;
+#warn dump $replicated->schema->storage->read_handler;
+
use warnings;
use Test::More;
+use Test::Exception;
use lib qw(t/lib);
use DBICTest;
-plan tests => 58;
+plan tests => 89;
my $schema = DBICTest->init_schema();
};
diag $@ if $@;
-# same as above but the child and parent have no values,
+# same as above but the child and parent have no values,
# except for an explicit parent pk
eval {
my $bm_rs = $schema->resultset('Bookmark');
# create over > 1 levels of has_many create (A => { has_many => { B => has_many => C } } )
eval {
- my $artist = $schema->resultset('Artist')->create(
- { name => 'Fred 2',
+ my $artist = $schema->resultset('Artist')->first;
+ my $cd = $artist->create_related (cds => {
+ title => 'Music to code by',
+ year => 2007,
+ tags => [
+ { 'tag' => 'rock' },
+ ],
+ });
+
+ isa_ok($cd, 'DBICTest::CD', 'Created CD');
+ is($cd->title, 'Music to code by', 'CD created correctly');
+ is($cd->tags->count, 1, 'One tag created for CD');
+ is($cd->tags->first->tag, 'rock', 'Tag created correctly');
+
+};
+diag $@ if $@;
+
+throws_ok (
+ sub {
+ # Create via update - add a new CD <--- THIS SHOULD HAVE NEVER WORKED!
+ $schema->resultset('Artist')->first->update({
cds => [
- { title => 'Music to code by',
- year => 2007,
- tags => [
- { 'tag' => 'rock' },
- ],
+ { title => 'Yet another CD',
+ year => 2006,
},
- ],
+ ],
+ });
+ },
+ qr/Recursive update is not supported over relationships of type multi/,
+ 'create via update of multi relationships throws an exception'
+);
+
+# Create m2m while originating in the linker table
+eval {
+ my $artist = $schema->resultset('Artist')->first;
+ my $c2p = $schema->resultset('CD_to_Producer')->create ({
+ cd => {
+ artist => $artist,
+ title => 'Bad investment',
+ year => 2008,
+ tracks => [
+ { position => 1, title => 'Just buy' },
+ { position => 2, title => 'Why did we do it' },
+ { position => 3, title => 'Burn baby burn' },
+ ],
+ },
+ producer => {
+ name => 'Lehman Bros.',
+ },
});
- isa_ok($artist, 'DBICTest::Artist', 'Created Artist');
- is($artist->name, 'Fred 2', 'Artist created correctly');
- is($artist->cds->count, 1, 'One CD created for artist');
- is($artist->cds->first->title, 'Music to code by', 'CD created correctly');
- is($artist->cds->first->tags->count, 1, 'One tag created for CD');
- is($artist->cds->first->tags->first->tag, 'rock', 'Tag created correctly');
-
- # Create via update - add a new CD
- $artist->update({
- cds => [ $artist->cds,
- { title => 'Yet another CD',
- year => 2006,
+ isa_ok ($c2p, 'DBICTest::CD_to_Producer', 'Linker object created');
+ my $prod = $schema->resultset ('Producer')->find ({ name => 'Lehman Bros.' });
+ isa_ok ($prod, 'DBICTest::Producer', 'Producer row found');
+ is ($prod->cds->count, 1, 'Producer has one production');
+ my $cd = $prod->cds->first;
+ is ($cd->title, 'Bad investment', 'CD created correctly');
+ is ($cd->tracks->count, 3, 'CD has 3 tracks');
+
+};
+diag $@ if $@;
+
+# create over > 1 levels of might_have (A => { might_have => { B => has_many => C } } )
+eval {
+ my $artist = $schema->resultset('Artist')->first;
+ my $cd = $schema->resultset('CD')->create ({
+ artist => $artist,
+ title => 'Music to code by at night',
+ year => 2008,
+ tracks => [
+ {
+ position => 1,
+ title => 'Off by one again',
+ },
+ {
+ position => 2,
+ title => 'The dereferencer',
+ cd_single => {
+ artist => $artist,
+ year => 2008,
+ title => 'Was that a null (Single)',
+ tracks => [
+ { title => 'The dereferencer', position => 1 },
+ { title => 'The dereferencer II', position => 2 },
+ ],
+ cd_to_producer => [
+ {
+ producer => {
+ name => 'K&R',
+ }
+ }
+ ]
+ },
},
],
});
- is(($artist->cds->search({}, { order_by => 'year' }))[0]->title, 'Yet another CD', 'Updated and added another CD');
- my $newartist = $schema->resultset('Artist')->find_or_create({ name => 'Fred 2'});
+ isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
+ is ($cd->title, 'Music to code by at night', 'Correct CD title');
+ is ($cd->tracks->count, 2, 'Two tracks on main CD');
+
+ my ($t1, $t2) = $cd->tracks->all;
+ is ($t1->title, 'Off by one again', 'Correct 1st track name');
+ is ($t1->cd_single, undef, 'No single for 1st track');
+ is ($t2->title, 'The dereferencer', 'Correct 2nd track name');
+ isa_ok ($t2->cd_single, 'DBICTest::CD', 'Created a single for 2nd track');
+
+ my $single = $t2->cd_single;
+ is ($single->tracks->count, 2, 'Two tracks on single CD');
+ is ($single->tracks->find ({ position => 1})->title, 'The dereferencer', 'Correct 1st track title');
+ is ($single->tracks->find ({ position => 2})->title, 'The dereferencer II', 'Correct 2nd track title');
+
+ is ($single->cd_to_producer->count, 1, 'One producer created with the single cd');
+ is ($single->cd_to_producer->first->producer->name, 'K&R', 'Producer name correct');
+};
+diag $@ if $@;
+
+TODO: {
+local $TODO = "Todoify for multicreate branch";
+# test might_have again but with a PK == FK in the middle (obviously not specified)
+eval {
+ my $artist = $schema->resultset('Artist')->first;
+ my $cd = $schema->resultset('CD')->create ({
+ artist => $artist,
+ title => 'Music to code by at twilight',
+ year => 2008,
+ artwork => {
+ images => [
+ { name => 'recursive descent' },
+ { name => 'tail packing' },
+ ],
+ },
+ });
+
+ isa_ok ($cd, 'DBICTest::CD', 'Main CD object created');
+ is ($cd->title, 'Music to code by at twilight', 'Correct CD title');
+ isa_ok ($cd->artwork, 'DBICTest::Artwork', 'Artwork created');
+
+ # this test might look weird, but it failed at one point, keep it there
+ my $art_obj = $cd->artwork;
+ ok ($art_obj->has_column_loaded ('cd_id'), 'PK/FK present on artwork object');
+ is ($art_obj->images->count, 2, 'Correct artwork image count via the new object');
+ is_deeply (
+ [ sort $art_obj->images->get_column ('name')->all ],
+ [ 'recursive descent', 'tail packing' ],
+ 'Images named correctly in objects',
+ );
+
+
+ my $artwork = $schema->resultset('Artwork')->search (
+ { 'cd.title' => 'Music to code by at twilight' },
+ { join => 'cd' },
+ )->single;
+
+ is ($artwork->images->count, 2, 'Correct artwork image count via a new search');
+
+ is_deeply (
+ [ sort $artwork->images->get_column ('name')->all ],
+ [ 'recursive descent', 'tail packing' ],
+ 'Images named correctly after search',
+ );
+};
+diag $@ if $@;
+
+# test might_have again but with just a PK and FK (neither specified) in the mid-table
+eval {
+ my $cd = $schema->resultset('CD')->first;
+ my $track = $schema->resultset ('Track')->create ({
+ cd => $cd,
+ position => 66,
+ title => 'Black',
+ lyrics => {
+ lyric_versions => [
+ { text => 'The color black' },
+ { text => 'The colour black' },
+ ],
+ },
+ });
+
+ isa_ok ($track, 'DBICTest::Track', 'Main track object created');
+ is ($track->title, 'Black', 'Correct track title');
+ isa_ok ($track->lyrics, 'DBICTest::Lyrics', 'Lyrics created');
+
+ # this test might look weird, but it was failing at one point, keep it there
+ my $lyric_obj = $track->lyrics;
+ ok ($lyric_obj->has_column_loaded ('lyric_id'), 'PK present on lyric object');
+ ok ($lyric_obj->has_column_loaded ('track_id'), 'FK present on lyric object');
+ is ($lyric_obj->lyric_versions->count, 2, 'Correct lyric versions count via the new object');
+ is_deeply (
+ [ sort $lyric_obj->lyric_versions->get_column ('text')->all ],
+ [ 'The color black', 'The colour black' ],
+ 'Lyrics text in objects matches',
+ );
+
+
+ my $lyric = $schema->resultset('Lyrics')->search (
+ { 'track.title' => 'Black' },
+ { join => 'track' },
+ )->single;
+
+ is ($lyric->lyric_versions->count, 2, 'Correct lyric versions count via a new search');
- is($newartist->name, 'Fred 2', 'Retrieved the artist');
+ is_deeply (
+ [ sort $lyric->lyric_versions->get_column ('text')->all ],
+ [ 'The color black', 'The colour black' ],
+ 'Lyrics text via search matches',
+ );
};
diag $@ if $@;
+}
# nested find_or_create
eval {
# multiple same level has_many create
eval {
my $artist2 = $schema->resultset('Artist')->create({
- name => 'Fred 3',
+ name => 'Fred 4',
cds => [
{
title => 'Music to code by',
);
};
diag $@ if $@;
+
+1;
use warnings;
use Test::More;
+use Test::Exception;
use lib qw(t/lib);
use DBICTest;
my $schema = DBICTest->init_schema();
-plan tests => 9;
+plan tests => 12;
{
my $cd_rc = $schema->resultset("CD")->result_class;
+ throws_ok {
+ $schema->resultset("Artist")
+ ->search_rs({}, {result_class => "IWillExplode"})
+ } qr/Can't locate IWillExplode/, 'nonexistant result_class exception';
+
+# to make ensure_class_loaded happy, dies on inflate
+ eval 'package IWillExplode; sub dummy {}';
+
my $artist_rs = $schema->resultset("Artist")
->search_rs({}, {result_class => "IWillExplode"});
is($artist_rs->result_class, 'IWillExplode', 'Correct artist result_class');
-
+
+ throws_ok {
+ $artist_rs->result_class('mtfnpy')
+ } qr/Can't locate mtfnpy/,
+ 'nonexistant result_access exception (from accessor)';
+
+ throws_ok {
+ $artist_rs->first
+ } qr/Can't locate object method "inflate_result" via package "IWillExplode"/,
+ 'IWillExplode explodes on inflate';
+
my $cd_rs = $artist_rs->related_resultset('cds');
is($cd_rs->result_class, $cd_rc, 'Correct cd result_class');
eval "use DBD::mysql; use SQL::Translator 0.09;";
plan $@
? ( skip_all => 'needs DBD::mysql and SQL::Translator 0.09 for testing' )
- : ( tests => 102 );
+ : ( tests => 114 );
}
my $schema = DBICTest->init_schema();
);
# Test that a disconnect doesnt harm anything.
-Film->db_Main->disconnect;
-@films = Film->search({ Rating => 'NC-17' });
-ok(@films == 1 && $films[0]->id eq $gone->id, 'auto reconnection');
-
-# Test discard_changes().
-my $orig_director = $btaste->Director;
-$btaste->Director('Lenny Bruce');
-is($btaste->Director, 'Lenny Bruce', 'set new Director');
-$btaste->discard_changes;
-is($btaste->Director, $orig_director, 'discard_changes()');
+{
+ # SQLite is loud on disconnect/reconnect.
+ # This is solved in DBIC but not in ContextualFetch
+ local $SIG{__WARN__} = sub {
+ warn @_ unless $_[0] =~
+ /active statement handles|inactive database handle/;
+ };
+
+ Film->db_Main->disconnect;
+ @films = Film->search({ Rating => 'NC-17' });
+ ok(@films == 1 && $films[0]->id eq $gone->id, 'auto reconnection');
+
+ # Test discard_changes().
+ my $orig_director = $btaste->Director;
+ $btaste->Director('Lenny Bruce');
+ is($btaste->Director, 'Lenny Bruce', 'set new Director');
+ $btaste->discard_changes;
+ is($btaste->Director, $orig_director, 'discard_changes()');
+}
SKIP: {
skip "ActiveState perl produces additional warnings", 3
next;
}
eval "use DBD::SQLite";
- plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 55);
+ plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 75);
}
INIT {
};
eval {
- my $data = $data;
+ my $data = { %$data };
$data->{NumExplodingSheep} = 1;
ok my $bt = Film->create($data), "Modified accessor - with column name";
isa_ok $bt, "Film";
+ is $bt->sheep, 1, 'sheep bursting violently';
};
is $@, '', "No errors";
eval {
- my $data = $data;
- $data->{sheep} = 1;
+ my $data = { %$data };
+ $data->{sheep} = 2;
ok my $bt = Film->create($data), "Modified accessor - with accessor";
isa_ok $bt, "Film";
+ is $bt->sheep, 2, 'sheep bursting violently';
+};
+is $@, '', "No errors";
+
+eval {
+ my $data = { %$data };
+ $data->{NumExplodingSheep} = 1;
+ ok my $bt = Film->find_or_create($data),
+ "find_or_create Modified accessor - find with column name";
+ isa_ok $bt, "Film";
+ is $bt->sheep, 1, 'sheep bursting violently';
+};
+is $@, '', "No errors";
+
+eval {
+ my $data = { %$data };
+ $data->{sheep} = 1;
+ ok my $bt = Film->find_or_create($data),
+ "find_or_create Modified accessor - find with accessor";
+ isa_ok $bt, "Film";
+ is $bt->sheep, 1, 'sheep bursting violently';
+};
+is $@, '', "No errors";
+
+TODO: { local $TODO = 'TODOifying failing tests, waiting for Schwern'; ok (1, 'remove me');
+eval {
+ my $data = { %$data };
+ $data->{NumExplodingSheep} = 3;
+ ok my $bt = Film->find_or_create($data),
+ "find_or_create Modified accessor - create with column name";
+ isa_ok $bt, "Film";
+ is $bt->sheep, 3, 'sheep bursting violently';
+};
+is $@, '', "No errors";
+
+eval {
+ my $data = { %$data };
+ $data->{sheep} = 4;
+ ok my $bt = Film->find_or_create($data),
+ "find_or_create Modified accessor - create with accessor";
+ isa_ok $bt, "Film";
+ is $bt->sheep, 4, 'sheep bursting violently';
};
is $@, '', "No errors";
my @film = Film->search({ sheep => 1 });
is @film, 2, "Can search with modified accessor";
};
+is $@, '', "No errors";
+
+}
{
like $@, qr/film/, "no hasa film";
eval {
+ local $SIG{__WARN__} = sub {
+ warn @_ unless $_[0] =~ /Query returned more than one row/;
+ };
ok my $f = $ac->movie, "hasa movie";
isa_ok $f, "Film";
is $f->id, $bt->id, " - Bad Taste";
use strict;
use Test::More;
+use Data::Dumper;
BEGIN {
eval "use DBIx::Class::CDBICompat;";
is $dir->nasties, 1, "We have one nasty";
ok $dir->delete;
- ok +Film->retrieve("Alligator"), "has_many with @{[ keys %$args ]} => @{[ values %$args ]}";
+ local $Data::Dumper::Terse = 1;
+ ok +Film->retrieve("Alligator"), 'has_many with ' . Dumper ($args);;
$kk->delete;
}
/,
{ 'DBICTest::Schema' => [qw/
LinerNotes
+ Artwork
+ Image
+ Lyrics
+ LyricVersion
OneKey
#dummy
TwoKeys
--- /dev/null
+package # hide from PAUSE
+ DBICTest::Schema::Artwork;
+
+use base qw/DBIx::Class::Core/;
+
+__PACKAGE__->table('cd_artwork');
+__PACKAGE__->add_columns(
+ 'cd_id' => {
+ data_type => 'integer',
+ },
+);
+__PACKAGE__->set_primary_key('cd_id');
+__PACKAGE__->belongs_to('cd', 'DBICTest::Schema::CD', 'cd_id');
+__PACKAGE__->has_many('images', 'DBICTest::Schema::Image', 'artwork_id');
+
+1;
'genreid' => {
data_type => 'integer',
is_nullable => 1,
+ },
+ 'single_track' => {
+ data_type => 'integer',
+ is_nullable => 1,
+ is_foreign_key => 1,
}
);
__PACKAGE__->set_primary_key('cdid');
is_deferrable => 1,
});
+# in case this is a single-cd it promotes a track from another cd
+__PACKAGE__->belongs_to( single_track => 'DBICTest::Schema::Track' );
+
__PACKAGE__->has_many( tracks => 'DBICTest::Schema::Track' );
__PACKAGE__->has_many(
tags => 'DBICTest::Schema::Tag', undef,
liner_notes => 'DBICTest::Schema::LinerNotes', undef,
{ proxy => [ qw/notes/ ] },
);
+__PACKAGE__->might_have(artwork => 'DBICTest::Schema::Artwork', 'cd_id');
+
__PACKAGE__->many_to_many( producers => cd_to_producer => 'producer' );
__PACKAGE__->many_to_many(
producers_sorted => cd_to_producer => 'producer',
join_type => 'left',
on_delete => 'SET NULL',
on_update => 'CASCADE',
+ },
+);
+#This second relationship was added to test the short-circuiting of pointless
+#queries provided by undef_on_null_fk. the relevant test in 66relationship.t
+__PACKAGE__->belongs_to('genre_inefficient', 'DBICTest::Schema::Genre',
+ { 'foreign.genreid' => 'self.genreid' },
+ {
+ join_type => 'left',
+ on_delete => 'SET NULL',
+ on_update => 'CASCADE',
+ undef_on_null_fk => 0,
},
);
+
#__PACKAGE__->add_relationship('genre', 'DBICTest::Schema::Genre',
# { 'foreign.genreid' => 'self.genreid' },
# { 'accessor' => 'single' }
--- /dev/null
+package # hide from PAUSE
+ DBICTest::Schema::Image;
+
+use base qw/DBIx::Class::Core/;
+
+__PACKAGE__->table('images');
+__PACKAGE__->add_columns(
+ 'id' => {
+ data_type => 'integer',
+ is_auto_increment => 1,
+ },
+ 'artwork_id' => {
+ data_type => 'integer',
+ is_foreign_key => 1,
+ },
+ 'name' => {
+ data_type => 'varchar',
+ size => 100,
+ },
+ 'data' => {
+ data_type => 'blob',
+ is_nullable => 1,
+ },
+);
+__PACKAGE__->set_primary_key('id');
+__PACKAGE__->belongs_to('artwork', 'DBICTest::Schema::Artwork', 'artwork_id');
+
+1;
--- /dev/null
+package # hide from PAUSE
+ DBICTest::Schema::LyricVersion;
+
+use base qw/DBIx::Class::Core/;
+
+__PACKAGE__->table('lyric_versions');
+__PACKAGE__->add_columns(
+ 'id' => {
+ data_type => 'integer',
+ is_auto_increment => 1,
+ },
+ 'lyric_id' => {
+ data_type => 'integer',
+ is_foreign_key => 1,
+ },
+ 'text' => {
+ data_type => 'varchar',
+ size => 100,
+ },
+);
+__PACKAGE__->set_primary_key('id');
+__PACKAGE__->belongs_to('lyric', 'DBICTest::Schema::Lyrics', 'lyric_id');
+
+1;
--- /dev/null
+package # hide from PAUSE
+ DBICTest::Schema::Lyrics;
+
+use base qw/DBIx::Class::Core/;
+
+__PACKAGE__->table('lyrics');
+__PACKAGE__->add_columns(
+ 'lyric_id' => {
+ data_type => 'integer',
+ is_auto_increment => 1,
+ },
+ 'track_id' => {
+ data_type => 'integer',
+ is_foreign_key => 1,
+ },
+);
+__PACKAGE__->set_primary_key('lyric_id');
+__PACKAGE__->belongs_to('track', 'DBICTest::Schema::Track', 'track_id');
+__PACKAGE__->has_many('lyric_versions', 'DBICTest::Schema::LyricVersion', 'lyric_id');
+
+1;
__PACKAGE__->has_many(
producer_to_cd => 'DBICTest::Schema::CD_to_Producer' => 'producer'
);
-
+__PACKAGE__->many_to_many('cds', 'producer_to_cd', 'cd');
1;
__PACKAGE__->belongs_to( cd => 'DBICTest::Schema::CD' );
__PACKAGE__->belongs_to( disc => 'DBICTest::Schema::CD' => 'cd');
+__PACKAGE__->might_have( cd_single => 'DBICTest::Schema::CD', 'single_track' );
+__PACKAGE__->might_have( lyrics => 'DBICTest::Schema::Lyrics', 'track_id' );
+
1;
--
-- Created by SQL::Translator::Producer::SQLite
--- Created on Mon Nov 10 23:52:55 2008
+-- Created on Mon Nov 17 02:53:11 2008
--
BEGIN TRANSACTION;
CREATE INDEX artist_undirected_map_idx_id2_ ON artist_undirected_map (id2);
--
+-- Table: cd_artwork
+--
+CREATE TABLE cd_artwork (
+ cd_id INTEGER PRIMARY KEY NOT NULL
+);
+
+CREATE INDEX cd_artwork_idx_cd_id_cd_artwor ON cd_artwork (cd_id);
+
+--
-- Table: bookmark
--
CREATE TABLE bookmark (
artist integer NOT NULL,
title varchar(100) NOT NULL,
year varchar(100) NOT NULL,
- genreid integer
+ genreid integer,
+ single_track integer
);
CREATE INDEX cd_idx_artist_cd ON cd (artist);
CREATE INDEX cd_idx_genreid_cd ON cd (genreid);
+CREATE INDEX cd_idx_single_track_cd ON cd (single_track);
CREATE UNIQUE INDEX cd_artist_title_cd ON cd (artist, title);
--
CREATE UNIQUE INDEX genre_name_genre ON genre (name);
--
+-- Table: images
+--
+CREATE TABLE images (
+ id INTEGER PRIMARY KEY NOT NULL,
+ artwork_id integer NOT NULL,
+ name varchar(100) NOT NULL,
+ data blob
+);
+
+CREATE INDEX images_idx_artwork_id_images ON images (artwork_id);
+
+--
-- Table: liner_notes
--
CREATE TABLE liner_notes (
--
+-- Table: lyric_versions
+--
+CREATE TABLE lyric_versions (
+ id INTEGER PRIMARY KEY NOT NULL,
+ lyric_id integer NOT NULL,
+ text varchar(100) NOT NULL
+);
+
+CREATE INDEX lyric_versions_idx_lyric_id_ly ON lyric_versions (lyric_id);
+
+--
+-- Table: lyrics
+--
+CREATE TABLE lyrics (
+ lyric_id INTEGER PRIMARY KEY NOT NULL,
+ track_id integer NOT NULL
+);
+
+CREATE INDEX lyrics_idx_track_id_lyrics ON lyrics (track_id);
+
+--
-- Table: noprimarykey
--
CREATE TABLE noprimarykey (
use vars qw/$dbh/;
-my @connect = ("dbi:mysql:test", "", "");
+# temporary, might get switched to the new test framework someday
+my @connect = ("dbi:mysql:test", "", "", { PrintError => 0});
$dbh = DBI->connect(@connect) or die DBI->errstr;
my @table;