summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject.pm
blob: 5bd6741d9fb276f15c5e66823623157f2549ef87 (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, keys => ['dbh', '_roles', '_user', '_locale']);
  54. }
  55. elsif (lc($mode) eq 'list'){
  56. $self->merge($base, keys => ['dbh', '_roles', '_user', '_locale']);
  57. $self->merge($base, keys => \@mergelist);
  58. }
  59. else {
  60. $self->merge($base);
  61. }
  62. $self->__validate__();
  63. return $self;
  64. }
  65. sub set_ordering {
  66. my $self = shift @_;
  67. my %args = @_;
  68. if (not defined $self->{_order_method}){
  69. $self->{_order_method} = {};
  70. }
  71. $self->{_order_method}->{$args{method}} = $args{column};
  72. }
  73. sub exec_method {
  74. my $self = shift @_;
  75. my %args = @_;
  76. my $funcname = $args{funcname};
  77. my @in_args;
  78. @in_args = @{ $args{args}} if $args{args};
  79. my @call_args;
  80. my $query = "
  81. SELECT proname, pronargs, proargnames FROM pg_proc
  82. WHERE proname = ?
  83. AND pronamespace =
  84. coalesce((SELECT oid FROM pg_namespace WHERE nspname = ?),
  85. pronamespace)
  86. ";
  87. my $sth = $self->{dbh}->prepare(
  88. $query
  89. );
  90. $sth->execute($funcname, $LedgerSMB::Sysconfig::db_namespace)
  91. || $self->error($DBI::errstr . "in exec_method");
  92. my $ref;
  93. $ref = $sth->fetchrow_hashref('NAME_lc');
  94. my $pargs = $ref->{proargnames};
  95. my @proc_args;
  96. if ( !$ref->{proname} ) { # no such function
  97. # If the function doesn't exist, $funcname gets zeroed?
  98. $self->error( "No such function: $funcname");
  99. # die;
  100. }
  101. $ref->{pronargs} = 0 unless defined $ref->{pronargs};
  102. # If the user provided args..
  103. if (!defined $args{args}) {
  104. @proc_args = $self->_parse_array($pargs);
  105. if (@proc_args) {
  106. for my $arg (@proc_args) {
  107. if ( $arg =~ s/^in_// ) {
  108. push @call_args, $self->{$arg};
  109. }
  110. }
  111. }
  112. for (@in_args) { push @call_args, $_ } ;
  113. $self->{call_args} = \@call_args;
  114. return $self->call_procedure( procname => $funcname, args => \@call_args );
  115. }
  116. else {
  117. return $self->call_procedure( procname => $funcname, args => \@in_args );
  118. }
  119. }
  120. sub run_custom_queries {
  121. my ( $self, $tablename, $query_type, $linenum ) = @_;
  122. my $dbh = $self->{dbh};
  123. if ( $query_type !~ /^(select|insert|update)$/i ) {
  124. # Commenting out this next bit until we figure out how the locale object
  125. # will operate. Chris
  126. #$self->error($locale->text(
  127. # "Passed incorrect query type to run_custom_queries."
  128. #));
  129. }
  130. my @rc;
  131. my %temphash;
  132. my @templist;
  133. my $did_insert;
  134. my @elements;
  135. my $query;
  136. my $ins_values;
  137. if ($linenum) {
  138. $linenum = "_$linenum";
  139. }
  140. $query_type = uc($query_type);
  141. for ( @{ $self->{custom_db_fields}{$tablename} } ) {
  142. @elements = split( /:/, $_ );
  143. push @{ $temphash{ $elements[0] } }, $elements[1];
  144. }
  145. for ( keys %temphash ) {
  146. my @data;
  147. $query = "$query_type ";
  148. if ( $query_type eq 'UPDATE' ) {
  149. $query = "DELETE FROM $_ WHERE row_id = ?";
  150. my $sth = $dbh->prepare($query);
  151. $sth->execute( $self->{ "id" . "$linenum" } )
  152. || $self->dberror($query);
  153. }
  154. elsif ( $query_type eq 'INSERT' ) {
  155. $query .= " INTO $_ (";
  156. }
  157. my $first = 1;
  158. for ( @{ $temphash{$_} } ) {
  159. $query .= "$_";
  160. if ( $query_type eq 'UPDATE' ) {
  161. $query .= '= ?';
  162. }
  163. $ins_values .= "?, ";
  164. $query .= ", ";
  165. $first = 0;
  166. if ( $query_type eq 'UPDATE' or $query_type eq 'INSERT' ) {
  167. push @data, $self->{"$_$linenum"};
  168. }
  169. }
  170. if ( $query_type ne 'INSERT' ) {
  171. $query =~ s/, $//;
  172. }
  173. if ( $query_type eq 'SELECT' ) {
  174. $query .= " FROM $_";
  175. }
  176. if ( $query_type eq 'SELECT' or $query_type eq 'UPDATE' ) {
  177. $query .= " WHERE row_id = ?";
  178. }
  179. if ( $query_type eq 'INSERT' ) {
  180. $query .= " row_id) VALUES ($ins_values ?)";
  181. }
  182. if ( $query_type eq 'SELECT' ) {
  183. push @rc, [$query];
  184. }
  185. else {
  186. unshift( @data, $query );
  187. push @rc, [@data];
  188. }
  189. }
  190. if ( $query_type eq 'INSERT' ) {
  191. for (@rc) {
  192. $query = shift( @{$_} );
  193. my $sth = $dbh->prepare($query)
  194. || $self->db_error($query);
  195. $sth->execute( @{$_}, $self->{id} )
  196. || $self->dberror($query);
  197. $sth->finish;
  198. $did_insert = 1;
  199. }
  200. }
  201. elsif ( $query_type eq 'UPDATE' ) {
  202. @rc = $self->run_custom_queries( $tablename, 'INSERT', $linenum );
  203. }
  204. elsif ( $query_type eq 'SELECT' ) {
  205. for (@rc) {
  206. $query = shift @{$_};
  207. my $sth = $self->{dbh}->prepare($query);
  208. $sth->execute( $self->{id} );
  209. my $ref = $sth->fetchrow_hashref('NAME_lc');
  210. $self->merge( $ref, keys(%$ref) );
  211. }
  212. }
  213. return @rc;
  214. }
  215. sub _parse_array {
  216. my ($self, $value) = @_;
  217. return @$value if ref $value eq 'ARRAY';
  218. return if !defined $value;
  219. my $next;
  220. my $separator;
  221. my @return_array;
  222. while ($value ne '{}') {
  223. $next = "";
  224. $separator = "";
  225. if ($value =~ /^\{"/){
  226. $value =~ s/^\{"(([^"]|\\")*[^\\])"/\{/;
  227. $next = $1;
  228. $next =~ /(.)$/;
  229. $value =~ s/^{,/{/;
  230. } elsif ($value =~ /^{({+)/){
  231. my $open_braces = $1;
  232. $next = [];
  233. my $close_braces = $open_braces;
  234. $close_braces =~ s/{/}/g;
  235. $value =~ /^{($open_braces[^}]*$close_braces)/;
  236. my $parse_next = $1;
  237. $value =~ s/^{$parse_next,?/{/;
  238. @$next = $self->_parse_array($parse_next);
  239. } else {
  240. $value =~ s/^\{([^,]*)(,|\})/\{/;
  241. $next = $1;
  242. $separator = $2;
  243. }
  244. $value .= '}' if $separator eq '}';
  245. $next =~ s/\\\\/\\/g;
  246. $next =~ s/\\"/"/g;
  247. push @return_array, $next;
  248. }
  249. return @return_array;
  250. }
  251. sub _db_array_scalars {
  252. my $self = shift @_;
  253. my @args = @_;
  254. for my $arg (@args){
  255. $arg =~ s/(["{},])/\\$1/g;
  256. if ($arg =~ /(\s|\\)/){
  257. $arg = qq|"$arg"|;
  258. }
  259. }
  260. return $self->_db_array_literal(@args);
  261. }
  262. sub _db_array_literal {
  263. my $self = shift @_;
  264. my @args = @_;
  265. my $return_string = '{}';
  266. for my $arg (@args){
  267. if ($return_string eq '{}'){
  268. $return_string = "{$arg}";
  269. }
  270. else {
  271. $return_string =~ s/\}$/,$arg\}/
  272. }
  273. }
  274. return $return_string;
  275. }
  276. 1;