[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SF.net SVN: ledger-smb:[5446] trunk/LedgerSMB/Database.pm



Revision: 5446
          http://ledger-smb.svn.sourceforge.net/ledger-smb/?rev=5446&view=rev
Author:   einhverfr
Date:     2012-12-31 11:03:40 +0000 (Mon, 31 Dec 2012)
Log Message:
-----------
Database enhancements for listing dbs and copying one to another for test purposes

Modified Paths:
--------------
    trunk/LedgerSMB/Database.pm

Modified: trunk/LedgerSMB/Database.pm
===================================================================
--- trunk/LedgerSMB/Database.pm	2012-12-31 07:16:33 UTC (rev 5445)
+++ trunk/LedgerSMB/Database.pm	2012-12-31 11:03:40 UTC (rev 5446)
@@ -22,6 +22,7 @@
 # Methods are documented inline.  
 
 package LedgerSMB::Database;
+use DBI;
 
 our $VERSION = '1';
 
@@ -275,7 +276,6 @@
 =cut 
 
 sub get_info {
-    use DBI;
     use LedgerSMB::Auth;
     my $self = shift @_;
     my $retval = { # defaults
@@ -397,6 +397,32 @@
     return $retval;
 }
 
+=item $db->list()
+
+Lists available databases except for those named "postgres" or starting with
+"template"
+
+Returns a list of strings of db names.
+
+=cut
+
+sub list {
+    my ($self) = @_;
+    my $creds = LedgerSMB::Auth->get_credentials();
+    my $dbh = DBI->connect(
+        "dbi:Pg:dbname=postgres", 
+         "$creds->{login}", "$creds->{password}", { AutoCommit => 0 }
+    );
+    my @results = $dbh->selectall_array(
+        "SELECT datname FROM pg_database 
+          WHERE datname <> 'postgres' AND datname NOT LIKE 'template%'"
+    );
+    $dbh->disconnect;
+    return @results;
+}
+
+
+    
 =item $db->create();
 
 Creates a database and loads the contrib files.  This is done from template0, 
@@ -425,7 +451,6 @@
     # 
     # Hat tip:  irc user RhodiumToad on #postgresql -- CT
 
-    use DBI;
     my $dbh = DBI->connect('dbi:Pg:dbname=postgres');
 
     $dbh->{RaiseError} = 1;
@@ -459,6 +484,21 @@
      return $rc;
 }
 
+=item $db->copy('new_name')
+
+Copies the existing database to a new name.
+
+=cut
+
+sub copy {
+    my ($self, $new_name) = @_;
+    my $dbh = DBI->connect('dbi:Pg:dbname=postgres');
+    my $dbname = $dbh->quote_ident($self->{dbname});
+    $new_name = $dbh->quote_ident($new_name);
+    $dbh->do("CREATE DATABASE $new_name WITH TEMPLATE $dbname");
+    $dbh->disconnect;
+}        
+
 =item $db->load_modules($loadorder)
 
 Loads or reloads sql modules from $loadorder

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.