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