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