summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject.pm
blob: be6ea7efdf24d2053dcb0bb0f1d18c3d25beeb0a (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. my $m_name = $ref->{proname};
  93. if ($args) {
  94. for my $arg (@proc_args) {
  95. if ( $arg =~ s/^in_// ) {
  96. push @call_args, $self->{$arg};
  97. }
  98. }
  99. }
  100. for (@in_args) { push @call_args, $_ } ;
  101. $self->{call_args} = \@call_args;
  102. $self->debug({file => '/tmp/dbobject'});
  103. return $self->call_procedure( procname => $funcname, args => \@call_args );
  104. }
  105. sub run_custom_queries {
  106. my ( $self, $tablename, $query_type, $linenum ) = @_;
  107. my $dbh = $self->{dbh};
  108. if ( $query_type !~ /^(select|insert|update)$/i ) {
  109. # Commenting out this next bit until we figure out how the locale object
  110. # will operate. Chris
  111. #$self->error($locale->text(
  112. # "Passed incorrect query type to run_custom_queries."
  113. #));
  114. }
  115. my @rc;
  116. my %temphash;
  117. my @templist;
  118. my $did_insert;
  119. my @elements;
  120. my $query;
  121. my $ins_values;
  122. if ($linenum) {
  123. $linenum = "_$linenum";
  124. }
  125. $query_type = uc($query_type);
  126. for ( @{ $self->{custom_db_fields}{$tablename} } ) {
  127. @elements = split( /:/, $_ );
  128. push @{ $temphash{ $elements[0] } }, $elements[1];
  129. }
  130. for ( keys %temphash ) {
  131. my @data;
  132. $query = "$query_type ";
  133. if ( $query_type eq 'UPDATE' ) {
  134. $query = "DELETE FROM $_ WHERE row_id = ?";
  135. my $sth = $dbh->prepare($query);
  136. $sth->execute( $self->{ "id" . "$linenum" } )
  137. || $self->dberror($query);
  138. }
  139. elsif ( $query_type eq 'INSERT' ) {
  140. $query .= " INTO $_ (";
  141. }
  142. my $first = 1;
  143. for ( @{ $temphash{$_} } ) {
  144. $query .= "$_";
  145. if ( $query_type eq 'UPDATE' ) {
  146. $query .= '= ?';
  147. }
  148. $ins_values .= "?, ";
  149. $query .= ", ";
  150. $first = 0;
  151. if ( $query_type eq 'UPDATE' or $query_type eq 'INSERT' ) {
  152. push @data, $self->{"$_$linenum"};
  153. }
  154. }
  155. if ( $query_type ne 'INSERT' ) {
  156. $query =~ s/, $//;
  157. }
  158. if ( $query_type eq 'SELECT' ) {
  159. $query .= " FROM $_";
  160. }
  161. if ( $query_type eq 'SELECT' or $query_type eq 'UPDATE' ) {
  162. $query .= " WHERE row_id = ?";
  163. }
  164. if ( $query_type eq 'INSERT' ) {
  165. $query .= " row_id) VALUES ($ins_values ?)";
  166. }
  167. if ( $query_type eq 'SELECT' ) {
  168. push @rc, [$query];
  169. }
  170. else {
  171. unshift( @data, $query );
  172. push @rc, [@data];
  173. }
  174. }
  175. if ( $query_type eq 'INSERT' ) {
  176. for (@rc) {
  177. $query = shift( @{$_} );
  178. my $sth = $dbh->prepare($query)
  179. || $self->db_error($query);
  180. $sth->execute( @{$_}, $self->{id} )
  181. || $self->dberror($query);
  182. $sth->finish;
  183. $did_insert = 1;
  184. }
  185. }
  186. elsif ( $query_type eq 'UPDATE' ) {
  187. @rc = $self->run_custom_queries( $tablename, 'INSERT', $linenum );
  188. }
  189. elsif ( $query_type eq 'SELECT' ) {
  190. for (@rc) {
  191. $query = shift @{$_};
  192. my $sth = $self->{dbh}->prepare($query);
  193. $sth->execute( $self->{id} );
  194. my $ref = $sth->fetchrow_hashref('NAME_lc');
  195. $self->merge( $ref, keys(%$ref) );
  196. }
  197. }
  198. return @rc;
  199. }
  200. sub _parse_array {
  201. my ($self, $value) = @_;
  202. my $next;
  203. my $separator;
  204. my @return_array;
  205. while ($value ne '{}') {
  206. my $next = "";
  207. my $separator = "";
  208. if ($value =~ /^\{"/){
  209. while ($next eq "" or ($next =~ /\\".$/)){
  210. $value =~ s/^\{("[^"]*".)/\{/;
  211. $next .= $1;
  212. $next =~ /(.)$/;
  213. $separator = $1;
  214. }
  215. $next =~ s/"(.*)"$separator$/$1/;
  216. } elsif ($value =~ /^{({+})/){
  217. my $open_braces = $1;
  218. my $close_braces = $open_braces;
  219. $close_braces =~ s/{/}/g;
  220. $value =~ /^{($open_braces.*$close_braces)/;
  221. $next = $1;
  222. $value =~ s/^{$next/{/;
  223. $next = $self->parse_array($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;