| 1 | package SPOPS::DBI::MySQL; | ||
| 2 | |||
| 3 | # $Id: MySQL.pm,v 3.5 2004/06/02 00:48:22 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use Log::Log4perl qw( get_logger ); | ||
| 7 | use SPOPS; | ||
| 8 | use SPOPS::ClassFactory qw( OK NOTIFY ); | ||
| 9 | use SPOPS::Key::DBI::HandleField; | ||
| 10 | |||
| 11 | my $log = get_logger(); | ||
| 12 | |||
| 13 | $SPOPS::DBI::MySQL::VERSION = sprintf("%d.%02d", q$Revision: 3.5 $ =~ /(\d+)\.(\d+)/); | ||
| 14 | |||
| 15 | sub sql_current_date { return 'NOW()' } | ||
| 16 | |||
| 17 | # Backward compatibility (basically) -- you just have to set a true | ||
| 18 | # value in the config if you have an auto-increment field in the | ||
| 19 | # table. If so we call the post_fetch_id method from | ||
| 20 | # SPOPS::Key::DBI::HandleField. | ||
| 21 | |||
| 22 | sub post_fetch_id { | ||
| 23 | my ( $item, @args ) = @_; | ||
| 24 | return undef unless ( $item->CONFIG->{increment_field} ); | ||
| 25 | $item->CONFIG->{handle_field} ||= 'mysql_insertid'; | ||
| 26 | $log->is_info && | ||
| 27 | $log->info( "Setting to handle field: $item->CONFIG->{handle_field}" ); | ||
| 28 | return SPOPS::Key::DBI::HandleField::post_fetch_id( $item, @args ); | ||
| 29 | } | ||
| 30 | |||
| 31 | |||
| 32 | # Code generation behavior -- find defaults if asked | ||
| 33 | |||
| 34 | sub behavior_factory { | ||
| 35 | my ( $class ) = @_; | ||
| 36 | $log->is_info && | ||
| 37 | $log->info( "Installing MySQL default discovery for ($class)" ); | ||
| 38 | return { manipulate_configuration => \&find_mysql_defaults }; | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | sub find_mysql_defaults { | ||
| 43 | my ( $class ) = @_; | ||
| 44 | my $CONFIG = $class->CONFIG; | ||
| 45 | return ( OK, undef ) unless ( $CONFIG->{find_defaults} and $CONFIG->{find_defaults} eq 'yes' ); | ||
| 46 | my $dbh = $class->global_datasource_handle( $CONFIG->{datasource} ); | ||
| 47 | unless ( $dbh ) { | ||
| 48 | return ( NOTIFY, "Cannot find defaults because no DBI database " . | ||
| 49 | "handle available to class ($class)" ); | ||
| 50 | } | ||
| 51 | |||
| 52 | my $sql = "DESCRIBE $CONFIG->{base_table}"; | ||
| 53 | my ( $sth ); | ||
| 54 | eval { | ||
| 55 | $sth = $dbh->prepare( $sql ); | ||
| 56 | $sth->execute; | ||
| 57 | }; | ||
| 58 | if ( $@ ) { | ||
| 59 | return ( NOTIFY, "Cannot find defaults because there was an error " . | ||
| 60 | "executing ($sql): $@. Class: $class" ); | ||
| 61 | } | ||
| 62 | while ( my $row = $sth->fetchrow_arrayref ) { | ||
| 63 | my $default = $row->[4]; | ||
| 64 | next unless ( $default ); | ||
| 65 | $CONFIG->{default_values}{ $row->[0] } = $default; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | 1; | ||
| 70 | |||
| 71 | __END__ |