<?PHP
/*
    IBTEST
    Copyright (C) 2006 Milan Babuskov (mbabuskov@yahoo.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    You can also find the license here:
    http://www.gnu.org/licenses/gpl.txt
*/

// we'll handle our own errors
error_reporting(0);

// if register globals is set, we need to convert stuff
if (isset($_POST['database']))
{
    $database = $_POST['database'];
    $username = $_POST['username'];
    $password = $_POST['password'];
}

if (empty($database))
{?>
    <FORM action="<?PHP echo $PHP_SELF; ?>" method=POST>
        <table border=0 cellspacing=1 cellpadding=10 bgcolor=0>
            <tr bgcolor=silver>
                <td><B>Database path</B></td>
                <td><input type=text name=database size=100
                     value="localhost:employee"></td>
            </tr>
            <tr bgcolor=silver>
                <td><B>Username</b></td>
                <td><input type=text name=username size=32 value="SYSDBA"></td>
            </tr>
            <tr bgcolor=silver>
                <td><B>Password</b></td>
                <td><input type=password name=password size=32 value="no_go_pa"></td>
            </tr>
            <tr bgcolor=silver>
                <td colspan=2 align=center>
                    <input type=submit name=submit value="Test">
                </td>
            </tr>
        </table>
    </FORM>
<?PHP
    exit();
}

    echo "Connecting...";
    $link = ibase_connect($database, $username, $password);
    if (!$link)
    {
        echo "Error while connecting: ".ibase_errmsg();
        exit();
    }
    echo "OK.<br><br>\nRunning tests...<br>\n";
?>
    <table border=0 cellspacing=1 cellpadding=5 bgcolor=0>
        <tr bgcolor=silver>
            <td>Test name</td>
            <td>SQL</td>
            <td>Result</td>
            <td>Expected</td>
            <td>Commit</td>
        </tr>
