Commit 30a07506 authored by Alexander Butenko's avatar Alexander Butenko

added test for join

parent a5f25d09
...@@ -15,37 +15,66 @@ $tables = Array ( ...@@ -15,37 +15,66 @@ $tables = Array (
'createdAt' => 'datetime', 'createdAt' => 'datetime',
'expires' => 'datetime', 'expires' => 'datetime',
'loginCount' => 'int(10) default 0' 'loginCount' => 'int(10) default 0'
),
'products' => Array (
'customerId' => 'int(10) not null',
'userId' => 'int(10) not null',
'productName' => 'char(50)'
) )
); );
$data = Array ( $data = Array (
Array ('login' => 'user1', 'users' => Array (
'customerId' => 10, Array ('login' => 'user1',
'firstName' => 'John', 'customerId' => 10,
'lastName' => 'Doe', 'firstName' => 'John',
'password' => $db->func('SHA1(?)',Array ("secretpassword+salt")), 'lastName' => 'Doe',
'createdAt' => $db->now(), 'password' => $db->func('SHA1(?)',Array ("secretpassword+salt")),
'expires' => $db->now('+1Y'), 'createdAt' => $db->now(),
'loginCount' => $db->inc() 'expires' => $db->now('+1Y'),
), 'loginCount' => $db->inc()
Array ('login' => 'user2', ),
'customerId' => 10, Array ('login' => 'user2',
'firstName' => 'Mike', 'customerId' => 10,
'lastName' => NULL, 'firstName' => 'Mike',
'password' => $db->func('SHA1(?)',Array ("secretpassword2+salt")), 'lastName' => NULL,
'createdAt' => $db->now(), 'password' => $db->func('SHA1(?)',Array ("secretpassword2+salt")),
'expires' => $db->now('+1Y'), 'createdAt' => $db->now(),
'loginCount' => $db->inc(2) 'expires' => $db->now('+1Y'),
'loginCount' => $db->inc(2)
),
Array ('login' => 'user3',
'customerId' => 11,
'firstName' => 'Pete',
'lastName' => 'D',
'password' => $db->func('SHA1(?)',Array ("secretpassword2+salt")),
'createdAt' => $db->now(),
'expires' => $db->now('+1Y'),
'loginCount' => $db->inc(3)
)
), ),
Array ('login' => 'user3', 'products' => Array (
'customerId' => 11, Array ('customerId' => 1,
'firstName' => 'Pete', 'userId' => 1,
'lastName' => 'D', 'productName' => 'product1',
'password' => $db->func('SHA1(?)',Array ("secretpassword2+salt")), ),
'createdAt' => $db->now(), Array ('customerId' => 1,
'expires' => $db->now('+1Y'), 'userId' => 1,
'loginCount' => $db->inc(3) 'productName' => 'product2',
) ),
Array ('customerId' => 1,
'userId' => 1,
'productName' => 'product3',
),
Array ('customerId' => 1,
'userId' => 2,
'productName' => 'product4',
),
Array ('customerId' => 1,
'userId' => 2,
'productName' => 'product5',
),
)
); );
function createTable ($name, $data) { function createTable ($name, $data) {
...@@ -64,12 +93,15 @@ foreach ($tables as $name => $fields) { ...@@ -64,12 +93,15 @@ foreach ($tables as $name => $fields) {
createTable ($name, $fields); createTable ($name, $fields);
} }
foreach ($data as $d) {
$id = $db->insert("users", $d); foreach ($data as $name => $datas) {
if ($id) foreach ($datas as $d) {
$d['id'] = $id; $id = $db->insert($name, $d);
else { if ($id)
echo "failed to insert: ".$db->getLastQuery() ."\n". $db->getLastError(); $d['id'] = $id;
else {
echo "failed to insert: ".$db->getLastQuery() ."\n". $db->getLastError();
}
} }
} }
...@@ -147,12 +179,21 @@ if ($db->count != 1) { ...@@ -147,12 +179,21 @@ if ($db->count != 1) {
exit; exit;
} }
$db->join("users u", "p.userId=u.id", "LEFT");
$db->where("u.login",'user2');
$products = $db->get ("products p", null, "u.login, p.productName");
if ($db->count != 2) {
echo "Invalid products count on join ()";
exit;
}
$db->delete("users"); $db->delete("users");
$db->get("users"); $db->get("users");
if ($db->count != 0) { if ($db->count != 0) {
echo "Invalid users count after delete"; echo "Invalid users count after delete";
exit; exit;
} }
$db->delete("products");
echo "All done"; echo "All done";
//print_r($db->rawQuery("CALL simpleproc(?)",Array("test"))); //print_r($db->rawQuery("CALL simpleproc(?)",Array("test")));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment