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