<?PHP
    $tests = array(
        'Check database' => array('Select rdb$relation_id, rdb$character_set_name from rdb$database', '*')
        ,'Bad query' => array('this one should fail', '*')
        ,'Create table' => array('create table PHP_TEST ( x integer, c char(10), v varchar(10) )', '*')
        ,'Add field Y' => array('alter table PHP_TEST add y integer not null', '*')
        ,'Insert1' => array('insert into PHP_TEST (x,y) values (1,2)', '*')
        ,'Select1 (check)' => array('select x+y, x, y from PHP_TEST', array(3,1,2))
        ,'Update2 X' => array('update PHP_TEST set x = x + 10', '*')
        ,'Select2 (check)' => array('select x, y from PHP_TEST', array(11, 2))
        ,'Add field Z' => array('alter table PHP_TEST add Z decimal(16,2) not null', '*')
        ,'Update3 Z' => array('update PHP_TEST set z = 1', '*')
        ,'Select3 (check)' => array('select z, z+1, z*11, z/12.50 from PHP_TEST', array(1, 2, 11, 0.08))
        ,'Update4 Z' => array('update PHP_TEST set z = -1', '*')
        ,'Select4 (check)' => array('select z, z+1, z*11, z/12.50 from PHP_TEST', array(-1, 0, -11, -0.08))
        ,'Update5 Z' => array('update PHP_TEST set z = 0.48', '*')
        ,'Select5 (check)' => array('select z, z+1, z*11, z/12.50 from PHP_TEST',
            array(0.48, 1.48, 5.28, 0.0384))
        ,'Update6 Z' => array('update PHP_TEST set z = -0.52', '*')
        ,'Select6 (check)' => array('select z, z+1, z*11, z/12.50 from PHP_TEST',
            array(-0.52, 0.48, -5.72,-0.0416))
        ,'Update7 Z' => array('update PHP_TEST set z = 1.18', '*')
        ,'Select7 (check)' => array('select z, z+1, z*11, z/12.50 from PHP_TEST',
            array(1.18, 2.18, 12.98, 0.0944))
        ,'Update8 Z' => array('update PHP_TEST set z = -1.38', '*')
        ,'Select8 (check)' => array('select z, z+1, z*11, z/12.50 from PHP_TEST',
            array(-1.38, -0.38, -15.18, -0.1104))
        ,'Update C' => array('update PHP_TEST set c = \'PHP test\'', '*')
        ,'Check char padding' => array('select c from PHP_TEST', 'PHP test  ')    // check CHAR padding
        ,'Update V' => array('update PHP_TEST set v = \'PHP test\'', '*')
        ,'Check varchar padding' => array('select v from PHP_TEST', 'PHP test')
        ,'Add decimal fields' => array('alter table PHP_TEST
            add d1 decimal(4,2),
            add d2 decimal(6,3),
            add d3 decimal(8,4),
            add d4 decimal(10,5),
            add d5 decimal(14,7),
            add d6 decimal(16,8),
            add dd double precision,
            add df float', '*')
        ,'Update decimals' => array('update PHP_TEST set
            d1 = 12.52, d2 = 918.812, d3 = 1234.8765, d4 = 54321.06789,
            d5 = 6543210.1234567, d6 = 87654321.01234567, dd = 1234.5678,
            df = 1234.56', '*')
        ,'Check types1' => array('select d1,d2,d3,dd,df from PHP_TEST',
            array(12.52, 918.812, 1234.8765))
        ,'Check types2' => array('select d4,d5,d6 from PHP_TEST',
            array(54321.06789, 6543210.1234567))
        ,'Create SP' => array('create procedure php_test_sp returns (
                x integer, c char(10), v varchar(10),
                d1 decimal(4,2),        d2 decimal(6,3),
                d3 decimal(8,4),        d4 decimal(10,5),
                d5 decimal(14,7),       d6 decimal(16,8),
                dd double precision,    df float) as BEGIN
            for select x, c, v, d1, d2, d3, d4, d5, d6, dd, df from php_test
            into :x, :c, :v, :d1, :d2, :d3, :d4, :d5, :d6, :dd, :df
            do suspend; END', '*')
        ,'Select SP1' => array('select c, v, x from php_test_sp', 'PHP test  ')
        ,'Select SP2' => array('select d1,d2,d3,dd,df from php_test_sp',
            array(12.52, 918.812, 1234.8765))
        ,'Select SP3' => array('select d4, d5, d6 from php_test_sp',
            array(54321.06789, 6543210.1234567))
        ,'Drop SP' => array('drop procedure PHP_TEST_SP', '*')
        ,'Drop table' => array('drop table PHP_TEST', '*')
        //,'' => array('', '*')
    );

    foreach($tests as $t => $p)
    {
        echo "<tr bgcolor=white>\n<td>" . htmlspecialchars($t) . "</td><td>"
         . htmlspecialchars($p[0]) . "</td><td>";
        $res = ibase_query($p[0]);
        if (!$res)
        {
            echo "<font color=red>".htmlspecialchars(ibase_errmsg()).
                "</font>";
        }
        else
        {
            print_results($res, $row);
            if (!$row)
                echo "Executed Ok. No resultset.";
        }
        echo "</td><td>";
        if (is_array($p[1]) || $p[1] != '*')
        {
            $a = array();
            if (!is_array($p[1]))
                $a[0] = $p[1];
            else
                $a = $p[1];
            for ($i = 0; $i < sizeof($a); $i++)
            {
                if ($i > 0)
                    echo ", ";
                if ($a[$i] == $row[$i])
                    echo '<font color=green>'.$a[$i].'</font>';
                else
                    echo '<font color=red>'.$a[$i].'</font>';
            }
        }
        echo "</td><td>";
        if (ibase_commit())
            echo "OK.";
        else
        {
            echo "<font color=red>Error while commiting: "
            . htmlspecialchars(ibase_errmsg()) . "</font>";
        }
        echo "</td></tr>\n";
    }
?>
</table>
<hr>
Testing complete.


<?PHP

function print_results($res, &$first_row)
{
    $first_row = ibase_fetch_row($res);
    if (!$first_row)
        return false;

    $row = $first_row;
    $coln = ibase_num_fields($res);
    echo "<TABLE cellspacing=1 cellpadding=2 border=0 bgcolor=black>\n";
    echo "<TR bgcolor=navy>";
    for ($i=0; $i < $coln; $i++)
    {
        $col_info = ibase_field_info($res, $i);
        echo "\n  <TD align=center><font color=white size=-1><B>".$col_info['alias']
            ."</B><br>".$col_info['type']."</FONT></TD>";
    }
    echo "</tr>\n";

    do
    {
        echo "<tr bgcolor=#FFFF99>\n";
        for ($i=0; $i < $coln; $i++)
        {
            echo "\t<td>".htmlspecialchars($row[$i])."</td>\n";
        }
        echo "</tr>\n";
    }
    while ($row = ibase_fetch_row($res));
    echo "</table>\n";
    return $first_row;
}

?>
