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