<?php

echo "<pre>TESTING MOODLE 2.0 OCI DRIVER WITH oci8.statement_cache_size = ".ini_get('oci8.statement_cache_size')." (from php.ini)";

/** SQLs to be used **/

$create1 = 'CREATE TABLE unit_table (
                id     NUMBER(10,0),
                course NUMBER(10,0),
                name   VARCHAR2(100),
            CONSTRAINT unit_table_pk PRIMARY KEY (id))';

$drop    = 'DROP TABLE unit_table';

$create2 = 'CREATE TABLE unit_table (
                id     NUMBER(10,0),
                course NUMBER(10,0),
            CONSTRAINT unit_table_pk PRIMARY KEY (id))';

$query   = 'SELECT * FROM unit_table';

/** Connect and execute **/

if (!$conn = oci_connect('scott', 'tiger', 'localhost/XE')) {
    $e = oci_error();
    print $e['message'];
    exit;
}

$stmt = oci_parse($conn, $create1);
if (!$stmt) {
    $e = oci_error($conn);
    print $e['message'];
    exit;
}

$r = oci_execute($stmt);
if (!$r) {
    $e = oci_error($stmt);
    echo $e['message'];
    exit;
}

oci_free_statement($stmt);

echo "<pre>Created table unit_table (id, course, name). Ok</pre>";

$stmt = oci_parse($conn, $query);
if (!$stmt) {
    $e = oci_error($conn);
    print $e['message'];
    exit;
}

$r = oci_execute($stmt);
if (!$r) {
    $e = oci_error($stmt);
    echo $e['message'];
    exit;
}

oci_free_statement($stmt);

echo "<pre>Selected 0 records from table. Ok</pre>";

$stmt = oci_parse($conn, $drop);
if (!$stmt) {
    $e = oci_error($conn);
    print $e['message'];
    exit;
}

$r = oci_execute($stmt);
if (!$r) {
    $e = oci_error($stmt);
    echo $e['message'];
    exit;
}

oci_free_statement($stmt);

echo "<pre>Dropped table unit_table (id, course, name). Ok</pre>";

$stmt = oci_parse($conn, $create2);
if (!$stmt) {
    $e = oci_error($conn);
    print $e['message'];
    exit;
}

$r = oci_execute($stmt);
if (!$r) {
    $e = oci_error($stmt);
    echo $e['message'];
    exit;
}

oci_free_statement($stmt);

echo "<pre>Created table unit_table (id, course). Ok</pre>";

$stmt = oci_parse($conn, $query);
if (!$stmt) {
    $e = oci_error($conn);
    print $e['message'];
    exit;
}

$r = oci_execute($stmt);
if (!$r) {
    echo "<pre>Error selecting records from table!!</pre>";
    $e = oci_error($stmt);
    echo $e['message'];
} else {
    echo "<pre>Selected 0 records from table. Ok</pre>";
}

oci_free_statement($stmt);

$stmt = oci_parse($conn, $drop);
if (!$stmt) {
    $e = oci_error($conn);
    print $e['message'];
    exit;
}

$r = oci_execute($stmt);
if (!$r) {
    $e = oci_error($stmt);
    echo $e['message'];
    exit;
}

oci_free_statement($stmt);

echo "<pre>Dropped table unit_table (id, course). Ok</pre>";

oci_close($conn);
?>

