From 00e7a2d6e874263aa0142bc70e979d415e2c8be0 Mon Sep 17 00:00:00 2001 From: einhverfr Date: Sat, 10 Mar 2007 06:58:47 +0000 Subject: More refactoring git-svn-id: https://ledger-smb.svn.sourceforge.net/svnroot/ledger-smb/trunk@878 4979c152-3d1c-0410-bac9-87ea11338e46 --- LedgerSMB.pm | 111 -------------------------------------------------- LedgerSMB/DBObject.pm | 95 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 111 deletions(-) diff --git a/LedgerSMB.pm b/LedgerSMB.pm index 4fdc1f6e..e827774f 100755 --- a/LedgerSMB.pm +++ b/LedgerSMB.pm @@ -393,117 +393,6 @@ sub db_init { } } -sub run_custom_queries { - my ($self, $tablename, $query_type, $linenum) = @_; - my $dbh = $self->{dbh}; - if ($query_type !~ /^(select|insert|update)$/i){ - $self->error($locale->text( - "Passed incorrect query type to run_custom_queries." - )); - } - my @rc; - my %temphash; - my @templist; - my @elements; - my $query; - my $ins_values; - if ($linenum){ - $linenum = "_$linenum"; - } - - $query_type = uc($query_type); - for (@{$self->{custom_db_fields}{$tablename}}){ - @elements = split (/:/, $_); - push @{$temphash{$elements[0]}}, $elements[1]; - } - for (keys %temphash){ - my @data; - my $ins_values; - $query = "$query_type "; - if ($query_type eq 'UPDATE'){ - $query = "DELETE FROM $_ WHERE row_id = ?"; - my $sth = $dbh->prepare($query); - $sth->execute->($self->{"id"."$linenum"}) - || $self->dberror($query); - } elsif ($query_type eq 'INSERT'){ - $query .= " INTO $_ ("; - } - my $first = 1; - for (@{$temphash{$_}}){ - $query .= "$_"; - if ($query_type eq 'UPDATE'){ - $query .= '= ?'; - } - $ins_values .= "?, "; - $query .= ", "; - $first = 0; - if ($query_type eq 'UPDATE' or $query_type eq 'INSERT'){ - push @data, $self->{"$_$linenum"}; - } - } - if ($query_type ne 'INSERT'){ - $query =~ s/, $//; - } - if ($query_type eq 'SELECT'){ - $query .= " FROM $_"; - } - if ($query_type eq 'SELECT' or $query_type eq 'UPDATE'){ - $query .= " WHERE row_id = ?"; - } - if ($query_type eq 'INSERT'){ - $query .= " row_id) VALUES ($ins_values ?)"; - } - if ($query_type eq 'SELECT'){ - push @rc, [ $query ]; - } else { - unshift (@data, $query); - push @rc, [ @data ]; - } - } - if ($query_type eq 'INSERT'){ - for (@rc){ - $query = shift (@{$_}); - $sth = $dbh->prepare($query) - || $self->db_error($query); - $sth->execute(@{$_}, $self->{id}) - || $self->dberror($query);; - $sth->finish; - $did_insert = 1; - } - } elsif ($query_type eq 'UPDATE'){ - @rc = $self->run_custom_queries( - $tablename, 'INSERT', $linenum); - } elsif ($query_type eq 'SELECT'){ - for (@rc){ - $query = shift @{$_}; - $sth = $self->{dbh}->prepare($query); - $sth->execute($self->{id}); - $ref = $sth->fetchrow_hashref(NAME_lc); - for (keys %{$ref}){ - $self->{$_} = $ref->{$_}; - } - } - } - @rc; -} - - -sub dbconnect { - - my ($self, $myconfig) = @_; - - # connect to database - my $dbh = DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser}, $myconfig->{dbpasswd}) or $self->dberror; - - # set db options - if ($myconfig->{dboptions}) { - $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions}); - } - - $dbh; -} - - sub dbconnect_noauto { my ($self, $myconfig) = @_; diff --git a/LedgerSMB/DBObject.pm b/LedgerSMB/DBObject.pm index d91c4899..17e4e4b0 100644 --- a/LedgerSMB/DBObject.pm +++ b/LedgerSMB/DBObject.pm @@ -82,4 +82,99 @@ sub exec_method { $self->callproc($funcname, @call_args); } +sub run_custom_queries { + my ($self, $tablename, $query_type, $linenum) = @_; + my $dbh = $self->{dbh}; + if ($query_type !~ /^(select|insert|update)$/i){ + $self->error($locale->text( + "Passed incorrect query type to run_custom_queries." + )); + } + my @rc; + my %temphash; + my @templist; + my @elements; + my $query; + my $ins_values; + if ($linenum){ + $linenum = "_$linenum"; + } + + $query_type = uc($query_type); + for (@{$self->{custom_db_fields}{$tablename}}){ + @elements = split (/:/, $_); + push @{$temphash{$elements[0]}}, $elements[1]; + } + for (keys %temphash){ + my @data; + my $ins_values; + $query = "$query_type "; + if ($query_type eq 'UPDATE'){ + $query = "DELETE FROM $_ WHERE row_id = ?"; + my $sth = $dbh->prepare($query); + $sth->execute->($self->{"id"."$linenum"}) + || $self->dberror($query); + } elsif ($query_type eq 'INSERT'){ + $query .= " INTO $_ ("; + } + my $first = 1; + for (@{$temphash{$_}}){ + $query .= "$_"; + if ($query_type eq 'UPDATE'){ + $query .= '= ?'; + } + $ins_values .= "?, "; + $query .= ", "; + $first = 0; + if ($query_type eq 'UPDATE' or $query_type eq 'INSERT'){ + push @data, $self->{"$_$linenum"}; + } + } + if ($query_type ne 'INSERT'){ + $query =~ s/, $//; + } + if ($query_type eq 'SELECT'){ + $query .= " FROM $_"; + } + if ($query_type eq 'SELECT' or $query_type eq 'UPDATE'){ + $query .= " WHERE row_id = ?"; + } + if ($query_type eq 'INSERT'){ + $query .= " row_id) VALUES ($ins_values ?)"; + } + if ($query_type eq 'SELECT'){ + push @rc, [ $query ]; + } else { + unshift (@data, $query); + push @rc, [ @data ]; + } + } + if ($query_type eq 'INSERT'){ + for (@rc){ + $query = shift (@{$_}); + $sth = $dbh->prepare($query) + || $self->db_error($query); + $sth->execute(@{$_}, $self->{id}) + || $self->dberror($query);; + $sth->finish; + $did_insert = 1; + } + } elsif ($query_type eq 'UPDATE'){ + @rc = $self->run_custom_queries( + $tablename, 'INSERT', $linenum); + } elsif ($query_type eq 'SELECT'){ + for (@rc){ + $query = shift @{$_}; + $sth = $self->{dbh}->prepare($query); + $sth->execute($self->{id}); + $ref = $sth->fetchrow_hashref(NAME_lc); + for (keys %{$ref}){ + $self->{$_} = $ref->{$_}; + } + } + } + @rc; +} + + 1; -- cgit v1.2.3