Location: PHPKode > projects > Druid Defect Control > defectcontrol_0.9.4/tts/database-postgres.php
<?php


/* This file is meant to be a wrapper implementation, so changing DB 
 * backend is more easily supported. 
 */

/* __ Defines _____________________________________________________________ */

require_once('config.php');

define('DATABASE_BACKEND', 'PostgreSQL');


/* __ postgreSQL implementation ___________________________________________ */


// Initialize an empty array for storing current records for every recordset in the current page

$recordsetPositions = array();


/* 
 * 
 */
function ttdb_connect(&$connection)
{
    // By not specifying host, we can use unix sockets if running local
    
    if (DB_SERVER == "")
        $connect = "dbname=".DB_DATABASE." user=".DB_USER." password=".DB_PASS;
    else
        $connect = "host=".DB_SERVER." dbname=".DB_DATABASE." user=".DB_USER." password=".DB_PASS;    
    
    $connection = pg_connect ($connect);
    
    return $connection ? true : false;
    
}

/* 
 * 
 */
function ttdb_close($connection)
{
    if ($connection) 
        return pg_close($connection);
}

/* 
 * 
 */
function ttdb_beginTransaction($connection)
{
    return pg_exec ($connection, "begin");
}

/* 
 * 
 */
function ttdb_commitTransaction($connection)
{
    if ($connection) 
        return pg_exec ($connection, "commit");
}

/* 
 * 
 */
function ttdb_rollbackTransaction($connection)
{
    if ($connection) 
        return pg_exec ($connection, "rollback");
}

/* 
 * 
 */
function ttdb_execQuery($connection, $query)
{
    global $recordsetPositions;
    
    if ($connection) {
        
        $res = pg_exec($connection, $query);
        $recordsetPositions[$res] = 0;          // Initialize current record position for this recordset
        
        //echo "[$query] ";
        //echo " <b>[setting position for $res]</b> ";
        
        return $res;
    }
    
    return 0;
}

/* 
 * Params: resource is the query to get numrows from.
 */
function ttdb_getNumRows($resource)
{
    return pg_numrows($resource);
}

/* 
 * Params: resource is the query to get fields from.
 */
function ttdb_getNumFields($resource)
{
    return pg_numfields($resource);
}


/* 
 * Returns: an array of fields with the current row (field names are key values)
 */
function ttdb_getArray($resource)
{    
    
    global $recordsetPositions;
    
    $currRec = $recordsetPositions[$resource]; 
    
    //echo "$resource, total recs: ".pg_numrows($resource)." "; print_r($recordsetPositions); echo "end recordset positions<br>";
    
    if ($currRec < pg_numrows($resource)) {
        
        $fields = pg_fetch_array($resource, $currRec,  PGSQL_ASSOC); 
        $recordsetPositions[$resource]++;
        return $fields;     
        
    } else
        return false; 
    
}

/* 
 * Returns: an array of fields with the given row 
 * NOTE: this function is not implemented in every layer, and thus, not portable!!
 */
function ttdb_getArrayEx($resource, $rowNum)
{
    return pg_fetch_array($resource, $rowNum,  PGSQL_ASSOC);
}


/* 
 * Returns: 
 */

/* Depreciate...
function ttdb_getFieldValue($resource, $rowNum, $field)
{
    return pg_result($resource, $rowNum, $field);
}
*/

/* 
 * Returns: 
 */
function ttdb_getFieldName($resource, $field)
{
    return pg_fieldname($resource, $field);
}


/* 
 * Returns: 
 */
function ttdb_getFieldType($resource, $field)
{
    return pg_fieldtype($resource, $field);
}


/*
 * Note: this one is portable between versions, as only uses calls
 *  from the abstraction layer.
 */
function ttdb_getSequenceValue($connection, $table)
{

    // Get current value from the DB

    $res = ttdb_execQuery($connection, "SELECT * FROM sequences WHERE stable = '$table'");    

    // This uses the new approach for retrieving records !!
    $fields = ttdb_getArray($res);
    
    if (!is_array($fields)) 
        return 0;           // Error, most likely table name didnĀ“t exist 

    $currValue = $fields['inextval'];

    
    // Check correctness of the value
    
    if ($currValue > 0) 
        $result = $currValue;
    else 
        return 0;           // Error, should always be 1 or more

    
    // Increment for the next time and update in the sequences table
        
    $currValue++;
    
    $res = ttdb_execQuery($connection, "UPDATE sequences SET inextval = $currValue WHERE ".
            "stable = '$table'");
            
    if ($res == false) 
        return 0;         // Error updating
    else
        return $result;

}

?>
Return current item: Druid Defect Control