This commit is contained in:
qgarnier 2020-12-28 16:15:01 +01:00 committed by GitHub
parent 426a4c5190
commit ca7f950f49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -44,13 +44,14 @@ sub new {
$options{output}->add_option_msg(short_msg => "Class DBI: Need to specify 'options' argument."); $options{output}->add_option_msg(short_msg => "Class DBI: Need to specify 'options' argument.");
$options{output}->option_exit(); $options{output}->option_exit();
} }
if (!defined($options{noptions})) { if (!defined($options{noptions})) {
$options{options}->add_options(arguments => { $options{options}->add_options(arguments => {
'datasource:s@' => { name => 'data_source' }, 'datasource:s@' => { name => 'data_source' },
'username:s@' => { name => 'username' }, 'username:s@' => { name => 'username' },
'password:s@' => { name => 'password' }, 'password:s@' => { name => 'password' },
'connect-options:s@' => { name => 'connect_options' }, 'connect-options:s@' => { name => 'connect_options' },
'connect-query:s@' => { name => 'connect_query' },
'sql-errors-exit:s' => { name => 'sql_errors_exit', default => 'unknown' }, 'sql-errors-exit:s' => { name => 'sql_errors_exit', default => 'unknown' },
'timeout:s' => { name => 'timeout' }, 'timeout:s' => { name => 'timeout' },
}); });
@ -62,16 +63,16 @@ sub new {
$self->{instance} = undef; $self->{instance} = undef;
$self->{statement_handle} = undef; $self->{statement_handle} = undef;
$self->{version} = undef; $self->{version} = undef;
$self->{data_source} = undef; $self->{data_source} = undef;
$self->{username} = undef; $self->{username} = undef;
$self->{password} = undef; $self->{password} = undef;
$self->{connect_options} = undef; $self->{connect_options} = undef;
$self->{connect_options_hash} = {}; $self->{connect_options_hash} = {};
# Sometimes, we need to set ENV # Sometimes, we need to set ENV
$self->{env} = undef; $self->{env} = undef;
return $self; return $self;
} }
@ -131,20 +132,21 @@ sub set_defaults {
sub check_options { sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{data_source} = (defined($self->{option_results}->{data_source})) ? shift(@{$self->{option_results}->{data_source}}) : undef; $self->{data_source} = (defined($self->{option_results}->{data_source})) ? shift(@{$self->{option_results}->{data_source}}) : undef;
$self->{username} = (defined($self->{option_results}->{username})) ? shift(@{$self->{option_results}->{username}}) : undef; $self->{username} = (defined($self->{option_results}->{username})) ? shift(@{$self->{option_results}->{username}}) : undef;
$self->{password} = (defined($self->{option_results}->{password})) ? shift(@{$self->{option_results}->{password}}) : undef; $self->{password} = (defined($self->{option_results}->{password})) ? shift(@{$self->{option_results}->{password}}) : undef;
$self->{connect_options} = (defined($self->{option_results}->{connect_options})) ? shift(@{$self->{option_results}->{connect_options}}) : undef; $self->{connect_options} = (defined($self->{option_results}->{connect_options})) ? shift(@{$self->{option_results}->{connect_options}}) : undef;
$self->{connect_query} = (defined($self->{option_results}->{connect_query})) ? shift(@{$self->{option_results}->{connect_query}}) : undef;
$self->{env} = (defined($self->{option_results}->{env})) ? shift(@{$self->{option_results}->{env}}) : undef; $self->{env} = (defined($self->{option_results}->{env})) ? shift(@{$self->{option_results}->{env}}) : undef;
$self->{sql_errors_exit} = $self->{option_results}->{sql_errors_exit}; $self->{sql_errors_exit} = $self->{option_results}->{sql_errors_exit};
$self->{timeout} = 10; $self->{timeout} = 10;
if (defined($self->{option_results}->{timeout}) && $self->{option_results}->{timeout} =~ /^\d+$/ && if (defined($self->{option_results}->{timeout}) && $self->{option_results}->{timeout} =~ /^\d+$/ &&
$self->{option_results}->{timeout} > 0) { $self->{option_results}->{timeout} > 0) {
$self->{timeout} = $self->{option_results}->{timeout}; $self->{timeout} = $self->{option_results}->{timeout};
} }
if (!defined($self->{data_source}) || $self->{data_source} eq '') { if (!defined($self->{data_source}) || $self->{data_source} eq '') {
$self->{output}->add_option_msg(short_msg => 'Need to specify data_source arguments.'); $self->{output}->add_option_msg(short_msg => 'Need to specify data_source arguments.');
$self->{output}->option_exit(exit_litteral => $self->{sql_errors_exit}); $self->{output}->option_exit(exit_litteral => $self->{sql_errors_exit});
@ -158,7 +160,7 @@ sub check_options {
$self->{connect_options_hash}->{$1} = $2; $self->{connect_options_hash}->{$1} = $2;
} }
} }
if (scalar(@{$self->{option_results}->{data_source}}) == 0) { if (scalar(@{$self->{option_results}->{data_source}}) == 0) {
return 0; return 0;
} }
@ -188,19 +190,19 @@ sub is_version_minimum {
return 0 if ($versions[$i] > int($1)); return 0 if ($versions[$i] > int($1));
return 1 if ($versions[$i] < int($1)); return 1 if ($versions[$i] < int($1));
} }
return 1; return 1;
} }
sub set_version { sub set_version {
my ($self) = @_; my ($self) = @_;
$self->{version} = $self->{instance}->get_info(18); # SQL_DBMS_VER $self->{version} = $self->{instance}->get_info(18); # SQL_DBMS_VER
} }
sub disconnect { sub disconnect {
my ($self) = @_; my ($self) = @_;
if (defined($self->{instance})) { if (defined($self->{instance})) {
$self->{statement_handle} = undef; $self->{statement_handle} = undef;
$self->{instance}->disconnect(); $self->{instance}->disconnect();
@ -213,7 +215,7 @@ sub connect {
my $dontquit = (defined($options{dontquit}) && $options{dontquit} == 1) ? 1 : 0; my $dontquit = (defined($options{dontquit}) && $options{dontquit} == 1) ? 1 : 0;
return if (defined($self->{instance})); return if (defined($self->{instance}));
# Set ENV # Set ENV
if (defined($self->{env})) { if (defined($self->{env})) {
foreach (keys %{$self->{env}}) { foreach (keys %{$self->{env}}) {
@ -240,14 +242,17 @@ sub connect {
} }
return (-1, $err_msg); return (-1, $err_msg);
} }
if (defined($self->{connect_query}) && $self->{connect_query} ne '') {
$self->query(query => $self->{connect_query});
}
$self->set_version(); $self->set_version();
return 0; return 0;
} }
sub get_id { sub get_id {
my ($self, %options) = @_; my ($self, %options) = @_;
return $self->{data_source}; return $self->{data_source};
} }
@ -259,26 +264,26 @@ sub get_unique_id4save {
sub fetchall_arrayref { sub fetchall_arrayref {
my ($self, %options) = @_; my ($self, %options) = @_;
return $self->{statement_handle}->fetchall_arrayref(); return $self->{statement_handle}->fetchall_arrayref();
} }
sub fetchrow_array { sub fetchrow_array {
my ($self, %options) = @_; my ($self, %options) = @_;
return $self->{statement_handle}->fetchrow_array(); return $self->{statement_handle}->fetchrow_array();
} }
sub fetchrow_hashref { sub fetchrow_hashref {
my ($self, %options) = @_; my ($self, %options) = @_;
return $self->{statement_handle}->fetchrow_hashref(); return $self->{statement_handle}->fetchrow_hashref();
} }
sub query { sub query {
my ($self, %options) = @_; my ($self, %options) = @_;
my $continue_error = defined($options{continue_error}) && $options{continue_error} == 1 ? 1 : 0; my $continue_error = defined($options{continue_error}) && $options{continue_error} == 1 ? 1 : 0;
$self->{statement_handle} = $self->{instance}->prepare($options{query}); $self->{statement_handle} = $self->{instance}->prepare($options{query});
if (!defined($self->{statement_handle})) { if (!defined($self->{statement_handle})) {
return 1 if ($continue_error == 1); return 1 if ($continue_error == 1);
@ -287,7 +292,7 @@ sub query {
$self->{output}->option_exit(exit_litteral => $self->{sql_errors_exit}); $self->{output}->option_exit(exit_litteral => $self->{sql_errors_exit});
} }
my $rv = $self->{statement_handle}->execute; my $rv = $self->{statement_handle}->execute();
if (!$rv) { if (!$rv) {
return 1 if ($continue_error == 1); return 1 if ($continue_error == 1);
$self->{output}->add_option_msg(short_msg => 'Cannot execute query: ' . $self->{statement_handle}->errstr); $self->{output}->add_option_msg(short_msg => 'Cannot execute query: ' . $self->{statement_handle}->errstr);
@ -331,6 +336,10 @@ Database password.
Add options in database connect. Add options in database connect.
Format: name=value,name2=value2,... Format: name=value,name2=value2,...
=item B<--connect-query>
Execute a query just after connection.
=item B<--sql-errors-exit> =item B<--sql-errors-exit>
Exit code for DB Errors (default: unknown) Exit code for DB Errors (default: unknown)