From: Matt S Trout Date: Tue, 24 Jan 2006 18:27:41 +0000 (+0000) Subject: on_connect_do patch from abraxxa X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d7c4c15c988b323f0add0149a3b7858fcfa7f5a8;p=dbsrgits%2FDBIx-Class-Historic.git on_connect_do patch from abraxxa --- diff --git a/lib/DBIx/Class.pm b/lib/DBIx/Class.pm index 5f19714..b59710a 100644 --- a/lib/DBIx/Class.pm +++ b/lib/DBIx/Class.pm @@ -135,6 +135,8 @@ Todd Lipcon Daniel Westermann-Clark +Alexander Hartmaier + =head1 LICENSE You may distribute this code under the same terms as Perl itself. diff --git a/lib/DBIx/Class/Manual/SchemaIntro.pod b/lib/DBIx/Class/Manual/SchemaIntro.pod index 02ac2e6..3ffaf85 100644 --- a/lib/DBIx/Class/Manual/SchemaIntro.pod +++ b/lib/DBIx/Class/Manual/SchemaIntro.pod @@ -93,6 +93,10 @@ a second database you want to access: Note that L does not cache connnections for you. If you use multiple connections, you need to do this manually. +To execute some sql statements on every connect you can pass them to your schema after the connect: + + $schema->storage->on_connect_do(\@on_connect_sql_statments); + The simplest way to get a record is by primary key: my $schema = My::Schema->connect( ... ); diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index 53c5f37..4291c8b 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -138,7 +138,7 @@ use base qw/DBIx::Class/; __PACKAGE__->load_components(qw/Exception AccessorGroup/); __PACKAGE__->mk_group_accessors('simple' => - qw/connect_info _dbh _sql_maker debug cursor/); + qw/connect_info _dbh _sql_maker debug cursor on_connect_do/); our $TRANSACTION = 0; @@ -163,6 +163,12 @@ This class represents the connection to the database =cut +=head2 on_connect_do + +Executes the sql statements given as a listref on every db connect. + +=cut + sub dbh { my ($self) = @_; my $dbh; @@ -184,6 +190,11 @@ sub _populate_dbh { my ($self) = @_; my @info = @{$self->connect_info || []}; $self->_dbh($self->_connect(@info)); + + # if on-connect sql statements are given execute them + foreach my $sql_statement (@{$self->on_connect_do || []}) { + $self->_dbh->do($sql_statement); + } } sub _connect {