<?php
/**
* PmWiki module to provide a standard connection interface for PmWiki recipes
* to the ADOdb abstraction layer, http://adodb.sourceforge.net .  If a connection
* has already been opened by another recipe, it will not be opened a second time.
* This recipe does not do anything on its own and is not intended to be included
* directly in config.php.  Please see below for usage instructions.
* copyleft 2006-11-01 by Ben Stallings <Ben@InterdependentWeb.com> .
*/

/** CONFIGURATION
* You must define the $Databases variable to match your database(s).  Copy and
* paste the six lines below into your config.php and change the values.
* If you have more than one database to connect, simply repeat as necessary,
* being sure to use a different connection_name each time.
* See http://phplens.com/adodb/supported.databases.html for the list of valid 
* driver names.
*
$Databases['connection_name'] = array(
 'driver' => 'mysql',
 'hostname' => 'db.example.net',
 'database' => 'database_name',
 'username' => 'user_name',
 'password' => 'example_password');
*
* For an LDAP server ('driver' => 'ldap'), the database parameter should be the
* ldapbase, for example 'ou=People,o=Baylor University,c=US' .
* For SQLite ('driver' => 'sqlite'), the database parameter should be the path
* to the database on your hard disk, for example 'c:\path\to\sqlite.db' .
*
* To override any of the default variables defined below, simply define them in
* your local/config.php.  For example,
* $ADOdbLocation = '/www/adodb/adodb.inc.php';
*/

SDV($ADOdbLocation, "$FarmD/cookbook/adodb/");

// End of variable definitions.

/** USAGE
* within a cookbook recipe, simply paste these lines:
* 
include_once "$FarmD/cookbook/adodb-connect.php";
ADOdbConnect('Database_Name');
*
* The database will be assigned to the global object $DB['Database_Name'].
* If another recipe has already opened a connection to $DB['Database_Name'],
* the function will return TRUE and take no further action.
*
* The ADOdbConnect function returns either TRUE (if successful) or an error message.
* To handle the error, you might do something like this instead of the function
* call above:
*
$out = ADOdbConnect('Database_Name');
if ($out !== TRUE) return $out;
*/
function ADOdbConnect($dbName) {
 global $ADOdbLocation, $Databases, $DB;
 include_once $ADOdbLocation.'adodb.inc.php';
 if (!function_exists('ADONewConnection')) return "Unable to include adodb.inc.php";
 if (is_object($DB[$dbName])) {
  if ($DB[$dbName]->IsConnected()) return TRUE;
 }

 // handle different types of databases
 // these two types can use the standard DSN connection with minor modification
 if ($Databases[$dbName]['driver'] == 'pdo') {
  $Databases[$dbName]['driver'] = "pdo_mysql";
 } elseif ($Databases[$dbName]['driver'] == 'sqlite') {
  $Databases[$dbName]['database'] = urlencode($Databases[$dbName]['database']);
 }
 // these need special connections
 
 if ($Databases[$dbName]['driver'] == 'access') {
  $DB[$dbName] =& ADONewConnection('access');
  $dsn = "Driver={Microsoft Access Driver (*.mdb)};Dbq=".$Databases[$dbName]['database']
  . ";Uid=".$Databases[$dbName]['username'].";Pwd=".$Databases[$dbName]['password'].";";
  if (!$DB[$dbName]->Connect($dsn)) return "Unable to connect to database: ".$DB[$dbName]->ErrorMsg();
 } elseif (($Databases[$dbName]['driver'] == 'mssql')
  or ($Databases[$dbName]['driver'] == 'odbc_mssql')) {
  $DB[$dbName] =& ADONewConnection('odbc_mssql');
  $dsn = "Driver={SQL Server};Server=".$Databases[$dbName]['hostname']
  . ";Database=".$Databases[$dbName]['database'].";";
  if (!$DB[$dbName]->Connect($dsn,$Databases[$dbName]['username'],
    $Databases[$dbName]['password'])) return "Unable to connect to database: ".$DB[$dbName]->ErrorMsg();
 } elseif ($Databases[$dbName]['driver'] == 'db2') {
  $DB[$dbName] =& ADONewConnection('db2');
  $dsn = "driver={IBM db2 odbc DRIVER};Database="
  . $Databases[$dbName]['database'].";hostname=".$Databases[$dbName]['hostname']
  . ";port=50000;protocol=TCPIP;uid=".$Databases[$dbName]['username']
  . "; pwd=".$Databases[$dbName]['password'];
  if (!$DB[$dbName]->Connect($dsn)) return "Unable to connect to database: ".$DB[$dbName]->ErrorMsg();
 } else {
  // standard DSN connection
  $dsn = $Databases[$dbName]['driver'].'://'
  . ($Databases[$dbName]['username'] ? $Databases[$dbName]['username'].':'
  . $Databases[$dbName]['password'].'@' : '')
  . ($Databases[$dbName]['hostname'] ? $Databases[$dbName]['hostname'].'/' : '')
  . $Databases[$dbName]['database'];
  $DB[$dbName] = ADONewConnection($dsn);
  if (!is_object($DB[$dbName])) return "Unable to connect to database: ".$DB[$dbName]->ErrorMsg();
 }
 if ($DB[$dbName]->IsConnected()) {
  return TRUE;
 } else {
  return "Database not connected: ".$DB[$dbName]->ErrorMsg();
 }
}
?>