summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject.pm
blob: 071acb68b5ca37e22851b045747c4cb2adf243d4 (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. @ISA = (LedgerSMB);
  21. sub new {
  22. my $lsmb = shift @_;
  23. if (! $lsmb->isa(LedgerSMB)){
  24. $self->error("Constructor called without LedgerSMB object arg");
  25. my $self = {};
  26. for $attr (keys $lsmb){
  27. $self->{$attr} = $lsmb->{$attr};
  28. }
  29. bless $self;
  30. }
  31. sub exec_method {
  32. my ($self) = shift @_;
  33. my ($funcname) = shift @_;
  34. my $query =
  35. "SELECT proname, proargnames FROM pg_proc WHERE proname = ?";
  36. my $sth = $self->{__dbh}->prepare($query);
  37. $sth->execute($funcname);
  38. my $ref;
  39. $ref = $sth->fetchrow_hashref(NAME_lc);
  40. if (!$ref){ # no such function
  41. $self->error($locale->text("No such function: ") .$funcname;
  42. die;
  43. }
  44. my $m_name = $ref->{proname};
  45. my $args = $ref->{proargnames};
  46. my @proc_args;
  47. if ($m_name ~= s/$name\_//){
  48. push @{$self->{__methods}}, $m_name;
  49. if ($args){
  50. for $arg (@$args){
  51. if ($arg =~ s/^in_//){
  52. push @proc_args, $ref->{$arg};
  53. }
  54. }
  55. }
  56. else {
  57. @proc_args = @_;
  58. }
  59. }
  60. $self->callproc($funcname, @proc_args);
  61. }