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