summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject.pm
blob: 63c27f7cf356b4008b182acbfab381687730a77b (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. order_by => $order_by);
  35. }
  36. sub new {
  37. my $class = shift @_;
  38. my %args = @_;
  39. my $base = $args{base};
  40. my $self = bless {}, $class;
  41. if ( !$base->isa('LedgerSMB') ) {
  42. $self->error("Constructor called without LedgerSMB object arg");
  43. }
  44. my $attr;
  45. $self->merge($base);
  46. $self;
  47. }
  48. sub set_ordering {
  49. my $self = shift @_;
  50. my %args = @_;
  51. if (not defined $self->{_order_method}){
  52. $self->{_order_method} = {};
  53. }
  54. $self->{_order_method}->{$args{method}} = $args{column};
  55. }
  56. sub exec_method {
  57. my $self = shift @_;
  58. my %args = @_;
  59. my $funcname = $args{funcname};
  60. my @in_args = @{ $args{args} };
  61. my @call_args;
  62. my $query = "SELECT proname, proargnames FROM pg_proc WHERE proname = ?";
  63. my $sth = $self->{dbh}->prepare($query);
  64. $sth->execute($funcname);
  65. my $ref;
  66. $ref = $sth->fetchrow_hashref('NAME_lc');
  67. my $args = $ref->{proargnames};
  68. $args =~ s/\{(.*)\}/$1/;
  69. my @proc_args = split /,/, $args;
  70. if ( !$ref ) { # no such function
  71. $self->error( "No such function: ", $funcname );
  72. die;
  73. }
  74. my $m_name = $ref->{proname};
  75. if ($args) {
  76. for my $arg (@proc_args) {
  77. if ( $arg =~ s/^in_// ) {
  78. push @call_args, $self->{$arg};
  79. }
  80. }
  81. }
  82. else {
  83. @call_args = @_;
  84. }
  85. $self->call_procedure( procname => $funcname, args => \@call_args );
  86. }
  87. sub run_custom_queries {
  88. my ( $self, $tablename, $query_type, $linenum ) = @_;
  89. my $dbh = $self->{dbh};
  90. if ( $query_type !~ /^(select|insert|update)$/i ) {
  91. # Commenting out this next bit until we figure out how the locale object
  92. # will operate. Chris
  93. #$self->error($locale->text(
  94. # "Passed incorrect query type to run_custom_queries."
  95. #));
  96. }
  97. my @rc;
  98. my %temphash;
  99. my @templist;
  100. my $did_insert;
  101. my @elements;
  102. my $query;
  103. my $ins_values;
  104. if ($linenum) {
  105. $linenum = "_$linenum";
  106. }
  107. $query_type = uc($query_type);
  108. for ( @{ $self->{custom_db_fields}{$tablename} } ) {
  109. @elements = split( /:/, $_ );
  110. push @{ $temphash{ $elements[0] } }, $elements[1];
  111. }
  112. for ( keys %temphash ) {
  113. my @data;
  114. my $ins_values;
  115. $query = "$query_type ";
  116. if ( $query_type eq 'UPDATE' ) {
  117. $query = "DELETE FROM $_ WHERE row_id = ?";
  118. my $sth = $dbh->prepare($query);
  119. $sth->execute->( $self->{ "id" . "$linenum" } )
  120. || $self->dberror($query);
  121. }
  122. elsif ( $query_type eq 'INSERT' ) {
  123. $query .= " INTO $_ (";
  124. }
  125. my $first = 1;
  126. for ( @{ $temphash{$_} } ) {
  127. $query .= "$_";
  128. if ( $query_type eq 'UPDATE' ) {
  129. $query .= '= ?';
  130. }
  131. $ins_values .= "?, ";
  132. $query .= ", ";
  133. $first = 0;
  134. if ( $query_type eq 'UPDATE' or $query_type eq 'INSERT' ) {
  135. push @data, $self->{"$_$linenum"};
  136. }
  137. }
  138. if ( $query_type ne 'INSERT' ) {
  139. $query =~ s/, $//;
  140. }
  141. if ( $query_type eq 'SELECT' ) {
  142. $query .= " FROM $_";
  143. }
  144. if ( $query_type eq 'SELECT' or $query_type eq 'UPDATE' ) {
  145. $query .= " WHERE row_id = ?";
  146. }
  147. if ( $query_type eq 'INSERT' ) {
  148. $query .= " row_id) VALUES ($ins_values ?)";
  149. }
  150. if ( $query_type eq 'SELECT' ) {
  151. push @rc, [$query];
  152. }
  153. else {
  154. unshift( @data, $query );
  155. push @rc, [@data];
  156. }
  157. }
  158. if ( $query_type eq 'INSERT' ) {
  159. for (@rc) {
  160. $query = shift( @{$_} );
  161. my $sth = $dbh->prepare($query)
  162. || $self->db_error($query);
  163. $sth->execute( @{$_}, $self->{id} )
  164. || $self->dberror($query);
  165. $sth->finish;
  166. $did_insert = 1;
  167. }
  168. }
  169. elsif ( $query_type eq 'UPDATE' ) {
  170. @rc = $self->run_custom_queries( $tablename, 'INSERT', $linenum );
  171. }
  172. elsif ( $query_type eq 'SELECT' ) {
  173. for (@rc) {
  174. $query = shift @{$_};
  175. my $sth = $self->{dbh}->prepare($query);
  176. $sth->execute( $self->{id} );
  177. my $ref = $sth->fetchrow_hashref('NAME_lc');
  178. $self->merge( $ref, keys(%$ref) );
  179. }
  180. }
  181. @rc;
  182. }
  183. 1;