|
From: Chris W. <la...@us...> - 2001-10-28 02:40:13
|
Update of /cvsroot/openinteract/OpenInteract/OpenInteract/SPOPS
In directory usw-pr-cvs1:/tmp/cvs-serv9816/OpenInteract/SPOPS
Modified Files:
DBI.pm
Log Message:
add (optional) field discovery to OI SPOPS DBI objects
Index: DBI.pm
===================================================================
RCS file: /cvsroot/openinteract/OpenInteract/OpenInteract/SPOPS/DBI.pm,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** DBI.pm 2001/10/25 15:25:14 1.8
--- DBI.pm 2001/10/28 02:40:09 1.9
***************
*** 5,8 ****
--- 5,9 ----
use strict;
use OpenInteract::SPOPS;
+ use SPOPS::ClassFactory qw( OK NOTIFY );
@OpenInteract::SPOPS::DBI::ISA = qw( OpenInteract::SPOPS );
***************
*** 29,32 ****
--- 30,65 ----
}
+ ########################################
+ # CLASS FACTORY BEHAVIOR
+ ########################################
+
+ sub behavior_factory {
+ my ( $class ) = @_;
+ DEBUG() && _w( 1, "Installing field discovery for ($class)" );
+ return { manipulate_configuration => \&discover_fields };
+ }
+
+ sub discover_fields {
+ my ( $class ) = @_;
+ my $CONFIG = $class->CONFIG;
+ return ( OK, undef ) unless ( $CONFIG->{field_discover} eq 'yes' );
+ my $dbh = $class->global_datasource_handle( $CONFIG->{datasource} );
+ unless ( $dbh ) {
+ return ( NOTIFY, "Cannot discover fields because no DBI database " .
+ "handle available to class ($class)" );
+ }
+ my $sql = $class->sql_fetch_types( $CONFIG->{base_table} );
+ my ( $sth );
+ eval {
+ $sth = $dbh->prepare( $sql );
+ $sth->execute;
+ };
+ return ( NOTIFY, "Cannot discover fields: $@" ) if ( $@ );
+ $CONFIG->{field} = $sth->{NAME};
+ DEBUG() && _w( 1, "Table: ($CONFIG->{base_table}); ",
+ "Fields: (", join( ', ', @{ $CONFIG->{field} } ), ")" );
+ return ( OK, undef );
+ }
+
1;
***************
*** 42,45 ****
--- 75,81 ----
'myobj' => {
'isa' => [ qw/ ... OpenInteract::SPOPS::DBI ... / ],
+
+ # Yes, I want OI to find my fields for me.
+ 'field_discover' => 'yes',
}
***************
*** 75,78 ****
--- 111,133 ----
hashref.
+ =head2 SPOPS::ClassFactory Methods
+
+ You will never need to call the following methods from your object,
+ but you should be aware of them.
+
+ B<behavior_factory( $class )>
+
+ Creates the 'discover_fields' behavior (see below) in the
+ 'manipulate_configuration' slot of the
+ L<SPOPS::ClassFactory|SPOPS::ClassFactory> process.
+
+ B<discover_fields( $class )>
+
+ If 'field_discover' is set to 'yes' in your class configuration, this
+ will find the fields in your database table and set the configuration
+ value 'field' as appropriate. Pragmatically, this means you do not
+ have to list your fields in your class configuration -- every time the
+ server starts up the class interrogates the table for its properties.
+
=head1 BUGS
***************
*** 86,89 ****
--- 141,148 ----
L<SPOPS::DBI|SPOPS::DBI>
+
+ L<SPOPS::ClassFactory|SPOPS::ClassFactory>
+
+ L<SPOPS::Manual::CodeGeneration|SPOPS::Manual::CodeGeneration>
=head1 COPYRIGHT
|