| 1 | package OpenInteract::SPOPS::DBI; | ||
| 2 | |||
| 3 | # $Id: DBI.pm,v 1.12 2002/01/02 02:43:53 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use OpenInteract::SPOPS; | ||
| 7 | use SPOPS::ClassFactory qw( OK NOTIFY ); | ||
| 8 | |||
| 9 | @OpenInteract::SPOPS::DBI::ISA = qw( OpenInteract::SPOPS ); | ||
| 10 | $OpenInteract::SPOPS::DBI::VERSION = sprintf("%d.%02d", q$Revision: 1.12 $ =~ /(\d+)\.(\d+)/); | ||
| 11 | |||
| 12 | sub global_datasource_handle { | ||
| 13 | my ( $self, $connect_key ) = @_; | ||
| 14 | my $R = OpenInteract::Request->instance; | ||
| 15 | $connect_key ||= $self->CONFIG->{datasource} || | ||
| 16 | $R->CONFIG->{datasource}{default_connection_db}; | ||
| 17 | return $R->db( $connect_key ); | ||
| 18 | } | ||
| 19 | |||
| 20 | sub global_db_handle { goto &global_datasource_handle } | ||
| 21 | |||
| 22 | sub connection_info { | ||
| 23 | my ( $self, $connect_key ) = @_; | ||
| 24 | my $R = OpenInteract::Request->instance; | ||
| 25 | $connect_key ||= $self->CONFIG->{datasource} || | ||
| 26 | $R->CONFIG->{datasource}{default_connection_db} || | ||
| 27 | $R->CONFIG->{default_connection_db}; | ||
| 28 | $connect_key = $connect_key->[0] if ( ref $connect_key eq 'ARRAY' ); | ||
| 29 | return \%{ $self->CONFIG->{db_info}->{ $connect_key } }; | ||
| 30 | } | ||
| 31 | |||
| 32 | ######################################## | ||
| 33 | # CLASS FACTORY BEHAVIOR | ||
| 34 | ######################################## | ||
| 35 | |||
| 36 | sub behavior_factory { | ||
| 37 | my ( $class ) = @_; | ||
| 38 | return { manipulate_configuration => \&discover_fields }; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub discover_fields { | ||
| 42 | my ( $class ) = @_; | ||
| 43 | my $CONFIG = $class->CONFIG; | ||
| 44 | unless ( $CONFIG->{field_discover} and $CONFIG->{field_discover} eq 'yes' ) { | ||
| 45 | return ( OK, undef ); | ||
| 46 | } | ||
| 47 | |||
| 48 | my $dbh = $class->global_datasource_handle( $CONFIG->{datasource} ); | ||
| 49 | unless ( $dbh ) { | ||
| 50 | return ( NOTIFY, "Cannot discover fields because no DBI database " . | ||
| 51 | "handle available to class ($class)" ); | ||
| 52 | } | ||
| 53 | my $sql = $class->sql_fetch_types( $CONFIG->{base_table} ); | ||
| 54 | my ( $sth ); | ||
| 55 | eval { | ||
| 56 | $sth = $dbh->prepare( $sql ); | ||
| 57 | $sth->execute; | ||
| 58 | }; | ||
| 59 | return ( NOTIFY, "Cannot discover fields\n -> $sql\n -> $@" ) if ( $@ ); | ||
| 60 | $CONFIG->{field} = $sth->{NAME}; | ||
| 61 | return ( OK, undef ); | ||
| 62 | } | ||
| 63 | |||
| 64 | 1; | ||
| 65 | |||
| 66 | =pod | ||
| 67 | |||
| 68 | =head1 NAME | ||
| 69 | |||
| 70 | OpenInteract::SPOPS::DBI - Common SPOPS::DBI-specific methods for objects | ||
| 71 | |||
| 72 | =head1 SYNOPSIS | ||
| 73 | |||
| 74 | # In configuration file | ||
| 75 | 'myobj' => { | ||
| 76 | 'isa' => [ qw/ ... OpenInteract::SPOPS::DBI ... / ], | ||
| 77 | |||
| 78 | # Yes, I want OI to find my fields for me. | ||
| 79 | 'field_discover' => 'yes', | ||
| 80 | } | ||
| 81 | |||
| 82 | =head1 DESCRIPTION | ||
| 83 | |||
| 84 | This class provides common datasource access methods required by | ||
| 85 | L<SPOPS::DBI|SPOPS::DBI>. | ||
| 86 | |||
| 87 | =head1 METHODS | ||
| 88 | |||
| 89 | B<global_datasource_handle( [ $connect_key ] )> | ||
| 90 | |||
| 91 | Returns a DBI handle corresponding to the connection key | ||
| 92 | C<$connect_key>. If C<$connect_key> is not given, then the connection | ||
| 93 | key specified for the object class is used. If the object class does | ||
| 94 | not have a connection key (which is normal if you are using only one | ||
| 95 | database), we use the key specified in the server configuration file | ||
| 96 | in 'default_connection_db'. | ||
| 97 | |||
| 98 | B<global_db_handle( [ $connect_key ] )> | ||
| 99 | |||
| 100 | Alias for C<global_datasource_handle()> (kept for backward | ||
| 101 | compatibility). | ||
| 102 | |||
| 103 | B<connection_info( [ $connect_key ] )> | ||
| 104 | |||
| 105 | Returns a hashref of DBI connection information. If no C<$connect_key> | ||
| 106 | is given then we get the value of 'datasource' from the object | ||
| 107 | configuration, and if that is not defined we get the default | ||
| 108 | datasource from the server configuration. | ||
| 109 | |||
| 110 | See the server configuration file for documentation on what is in the | ||
| 111 | hashref. | ||
| 112 | |||
| 113 | =head2 SPOPS::ClassFactory Methods | ||
| 114 | |||
| 115 | You will never need to call the following methods from your object, | ||
| 116 | but you should be aware of them. | ||
| 117 | |||
| 118 | B<behavior_factory( $class )> | ||
| 119 | |||
| 120 | Creates the 'discover_fields' behavior (see below) in the | ||
| 121 | 'manipulate_configuration' slot of the | ||
| 122 | L<SPOPS::ClassFactory|SPOPS::ClassFactory> process. | ||
| 123 | |||
| 124 | B<discover_fields( $class )> | ||
| 125 | |||
| 126 | If 'field_discover' is set to 'yes' in your class configuration, this | ||
| 127 | will find the fields in your database table and set the configuration | ||
| 128 | value 'field' as appropriate. Pragmatically, this means you do not | ||
| 129 | have to list your fields in your class configuration -- every time the | ||
| 130 | server starts up the class interrogates the table for its properties. | ||
| 131 | |||
| 132 | =head1 BUGS | ||
| 133 | |||
| 134 | None known. | ||
| 135 | |||
| 136 | =head1 TO DO | ||
| 137 | |||
| 138 | Nothing known. | ||
| 139 | |||
| 140 | =head1 SEE ALSO | ||
| 141 | |||
| 142 | L<SPOPS::DBI|SPOPS::DBI> | ||
| 143 | |||
| 144 | L<SPOPS::ClassFactory|SPOPS::ClassFactory> | ||
| 145 | |||
| 146 | L<SPOPS::Manual::CodeGeneration|SPOPS::Manual::CodeGeneration> | ||
| 147 | |||
| 148 | =head1 COPYRIGHT | ||
| 149 | |||
| 150 | Copyright (c) 2001-2002 intes.net, inc.. All rights reserved. | ||
| 151 | |||
| 152 | This library is free software; you can redistribute it and/or modify | ||
| 153 | it under the same terms as Perl itself. | ||
| 154 | |||
| 155 | =head1 AUTHORS | ||
| 156 | |||
| 157 | Chris Winters <chris@cwinters.com> | ||
| 158 | |||
| 159 | =cut |