NConf::DB::dbConnect

User / Community contributed content: modifications, patches, extensions, modules, scripts etc.

NConf::DB::dbConnect

Postby benediktkoeppel » Wed Dec 14, 2011 22:07

Hi

I want to use the NConf Perl API without having a mysql.php file lying around, because I use the API on a different host than the NConf Web runs. Therefore, I have added optional paramters to the NConf::DB::dbConnect function.

Code: Select all
diff --git a/lib/NConf/DB.pm b/lib/NConf/DB.pm
index e126092..4a8598c 100644
--- a/lib/NConf/DB.pm
+++ b/lib/NConf/DB.pm
@@ -51,8 +51,18 @@ sub dbConnect {
     &logger(5,"Entered dbConnect()");
 
     # SUB use: connect to NConf database, if not yet connected
+    # The parameters are optional (either all or none). If the parameter are
+    # omitted, dbConnect will read the configuration from the mysql.php config
 
     # SUB specs: ###################
+    # 0: dbhost: the hostname of the database (optional)
+    # 1: dbname: the database name (optional)
+    # 2: dbuser: the username to connect to the database (optional)
+    # 3: dbpass: the database password (optional)
+    my $dbhost = shift;
+    my $dbname = shift;
+    my $dbuser = shift;
+    my $dbpass = shift;
 
     # Return values:
     # 0: returns a DBI:mysql database handle
@@ -65,10 +75,14 @@ sub dbConnect {
         return $NC_dbh;
     }
 
-    my $dbhost = &readNConfConfig(NC_CONFDIR."/mysql.php","DBHOST","scalar");
-    my $dbname = &readNConfConfig(NC_CONFDIR."/mysql.php","DBNAME","scalar");
-    my $dbuser = &readNConfConfig(NC_CONFDIR."/mysql.php","DBUSER","scalar");
-    my $dbpass = &readNConfConfig(NC_CONFDIR."/mysql.php","DBPASS","scalar");
+    unless($dbhost && $dbname && $dbuser && $dbpass){
+        &logger(4,"readNConfConfig(): No arguments specified on dbConnect call, reading from config file.");
+
+        $dbhost = &readNConfConfig(NC_CONFDIR."/mysql.php","DBHOST","scalar");
+        $dbname = &readNConfConfig(NC_CONFDIR."/mysql.php","DBNAME","scalar");
+        $dbuser = &readNConfConfig(NC_CONFDIR."/mysql.php","DBUSER","scalar");
+        $dbpass = &readNConfConfig(NC_CONFDIR."/mysql.php","DBPASS","scalar");
+    }
 
     &logger(4,"Connecting to database '$dbname' on host '$dbhost'");
     my $dsn = "DBI:mysql:database=$dbname;host=$dbhost";


Usage:
You can continue to use the library as is, without calling dbConnect manually. If you want though, you can call NConf::DB::dbConnect("hostname", "dbname", "dbuser", "dbpass"); right after including NConf::DB. The call has to be made before the first database operation is used, otherwise NConf defaults back to to using the mysql.php config file.
Code: Select all
#!/usr/bin/env perl

use strict;
use warnings;

use NConf;
setLoglevel(5);
use NConf::DB;
use NConf::DB::Read;
use NConf::DB::Modify;
use NConf::Logger;
NConf::DB::dbConnect("hostname", "dbname", "dbuser", "dbpass");
NConf::Logger::logger(3, "NConf libraries loaded");


Regards,
Benedikt

--
benediktkoeppel.ch
benediktkoeppel
 

Return to Contributions

Who is online

Users browsing this forum: No registered users and 1 guest

cron