Commit 5fdf5f27 authored by Josh Campbell's avatar Josh Campbell

Merge pull request #114 from avbdr/master

Subquery / nicer where() api
parents 0c024935 ce7d9e8b
This diff is collapsed.
......@@ -10,7 +10,7 @@ After that, create a new instance of the class.
$db = new Mysqlidb('host', 'username', 'password', 'databaseName');
```
It's also possible to set a table prefix:
Its also possible to set a table prefix:
```php
$db->setPrefix('tablePrefix');
```
......@@ -133,38 +133,42 @@ $results = $db->get('users');
```
```php
$db->where('id', Array('>=' => 50));
$db->where('id', 50, ">=");
// or $db->where('id', Array('>=' => 50));
$results = $db->get('users');
// Gives: SELECT * FROM users WHERE id >= 50;
```
BETWEEN:
BETWEEN / NOT BETWEEN:
```php
$db->where('id', Array('between' => Array(4, 20) ) );
//$db->where('id', Array('not between' => Array(4, 20) ) );
$db->where('id', Array(4, 20), 'between');
// or $db->where('id', Array('between' => Array(4, 20) ) );
$results = $db->get('users');
// Gives: SELECT * FROM users WHERE id BETWEEN 4 AND 20
```
IN:
IN / NOT IN:
```php
$db->where('id', Array( 'in' => Array(1, 5, 27, -1, 'd') ) );
//$db->where('id', Array( 'not in' => Array(1, 5, 27, -1, 'd') ) );
$db->where('id', Array(1, 5, 27, -1, 'd'), 'IN');
// or $db->where('id', Array( 'in' => Array(1, 5, 27, -1, 'd') ) );
$results = $db->get('users');
// Gives: SELECT * FROM users WHERE id IN (1, 5, 27, -1, 'd');
```
OR CASE
```php
$db->where('firstName','John');
$db->orWhere('firstName','Peter');
$results = $db->get('users');
$db->where ('firstName', 'John');
$db->orWhere ('firstName', 'Peter');
$results = $db->get ('users');
// Gives: SELECT * FROM users WHERE firstName='John' OR firstName='peter'
```
NULL comparison:
```php
$db->where ("lastName", Array("<=>" => NULL));
$db->where ("lastName", NULL, '<=>');
$results = $db->get("users");
// Gives: SELECT * FROM users where lastName <=> NULL
```
......@@ -202,8 +206,8 @@ $results = $db->get('users');
### Grouping method
```php
$db->groupBy("name");
$results = $db->get('users');
$db->groupBy ("name");
$results = $db->get ('users');
// Gives: SELECT * FROM users GROUP BY name;
```
......@@ -216,6 +220,46 @@ $products = $db->get ("products p", null, "u.name, p.productName");
print_r ($products);
```
### Properties sharing
Its is also possible to copy properties
```php
$db->where ("agentId", 10);
$customers = $db->copy ();
$res = $customers->get ("customers");
// SELECT * FROM customers where agentId = 10
$db->orWhere ("agentId", 20);
$res = $db->get ("users");
// SELECT * FROM users where agentId = 10 or agentId = 20
```
### Subqueries
Subquery in selects:
```php
$ids = $db->subQuery ();
$ids->where ("qty", 2, ">");
$ids->get ("products", null, "userId");
$db->where ("id", $ids, 'in');
$res = $db->get ("users");
// Gives SELECT * FROM users WHERE id IN (SELECT userId FROM products WHERE qty > 2)
```
Subquery in inserts:
```php
$userIdQ = $db->subQuery ();
$userIdQ->where ("id", 6);
$userIdQ->getOne ("users", "name"),
$data = Array (
"productName" => "test product",
"userId" => $userIdQ,
"lastUpdated" => $db->now()
);
$id = $db->insert ("products", $data);
// Gives INSERT INTO PRODUCTS (productName, userId, lastUpdated) values ("test product", (SELECT name FROM users WHERE id = 6), NOW());
```
### Helper commands
Reconnect in case mysql connection died
```php
......@@ -227,3 +271,25 @@ Obtain an initialized instance of the class from another class
```php
$db = MysqliDb::getInstance();
```
Get last executed SQL query.
Please note that function returns SQL query only for debugging purposes as its execution most likely will fail due missing quotes around char variables.
```php
$db->get('users');
echo "Last executed query was ". $db->getLastQuery();
```
### Transaction helpers
Please keep in mind that transactions are working on innoDB tables.
Rollback transaction if insert fails:
```php
$db->startTransaction();
...
if (!$db->insert ('myTable', $insertData)) {
//Error while saving, cancel new record
$db->rollback();
} else {
//OK
$db->commit();
}
```
......@@ -94,7 +94,7 @@ function createTable ($name, $data) {
}
foreach ($tables as $name => $fields) {
$db->rawQuery("DROP TABLE $name");
$db->rawQuery("DROP TABLE ".$prefix.$name);
createTable ($prefix.$name, $fields);
}
......@@ -138,7 +138,7 @@ if ($db->count != 3) {
//$users = $db->get("users");
//print_r ($users);
$db->where("firstname", Array("LIKE" => '%John%'));
$db->where("firstname", '%John%', 'LIKE');
$users = $db->get("users");
if ($db->count != 1) {
echo "Invalid insert count in LIKE: ".$db->count;
......@@ -172,14 +172,14 @@ if ($r['password'] != '546f98b24edfdc3b9bbe0d241bd8b29783f71b32') {
exit;
}
$db->where ("id", Array('in' => Array('1','2','3')));
$db->where ("id", Array('1','2','3'), 'IN');
$db->get("users");
if ($db->count != 3) {
echo "Invalid users count on where() with in ";
exit;
}
$db->where ("id", Array('between' => Array('2','3')));
$db->where ("id", Array('2','3'), 'between');
$db->get("users");
if ($db->count != 2) {
echo "Invalid users count on where() with between";
......@@ -194,7 +194,7 @@ if ($db->count != 2) {
exit;
}
$db->where ("lastName", Array("<=>" => NULL));
$db->where ("lastName", NULL, '<=>');
$r = $db->get("users");
if ($db->count != 1) {
echo "Invalid users count on null where()";
......@@ -217,6 +217,25 @@ if ($db->count != 2) {
exit;
}
$db->where("id = 1 or id = 2");
$res = $db->get ("users");
if ($db->count != 2) {
echo "Invalid users count on select with multiple params";
exit;
}
$usersQ = $db->subQuery();
$usersQ->where ("login", "user2");
$usersQ->getOne ("users", "id");
$db2 = $db->copy();
$db2->where ("userId", $usersQ);
$res = $db2->getOne ("products", "count(id) as cnt");
if ($res['cnt'] != 2) {
echo "Invalid select result with subquery";
exit;
}
//TODO: insert test
$db->delete("users");
$db->get("users");
if ($db->count != 0) {
......
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