summaryrefslogtreecommitdiff
path: root/LedgerSMB/Database.pm
blob: 0bfc2666989f5216d836513d12c40b180086de51 (plain)
  1. #!/usr/bin/perl
  2. =head1 NAMR
  3. LedgerSMB::Database
  4. =head1 SYNOPSIS
  5. This module provides the APIs for database creation and management
  6. =head1 COPYRIGHT
  7. This module is copyright (C) 2007, the LedgerSMB Core Team and subject to
  8. the GNU General Public License (GPL) version 2, or at your option, any later
  9. version. See the COPYRIGHT and LICENSE files for more information.
  10. =head1 METHODS
  11. =over
  12. =cut
  13. # Methods are documented inline.
  14. package LedgerSMB::Database;
  15. our $VERSION = '0';
  16. use LedgerSMB::Sysconfig;
  17. use base('LedgerSMB');
  18. =item LedgerSMB::Database->new({dbname = $dbname, countrycode = $cc, chart_name = $name, company_name = $company, username = $username, password = $password})
  19. This function creates a new database management object with the specified
  20. characteristics. The $dbname is the name of the database. the countrycode
  21. is the two-letter ISO code. The company name is the friendly name for
  22. dropdown boxes on the Login screen.
  23. As some countries may have multiple available charts, you can also specify
  24. a chart name as well.
  25. Note that the arguments can be any hashref. If it is a LedgerSMB object,
  26. however, it will attempt to copy all attributes beginning with _ into the
  27. current object (_user, _locale, etc).
  28. =cut
  29. sub new {
  30. my ($class, $args) = @_;
  31. my $self = {};
  32. for (qw(dbname countrycode chart_name company_name username password)){
  33. $self->{$_} = $args->{$_};
  34. }
  35. if (isa($args, 'LedgerSMB')){
  36. for (keys %$args){
  37. if ($_ =~ /^_/){
  38. $self->{$_} = $arg->{$_};
  39. }
  40. }
  41. }
  42. bless $self, $class;
  43. return $self;
  44. }
  45. =item $db->create();
  46. Creates a database with the characteristics in the object
  47. =cut
  48. sub create {
  49. my $self = (@_);
  50. $self->_init_environment();
  51. `createdb $self->{dbname}`;
  52. my $error = $!;
  53. if ($error){
  54. $self->error($!);
  55. }
  56. for (qw(Database Central)){
  57. $self->_execute_script("Pg-$_.sql");
  58. }
  59. my $chart_path = "sql/$self->{country_code}/";
  60. $self->_execute_script(
  61. "coa/$self->{country_code}/chart/$self->{chart_name}"
  62. );
  63. my @gifis = glob('sql/$self->{country_code}/gifi/*.sql');
  64. my @gifi_search;
  65. my $search_string = $self->{chart_name};
  66. while ($search_string and (scalar @gifi_search == 0)){
  67. @gifi_search = grep /^$search_string.sql$/, @gifis;
  68. if (scalar @gifi_search == 0){
  69. if ($search_string !~ /[_-]/){
  70. $search_string = "";
  71. } else {
  72. $search_string =~ s/(.*)[_-].*$/$1/;
  73. }
  74. }
  75. }
  76. if (! scalar @gifi_search){
  77. push @gifi_search, 'Default';
  78. }
  79. my $gifi = $gifi_search[0];
  80. $gifi =~ s/\.sql$//;
  81. $self->_execute_script("coa/$self->{country_code}/gifi/$gifi");
  82. $self->_create_roles();
  83. }
  84. # Private method. Executes the sql script in psql.
  85. sub _execute_script {
  86. my ($self, $script) = @_;
  87. `psql $self->{dbname} < 'sql/$script.sql'`;
  88. return $!;
  89. }
  90. sub _create_roles {
  91. #TODO
  92. }
  93. sub update {
  94. # TODO
  95. }
  96. =back