From 03457b5be4b3c0c62dbbb23bb682205669ea65d4 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Mon, 6 May 2013 14:15:27 +0200 Subject: [PATCH] Perl-Upgrade-Scripte: db_query nun auch mit Bind-Parametern --- SL/DBUpgrade2/Base.pm | 29 +++++++++++++------ sql/Pg-upgrade2/USTVA_abstraction.pl | 4 +-- sql/Pg-upgrade2/USTVA_at.pl | 8 ++--- sql/Pg-upgrade2/acc_trans_id_uniqueness.pl | 4 +-- sql/Pg-upgrade2/contacts_add_cp_position.pl | 2 +- ...ontacts_add_street_and_zipcode_and_city.pl | 2 +- sql/Pg-upgrade2/defaults_datev_check.pl | 10 +++---- sql/Pg-upgrade2/defaults_posting_config.pl | 2 +- sql/Pg-upgrade2/defaults_show_bestbefore.pl | 2 +- .../globalprojectnumber_ap_ar_oe.pl | 4 +-- sql/Pg-upgrade2/umstellung_eur.pl | 2 +- 11 files changed, 38 insertions(+), 31 deletions(-) diff --git a/SL/DBUpgrade2/Base.pm b/SL/DBUpgrade2/Base.pm index 09e4e5fff..2c1e11114 100644 --- a/SL/DBUpgrade2/Base.pm +++ b/SL/DBUpgrade2/Base.pm @@ -41,11 +41,11 @@ sub db_error { } sub db_query { - my ($self, $query, $may_fail) = @_; + my ($self, $query, %params) = @_; - return if $self->dbh->do($query); + return if $self->dbh->do($query, undef, @{ $params{bind} || [] }); - $self->db_error($query) unless $may_fail; + $self->db_error($query) unless $params{may_fail}; $self->dbh->rollback; $self->dbh->begin_work; @@ -204,13 +204,24 @@ C<$coa_name>. Outputs an error message C<$message> to the user and aborts execution. -=item C +=item C -Executes an SQL query. What the method does if the query fails depends -on C<$may_fail>. If it is falsish then the method will simply die -outputting the error message via L. If C<$may_fail> is -trueish then the current transaction will be rolled back, a new one -will be started +Executes an SQL query. The following parameters are supported: + +=over 2 + +=item C + +What the method does if the query fails depends on this parameter. If +it is falsish (the default) then the method will simply die outputting +the error message via L. If C is trueish then the +current transaction will be rolled back, a new one will be started. + +=item C + +An optional array reference containing bind parameter for the query. + +=back =item C diff --git a/sql/Pg-upgrade2/USTVA_abstraction.pl b/sql/Pg-upgrade2/USTVA_abstraction.pl index a1b4a9caa..fc1638cd0 100644 --- a/sql/Pg-upgrade2/USTVA_abstraction.pl +++ b/sql/Pg-upgrade2/USTVA_abstraction.pl @@ -47,8 +47,8 @@ sub create_tables { }, ); - $self->db_query("DROP SCHEMA tax CASCADE;", 1); - map({ $self->db_query($_, 0); } @queries); + $self->db_query("DROP SCHEMA tax CASCADE;", may_fail => 1); + $self->db_query($_) for @queries; return 1; diff --git a/sql/Pg-upgrade2/USTVA_at.pl b/sql/Pg-upgrade2/USTVA_at.pl index 2cffc3d78..02143aec9 100644 --- a/sql/Pg-upgrade2/USTVA_at.pl +++ b/sql/Pg-upgrade2/USTVA_at.pl @@ -48,7 +48,7 @@ sub clear_tables { q{ DELETE FROM tax.report_variables; }, ); - map({ $self->db_query("DELETE FROM $_ ;", 0); } @clear); + $self->db_query("DELETE FROM $_") for @clear; return 1; @@ -303,11 +303,7 @@ sub do_insert_chart { ); - for my $statement ( 0 .. $#copy_statements ) { - my $query = $copy_statements[$statement]; - #print $query . "
"; # Diagnose only! - $self->db_query($query, 0); - } + $self->db_query($_) for @copy_statements; return 1; } diff --git a/sql/Pg-upgrade2/acc_trans_id_uniqueness.pl b/sql/Pg-upgrade2/acc_trans_id_uniqueness.pl index f90d73157..f83e99ecc 100644 --- a/sql/Pg-upgrade2/acc_trans_id_uniqueness.pl +++ b/sql/Pg-upgrade2/acc_trans_id_uniqueness.pl @@ -33,7 +33,7 @@ SQL )) SQL - $self->db_query($query, 0); + $self->db_query($query); my %skipped_acc_trans_ids; foreach my $entry (@entries) { @@ -50,7 +50,7 @@ SQL AND (mtime $mtime) SQL - $self->db_query($query, 0); + $self->db_query($query); } } diff --git a/sql/Pg-upgrade2/contacts_add_cp_position.pl b/sql/Pg-upgrade2/contacts_add_cp_position.pl index 892284799..6bcbbb6eb 100644 --- a/sql/Pg-upgrade2/contacts_add_cp_position.pl +++ b/sql/Pg-upgrade2/contacts_add_cp_position.pl @@ -11,7 +11,7 @@ use parent qw(SL::DBUpgrade2::Base); sub run { my ($self) = @_; - $self->db_query('ALTER TABLE contacts ADD COLUMN cp_position VARCHAR(75)', 1); + $self->db_query('ALTER TABLE contacts ADD COLUMN cp_position VARCHAR(75)', may_fail => 1); return 1; } diff --git a/sql/Pg-upgrade2/contacts_add_street_and_zipcode_and_city.pl b/sql/Pg-upgrade2/contacts_add_street_and_zipcode_and_city.pl index f830e7c96..9d684c123 100644 --- a/sql/Pg-upgrade2/contacts_add_street_and_zipcode_and_city.pl +++ b/sql/Pg-upgrade2/contacts_add_street_and_zipcode_and_city.pl @@ -17,7 +17,7 @@ sub run { 'ALTER TABLE contacts ADD COLUMN cp_city text;', ); - $self->db_query($_, 1) for @queries; + $self->db_query($_, may_fail => 1) for @queries; return 1; } diff --git a/sql/Pg-upgrade2/defaults_datev_check.pl b/sql/Pg-upgrade2/defaults_datev_check.pl index e213e622d..175da4208 100644 --- a/sql/Pg-upgrade2/defaults_datev_check.pl +++ b/sql/Pg-upgrade2/defaults_datev_check.pl @@ -12,11 +12,11 @@ sub run { my ($self) = @_; # this query will fail if column already exist (new database) - $self->db_query(qq|ALTER TABLE defaults ADD COLUMN datev_check_on_sales_invoice boolean DEFAULT true|, 1); - $self->db_query(qq|ALTER TABLE defaults ADD COLUMN datev_check_on_purchase_invoice boolean DEFAULT true|, 1); - $self->db_query(qq|ALTER TABLE defaults ADD COLUMN datev_check_on_ar_transaction boolean DEFAULT true|, 1); - $self->db_query(qq|ALTER TABLE defaults ADD COLUMN datev_check_on_ap_transaction boolean DEFAULT true|, 1); - $self->db_query(qq|ALTER TABLE defaults ADD COLUMN datev_check_on_gl_transaction boolean DEFAULT true|, 1); + $self->db_query(qq|ALTER TABLE defaults ADD COLUMN datev_check_on_sales_invoice boolean DEFAULT true|, may_fail => 1); + $self->db_query(qq|ALTER TABLE defaults ADD COLUMN datev_check_on_purchase_invoice boolean DEFAULT true|, may_fail => 1); + $self->db_query(qq|ALTER TABLE defaults ADD COLUMN datev_check_on_ar_transaction boolean DEFAULT true|, may_fail => 1); + $self->db_query(qq|ALTER TABLE defaults ADD COLUMN datev_check_on_ap_transaction boolean DEFAULT true|, may_fail => 1); + $self->db_query(qq|ALTER TABLE defaults ADD COLUMN datev_check_on_gl_transaction boolean DEFAULT true|, may_fail => 1); # check current configuration and set default variables accordingly, so that # kivitendo's behaviour isn't changed by this update diff --git a/sql/Pg-upgrade2/defaults_posting_config.pl b/sql/Pg-upgrade2/defaults_posting_config.pl index c638494f7..a94f2f53e 100644 --- a/sql/Pg-upgrade2/defaults_posting_config.pl +++ b/sql/Pg-upgrade2/defaults_posting_config.pl @@ -12,7 +12,7 @@ sub run { my ($self) = @_; # this query will fail if column already exist (new database) - $self->db_query(qq|ALTER TABLE defaults ADD COLUMN payments_changeable integer NOT NULL DEFAULT 0|, 1); + $self->db_query(qq|ALTER TABLE defaults ADD COLUMN payments_changeable integer NOT NULL DEFAULT 0|, may_fail => 1); # check current configuration and set default variables accordingly, so that # kivitendo behaviour isn't changed by this update diff --git a/sql/Pg-upgrade2/defaults_show_bestbefore.pl b/sql/Pg-upgrade2/defaults_show_bestbefore.pl index 44f3cf96d..a7c144c4f 100644 --- a/sql/Pg-upgrade2/defaults_show_bestbefore.pl +++ b/sql/Pg-upgrade2/defaults_show_bestbefore.pl @@ -12,7 +12,7 @@ sub run { my ($self) = @_; # this query will fail if column already exist (new database) - $self->db_query(qq|ALTER TABLE defaults ADD COLUMN show_bestbefore boolean DEFAULT false|, 1); + $self->db_query(qq|ALTER TABLE defaults ADD COLUMN show_bestbefore boolean DEFAULT false|, may_fail => 1); # check current configuration and set default variables accordingly, so that # kivitendo behaviour isn't changed by this update diff --git a/sql/Pg-upgrade2/globalprojectnumber_ap_ar_oe.pl b/sql/Pg-upgrade2/globalprojectnumber_ap_ar_oe.pl index df31c04e4..f12c92a52 100644 --- a/sql/Pg-upgrade2/globalprojectnumber_ap_ar_oe.pl +++ b/sql/Pg-upgrade2/globalprojectnumber_ap_ar_oe.pl @@ -19,8 +19,8 @@ sub run { "ALTER TABLE oe ADD COLUMN globalproject_id integer;", "ALTER TABLE oe ADD FOREIGN KEY (globalproject_id) REFERENCES project (id);"); - $self->db_query("ALTER TABLE project ADD PRIMARY KEY (id);", 1); - map({ $self->db_query($_, 0); } @queries); + $self->db_query("ALTER TABLE project ADD PRIMARY KEY (id)", may_fail => 1); + $self->db_query($_) for @queries; return 1; } diff --git a/sql/Pg-upgrade2/umstellung_eur.pl b/sql/Pg-upgrade2/umstellung_eur.pl index 0f7606065..cf93d76c6 100644 --- a/sql/Pg-upgrade2/umstellung_eur.pl +++ b/sql/Pg-upgrade2/umstellung_eur.pl @@ -20,7 +20,7 @@ sub run { foreach my $column (qw(accounting_method inventory_system profit_determination)) { # this query will fail if columns already exist (new database) - $self->db_query(qq|ALTER TABLE defaults ADD COLUMN ${column} TEXT|, 1); + $self->db_query(qq|ALTER TABLE defaults ADD COLUMN ${column} TEXT|, may_fail => 1); } my $accounting_method; -- 2.20.1