summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject.pm
blob: fed0a8e60d9c0777b74fd35fe7b9ba7730150088 (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. Most methods are dynamically created. The following methods are static, however.
  7. =item make_object hashref, string,
  8. This creates a new data object instance based on information in the PostgreSQL
  9. catalogs.
  10. =back
  11. =cut
  12. use LedgerSMB;
  13. package LedgerSMB::DBObject;
  14. use strict;
  15. no strict 'refs';
  16. use warnings;
  17. sub make_object {
  18. my ($request, $name, $package_name) = @_;
  19. my $self = {};
  20. $self->{__dbh} = $request->{dbh};
  21. $self->{__name} = $name;
  22. $self->{__methods} = [];
  23. my $query =
  24. "SELECT proname, proargnames FROM pg_proc
  25. WHERE proname ilike ?";
  26. my $sth = $self->{__dbh}->prepare($query);
  27. $sth->execute("$name".'_%');
  28. my $ref;
  29. while ($ref = $sth->fetchrow_hashref(NAME_lc)){
  30. my $m_name = $ref->{proname};
  31. my $args = $ref->{proargnames};
  32. my $subcode
  33. if ($m_name ~= s/$name\_//){
  34. push @{$self->{__methods}}, $m_name;
  35. if ($args){
  36. $subcode = "sub {
  37. LedgerSMB::callproc($self->{proname}"
  38. for $arg (@$args){
  39. if ($arg =~ s/in_//){
  40. $subcode .= ", \$self->{$arg}";
  41. }
  42. }
  43. $subcode .= "); }"
  44. *{$package_name . "::" . $m_name}
  45. = eval $subcode;
  46. }
  47. else {
  48. $subcode = "sub {
  49. LedgerSMB::callproc($self->{proname}, ".
  50. "\@_); }"
  51. }
  52. }
  53. *{$package_name . "::" . $m_name} = eval $subcode;
  54. }
  55. }