From: Matt S Trout Date: Fri, 20 Jan 2006 02:05:14 +0000 (+0000) Subject: Fixed up PK::Auto::* to use result_source, added connection, connect and clone method... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=66d9ef6bf10ccbdc9391e81e5b1ad1b615fb67f7;p=dbsrgits%2FDBIx-Class-Historic.git Fixed up PK::Auto::* to use result_source, added connection, connect and clone methods to Schema --- diff --git a/lib/DBIx/Class/DB.pm b/lib/DBIx/Class/DB.pm index cf96a37..99b4d33 100644 --- a/lib/DBIx/Class/DB.pm +++ b/lib/DBIx/Class/DB.pm @@ -25,7 +25,7 @@ sub resultset_instance { =head1 NAME -DBIx::Class::DB - Simple DBIx::Class Database connection by class inheritance +DBIx::Class::DB - Non-recommended classdata schema component =head1 SYNOPSIS @@ -45,7 +45,10 @@ DBIx::Class::DB - Simple DBIx::Class Database connection by class inheritance =head1 DESCRIPTION -This class provides a simple way of specifying a database connection. +This class is designed to support the Class::DBI connection-as-classdata style +for DBIx::Class. You are *strongly* recommended to use a DBIx::Class::Schema +instead; DBIx::Class::DB will continue to be supported but new development +will be focused on Schema-based DBIx::Class setups. =head1 METHODS @@ -75,9 +78,23 @@ instantiate the class dbh when required. sub connection { my ($class, @info) = @_; - my $storage = DBIx::Class::Storage::DBI->new; - $storage->connect_info(\@info); - my $schema = bless({ storage => $storage }, 'DBIx::Class::Schema'); + $class->setup_schema_instance unless $class->can('schema_instance'); + $class->schema_instance->connection(@info); +} + +=head2 setup_schema_instance + +Creates a class method ->schema_instance which contains a DBIx::Class::Schema; +all class-method operations are proxies through to this object. If you don't +call ->connection in your DBIx::Class::DB subclass at load time you *must* +call ->setup_schema_instance in order for subclasses to find the schema and +register themselves with it. + +=cut + +sub setup_schema_instance { + my $class = shift; + my $schema = bless({}, 'DBIx::Class::Schema'); $class->mk_classdata('schema_instance' => $schema); } diff --git a/lib/DBIx/Class/PK/Auto/DB2.pm b/lib/DBIx/Class/PK/Auto/DB2.pm index 29973d2..a9732d5 100644 --- a/lib/DBIx/Class/PK/Auto/DB2.pm +++ b/lib/DBIx/Class/PK/Auto/DB2.pm @@ -11,7 +11,7 @@ sub last_insert_id { my ($self) = @_; - my $dbh = $self->storage->dbh; + my $dbh = $self->result_source->storage->dbh; my $sth = $dbh->prepare_cached("VALUES(IDENTITY_VAL_LOCAL())"); $sth->execute(); diff --git a/lib/DBIx/Class/PK/Auto/MSSQL.pm b/lib/DBIx/Class/PK/Auto/MSSQL.pm index 3ae6287..4619998 100644 --- a/lib/DBIx/Class/PK/Auto/MSSQL.pm +++ b/lib/DBIx/Class/PK/Auto/MSSQL.pm @@ -8,7 +8,8 @@ use base qw/DBIx::Class/; __PACKAGE__->load_components(qw/PK::Auto/); sub last_insert_id { - my( $id ) = $_[0]->storage->dbh->selectrow_array( 'SELECT @@IDENTITY' ); + my( $id ) = $_[0]->result_source->storage->dbh->selectrow_array( + 'SELECT @@IDENTITY' ); return $id; } diff --git a/lib/DBIx/Class/PK/Auto/MySQL.pm b/lib/DBIx/Class/PK/Auto/MySQL.pm index 57b428d..59fb00f 100644 --- a/lib/DBIx/Class/PK/Auto/MySQL.pm +++ b/lib/DBIx/Class/PK/Auto/MySQL.pm @@ -8,7 +8,7 @@ use base qw/DBIx::Class/; __PACKAGE__->load_components(qw/PK::Auto/); sub last_insert_id { - return $_[0]->storage->dbh->{mysql_insertid}; + return $_[0]->result_source->storage->dbh->{mysql_insertid}; } 1; diff --git a/lib/DBIx/Class/PK/Auto/Oracle.pm b/lib/DBIx/Class/PK/Auto/Oracle.pm index 1b7a8e9..f1b318b 100644 --- a/lib/DBIx/Class/PK/Auto/Oracle.pm +++ b/lib/DBIx/Class/PK/Auto/Oracle.pm @@ -11,7 +11,7 @@ sub last_insert_id { my $self = shift; $self->get_autoinc_seq unless $self->{_autoinc_seq}; my $sql = "SELECT " . $self->{_autoinc_seq} . ".currval FROM DUAL"; - my ($id) = $self->storage->dbh->selectrow_array($sql); + my ($id) = $self->result_source->storage->dbh->selectrow_array($sql); return $id; } @@ -24,7 +24,7 @@ sub get_autoinc_seq { } # look up the correct sequence automatically - my $dbh = $self->storage->dbh; + my $dbh = $self->result_source->storage->dbh; my $sql = qq{ SELECT trigger_body FROM ALL_TRIGGERS t WHERE t.table_name = ? diff --git a/lib/DBIx/Class/PK/Auto/Pg.pm b/lib/DBIx/Class/PK/Auto/Pg.pm index 2cd2c41..4320433 100644 --- a/lib/DBIx/Class/PK/Auto/Pg.pm +++ b/lib/DBIx/Class/PK/Auto/Pg.pm @@ -10,7 +10,7 @@ __PACKAGE__->load_components(qw/PK::Auto/); sub last_insert_id { my $self = shift; $self->get_autoinc_seq unless $self->{_autoinc_seq}; - $self->storage->dbh->last_insert_id(undef,undef,undef,undef, + $self->result_source->storage->dbh->last_insert_id(undef,undef,undef,undef, {sequence=>$self->{_autoinc_seq}}); } diff --git a/lib/DBIx/Class/PK/Auto/SQLite.pm b/lib/DBIx/Class/PK/Auto/SQLite.pm index 8c83cff..f761bc0 100644 --- a/lib/DBIx/Class/PK/Auto/SQLite.pm +++ b/lib/DBIx/Class/PK/Auto/SQLite.pm @@ -8,7 +8,7 @@ use base qw/DBIx::Class/; __PACKAGE__->load_components(qw/PK::Auto/); sub last_insert_id { - return $_[0]->storage->dbh->func('last_insert_rowid'); + return $_[0]->result_source->storage->dbh->func('last_insert_rowid'); } 1; diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 53e06ef..ae6fb3e 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -481,16 +481,6 @@ sub find_or_create { return defined($exists) ? $exists : $self->create($hash); } -=head2 self - - my $rs = $rs->self; - -Just returns the resultset. Useful for Template Toolkit. - -=cut - -sub self { shift; } - =head1 ATTRIBUTES The resultset takes various attributes that modify its behavior. diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm index 8c3d288..d9971d4 100644 --- a/lib/DBIx/Class/Schema.pm +++ b/lib/DBIx/Class/Schema.pm @@ -2,7 +2,7 @@ package DBIx::Class::Schema; use strict; use warnings; -use DBIx::Class::DB; +use UNIVERSAL::require; use base qw/DBIx::Class/; @@ -61,9 +61,9 @@ particular which module inherits off which. =head2 register_class -Registers the class in the schema's class_registrations. This is a hash -containing database classes, keyed by their monikers. It's used by -compose_connection to create/modify all the existing database classes. +Registers a class which isa ResultSourceInstance; equivalent to calling + + $schema->register_source($moniker, $class->result_source_instance); =cut @@ -232,37 +232,35 @@ you expect. sub compose_connection { my ($self, $target, @info) = @_; - my $conn_class = "${target}::_db"; - $self->setup_connection_class($conn_class, @info); - my $schema = $self->compose_namespace($target, $conn_class); - $schema->storage($conn_class->storage); + my $base = 'DBIx::Class::ResultSetInstance'; + $base->require; + my $schema = $self->compose_namespace($target, $base); + $schema->connection(@info); foreach my $moniker ($schema->sources) { my $source = $schema->source($moniker); my $class = $source->result_class; #warn "$moniker $class $source ".$source->storage; $class->mk_classdata(result_source_instance => $source); $class->mk_classdata(resultset_instance => $source->resultset); + $class->mk_classdata(class_resolver => $schema); } return $schema; } sub compose_namespace { - my ($class, $target, $base) = @_; - my %reg = %{ $class->source_registrations }; + my ($self, $target, $base) = @_; + my %reg = %{ $self->source_registrations }; my %target; my %map; - my $schema = bless({ }, $class); - while (my ($moniker, $source) = each %reg) { + my $schema = $self->clone; + foreach my $moniker ($schema->sources) { + my $source = $schema->source($moniker); my $target_class = "${target}::${moniker}"; - $class->inject_base( + $self->inject_base( $target_class => $source->result_class, ($base ? $base : ()) ); - my $new_source = $source->new($source); - $new_source->result_class($target_class); - $new_source->schema($schema); - $map{$moniker} = $new_source; + $source->result_class($target_class); } - $schema->source_registrations(\%map); { no strict 'refs'; *{"${target}::schema"} = @@ -272,7 +270,6 @@ sub compose_namespace { sub { shift->schema->$meth(@_) }; } } - $base->class_resolver($target); return $schema; } @@ -290,6 +287,50 @@ sub setup_connection_class { $target->connection(@info); } +=head2 connection(@args) + +Instantiates a new Storage object of type storage_type and passes the +arguments to $storage->connection_info. Sets the connection in-place on +the schema. + +=cut + +sub connection { + my ($self, @info) = @_; + my $storage_class = 'DBIx::Class::Storage::'.$self->storage_type; + $storage_class->require; + my $storage = $storage_class->new; + $storage->connect_info(\@info); + $self->storage($storage); + return $self; +} + +=head2 connect(@info) + +Conveneience method, equivalent to $schema->clone->connection(@info) + +=cut + +sub connect { shift->clone->connection(@_) }; + +=head2 clone + +Clones the schema and its associated result_source objects and returns the +copy. + +=cut + +sub clone { + my ($self) = @_; + my $clone = bless({ (ref $self ? %$self : ()) }, ref $self || $self); + foreach my $moniker ($self->sources) { + my $source = $self->source($moniker); + my $new = $source->new($source); + $clone->register_source($moniker => $new); + } + return $clone; +} + 1; =head1 AUTHORS diff --git a/t/19quotes.t b/t/19quotes.t index 79d02ee..69ba5b5 100644 --- a/t/19quotes.t +++ b/t/19quotes.t @@ -15,8 +15,8 @@ use_ok('DBICTest'); use_ok('DBICTest::HelperRels'); -DBICTest::_db->storage->sql_maker->{'quote_char'} = q!'!; -DBICTest::_db->storage->sql_maker->{'name_sep'} = '.'; +DBICTest->schema->storage->sql_maker->{'quote_char'} = q!'!; +DBICTest->schema->storage->sql_maker->{'name_sep'} = '.'; my $rs = DBICTest::CD->search( { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },