| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Footprintless::Plugin::Database::OracleProvider; | ||
| 5 | $Footprintless::Plugin::Database::OracleProvider::VERSION = '1.06'; | ||
| 6 | # ABSTRACT: A Oracle provider implementation | ||
| 7 | # PODNAME: Footprintless::Plugin::Database::OracleProvider | ||
| 8 | |||
| 9 | use parent qw(Footprintless::Plugin::Database::AbstractProvider); | ||
| 10 | |||
| 11 | use overload q{""} => 'to_string', fallback => 1; | ||
| 12 | |||
| 13 | use Carp; | ||
| 14 | use File::Temp; | ||
| 15 | use Footprintless::Command qw( | ||
| 16 | command | ||
| 17 | pipe_command | ||
| 18 | sed_command | ||
| 19 | ); | ||
| 20 | use Footprintless::Mixins qw( | ||
| 21 | _run_or_die | ||
| 22 | ); | ||
| 23 | use Log::Any; | ||
| 24 | |||
| 25 | my $logger = Log::Any->get_logger(); | ||
| 26 | |||
| 27 | sub _client_command { | ||
| 28 | my ( $self, $command, @additional_options ) = @_; | ||
| 29 | |||
| 30 | my $cnf = $self->_cnf(); | ||
| 31 | my ( $hostname, $port ) = $self->_hostname_port(); | ||
| 32 | |||
| 33 | return join( ' ', $command, @additional_options ); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub client { | ||
| 37 | my ( $self, %options ) = @_; | ||
| 38 | |||
| 39 | my $in_file; | ||
| 40 | eval { | ||
| 41 | my $in_handle = delete( $options{in_handle} ); | ||
| 42 | if ( $options{in_file} ) { | ||
| 43 | open( $in_file, '<', delete( $options{in_file} ) ) | ||
| 44 | || croak("invalid in_file: $!"); | ||
| 45 | } | ||
| 46 | if ( $options{in_string} ) { | ||
| 47 | my $string = delete( $options{in_string} ); | ||
| 48 | open( $in_file, '<', \$string ) | ||
| 49 | || croak("invalid in_string: $!"); | ||
| 50 | } | ||
| 51 | $self->_connect_tunnel(); | ||
| 52 | |||
| 53 | my $command = $self->_client_command( | ||
| 54 | 'sqlplus', @{ $options{client_options} }, | ||
| 55 | '/nolog', sprintf( "'\@%s'", $self->_cnf() ) | ||
| 56 | ); | ||
| 57 | $self->_run_or_die( | ||
| 58 | $command, | ||
| 59 | { in_handle => $in_file || $in_handle || \*STDIN, | ||
| 60 | out_handle => \*STDOUT, | ||
| 61 | err_handle => \*STDERR | ||
| 62 | } | ||
| 63 | ); | ||
| 64 | }; | ||
| 65 | my $error = $@; | ||
| 66 | $self->disconnect(); | ||
| 67 | if ($in_file) { | ||
| 68 | close($in_file); | ||
| 69 | } | ||
| 70 | |||
| 71 | croak($error) if ($error); | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _cnf { | ||
| 75 | my ($self) = @_; | ||
| 76 | |||
| 77 | if ( !$self->{cnf} ) { | ||
| 78 | my ( $hostname, $port ) = $self->_hostname_port(); | ||
| 79 | File::Temp->safe_level(File::Temp::HIGH); | ||
| 80 | my $cnf = File::Temp->new( SUFFIX => '.sql' ); | ||
| 81 | if ( !chmod( 0600, $cnf ) ) { | ||
| 82 | croak("unable to create secure temp file"); | ||
| 83 | } | ||
| 84 | printf( $cnf "connect %s/%s\@%s:%d/%s\n", | ||
| 85 | $self->{username}, $self->{password}, $hostname, $port, $self->{schema} ); | ||
| 86 | close($cnf); | ||
| 87 | $self->{cnf} = $cnf; | ||
| 88 | if ( lc($^O) eq 'cygwin' ) { | ||
| 89 | $self->{cnf_ms_windows} = `cygpath --windows $cnf`; | ||
| 90 | chomp( $self->{cnf_ms_windows} ); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | return $self->{cnf_ms_windows} || $self->{cnf}; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub _connection_string { | ||
| 98 | my ($self) = @_; | ||
| 99 | my ( $hostname, $port ) = $self->_hostname_port(); | ||
| 100 | return sprintf( "dbi:Oracle://%s:%d/%s", $hostname, $port, $self->{schema} ); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _init { | ||
| 104 | my ( $self, %options ) = @_; | ||
| 105 | $self->Footprintless::Plugin::Database::AbstractProvider::_init(%options); | ||
| 106 | |||
| 107 | $self->{port} = 3306 unless ( $self->{port} ); | ||
| 108 | |||
| 109 | return $self; | ||
| 110 | } | ||
| 111 | |||
| 112 | 1; | ||
| 113 | |||
| 114 | __END__ |