<?php
/* __ Defines _____________________________________________________________ */
require_once('config.php');
define('DATABASE_BACKEND', 'MySQL');
/* __ MySQL implementation ________________________________________________ */
/*
*
*/
function ttdb_connect(&$connection)
{
$connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
//echo "[connect: $connection] ";
if (!mysql_select_db(DB_DATABASE))
return false;
return true;
}
/*
*
*/
function ttdb_close($connection)
{
//echo "[close: $connection] ";
if ($connection)
return mysql_close($connection);
}
/*
*
*/
function ttdb_beginTransaction($connection)
{
if ($connection)
return mysql_query("begin", $connection);
}
/*
*
*/
function ttdb_commitTransaction($connection)
{
if ($connection)
return mysql_query("commit", $connection);
}
/*
*
*/
function ttdb_rollbackTransaction($connection)
{
if ($connection)
return mysql_query("rollback", $connection);
}
/*
*
*/
function ttdb_execQuery($connection, $query)
{
//echo "[$query] ";
$res = mysql_query($query, $connection);
return $res;
}
/*
* Params: resource is the query to get numrows from.
*/
function ttdb_getNumRows($resource)
{
return mysql_num_rows($resource);
}
/*
* Params: resource is the query to get fields from.
*/
function ttdb_getNumFields($resource)
{
return mysql_num_fields($resource);
}
/*
* Returns: an array of fields with the current row (field names are key values)
*/
function ttdb_getArray($resource)
{
return mysql_fetch_assoc($resource);
}
/*
* 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)
{
mysql_data_seek($resource, $rowNum);
return mysql_fetch_assoc($resource);
}
/*
* Returns:
*/
function ttdb_getFieldName($resource, $field)
{
return mysql_field_name($resource, $field);
}
/*
* Returns:
*/
function ttdb_getFieldType($resource, $field)
{
return mysql_field_type($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;
}
?>