summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject.pm
blob: d91c48996ace3b3004043d478575d0d24dc18700 (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. =item find_method ($hashref, $function_name, @args)
  7. =item merge ($hashref, @attrs)
  8. copies @attrs from $hashref to $self.
  9. =head1 Copyright (C) 2007, The LedgerSMB core team.
  10. This file is licensed under the Gnu General Public License version 2, or at your
  11. option any later version. A copy of the license should have been included with
  12. your software.
  13. =back
  14. =cut
  15. package LedgerSMB::DBObject;
  16. use LedgerSMB;
  17. use strict;
  18. no strict 'refs';
  19. use warnings;
  20. our @ISA = qw(LedgerSMB);
  21. sub new {
  22. my $self = shift @_;
  23. my $lsmb = shift @_;
  24. if (! $lsmb->isa('LedgerSMB')){
  25. $self->error("Constructor called without LedgerSMB object arg");
  26. }
  27. $self = {};
  28. my $attr;
  29. for $attr (keys %{$lsmb}){
  30. $self->{$attr} = $lsmb->{$attr};
  31. }
  32. bless $self;
  33. }
  34. sub exec_method {
  35. my ($self) = shift @_;
  36. my ($funcname) = shift @_;
  37. my $query =
  38. "SELECT proname, proargnames FROM pg_proc WHERE proname = ?";
  39. my $sth = $self->{dbh}->prepare($query);
  40. $sth->execute($funcname);
  41. my $ref;
  42. $ref = $sth->fetchrow_hashref('NAME_lc');
  43. my $args = $ref->{proargnames};
  44. $args =~ s/\{(.*)\}/$1/;
  45. my @proc_args = split /,/, $args;
  46. if (!$ref){ # no such function
  47. $self->error("No such function: ", $funcname);
  48. die;
  49. }
  50. my $m_name = $ref->{proname};
  51. my @call_args;
  52. if ($args){
  53. for my $arg (@proc_args){
  54. if ($arg =~ s/^in_//){
  55. push @call_args, $self->{$arg};
  56. }
  57. }
  58. }
  59. else {
  60. @call_args = @_;
  61. }
  62. $self->callproc($funcname, @call_args);
  63. }
  64. 1;