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