| 1 | package OpenInteract2::Datasource::DBI; | ||
| 2 | |||
| 3 | # $Id: DBI.pm,v 1.17 2005/03/17 14:58:01 sjn Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use DBI qw(); | ||
| 7 | use Log::Log4perl qw( get_logger ); | ||
| 8 | use OpenInteract2::Constants qw( :log ); | ||
| 9 | use OpenInteract2::Context qw( CTX ); | ||
| 10 | use OpenInteract2::Exception qw( oi_error oi_datasource_error ); | ||
| 11 | |||
| 12 | $OpenInteract2::Datasource::DBI::VERSION = sprintf("%d.%02d", q$Revision: 1.17 $ =~ /(\d+)\.(\d+)/); | ||
| 13 | |||
| 14 | my ( $log ); | ||
| 15 | |||
| 16 | use constant DEFAULT_READ_LEN => 32768; | ||
| 17 | use constant DEFAULT_TRUNC_OK => 0; | ||
| 18 | |||
| 19 | sub connect { | ||
| 20 | my ( $class, $ds_name, $ds_info ) = @_; | ||
| 21 | $log ||= get_logger( LOG_DS ); | ||
| 22 | unless ( ref $ds_info ) { | ||
| 23 | $log->error( "No data given to create DBI [$ds_name] handle" ); | ||
| 24 | oi_error "Cannot create connection without datasource info"; | ||
| 25 | } | ||
| 26 | unless ( $ds_name ) { | ||
| 27 | $log->warn( 'Correct usage of connect() is', | ||
| 28 | '$class->connect( $ds_name, \%ds_info ). ', | ||
| 29 | 'Continuing...' ); | ||
| 30 | } | ||
| 31 | |||
| 32 | my $full = $class->resolve_datasource_info( $ds_name, $ds_info ); | ||
| 33 | |||
| 34 | unless ( $full->{driver_name} ) { | ||
| 35 | $log->error( "Required configuration key undefined ", | ||
| 36 | "'datasource.$ds_name.driver_name'" ); | ||
| 37 | oi_error "Value for 'driver_name' must be defined in ", | ||
| 38 | "datasource [$ds_name]"; | ||
| 39 | } | ||
| 40 | |||
| 41 | # Make the connection -- let the 'die' trickle up to our caller if | ||
| 42 | # it happens | ||
| 43 | |||
| 44 | my $dsn = join( ':', 'DBI', $full->{driver_name}, $full->{dsn} ); | ||
| 45 | my $username = $full->{username}; | ||
| 46 | my $password = $full->{password}; | ||
| 47 | |||
| 48 | if ( $log->is_debug ) { | ||
| 49 | my %dumpable = %{ $full }; | ||
| 50 | $dumpable{password} = '*' x length $password; | ||
| 51 | $log->debug( "Trying to connect to DBI with: ", | ||
| 52 | CTX->dump( \%dumpable ) ); | ||
| 53 | } | ||
| 54 | |||
| 55 | my $db = DBI->connect( $dsn, $username, $password, | ||
| 56 | { RaiseError => 0, | ||
| 57 | PrintError => 0 } ); | ||
| 58 | unless ( $db ) { | ||
| 59 | oi_datasource_error | ||
| 60 | "Error connecting to DBI database '$ds_name': $DBI::errstr", | ||
| 61 | { datasource_name => $ds_name, | ||
| 62 | datasource_type => 'DBI', | ||
| 63 | connect_params => "$dsn , $username , $password" }; | ||
| 64 | } | ||
| 65 | |||
| 66 | # We don't set these until here (esp RaiseError) so we can control | ||
| 67 | # the format of the error... | ||
| 68 | |||
| 69 | $db->{RaiseError} = 1; | ||
| 70 | $db->{PrintError} = 0; | ||
| 71 | $db->{ChopBlanks} = 1; | ||
| 72 | $db->{AutoCommit} = 1; | ||
| 73 | $db->{LongReadLen} = $full->{long_read_len} || DEFAULT_READ_LEN; | ||
| 74 | $db->{LongTruncOk} = $full->{long_trunc_ok} || DEFAULT_TRUNC_OK; | ||
| 75 | |||
| 76 | my $trace_level = $full->{trace_level} || '0'; | ||
| 77 | $db->trace( $trace_level ); | ||
| 78 | |||
| 79 | $log->is_debug && | ||
| 80 | $log->debug( "Extra parameters [LongReadLen: $db->{LongReadLen}] ", | ||
| 81 | "[LongTruncOk: $db->{LongTruncOk}] ", | ||
| 82 | "[Trace: $trace_level]" ); | ||
| 83 | $log->is_info && | ||
| 84 | $log->info( "DBI connection [$ds_name] made ok" ); | ||
| 85 | return $db; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub disconnect { | ||
| 89 | my ( $class, $handle ) = @_; | ||
| 90 | $log ||= get_logger( LOG_DS ); | ||
| 91 | $log->is_info && | ||
| 92 | $log->info( "Disconnecting handle [$handle->{Name}]" ); | ||
| 93 | eval { $handle->disconnect }; | ||
| 94 | oi_error $@ if ( $@ ); | ||
| 95 | } | ||
| 96 | |||
| 97 | # OIN-54: resolve user-friendly configuration items | ||
| 98 | |||
| 99 | my %DBI_INFO = ( | ||
| 100 | pg => [ 'SPOPS::DBI::Pg', 'Pg' ], | ||
| 101 | mysql => [ 'SPOPS::DBI::MySQL', 'mysql' ], | ||
| 102 | sqlite => [ 'SPOPS::DBI::SQLite', 'SQLite' ], | ||
| 103 | oracle => [ 'SPOPS::DBI::Oracle', 'Oracle' ], | ||
| 104 | asany => [ 'SPOPS::DBI::Sybase', 'ASAny' ], | ||
| 105 | mssql => [ 'SPOPS::DBI::Sybase', 'Sybase' ], | ||
| 106 | sybase => [ 'SPOPS::DBI::Sybase', 'Sybase' ], | ||
| 107 | ); | ||
| 108 | |||
| 109 | sub resolve_datasource_info { | ||
| 110 | my ( $self, $name, $ds_info ) = @_; | ||
| 111 | |||
| 112 | # backwards compatibility - if 'spops' key exists just return as-is | ||
| 113 | if ( $ds_info->{spops} ) { | ||
| 114 | return { %{ $ds_info } }; | ||
| 115 | } | ||
| 116 | |||
| 117 | my @copy_properties = qw( | ||
| 118 | type dsn username password long_read_len long_trunc_ok trace_level | ||
| 119 | ); | ||
| 120 | my %info = map { $_ => $ds_info->{ $_ } } @copy_properties; | ||
| 121 | |||
| 122 | my $dbi_type = lc $ds_info->{dbi_type}; | ||
| 123 | if ( $dbi_type ) { | ||
| 124 | unless ( $DBI_INFO{ $dbi_type } ) { | ||
| 125 | oi_error "DBI datasource configuration '$name' has an invalid ", | ||
| 126 | "'dbi_type' of '$dbi_type'; allowed values: ", | ||
| 127 | join( ', ', sort keys %DBI_INFO ); | ||
| 128 | } | ||
| 129 | my $dbi_info = $DBI_INFO{ $dbi_type }; | ||
| 130 | $info{spops} = $dbi_info->[0]; | ||
| 131 | $info{driver_name} = ( 'yes' eq lc $ds_info->{use_odbc} ) | ||
| 132 | ? 'ODBC' : $dbi_info->[1]; | ||
| 133 | } | ||
| 134 | return \%info; | ||
| 135 | } | ||
| 136 | |||
| 137 | 1; | ||
| 138 | |||
| 139 | __END__ |