summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject.pm
blob: 2a8d976b6a00260b4f0add8b07992daa920fbf89 (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. Provides the basic mapping of parameters to the SQL stored procedure function
  15. arguments.
  16. =item __validate__ is called on every new() invocation. It is blank in this
  17. module but can be overridden in decendant modules.
  18. =back
  19. =head1 Copyright (C) 2007, The LedgerSMB core team.
  20. This file is licensed under the Gnu General Public License version 2, or at your
  21. option any later version. A copy of the license should have been included with
  22. your software.
  23. =cut
  24. package LedgerSMB::DBObject;
  25. use Scalar::Util;
  26. use base qw(LedgerSMB);
  27. use strict;
  28. use warnings;
  29. sub __validate__ {}
  30. sub new {
  31. my $class = shift @_;
  32. my %args = (ref($_[0]) eq 'HASH')? %{$_[0]}: @_;
  33. my $base = $args{base};
  34. my $mode = $args{copy};
  35. my $self = bless {}, $class;
  36. my @mergelist;
  37. if ( defined $args{merge} ){
  38. @mergelist = @{ $args{merge} };
  39. } elsif (defined $mode && ( $mode eq 'list')) {
  40. $self->error('Mergelist not set');
  41. }
  42. else {
  43. @mergelist = [];
  44. }
  45. if ( !$base->isa('LedgerSMB') ) {
  46. $self->error("Constructor called without LedgerSMB object arg");
  47. }
  48. my $attr;
  49. if (lc($mode) eq 'base'){
  50. $self->merge($base, 'dbh', '_roles');
  51. }
  52. elsif (lc($mode) eq 'list'){
  53. $self->merge($base, @mergelist);
  54. }
  55. else {
  56. $self->merge($base);
  57. }
  58. $self->__validate__();
  59. return $self;
  60. }
  61. sub set_ordering {
  62. my $self = shift @_;
  63. my %args = @_;
  64. if (not defined $self->{_order_method}){
  65. $self->{_order_method} = {};
  66. }
  67. $self->{_order_method}->{$args{method}} = $args{column};
  68. }
  69. sub exec_method {
  70. my $self = shift @_;
  71. my %args = @_;
  72. my $funcname = $args{funcname};
  73. my @in_args;
  74. @in_args = @{ $args{args}} if $args{args};
  75. my @call_args;
  76. my $query = "SELECT proname, pronargs, proargnames FROM pg_proc WHERE proname = ?";
  77. my $sth = $self->{dbh}->prepare($query);
  78. $sth->execute($funcname) || $self->error($DBI::errstr . "in exec_method");
  79. my $ref;
  80. $ref = $sth->fetchrow_hashref('NAME_lc');
  81. my $args = $ref->{proargnames};
  82. my @proc_args;
  83. $ref->{pronargs} = 0 unless defined $ref->{pronargs};
  84. if ($ref->{pronargs}){
  85. $args =~ s/\{(.*)\}/$1/;
  86. @proc_args = split /,/, $args if $args;
  87. }
  88. if ( !$ref->{proname} ) { # no such function
  89. $self->error( "No such function: $funcname");
  90. # die;
  91. }
  92. if ($args) {
  93. for my $arg (@proc_args) {
  94. if ( $arg =~ s/^in_// ) {
  95. push @call_args, $self->{$arg};
  96. }
  97. }
  98. }
  99. for (@in_args) { push @call_args, $_ } ;
  100. $self->{call_args} = \@call_args;
  101. return $self->call_procedure( procname => $funcname, args => \@call_args );
  102. }
  103. sub run_custom_queries {
  104. my ( $self, $tablename, $query_type, $linenum ) = @_;
  105. my $dbh = $self->{dbh};
  106. if ( $query_type !~ /^(select|insert|update)$/i ) {
  107. # Commenting out this next bit until we figure out how the locale object
  108. # will operate. Chris
  109. #$self->error($locale->text(
  110. # "Passed incorrect query type to run_custom_queries."
  111. #));
  112. }
  113. my @rc;
  114. my %temphash;
  115. my @templist;
  116. my $did_insert;
  117. my @elements;
  118. my $query;
  119. my $ins_values;
  120. if ($linenum) {
  121. $linenum = "_$linenum";
  122. }
  123. $query_type = uc($query_type);
  124. for ( @{ $self->{custom_db_fields}{$tablename} } ) {
  125. @elements = split( /:/, $_ );
  126. push @{ $temphash{ $elements[0] } }, $elements[1];
  127. }
  128. for ( keys %temphash ) {
  129. my @data;
  130. $query = "$query_type ";
  131. if ( $query_type eq 'UPDATE' ) {
  132. $query = "DELETE FROM $_ WHERE row_id = ?";
  133. my $sth = $dbh->prepare($query);
  134. $sth->execute( $self->{ "id" . "$linenum" } )
  135. || $self->dberror($query);
  136. }
  137. elsif ( $query_type eq 'INSERT' ) {
  138. $query .= " INTO $_ (";
  139. }
  140. my $first = 1;
  141. for ( @{ $temphash{$_} } ) {
  142. $query .= "$_";
  143. if ( $query_type eq 'UPDATE' ) {
  144. $query .= '= ?';
  145. }
  146. $ins_values .= "?, ";
  147. $query .= ", ";
  148. $first = 0;
  149. if ( $query_type eq 'UPDATE' or $query_type eq 'INSERT' ) {
  150. push @data, $self->{"$_$linenum"};
  151. }
  152. }
  153. if ( $query_type ne 'INSERT' ) {
  154. $query =~ s/, $//;
  155. }
  156. if ( $query_type eq 'SELECT' ) {
  157. $query .= " FROM $_";
  158. }
  159. if ( $query_type eq 'SELECT' or $query_type eq 'UPDATE' ) {
  160. $query .= " WHERE row_id = ?";
  161. }
  162. if ( $query_type eq 'INSERT' ) {
  163. $query .= " row_id) VALUES ($ins_values ?)";
  164. }
  165. if ( $query_type eq 'SELECT' ) {
  166. push @rc, [$query];
  167. }
  168. else {
  169. unshift( @data, $query );
  170. push @rc, [@data];
  171. }
  172. }
  173. if ( $query_type eq 'INSERT' ) {
  174. for (@rc) {
  175. $query = shift( @{$_} );
  176. my $sth = $dbh->prepare($query)
  177. || $self->db_error($query);
  178. $sth->execute( @{$_}, $self->{id} )
  179. || $self->dberror($query);
  180. $sth->finish;
  181. $did_insert = 1;
  182. }
  183. }
  184. elsif ( $query_type eq 'UPDATE' ) {
  185. @rc = $self->run_custom_queries( $tablename, 'INSERT', $linenum );
  186. }
  187. elsif ( $query_type eq 'SELECT' ) {
  188. for (@rc) {
  189. $query = shift @{$_};
  190. my $sth = $self->{dbh}->prepare($query);
  191. $sth->execute( $self->{id} );
  192. my $ref = $sth->fetchrow_hashref('NAME_lc');
  193. $self->merge( $ref, keys(%$ref) );
  194. }
  195. }
  196. return @rc;
  197. }
  198. sub _parse_array {
  199. my ($self, $value) = @_;
  200. my $next;
  201. my $separator;
  202. my @return_array;
  203. while ($value ne '{}') {
  204. my $next;
  205. my $separator = "";
  206. if ($value =~ /^\{"/){
  207. while ($next eq "" or ($next =~ /\\".$/)){
  208. $value =~ s/^\{("[^"]*".)/\{/;
  209. $next .= $1;
  210. $next =~ /(.)$/;
  211. $separator = $1;
  212. $next .= "quoted";
  213. }
  214. $next =~ s/"(.*)"$separator$/$1/;
  215. } elsif ($value =~ /^{({+)/){
  216. my $open_braces = $1;
  217. my $close_braces = $open_braces;
  218. $close_braces =~ s/{/}/g;
  219. $value =~ /^{($open_braces[^}]*$close_braces)/;
  220. my $parse_next = $1;
  221. $value =~ s/^{$parse_next/{/;
  222. $value =~ s/^{,/{/;
  223. @$next = $self->_parse_array($parse_next);
  224. } else {
  225. $value =~ s/^\{([^,]*)(,|\})/\{/;
  226. $next = $1;
  227. $separator = $2;
  228. }
  229. $value .= '}' if $separator eq '}';
  230. $next =~ s/\\\\/\\/g;
  231. $next =~ s/\\"/"/g;
  232. push @return_array, $next;
  233. }
  234. return @return_array;
  235. }
  236. 1;