<?php
$ADOPATH = 'adodb/adodb.inc.php';

# try to load alternative configuration if necessary - should be sufficient to 
# support multiple databases, hosts etc. {{{
if ($_SERVER['SERVER_ADDR'] != '127.0.0.1') {
    $ALTCONFIG = 'altConfig.inc.php';
    include($ALTCONFIG);
}
# }}}


# load ADO library
include($ADOPATH);	   # load code common to ADOdb

require_once('config.inc.php');   # load database library


# all results columns will be referred by name
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;


# connects to the database, returns the db connection
function connect($persistent = true)
{
    if ($persistent)
        $options = '?persist';
    else
        $options = '';

    $db= ADONewConnection('postgres');

    $connectionString = "user=" . BenchConfig::$dbUser . " port=" . BenchConfig::$dbPort . " dbname=" . BenchConfig::$dbName;

    $db->connect($connectionString);
    
    if (!$db) 
        die("Connection failed");

    #$db->debug = true;

    return $db;
}


# create table of name $tableName, with fields $fields and options $tabOptArray
function createTable($tableName, $fields, $tabOptArray=false)
{
    $db = connect();
    $dict = NewDataDictionary($db);
    $sqlArray = $dict->CreateTableSQL($tableName, $fields, $tabOptArray);
    print_r($sqlArray);
    $result = $dict->ExecuteSQLArray($sqlArray);
    print($result);
}


# drops table by name
function dropTable($tableName)
{
    $db = connect();
    $dict = NewDataDictionary($db);
    $sqlArray = $dict->DropTableSQL($tableName);
    $dict->ExecuteSQLArray($sqlArray);    
}
?>
