log = $logger;
// $db = new CoreLibs\DB\IO(DB_CONFIG, $basic->log);
print "
TEST CLASS: DB";
print "";
print '';
print "DBINFO: " . $db->dbInfo() . "
";
echo "DB_CONFIG_SET constant: " . print_r(DB_CONFIG, true) . "
";
// DB client encoding
print "DB Client encoding: " . $db->dbGetEncoding() . "
";
while ($res = $db->dbReturn("SELECT * FROM max_test", 0, true)) {
print "TIME: " . $res['time'] . "
";
}
print "CACHED DATA: " . print_r($db->cursor_ext, true) . "
";
while ($res = $db->dbReturn("SELECT * FROM max_test")) {
print "[CACHED] TIME: " . $res['time'] . "
";
}
print "";
$status = $db->dbExec("INSERT INTO foo (test) VALUES ('FOO TEST " . time() . "') RETURNING test");
print "DIRECT INSERT STATUS: $status | "
. "PRIMARY KEY: " . $db->dbGetInsertPK() . " | "
. "RETURNING EXT: " . print_r($db->dbGetReturningExt(), true) . " | "
. "RETURNING ARRAY: " . print_r($db->dbGetReturningArray(), true) . "
";
// should throw deprecated error
// $db->getReturningExt();
print "DIRECT INSERT PREVIOUS INSERTED: "
. print_r($db->dbReturnRow("SELECT foo_id, test FROM foo WHERE foo_id = " . $db->dbGetInsertPK()), true) . "
";
$db->dbPrepare("ins_foo", "INSERT INTO foo (test) VALUES ($1)");
$status = $db->dbExecute("ins_foo", array('BAR TEST ' . time()));
print "PREPARE INSERT STATUS: $status | "
. "PRIMARY KEY: " . $db->dbGetInsertPK() . " | "
. "RETURNING EXT: " . print_r($db->dbGetReturningExt(), true) . " | "
. "RETURNING RETURN: " . print_r($db->dbGetReturningArray(), true) . "
";
print "PREPARE INSERT PREVIOUS INSERTED: "
. print_r($db->dbReturnRow("SELECT foo_id, test FROM foo WHERE foo_id = " . $db->dbGetInsertPK()), true) . "
";
// returning test with multiple entries
// $status = $db->db_exec(
// "INSERT INTO foo (test) VALUES "
// . "('BAR 1 " . time() . "'), "
// . "('BAR 2 " . time() . "'), "
// . "('BAR 3 " . time() . "') "
// . "RETURNING foo_id"
// );
$status = $db->dbExec(
"INSERT INTO foo (test) VALUES "
. "('BAR 1 " . time() . "'), "
. "('BAR 2 " . time() . "'), "
. "('BAR 3 " . time() . "') "
. "RETURNING foo_id, test"
);
print "DIRECT MULTIPLE INSERT STATUS: $status | "
. "PRIMARY KEYS: " . print_r($db->dbGetInsertPK(), true) . " | "
. "RETURNING EXT: " . print_r($db->dbGetReturningExt(), true) . " | "
. "RETURNING ARRAY: " . print_r($db->dbGetReturningArray(), true) . "
";
// no returning, but not needed ;
$status = $db->dbExec("INSERT INTO foo (test) VALUES ('FOO; TEST " . time() . "');");
print "DIRECT INSERT STATUS: $status | "
. "PRIMARY KEY: " . $db->dbGetInsertPK() . " | "
. "RETURNING EXT: " . print_r($db->dbGetReturningExt(), true) . " | "
. "RETURNING ARRAY: " . print_r($db->dbGetReturningArray(), true) . "
";
// UPDATE WITH RETURNING
$status = $db->dbExec("UPDATE foo SET test = 'SOMETHING DIFFERENT' WHERE foo_id = 3688452 RETURNING test");
print "UPDATE STATUS: $status | "
. "RETURNING EXT: " . print_r($db->dbGetReturningExt(), true) . " | "
. "RETURNING ARRAY: " . print_r($db->dbGetReturningArray(), true) . "
";
print "";
// REEAD PREPARE
if (
$db->dbPrepare(
'sel_foo',
"SELECT foo_id, test, some_bool, string_a, number_a, number_a_numeric, some_time "
. "FROM foo ORDER BY foo_id DESC LIMIT 5"
) === false
) {
print "Error in sel_foo prepare
";
} else {
$max_rows = 6;
// do not run this in dbFetchArray directly as
// dbFetchArray(dbExecute(...))
// this will end in an endless loop
$cursor = $db->dbExecute('sel_foo', []);
$i = 1;
while (($res = $db->dbFetchArray($cursor, true)) !== false) {
print "DB PREP EXEC FETCH ARR: " . $i . ": " . print_r($res, true) . "
";
$i++;
}
}
# db write class test
$table = 'foo';
print "TABLE META DATA: " . DgS::printAr($db->dbShowTableMetaData($table)) . "
";
$primary_key = ''; # unset
$db_write_table = array('test', 'string_a', 'number_a', 'some_bool');
// $db_write_table = array('test');
$object_fields_not_touch = array();
$object_fields_not_update = array();
$data = array('test' => 'BOOL TEST SOMETHING ' . time(), 'string_a' => 'SOME TEXT', 'number_a' => 5);
$primary_key = $db->dbWriteDataExt(
$db_write_table,
$primary_key,
$table,
$object_fields_not_touch,
$object_fields_not_update,
$data
);
print "Wrote to DB tabel $table and got primary key $primary_key
";
$data = array('test' => 'BOOL TEST ON ' . time(), 'string_a' => '', 'number_a' => 0, 'some_bool' => 1);
$primary_key = $db->dbWriteDataExt(
$db_write_table,
$primary_key,
$table,
$object_fields_not_touch,
$object_fields_not_update,
$data
);
print "Wrote to DB tabel $table and got primary key $primary_key
";
$data = array('test' => 'BOOL TEST OFF ' . time(), 'string_a' => null, 'number_a' => null, 'some_bool' => 0);
$primary_key = $db->dbWriteDataExt(
$db_write_table,
$primary_key,
$table,
$object_fields_not_touch,
$object_fields_not_update,
$data
);
print "Wrote to DB tabel $table and got primary key $primary_key
";
$data = array('test' => 'BOOL TEST UNSET ' . time());
$primary_key = $db->dbWriteDataExt(
$db_write_table,
$primary_key,
$table,
$object_fields_not_touch,
$object_fields_not_update,
$data
);
print "Wrote to DB tabel $table and got primary key $primary_key
";
// return Array Test
$query = "SELECT type, sdate, integer FROM foobar";
$data = $db->dbReturnArray($query, true);
print "Full foobar list:
" . print_r($data, true) . "
";
# async test queries
/* $db->dbExecAsync("SELECT test FROM foo, (SELECT pg_sleep(10)) as sub WHERE foo_id IN (27, 50, 67, 44, 10)");
echo "WAITING FOR ASYNC: ";
$chars = array('|', '/', '-', '\\');
while (($ret = $db->dbCheckAsync()) === true)
{
if ((list($_, $char) = each($chars)) === FALSE)
{
reset($chars);
list($_, $char) = each($chars);
}
print $char;
sleep(1);
flush();
}
print "
END STATUS: " . $ret . "
";
// while ($res = $db->dbFetchArray($ret))
while ($res = $db->dbFetchArray())
{
echo "RES: " . $res['test'] . "
";
}
# test async insert
$db->dbExecAsync("INSERT INTO foo (Test) VALUES ('ASYNC TEST " . time() . "')");
echo "WAITING FOR ASYNC INSERT: ";
while (($ret = $db->dbCheckAsync()) === true)
{
print " . ";
sleep(1);
flush();
}
print "
END STATUS: " . $ret . " | PK: " . $db->insert_id . "
";
print "ASYNC PREVIOUS INSERTED: "
. print_r($db->dbReturnRow("SELECT foo_id, test FROM foo WHERE foo_id = " . $db->insert_id), true) . "
"; */
$to_db_version = '9.1.9';
print "VERSION DB: " . $db->dbVersion() . "
";
print "DB Version smaller $to_db_version: " . $db->dbCompareVersion('<' . $to_db_version) . "
";
print "DB Version smaller than $to_db_version: " . $db->dbCompareVersion('<=' . $to_db_version) . "
";
print "DB Version equal $to_db_version: " . $db->dbCompareVersion('=' . $to_db_version) . "
";
print "DB Version bigger than $to_db_version: " . $db->dbCompareVersion('>=' . $to_db_version) . "
";
print "DB Version bigger $to_db_version: " . $db->dbCompareVersion('>' . $to_db_version) . "
";
/* $q = "SELECT FOO FRO BAR";
// $q = "Select * from foo";
$foo = $db->dbExecAsync($q);
print "[ERR] Query: " . $q . "
";
print "[ERR] RESOURCE: $foo
";
while (($ret = $db->dbCheckAsync()) === true)
{
print "[ERR]: $ret
";
sleep(5);
} */
// search path check
$q = "SHOW search_path";
$cursor = $db->dbExec($q);
$data = $db->dbFetchArray($cursor)['search_path'];
print "RETURN DATA FOR search_path: " . $data . "
";
// print "RETURN DATA FOR search_path: " . DgS::printAr($data) . "
";
// insert something into test.schema_test and see if we get the PK back
$status = $db->dbExec(
"INSERT INTO test.schema_test (contents, id) VALUES "
. "('TIME: " . time() . "', " . rand(1, 10) . ")"
);
print "OTHER SCHEMA INSERT STATUS: "
. $status . " | PK NAME: " . $db->pk_name . ", PRIMARY KEY: " . $db->insert_id . "
";
print "NULL TEST DB READ
";
$q = "SELECT uid, null_varchar, null_int FROM test_null_data WHERE uid = 'A'";
$res = $db->dbReturnRow($q);
var_dump($res);
print "RES: " . DgS::printAr($res) . "
";
print "ISSET: " . isset($res['null_varchar']) . "
";
print "EMPTY: " . empty($res['null_varchar']) . "
";
// error message
print $basic->log->printErrorMsg();
print "";
// __END__