summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject.pm
blob: 38e96adb7cda77915c159fbe15c2bf2e596bab8e (plain)
  1. =head1 NAME
  2. LedgerSMB::DBObject - LedgerSMB class for building objects from db relations
  3. =head1 SYOPSIS
  4. This module creates object instances based on LedgerSMB's in-database ORM.
  5. =head1 METHODS
  6. =item new ($class, base => $LedgerSMB::hash)
  7. This is the base constructor for all child classes. It must be used with base
  8. argument because this is necessary for database connectivity and the like.
  9. Of course the base object can be any object that inherits LedgerSMB, so you can
  10. use any subclass of that. The per-session dbh is passed between the objects
  11. this way as is any information that is needed.
  12. =item exec_method ($self, procname => $function_name, args => \@args)
  13. =item merge ($hashref, @attrs)
  14. copies @attrs from $hashref to $self.
  15. =head1 Copyright (C) 2007, The LedgerSMB core team.
  16. This file is licensed under the Gnu General Public License version 2, or at your
  17. option any later version. A copy of the license should have been included with
  18. your software.
  19. =back
  20. =cut
  21. package LedgerSMB::DBObject;
  22. use Scalar::Util;
  23. use base qw(LedgerSMB);
  24. use strict;
  25. use warnings;
  26. our $AUTOLOAD;
  27. sub AUTOLOAD {
  28. my ($self) = shift;
  29. my $type = Scalar::Util::blessed $self;
  30. $type =~ m/::(.*?)$/;
  31. $type = lc $1;
  32. print "Type: $type\n";
  33. $self->exec_method(procname => "$type" . "_" . $AUTOLOAD, args => \@_);
  34. }
  35. sub new {
  36. my $class = shift @_;
  37. my %args = @_;
  38. my $base = $args{base};
  39. my $self = bless {}, $class;
  40. if (! $base->isa('LedgerSMB')){
  41. $self->error("Constructor called without LedgerSMB object arg");
  42. }
  43. my $attr;
  44. $self->merge($base);
  45. $self;
  46. }
  47. sub exec_method {
  48. my ($self) = shift @_;
  49. my %args = @_;
  50. my $funcname = $args{funcname};
  51. my @in_args = @{$args{args}};
  52. my @call_args;
  53. my $query =
  54. "SELECT proname, proargnames FROM pg_proc WHERE proname = ?";
  55. my $sth = $self->{dbh}->prepare($query);
  56. $sth->execute($funcname);
  57. my $ref;
  58. $ref = $sth->fetchrow_hashref('NAME_lc');
  59. my $args = $ref->{proargnames};
  60. $args =~ s/\{(.*)\}/$1/;
  61. my @proc_args = split /,/, $args;
  62. if (!$ref){ # no such function
  63. $self->error("No such function: ", $funcname);
  64. die;
  65. }
  66. my $m_name = $ref->{proname};
  67. if ($args){
  68. for my $arg (@proc_args){
  69. if ($arg =~ s/^in_//){
  70. push @call_args, $self->{$arg};
  71. }
  72. }
  73. }
  74. else {
  75. @call_args = @_;
  76. }
  77. $self->call_procedure(procname => $funcname, args => \@call_args);
  78. }
  79. sub run_custom_queries {
  80. my ($self, $tablename, $query_type, $linenum) = @_;
  81. my $dbh = $self->{dbh};
  82. if ($query_type !~ /^(select|insert|update)$/i){
  83. # Commenting out this next bit until we figure out how the locale object
  84. # will operate. Chris
  85. #$self->error($locale->text(
  86. # "Passed incorrect query type to run_custom_queries."
  87. #));
  88. }
  89. my @rc;
  90. my %temphash;
  91. my @templist;
  92. my $did_insert;
  93. my @elements;
  94. my $query;
  95. my $ins_values;
  96. if ($linenum){
  97. $linenum = "_$linenum";
  98. }
  99. $query_type = uc($query_type);
  100. for (@{$self->{custom_db_fields}{$tablename}}){
  101. @elements = split (/:/, $_);
  102. push @{$temphash{$elements[0]}}, $elements[1];
  103. }
  104. for (keys %temphash){
  105. my @data;
  106. my $ins_values;
  107. $query = "$query_type ";
  108. if ($query_type eq 'UPDATE'){
  109. $query = "DELETE FROM $_ WHERE row_id = ?";
  110. my $sth = $dbh->prepare($query);
  111. $sth->execute->($self->{"id"."$linenum"})
  112. || $self->dberror($query);
  113. } elsif ($query_type eq 'INSERT'){
  114. $query .= " INTO $_ (";
  115. }
  116. my $first = 1;
  117. for (@{$temphash{$_}}){
  118. $query .= "$_";
  119. if ($query_type eq 'UPDATE'){
  120. $query .= '= ?';
  121. }
  122. $ins_values .= "?, ";
  123. $query .= ", ";
  124. $first = 0;
  125. if ($query_type eq 'UPDATE' or $query_type eq 'INSERT'){
  126. push @data, $self->{"$_$linenum"};
  127. }
  128. }
  129. if ($query_type ne 'INSERT'){
  130. $query =~ s/, $//;
  131. }
  132. if ($query_type eq 'SELECT'){
  133. $query .= " FROM $_";
  134. }
  135. if ($query_type eq 'SELECT' or $query_type eq 'UPDATE'){
  136. $query .= " WHERE row_id = ?";
  137. }
  138. if ($query_type eq 'INSERT'){
  139. $query .= " row_id) VALUES ($ins_values ?)";
  140. }
  141. if ($query_type eq 'SELECT'){
  142. push @rc, [ $query ];
  143. } else {
  144. unshift (@data, $query);
  145. push @rc, [ @data ];
  146. }
  147. }
  148. if ($query_type eq 'INSERT'){
  149. for (@rc){
  150. $query = shift (@{$_});
  151. my $sth = $dbh->prepare($query)
  152. || $self->db_error($query);
  153. $sth->execute(@{$_}, $self->{id})
  154. || $self->dberror($query);;
  155. $sth->finish;
  156. $did_insert = 1;
  157. }
  158. } elsif ($query_type eq 'UPDATE'){
  159. @rc = $self->run_custom_queries(
  160. $tablename, 'INSERT', $linenum);
  161. } elsif ($query_type eq 'SELECT'){
  162. for (@rc){
  163. $query = shift @{$_};
  164. my $sth = $self->{dbh}->prepare($query);
  165. $sth->execute($self->{id});
  166. my $ref = $sth->fetchrow_hashref('NAME_lc');
  167. $self->merge($ref, keys(%$ref));
  168. }
  169. }
  170. @rc;
  171. }
  172. 1;