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