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