From: Chris W. <la...@us...> - 2005-10-18 21:21:38
|
Update of /cvsroot/openinteract/OpenInteract2/lib/OpenInteract2/Datasource In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1923 Modified Files: DBI.pm Log Message: OIN-191: data are scrubbed before connect() gets it, so modify resolve_datasource_info() to capture the driver-specific parameters (that's what happens when you don't work with the code for a while...) Index: DBI.pm =================================================================== RCS file: /cvsroot/openinteract/OpenInteract2/lib/OpenInteract2/Datasource/DBI.pm,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** DBI.pm 18 Oct 2005 01:43:00 -0000 1.19 --- DBI.pm 18 Oct 2005 21:21:30 -0000 1.20 *************** *** 19,22 **** --- 19,26 ---- my @CONNECT_ATTRIBUTES = qw( RootClass ); + # Note that the \%ds_info has already been scrubbed by + # resolve_datasource_info() at server startup (see + # OI2::Setup::CheckDatasources) + sub connect { my ( $class, $ds_name, $ds_info ) = @_; *************** *** 31,38 **** 'Continuing...' ); } ! ! my $full = $class->resolve_datasource_info( $ds_name, $ds_info ); ! ! unless ( $full->{driver_name} ) { $log->error( "Required configuration key undefined ", "'datasource.$ds_name.driver_name'" ); --- 35,40 ---- 'Continuing...' ); } ! ! unless ( $ds_info->{driver_name} ) { $log->error( "Required configuration key undefined ", "'datasource.$ds_name.driver_name'" ); *************** *** 44,53 **** # it happens ! my $dsn = join( ':', 'DBI', $full->{driver_name}, $full->{dsn} ); ! my $username = $full->{username}; ! my $password = $full->{password}; ! my %connect_attr = map { $_ => $full->{ $_ } } ! grep { defined $full->{ $_ } } @CONNECT_ATTRIBUTES; $connect_attr{RaiseError} = 0; --- 46,55 ---- # it happens ! my $dsn = join( ':', 'DBI', $ds_info->{driver_name}, $ds_info->{dsn} ); ! my $username = $ds_info->{username}; ! my $password = $ds_info->{password}; ! my %connect_attr = map { $_ => $ds_info->{ $_ } } ! grep { defined $ds_info->{ $_ } } @CONNECT_ATTRIBUTES; $connect_attr{RaiseError} = 0; *************** *** 55,59 **** if ( $log->is_debug ) { ! my %dumpable = %{ $full }; $dumpable{password} = '*' x length $password; $log->debug( "Trying to connect to DBI with: ", --- 57,61 ---- if ( $log->is_debug ) { ! my %dumpable = %{ $ds_info }; $dumpable{password} = '*' x length $password; $log->debug( "Trying to connect to DBI with: ", *************** *** 78,94 **** $db->{ChopBlanks} = 1; $db->{AutoCommit} = 1; ! $db->{LongReadLen} = $full->{long_read_len} || DEFAULT_READ_LEN; ! $db->{LongTruncOk} = $full->{long_trunc_ok} || DEFAULT_TRUNC_OK; ! my $lc_driver = lc $full->{driver_name}; ! foreach my $attrib ( keys %{ $full } ) { ! next unless ( $attrib =~ /^$lc_driver/ ); ! $db->{ $attrib } = $full->{ $attrib }; $log->is_debug && $log->debug( "Assigning driver-specific attribute to db ", ! "handle: $attrib -> $full->{ $attrib }" ); } ! my $trace_level = $full->{trace_level} || '0'; $db->trace( $trace_level ); --- 80,94 ---- $db->{ChopBlanks} = 1; $db->{AutoCommit} = 1; ! $db->{LongReadLen} = $ds_info->{long_read_len} || DEFAULT_READ_LEN; ! $db->{LongTruncOk} = $ds_info->{long_trunc_ok} || DEFAULT_TRUNC_OK; ! while ( my ( $attrib, $value ) = each %{ $ds_info->{driver_attributes} } ) { ! $db->{ $attrib } = $value; $log->is_debug && $log->debug( "Assigning driver-specific attribute to db ", ! "handle: $attrib -> $value" ); } ! my $trace_level = $ds_info->{trace_level} || '0'; $db->trace( $trace_level ); *************** *** 126,129 **** --- 126,130 ---- my ( $self, $name, $ds_info ) = @_; + # TODO: keep this? it means we're not copying over handle attribs... # backwards compatibility - if 'spops' key exists just return as-is if ( $ds_info->{spops} ) { *************** *** 148,151 **** --- 149,160 ---- ? 'ODBC' : $dbi_info->[1]; } + my $lc_driver = lc $info{driver_name}; + my %driver_attribs = (); + foreach my $attrib ( keys %{ $ds_info } ) { + if ( $attrib =~ /^$lc_driver/ ) { + $driver_attribs{ $attrib } = $ds_info->{ $attrib }; + } + } + $info{driver_attributes} = \%driver_attribs; return \%info; } *************** *** 221,225 **** The parameter C<\%datasource_info> defines how we connect to the database and is pulled from your 'datasource.$name' server ! configuration. =over 4 --- 230,237 ---- The parameter C<\%datasource_info> defines how we connect to the database and is pulled from your 'datasource.$name' server ! configuration. But by the time this method gets the data it's already ! been scrubbed by the C<resolve_datasource_info()> method since that ! method is invoked at server startup by ! L<OpenInteract2::Setup::CheckDatasources>. =over 4 *************** *** 310,331 **** =back - You may also define driver-specific parameters that get passed through - to the database handle. A parameter is identified as driver-specific - if it beginns with the driver name. So if we were using L<DBD::Pg> we - might do: - - [datasource main] - type = DBI - dbi_type = Pg - dsn = dbname=oi2 - username = oi2 - password = oi2 - pg_server_prepare = 0 - - which is equivalent to: - - my $dbh = DBI->connect( ... ); - $dbh->{pg_server_prepare} = 0; - Any errors encountered will throw an exception, usually of the L<OpenInteract2::Exception::Datasource> variety. --- 322,325 ---- *************** *** 333,337 **** B<resolve_datasource_info( $name, \%datasource_info )> ! Internal method used to resolve some shortcuts we allow for usability. This will look at the 'dbi_type' and add keys to the datasource information: --- 327,332 ---- B<resolve_datasource_info( $name, \%datasource_info )> ! Internal method called by L<OpenInteract2::Setup::CheckDatasources> at ! server startup, used to resolve some shortcuts we allow for usability. This will look at the 'dbi_type' and add keys to the datasource information: *************** *** 350,353 **** --- 345,367 ---- =back + You may also define driver-specific parameters that get passed through + to the C<connect()> method in the key 'driver_attributes'; eventually + these get assigned directly to the database handle just after it's + created. A parameter is identified as driver-specific if it begins + with the driver name. So if we were using L<DBD::Pg> we might do: + + [datasource main] + type = DBI + dbi_type = Pg + dsn = dbname=oi2 + username = oi2 + password = oi2 + pg_server_prepare = 0 + + which, when you create a database handle, is equivalent to: + + my $dbh = DBI->connect( ... ); + $dbh->{pg_server_prepare} = 0; + Returns a new hashref of information. For backwards compatibility, if we see the key C<spops> in C<\%datasource_info> we just return a new *************** *** 356,360 **** =head1 SEE ALSO ! L<OpenInteract2::Exception::Datasource|OpenInteract2::Exception::Datasource> L<Apache::DBI|Apache::DBI> --- 370,376 ---- =head1 SEE ALSO ! L<OpenInteract2::Setup::CheckDatasources> ! ! L<OpenInteract2::Exception::Datasource> L<Apache::DBI|Apache::DBI> |