Location: PHPKode > projects > Druid Defect Control > defectcontrol_0.9.4/tts/database-test.php
<?php ob_start();

require_once 'database-mysql.php'; 

?>


<?php


/*  

// This is some kind of test template for the migration from the current postgres-based 
// database abstraction layer to a more generic one supporting several other DB backends. 

function ttdb_connect(&$connection)
function ttdb_close($connection)
function ttdb_beginTransaction($connection)
function ttdb_commitTransaction($connection)
function ttdb_rollbackTransaction($connection)
function ttdb_execQuery($connection, $query)
function ttdb_getNumRows($resource)         // Seguramente nos conduzca a todos los bucles que hay que modificar
function ttdb_getNumFields($resource)       // Admin interface, query results

function ttdb_getArray($resource, $rowNum)

function ttdb_getFieldValue($resource, $rowNum, $field)     // deprecate

function ttdb_getFieldName($resource, $field)  // Admin interface
function ttdb_getFieldType($resource, $field)  // Admin interface

*/

function Result($result)
{
    if ($result == "OK")
        echo "<font color='green'>OK</font><br>";
    else 
        echo "<font color='red'>failed</font><br>";

}

?>
<html>

<body>
function ttdb_connect(&$connection)

<?php  ttdb_connect($conn);
    if ($conn != false) 
        Result("OK");
    else 
        Result("Failed");
 ?>

function ttdb_beginTransaction($connection)

<?php  
    if (ttdb_beginTransaction($conn) != false) 
        Result("OK");
    else 
        Result("Failed");
 ?>

function ttdb_commitTransaction($connection)

<?php  
    if (ttdb_commitTransaction($conn) != false) 
        Result("OK");
    else 
        Result("Failed");
 ?>

function ttdb_rollbackTransaction($connection)

<?php  ttdb_beginTransaction($conn); 
    if (ttdb_rollbackTransaction($conn) != false) 
        Result("OK");
    else 
        Result("Failed");
 ?>

function ttdb_execQuery($connection, $query)

<?php  $res = ttdb_execQuery($conn, "SELECT * FROM software");
    if ($res != false) 
        Result("OK");
    else 
        Result("Failed");
 ?>

function ttdb_getNumRows($resource)         // Seguramente nos conduzca a todos los bucles que hay que modificar

<?php  
    if (ttdb_getNumRows($res) != -1) 
        Result("OK");
    else 
        Result("Failed");
 ?>


function ttdb_getNumFields($resource)       // Admin interface, query results

<?php  
    $numFields = ttdb_getNumFields($res);
    
    if ($numFields != -1) 
        Result("OK");
    else 
        Result("Failed");
 ?>


<br>
function ttdb_getArray($resource, $rowNum)

<?php  
    // This will change, as no rowNum should be supplied. Something like ttdb_moveNextRecord() should be implemented
    $fields = ttdb_getArray($res, 0);
    
    print_r($fields);
    
    // The test just gets the value of a field by name. Is this enough?
    if ($fields['iid'] != "") 
        Result("OK");
    else 
        Result("Failed");
    
 ?>

<br>
function ttdb_getFieldName($resource, $field)  // Admin interface

<?php  
    $field = ttdb_getFieldName($res, 0, 0);    
    
    if ($field != "") 
        Result("OK");
    else 
        Result("Failed");
 ?>


function ttdb_getFieldType($resource, $field)  // Admin interface

<?php  

    // more tests for all types used should be added here (this is DB dependant, so all values should be checked)
    
    $result = "OK"; 
    
    //echo ttdb_getFieldType($res, 11);     // for adding new types...
    
    if (ttdb_getFieldType($res, 0) != "int4") $result = "fail";    // iid, int4
    if (ttdb_getFieldType($res, 3) != "varchar") $result = "fail";    // iid, varchar
    if (ttdb_getFieldType($res, 9) != "bool") $result = "fail";    // iid, boolean
    if (ttdb_getFieldType($res, 11) != "date") $result = "fail";    // iid, date (timestamp in the future)
    
    Result($result);
    
 ?>

function ttdb_getSequenceValue($connection, $table)

<?php  $val = ttdb_getSequenceValue($conn, 'attachment');
    
    if (ttdb_getSequenceValue($conn, 'attachment') == $val + 1)    
        Result("OK");
    else 
        Result("Failed");        
 ?>

function ttdb_close($connection)

<?php 
    if (ttdb_close($conn) != false) 
        Result("OK");
    else 
        Result("Failed");
 ?>

<br>
<br>

Tests for new navigation approach. <br>

1.- Navigate one recordset<br>

<?php
    $result = "OK"; 
    
    ttdb_connect($conn);
    
    $res = ttdb_execQuery($conn, "SELECT * FROM defecttype ORDER BY iid");
    
    /*
    for ($i = 0; $i < 10; $i++) {
        if (($fields = ttdb_getArrayEx($res)) != false) {
            echo "$i .- ".$fields['iid']."<br>";
        }
    }
    */
    
    while(($fields = ttdb_getArray($res)) != false) {    
        
        // Check 1: iid changes from record to record
        if ($lastId == $fields['iid']) $result = "fail";
        $lastId  = $fields['iid'];
        
        echo $fields['iid']." ".$fields['sname']."<br>";
        
    }    
    
    ttdb_close($conn);

    Result($result);

?>

2.- is_array() method for checking empty rows

<?php
    $result = "OK"; 
    
    ttdb_connect($conn);
    
    // Negative test
    $res = ttdb_execQuery($conn, "SELECT * FROM defecttype WHERE iid = 5000");
    $fields = ttdb_getArray($res);  // should be empty    
    if (is_array($fields) == true) 
        $result = "fail";
    
    // Positive test
    $res = ttdb_execQuery($conn, "SELECT * FROM defecttype ORDER BY iid");
    $fields = ttdb_getArray($res);  // should contain something    
    if (is_array($fields) == false) 
        $result = "fail";

    ttdb_close($conn);

    Result($result);

?>

2.- Navigate nested recordsets<br>

3.- Navigate nested recordsets in different connections<br>

<?php
    $result = "OK"; 
    
    ttdb_connect($conn);
    ttdb_connect($conn2);
    
    $res = ttdb_execQuery($conn, "SELECT * FROM defecttype ORDER BY iid");
    
    $res2 = ttdb_execQuery($conn2, "SELECT * FROM users ORDER BY iid");
    
    while(($fields = ttdb_getArray($res)) != false) {    
        
        // Check 1: iid changes from record to record
        if ($lastId == $fields['iid']) $result = "fail";
        $lastId  = $fields['iid'];
        
        echo $fields['iid']." ".$fields['sname']."<br>";
        
        while(($fields2 = ttdb_getArray($res2)) != false) {
            echo "  ".$fields2['iid']." ".$fields2['sname']."<br>";
        }
        
    }   
    
    ttdb_close($conn2);
    ttdb_close($conn);

    Result($result);

?>


<br>
<br>

Changes are as follows: <br>

- Every function requiring a rownumber should be replaced by one that gets the current record, and another one that advances later.<br>
- getFieldValue should be replaced everywhere in favour of getArray. This minimizes the impact of the rownumber update. <br>
- A new function for getting the sequence number should be put in place
<?php phpinfo(); ?>

</body></html>
Return current item: Druid Defect Control