matching is done by assuming the package name of the ResultSet class
is the same as that of the Result class.
-You will be warned if ResulSet classes are discovered for which there
+You will be warned if ResultSet classes are discovered for which there
are no matching Result classes like this:
load_namespaces found ResultSet class $classname with no corresponding Result class
return $name;
}
+# Finds all modules in the supplied namespace, or if omitted in the
+# namespace of $class. Untaints all findings as they can be assumed
+# to be safe
+sub _findallmod {
+ my $proto = shift;
+ my $ns = shift || ref $proto || $proto;
+
+ my @mods = Module::Find::findallmod($ns);
+
+ # try to untaint module names. mods where this fails
+ # are left alone so we don't have to change the old behavior
+ no locale; # localized \w doesn't untaint expression
+ return map { $_ =~ m/^( (?:\w+::)* \w+ )$/x ? $1 : $_ } @mods;
+}
+
# returns a hash of $shortname => $fullname for every package
# found in the given namespaces ($shortname is with the $fullname's
# namespace stripped off)
push(
@results_hash,
map { (substr($_, length "${namespace}::"), $_) }
- Module::Find::findallmod($namespace)
+ $class->_findallmod($namespace)
);
}
local *Class::C3::reinitialize = sub { };
use warnings 'redefine';
- my %done;
- foreach my $result (keys %results) {
+ # ensure classes are loaded and fetch properly sorted classes
+ $class->ensure_class_loaded($_) foreach(values %results);
+ my @subclass_last = sort { $results{$a}->isa($results{$b}) } keys(%results);
+
+ foreach my $result (@subclass_last) {
my $result_class = $results{$result};
- $class->ensure_class_loaded($result_class);
- $result_class->source_name($result) unless $result_class->source_name;
my $rs_class = delete $resultsets{$result};
my $rs_set = $result_class->resultset_class;
- $done{$rs_set} = $rs_set unless exists $done{$rs_set};
+
if($rs_set && $rs_set ne 'DBIx::Class::ResultSet') {
- if($rs_class && !exists($done{$rs_set}) && $rs_class ne $rs_set) {
+ if($rs_class && $rs_class ne $rs_set) {
warn "We found ResultSet class '$rs_class' for '$result', but it seems "
. "that you had already set '$result' to use '$rs_set' instead";
}
$result_class->resultset_class($rs_class);
}
- push(@to_register, [ $result_class->source_name, $result_class ]);
+ my $source_name = $result_class->source_name || $result;
+
+ push(@to_register, [ $source_name, $result_class ]);
}
}
}
} else {
my @comp = map { substr $_, length "${class}::" }
- Module::Find::findallmod($class);
+ $class->_findallmod;
$comps_for{$class} = \@comp;
}
foreach my $prefix (keys %comps_for) {
foreach my $comp (@{$comps_for{$prefix}||[]}) {
my $comp_class = "${prefix}::${comp}";
- { # try to untaint module name. mods where this fails
- # are left alone so we don't have to change the old behavior
- no locale; # localized \w doesn't untaint expression
- if ( $comp_class =~ m/^( (?:\w+::)* \w+ )$/x ) {
- $comp_class = $1;
- }
- }
$class->ensure_class_loaded($comp_class);
my $snsub = $comp_class->can('source_name');
For an example of what you can do with this, see
L<DBIx::Class::Manual::Cookbook/Adding Indexes And Functions To Your SQL>.
+Note that sqlt_deploy_hook is called by L</deployment_statements>, which in turn
+is called before L</deploy>. Therefore the hook can be used only to manipulate
+the L<SQL::Translator::Schema> object before it is turned into SQL fed to the
+database. If you want to execute post-deploy statements which can not be generated
+by L<SQL::Translator>, the currently suggested method is to overload L</deploy>
+and use L<dbh_do|DBIx::Class::Storage::DBI/dbh_do>.
+
=head1 METHODS
=head2 connect
C<connect> does not. C<connect> wraps it's arguments in an arrayref
before passing them to C<connect_info>.
+=head3 Overloading
+
+C<connect> is a convenience method. It is equivalent to calling
+$schema->clone->connection(@connectinfo). To write your own overloaded
+version, overload L</connection> instead.
+
=cut
sub connect { shift->clone->connection(@_) }
This interface is preferred over using the individual methods L</txn_begin>,
L</txn_commit>, and L</txn_rollback> below.
+WARNING: If you are connected with C<AutoCommit => 0> the transaction is
+considered nested, and you will still need to call L</txn_commit> to write your
+changes when appropriate. You will also want to connect with C<auto_savepoint =>
+1> to get partial rollback to work, if the storage driver for your database
+supports it.
+
+Connecting with C<AutoCommit => 1> is recommended.
+
=cut
sub txn_do {
$self->storage->txn_do(@_);
}
-=head2 txn_scope_guard (EXPERIMENTAL)
+=head2 txn_scope_guard
Runs C<txn_scope_guard> on the schema's storage. See
L<DBIx::Class::Storage/txn_scope_guard>.
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
data in-place on the Schema class. You should probably be calling
L</connect> to get a proper Schema object instead.
+=head3 Overloading
+
+Overload C<connection> to change the behaviour of C<connect>.
=cut
See L<SQL::Translator/METHODS> for a list of values for C<$sqlt_args>. The most
common value for this would be C<< { add_drop_table => 1, } >> to have the SQL
-produced include a DROP TABLE statement for each table created.
+produced include a DROP TABLE statement for each table created. For quoting
+purposes use C<producer_options> value with C<quote_table_names> and
+C<quote_field_names>.
Additionally, the DBIx::Class parser accepts a C<sources> parameter as a hash
ref or an array ref, containing a list of source to deploy. If present, then
sub _register_source {
my ($self, $moniker, $source, $params) = @_;
- %$source = %{ $source->new( { %$source, source_name => $moniker }) };
+ my $orig_source = $source;
+
+ $source = $source->new({ %$source, source_name => $moniker });
+ $source->schema($self);
+ weaken($source->{schema}) if ref($self);
+
+ my $rs_class = $source->result_class;
+
my %reg = %{$self->source_registrations};
$reg{$moniker} = $source;
$self->source_registrations(\%reg);
- $source->schema($self);
- weaken($source->{schema}) if ref($self);
return if ($params->{extra});
- if ($source->result_class) {
- my %map = %{$self->class_mappings};
- if (exists $map{$source->result_class}) {
- warn $source->result_class . ' already has a source, use register_extra_source for additional sources';
- }
- $map{$source->result_class} = $moniker;
- $self->class_mappings(\%map);
+ my %map = %{$self->class_mappings};
+ if (
+ exists $map{$rs_class}
+ and
+ $map{$rs_class} ne $moniker
+ and
+ $rs_class->result_source_instance ne $orig_source
+ ) {
+ carp "$rs_class already has a source, use register_extra_source for additional sources";
}
+ $map{$rs_class} = $moniker;
+ $self->class_mappings(\%map);
}
sub _unregister_source {