summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject.pm
blob: 2b4b35da5aae197de7f4f146bf22ecef8daa77d7 (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 = "SELECT proname, proargnames FROM pg_proc WHERE proname = ?";
  54. my $sth = $self->{dbh}->prepare($query);
  55. $sth->execute($funcname);
  56. my $ref;
  57. $ref = $sth->fetchrow_hashref('NAME_lc');
  58. my $args = $ref->{proargnames};
  59. $args =~ s/\{(.*)\}/$1/;
  60. my @proc_args = split /,/, $args;
  61. if ( !$ref ) { # no such function
  62. $self->error( "No such function: ", $funcname );
  63. die;
  64. }
  65. my $m_name = $ref->{proname};
  66. if ($args) {
  67. for my $arg (@proc_args) {
  68. if ( $arg =~ s/^in_// ) {
  69. push @call_args, $self->{$arg};
  70. }
  71. }
  72. }
  73. else {
  74. @call_args = @_;
  75. }
  76. $self->call_procedure( procname => $funcname, args => \@call_args );
  77. }
  78. sub run_custom_queries {
  79. my ( $self, $tablename, $query_type, $linenum ) = @_;
  80. my $dbh = $self->{dbh};
  81. if ( $query_type !~ /^(select|insert|update)$/i ) {
  82. # Commenting out this next bit until we figure out how the locale object
  83. # will operate. Chris
  84. #$self->error($locale->text(
  85. # "Passed incorrect query type to run_custom_queries."
  86. #));
  87. }
  88. my @rc;
  89. my %temphash;
  90. my @templist;
  91. my $did_insert;
  92. my @elements;
  93. my $query;
  94. my $ins_values;
  95. if ($linenum) {
  96. $linenum = "_$linenum";
  97. }
  98. $query_type = uc($query_type);
  99. for ( @{ $self->{custom_db_fields}{$tablename} } ) {
  100. @elements = split( /:/, $_ );
  101. push @{ $temphash{ $elements[0] } }, $elements[1];
  102. }
  103. for ( keys %temphash ) {
  104. my @data;
  105. my $ins_values;
  106. $query = "$query_type ";
  107. if ( $query_type eq 'UPDATE' ) {
  108. $query = "DELETE FROM $_ WHERE row_id = ?";
  109. my $sth = $dbh->prepare($query);
  110. $sth->execute->( $self->{ "id" . "$linenum" } )
  111. || $self->dberror($query);
  112. }
  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. }
  144. else {
  145. unshift( @data, $query );
  146. push @rc, [@data];
  147. }
  148. }
  149. if ( $query_type eq 'INSERT' ) {
  150. for (@rc) {
  151. $query = shift( @{$_} );
  152. my $sth = $dbh->prepare($query)
  153. || $self->db_error($query);
  154. $sth->execute( @{$_}, $self->{id} )
  155. || $self->dberror($query);
  156. $sth->finish;
  157. $did_insert = 1;
  158. }
  159. }
  160. elsif ( $query_type eq 'UPDATE' ) {
  161. @rc = $self->run_custom_queries( $tablename, 'INSERT', $linenum );
  162. }
  163. elsif ( $query_type eq 'SELECT' ) {
  164. for (@rc) {
  165. $query = shift @{$_};
  166. my $sth = $self->{dbh}->prepare($query);
  167. $sth->execute( $self->{id} );
  168. my $ref = $sth->fetchrow_hashref('NAME_lc');
  169. $self->merge( $ref, keys(%$ref) );
  170. }
  171. }
  172. @rc;
  173. }
  174. 1;