/**
 * This PHP script illustrates how to operate over a single table (INSERT, SELECT and DELETE).
 * 
 * For more complex examples see test2.php.
 */
header("Content-Type: text/plain; charset=utf-8");
require_once "../vendor/autoload.php";
use soloproyectos\db\DbConnector;
use soloproyectos\db\record\DbRecord;
// creates a new connector instance and prints each SQL statement (debugging)
$db = new DbConnector("test", "test", "test");
$db->addDebugListener(function ($sql) {
    echo "--{$sql}\n";
});
// creates a new record (INSERT)
echo "### Creates a new record\n";
$r = new DbRecord($db, "table0");
$r->save(["title" => "Title", "created_at" => date("Y-m-d H:i:s")]);
// fetches column values (SELECT)
echo "\n### Fetches column values\n";
list($id, $title, $createdAt) = $r->fetch(["id", "title", "created_at"]);
echo "id: {$id}, title: {$title}, created_at: {$createdAt}\n";
// selects a record and prints its column values (SELECT)
echo "\n### Selects a record and prints its column values\n";
$r = new DbRecord($db, "table0", $id);
list($title, $createdAt) = $r->fetch(["title", "created_at"]);
echo "title: {$title}, created_at: {$createdAt}\n";
// deletes the previous record (DELETE)
echo "\n### Deletes the previous record\n";
$r->delete();
header("Content-Type: text/plain; charset=utf-8");
require_once "../vendor/autoload.php";
use soloproyectos\db\DbConnector;
use soloproyectos\db\record\DbRecord;
// creates a new connector instance and prints each SQL statement (debugging)
$db = new DbConnector("test", "test", "test");
$db->addDebugListener(function ($sql) {
    echo "--{$sql}\n";
});
// creates a new record (INSERT)
// The following code inserts a record into table0, but also into the
// tables table1, table2 and table3. Finally, the fields table1_id, table2_id and table3_id
// are updated with the primary keys of the previous tables.
echo "### Creates a new record\n";
$r = new DbRecord($db, "table0");
$r->save(["title" => "Title", "created_at" => date("Y-m-d H:i:s"), "table1.title" => "Title 1", "table2.title" => "Title 2", "table3.title" => "Title 3"]);
// fetches column values (SELECT)
// The following codes fetches the column values of 'table0', but also of the
// tables 'table1', 'table2' and 'table3'.
echo "\n### Fetches column values\n";
list($id, $title, $createdAt, $t1Title, $t2Title, $t3Title) = $r->fetch(["id", "title", "created_at", "table1.title", "table2.title", "table3.title"]);
echo "id: {$id}, title: {$title}, created_at: {$createdAt}, ";
echo "table1.title: {$t1Title}, table2.title: {$t2Title}, table3.title: {$t3Title}\n";
// selects a record and prints its column values (SELECT)
// The following code fetches column values from table0, but also from table1
echo "\n### Selects a record and prints its column values\n";
$r = new DbRecord($db, "table0", $id);
list($title, $createdAt, $t1Title, $t2Title, $t3Title) = $r->fetch(["title", "created_at", "table1.title", "table2.title", "table3.title"]);
echo "title: {$title}, created_at: {$createdAt}, ";
echo "table1.title: {$t1Title}, table2.title: {$t2Title}, table3.title: {$t3Title}\n";
// deletes the previous record (DELETE)
 * In this example table2 is linked to table1 which is linked to table0.
 * 
 * For a triple somersault see test5.php.
 */
header("Content-Type: text/plain; charset=utf-8");
require_once "../vendor/autoload.php";
use soloproyectos\db\DbConnector;
use soloproyectos\db\record\DbRecord;
// creates a new connector instance and prints each SQL statement (debugging)
$db = new DbConnector("test", "test", "test");
$db->addDebugListener(function ($sql) {
    echo "--{$sql}\n";
});
// First of all, let's create a new record
// the following code operates over three tables: table0, table1 and table2
// table1 is linked to table0 (table2[id = table1.table2_id])
// and table2 is linked to the table1 (table2[id = table1.table2_id])
echo "### Creates a new record\n";
$r = new DbRecord($db, "table0");
$r->save(["title" => "Title", "created_at" => date("Y-m-d H:i:s"), "table1.title" => "Title 1", "table2[table1.table2_id].title" => "Title 12"]);
// And now prints table2[table1.table2_id].title
// table2 is linked to table1 by 'table2[id = table1.table2_id]'
// table1 is linked to table0 by 'table0[id = table1_id]'
echo "\n### General example: table2[id = table1.table2_id].title\n";
list($table2Title) = $r->fetch(["table2[id = table1.table2_id].title"]);
echo "table2.title: {$table2Title}\n";
// AS THE PREVIOUS EXAMPLE IS VERY COMMON, it can be written as follows:
// note that id and <table>_id can be ommmited
echo "\n### Shorthand example: table2[table1.table2_id].title\n";
list($table2Title) = $r->fetch(["table2[table1.table2_id].title"]);
echo "table2.title: {$table2Title}\n";