|
From: Chris W. <la...@us...> - 2001-10-22 06:26:29
|
Update of /cvsroot/openinteract/SPOPS/SPOPS/DBI
In directory usw-pr-cvs1:/tmp/cvs-serv24578
Modified Files:
MySQL.pm
Log Message:
set defaults if asked
Index: MySQL.pm
===================================================================
RCS file: /cvsroot/openinteract/SPOPS/SPOPS/DBI/MySQL.pm,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -d -r1.11 -r1.12
*** MySQL.pm 2001/10/12 21:00:26 1.11
--- MySQL.pm 2001/10/22 06:26:26 1.12
***************
*** 5,8 ****
--- 5,9 ----
use strict;
use SPOPS qw( _w DEBUG );
+ use SPOPS::ClassFactory qw( OK NOTIFY );
use SPOPS::Key::DBI::HandleField;
***************
*** 25,28 ****
--- 26,66 ----
DEBUG() && _w( 1, "Setting to handle field: $item->CONFIG->{handle_field}" );
return SPOPS::Key::DBI::HandleField::post_fetch_id( $item, @args );
+ }
+
+
+ # Code generation behavior -- find defaults if asked
+
+ sub behavior_factory {
+ my ( $class ) = @_;
+ DEBUG() && _w( 1, "Installing MySQL default discovery for ($class)" );
+ return { manipulate_configuration => \&find_mysql_defaults };
+ }
+
+
+ sub find_mysql_defaults {
+ my ( $class ) = @_;
+ my $CONFIG = $class->CONFIG;
+ return ( OK, undef ) unless ( $CONFIG->{find_defaults} eq 'yes' );
+ my $dbh = $class->global_datasource_handle( $CONFIG->{datasource} );
+ unless ( $dbh ) {
+ return ( NOTIFY, "Cannot find defaults because no DBI database " .
+ "handle available to class ($class)" );
+ }
+
+ my $sql = "DESCRIBE $CONFIG->{base_table}";
+ my ( $sth );
+ eval {
+ $sth = $dbh->prepare( $sql );
+ $sth->execute;
+ };
+ if ( $@ ) {
+ return ( NOTIFY, "Cannot find defaults because there was an error " .
+ "executing ($sql): $@. Class: $class" );
+ }
+ while ( my $row = $sth->fetchrow_arrayref ) {
+ my $default = $row->[4];
+ next unless ( $default );
+ $CONFIG->{default_values}{ $row->[0] } = $default;
+ }
}
|