[Dbix-class] [patch] on_connect_do

Hartmaier Alexander Alexander.Hartmaier at t-systems.at
Mon Jan 23 18:48:24 CET 2006


For the usage look at the docs (Manual::SchemaIntro).

Tests are missing, if anyone knows which can be set for sqlite and checked 
later please do a patch or talk to me on irc.perl.org #dbix-class!

*snip*
Index: lib/DBIx/Class/Storage/DBI.pm
===================================================================
--- lib/DBIx/Class/Storage/DBI.pm       (revision 556)
+++ lib/DBIx/Class/Storage/DBI.pm       (working copy)
@@ -138,7 +138,7 @@
 __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 @@

 =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 @@
   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 {
Index: lib/DBIx/Class/Manual/SchemaIntro.pod
===================================================================
--- lib/DBIx/Class/Manual/SchemaIntro.pod       (revision 556)
+++ lib/DBIx/Class/Manual/SchemaIntro.pod       (working copy)
@@ -93,6 +93,10 @@
 Note that L<DBIx::Class::Schema> 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( ... );
Index: README
===================================================================
--- README      (revision 556)
+++ README      (working copy)
@@ -71,6 +71,8 @@
     Matt S. Trout <mst at shadowcatsystems.co.uk>

 CONTRIBUTORS
+    Alexander Hartmaier <alex_hartmaier at hotmail.com>
+
     Andy Grundman <andy at hybridized.org>

     Brian Cassidy <bricas at cpan.org>
*snip*


-Alex


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 5544 bytes
Desc: not available
Url : http://lists.rawmode.org/pipermail/dbix-class/attachments/20060123/7fbef774/smime.bin


More information about the Dbix-class mailing list