From: Matt S Trout Date: Thu, 4 Aug 2005 04:53:29 +0000 (+0000) Subject: DBIx::Class is now a component loader X-Git-Tag: v0.03001~81 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=126042ee4b9394c4eecc6ece49469da6fce23ba3;p=dbsrgits%2FDBIx-Class.git DBIx::Class is now a component loader --- diff --git a/lib/DBIx/Class.pm b/lib/DBIx/Class.pm index c9d846b..c1cc569 100644 --- a/lib/DBIx/Class.pm +++ b/lib/DBIx/Class.pm @@ -3,12 +3,22 @@ package DBIx::Class; use strict; use warnings; -use base qw/DBIx::Class::CDBICompat DBIx::Class::Core/; - use vars qw($VERSION); +use base; $VERSION = '0.01'; +sub load_components { + my $class = shift; + my @comp = map { "DBIx::Class::$_" } @_; + foreach my $comp (@comp) { + eval "use $comp"; + die $@ if $@; + } + no strict 'refs'; + unshift(@{"${class}::ISA"}, @comp); +} + 1; =head1 NAME @@ -32,17 +42,24 @@ use base qw/Class::DBI/; with -use base qw/DBIx::Class::CDBICompat DBIx::Class::Core/; +use base qw/DBIx::Class/; +__PACKAGE__->load_components(qw/CDBICompat Core/); will probably get you started. If you're using AUTO_INCREMENT for your primary columns, you'll also want -PK::Auto and an appropriate PK::Auto::DBName (e.g. ::SQLite). +yo load the approriate PK::Auto subclass - e.g. + +__PACKAGE__->load_components(qw/CDBICompat PK::Auto::SQLite Core/); + +(with is what ::Test::SQLite does to present the Class::DBI::Test::SQLite +interface) If you fancy playing around with DBIx::Class from scratch, then read the docs for ::Table and ::Relationship, use base qw/DBIx::Class/; +__PACKAGE__->load_components(qw/Core/); and have a look at t/lib/DBICTest.pm for a brief example. diff --git a/lib/DBIx/Class/CDBICompat.pm b/lib/DBIx/Class/CDBICompat.pm index ae25ddd..60857f6 100644 --- a/lib/DBIx/Class/CDBICompat.pm +++ b/lib/DBIx/Class/CDBICompat.pm @@ -2,26 +2,28 @@ package DBIx::Class::CDBICompat; use strict; use warnings; - -use base qw/DBIx::Class::CDBICompat::Constraints - DBIx::Class::CDBICompat::Triggers - DBIx::Class::CDBICompat::ReadOnly - DBIx::Class::CDBICompat::GetSet - DBIx::Class::CDBICompat::LiveObjectIndex - DBIx::Class::CDBICompat::AttributeAPI - DBIx::Class::CDBICompat::Stringify - DBIx::Class::CDBICompat::DestroyWarning - DBIx::Class::CDBICompat::Constructor - DBIx::Class::CDBICompat::AccessorMapping - DBIx::Class::CDBICompat::ColumnCase - DBIx::Class::CDBICompat::MightHave - DBIx::Class::CDBICompat::HasMany - DBIx::Class::CDBICompat::HasA - DBIx::Class::CDBICompat::LazyLoading - DBIx::Class::CDBICompat::AutoUpdate - DBIx::Class::CDBICompat::TempColumns - DBIx::Class::CDBICompat::ColumnGroups - DBIx::Class::CDBICompat::ImaDBI/; +use base qw/DBIx::Class/; + +__PACKAGE__->load_components(qw/ + CDBICompat::Constraints + CDBICompat::Triggers + CDBICompat::ReadOnly + CDBICompat::GetSet + CDBICompat::LiveObjectIndex + CDBICompat::AttributeAPI + CDBICompat::Stringify + CDBICompat::DestroyWarning + CDBICompat::Constructor + CDBICompat::AccessorMapping + CDBICompat::ColumnCase + CDBICompat::MightHave + CDBICompat::HasMany + CDBICompat::HasA + CDBICompat::LazyLoading + CDBICompat::AutoUpdate + CDBICompat::TempColumns + CDBICompat::ColumnGroups + CDBICompat::ImaDBI/); #DBIx::Class::CDBICompat::ObjIndexStubs 1; diff --git a/lib/DBIx/Class/CDBICompat/HasA.pm b/lib/DBIx/Class/CDBICompat/HasA.pm index 7f86b25..c4595c6 100644 --- a/lib/DBIx/Class/CDBICompat/HasA.pm +++ b/lib/DBIx/Class/CDBICompat/HasA.pm @@ -24,7 +24,7 @@ sub has_a { if $too_many; $self->add_relationship($col, $f_class, { "foreign.${pri}" => "self.${col}" }, - { _type => 'has_a', accessor => 'filter' } ); + { accessor => 'filter' } ); return 1; } diff --git a/lib/DBIx/Class/Core.pm b/lib/DBIx/Class/Core.pm index a3fb44e..7f36763 100644 --- a/lib/DBIx/Class/Core.pm +++ b/lib/DBIx/Class/Core.pm @@ -3,19 +3,22 @@ package DBIx::Class::Core; use strict; use warnings; -use base qw/DBIx::Class::Relationship::Accessor - DBIx::Class::Relationship::CascadeActions - DBIx::Class::Relationship::ProxyMethods - DBIx::Class::Relationship - DBIx::Class::InflateColumn - DBIx::Class::SQL::OrderBy - DBIx::Class::SQL::Abstract - DBIx::Class::PK - DBIx::Class::Table - DBIx::Class::SQL - DBIx::Class::DB - DBIx::Class::Exception - DBIx::Class::AccessorGroup/; +use base qw/DBIx::Class/; + +__PACKAGE__->load_components(qw/ + Relationship::Accessor + Relationship::CascadeActions + Relationship::ProxyMethods + Relationship + InflateColumn + SQL::OrderBy + SQL::Abstract + PK + Table + SQL + DB + Exception + AccessorGroup/); 1; @@ -26,7 +29,7 @@ DBIx::Class::Core - Core set of DBIx::Class modules. =head1 DESCRIPTION This class just inherits from the various modules that makes -up the Class::DBI core features. +up the DBIx::Class core features. =head1 AUTHORS diff --git a/lib/DBIx/Class/PK/Auto.pm b/lib/DBIx/Class/PK/Auto.pm index ff4ed69..f4ee81c 100644 --- a/lib/DBIx/Class/PK/Auto.pm +++ b/lib/DBIx/Class/PK/Auto.pm @@ -36,8 +36,8 @@ sub insert { if $too_many; unless (defined $self->get_column($pri)) { $self->throw( "Can't auto-inc for $pri on ".ref $self.": no _last_insert_id method" ) - unless $self->can('_last_insert_id'); - my $id = $self->_last_insert_id; + unless $self->can('last_insert_id'); + my $id = $self->last_insert_id; $self->throw( "Can't get last insert id" ) unless $id; $self->store_column($pri => $id); } diff --git a/lib/DBIx/Class/PK/Auto/SQLite.pm b/lib/DBIx/Class/PK/Auto/SQLite.pm index 9a526a5..5ddd2c0 100644 --- a/lib/DBIx/Class/PK/Auto/SQLite.pm +++ b/lib/DBIx/Class/PK/Auto/SQLite.pm @@ -3,7 +3,11 @@ package DBIx::Class::PK::Auto::SQLite; use strict; use warnings; -sub _last_insert_id { +use base qw/DBIx::Class/; + +__PACKAGE__->load_components(qw/PK::Auto/); + +sub last_insert_id { return $_[0]->_get_dbh->func('last_insert_rowid'); } diff --git a/lib/DBIx/Class/Table.pm b/lib/DBIx/Class/Table.pm index be871c0..71f41cb 100644 --- a/lib/DBIx/Class/Table.pm +++ b/lib/DBIx/Class/Table.pm @@ -373,17 +373,17 @@ sub copy { return $new->insert; } -sub _cond_resolve { - my ($self, $query, $attrs) = @_; - return '1 = 1' unless keys %$query; - my $op = $attrs->{'cmp'} || '='; - my $cond = join(' AND ', - map { (defined $query->{$_} - ? "$_ $op ?" - : (do { delete $query->{$_}; "$_ IS NULL"; })); - } keys %$query); - return ($cond, values %$query); -} +#sub _cond_resolve { +# my ($self, $query, $attrs) = @_; +# return '1 = 1' unless keys %$query; +# my $op = $attrs->{'cmp'} || '='; +# my $cond = join(' AND ', +# map { (defined $query->{$_} +# ? "$_ $op ?" +# : (do { delete $query->{$_}; "$_ IS NULL"; })); +# } keys %$query); +# return ($cond, values %$query); +#} =item table diff --git a/lib/DBIx/Class/Test/SQLite.pm b/lib/DBIx/Class/Test/SQLite.pm index 1ddacf8..b6b22fa 100644 --- a/lib/DBIx/Class/Test/SQLite.pm +++ b/lib/DBIx/Class/Test/SQLite.pm @@ -32,12 +32,15 @@ tie it to the class. use strict; -use base qw/DBIx::Class::CDBICompat DBIx::Class::PK::Auto::SQLite DBIx::Class::PK::Auto DBIx::Class::Core/; +use base qw/DBIx::Class/; + +__PACKAGE__->load_components(qw/CDBICompat PK::Auto::SQLite Core/); + use File::Temp qw/tempfile/; my (undef, $DB) = tempfile(); END { unlink $DB if -e $DB } -my @DSN = ("dbi:SQLite:dbname=$DB", '', '', { AutoCommit => 1 }); +my @DSN = ("dbi:SQLite:dbname=$DB", '', '', { AutoCommit => 1, RaiseError => 1 }); __PACKAGE__->connection(@DSN); __PACKAGE__->set_sql(_table_pragma => 'PRAGMA table_info(__TABLE__)'); diff --git a/t/10auto.t b/t/10auto.t index 4cf63c3..79d32dc 100644 --- a/t/10auto.t +++ b/t/10auto.t @@ -1,16 +1,12 @@ use Test::More; -use DBIx::Class::PK::Auto; -use DBIx::Class::PK::Auto::SQLite; - plan tests => 2; use lib qw(t/lib); use_ok('DBICTest'); -unshift(@DBICTest::Artist::ISA, qw/DBIx::Class::PK::Auto - DBIx::Class::PK::Auto::SQLite/); +DBICTest::Artist->load_components(qw/PK::Auto::SQLite/); # add an artist without primary key to test Auto my $artist = DBICTest::Artist->create( { name => 'Auto' } ); diff --git a/t/cdbi-t/01-columns.t b/t/cdbi-t/01-columns.t index 0841e1e..3380f52 100644 --- a/t/cdbi-t/01-columns.t +++ b/t/cdbi-t/01-columns.t @@ -8,6 +8,7 @@ use Test::More tests => 25; package State; use base 'DBIx::Class'; +State->load_components(qw/CDBICompat Core/); State->table('State'); State->columns(Essential => qw/Abbreviation Name/); @@ -34,6 +35,7 @@ sub Snowfall { 1 } package City; use base 'DBIx::Class'; +City->load_components(qw/CDBICompat Core/); City->table('City'); City->columns(All => qw/Name State Population/); @@ -43,6 +45,7 @@ City->has_a(State => 'State'); #------------------------------------------------------------------------- package CD; use base 'DBIx::Class'; +CD->load_components(qw/CDBICompat Core/); CD->table('CD'); CD->columns('All' => qw/artist title length/); @@ -101,8 +104,12 @@ ok(!State->find_column('HGLAGAGlAG'), '!find_column HGLAGAGlAG'); } { + package DieTest; + @DieTest::ISA = qw(DBIx::Class); + DieTest->load_components(qw/Core/); + package main; local $SIG{__WARN__} = sub { }; - eval { DBIx::Class->retrieve(1) }; + eval { DieTest->retrieve(1) }; like $@, qr/Can't retrieve unless primary columns are defined/, "Need primary key for retrieve"; } @@ -113,6 +120,7 @@ package State; package A; @A::ISA = qw(DBIx::Class); +__PACKAGE__->load_components(qw/CDBICompat Core/); __PACKAGE__->columns(Primary => 'id'); package A::B; diff --git a/t/cdbi-t/08-inheritcols.t b/t/cdbi-t/08-inheritcols.t index c23de9c..3e71a9e 100644 --- a/t/cdbi-t/08-inheritcols.t +++ b/t/cdbi-t/08-inheritcols.t @@ -7,6 +7,7 @@ use DBIx::Class; package A; @A::ISA = qw(DBIx::Class); +__PACKAGE__->load_components(qw/CDBICompat Core/); __PACKAGE__->columns(Primary => 'id'); package A::B; diff --git a/t/testlib/CDBase.pm b/t/testlib/CDBase.pm index 0a9ecc0..6cc4c6d 100644 --- a/t/testlib/CDBase.pm +++ b/t/testlib/CDBase.pm @@ -2,6 +2,7 @@ package CDBase; use strict; use base qw(DBIx::Class); +__PACKAGE__->load_components(qw/CDBICompat Core/); use File::Temp qw/tempfile/; my (undef, $DB) = tempfile();