Skip to content

Commit adbede9

Browse files
authored
Merge pull request #481 from joshbenhamou/master
Add joinWhere, joinOrWhere
2 parents b145912 + 97be5b3 commit adbede9

File tree

3 files changed

+166
-2
lines changed

3 files changed

+166
-2
lines changed

MysqliDb.php

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ class MysqliDb
6464
*/
6565
protected $_where = array();
6666

67+
/**
68+
* An array that holds where join ands
69+
*
70+
* @var array
71+
*/
72+
protected $_joinAnd = array();
73+
6774
/**
6875
* An array that holds having conditions
6976
* @var array
@@ -311,6 +318,7 @@ protected function reset()
311318
$this->_where = array();
312319
$this->_having = array();
313320
$this->_join = array();
321+
$this->_joinAnd = array();
314322
$this->_orderBy = array();
315323
$this->_groupBy = array();
316324
$this->_bindParams = array(''); // Create the empty 0 index
@@ -1081,6 +1089,7 @@ private function _buildInsert($tableName, $insertData, $operation)
10811089
*/
10821090
protected function _buildQuery($numRows = null, $tableData = null)
10831091
{
1092+
// $this->_buildJoinOld();
10841093
$this->_buildJoin();
10851094
$this->_buildInsertQuery($tableData);
10861095
$this->_buildCondition('WHERE', $this->_where);
@@ -1229,7 +1238,7 @@ protected function _dynamicBindResults(mysqli_stmt $stmt)
12291238
*
12301239
* @return void
12311240
*/
1232-
protected function _buildJoin()
1241+
protected function _buildJoinOld()
12331242
{
12341243
if (empty($this->_join)) {
12351244
return;
@@ -1906,6 +1915,106 @@ public function paginate ($table, $page, $fields = null) {
19061915
$this->totalPages = ceil($this->totalCount / $this->pageLimit);
19071916
return $res;
19081917
}
1918+
1919+
/**
1920+
* This method allows you to specify multiple (method chaining optional) AND WHERE statements for the join table on part of the SQL query.
1921+
*
1922+
* @uses $dbWrapper->joinWhere('user u', 'u.id', 7)->where('user u', 'u.title', 'MyTitle');
1923+
*
1924+
* @param string $whereJoin The name of the table followed by its prefix.
1925+
* @param string $whereProp The name of the database field.
1926+
* @param mixed $whereValue The value of the database field.
1927+
*
1928+
* @return dbWrapper
1929+
*/
1930+
public function joinWhere($whereJoin, $whereProp, $whereValue = 'DBNULL', $operator = '=', $cond = 'AND')
1931+
{
1932+
$this->_joinAnd[$whereJoin][] = Array ($cond, $whereProp, $operator, $whereValue);
1933+
return $this;
1934+
}
1935+
1936+
/**
1937+
* This method allows you to specify multiple (method chaining optional) OR WHERE statements for the join table on part of the SQL query.
1938+
*
1939+
* @uses $dbWrapper->joinWhere('user u', 'u.id', 7)->where('user u', 'u.title', 'MyTitle');
1940+
*
1941+
* @param string $whereJoin The name of the table followed by its prefix.
1942+
* @param string $whereProp The name of the database field.
1943+
* @param mixed $whereValue The value of the database field.
1944+
*
1945+
* @return dbWrapper
1946+
*/
1947+
public function joinOrWhere($whereJoin, $whereProp, $whereValue = 'DBNULL', $operator = '=', $cond = 'AND')
1948+
{
1949+
return $this->joinWhere($whereJoin, $whereProp, $whereValue, $operator, 'OR');
1950+
}
1951+
1952+
/**
1953+
* Abstraction method that will build an JOIN part of the query
1954+
*/
1955+
protected function _buildJoin () {
1956+
if (empty ($this->_join))
1957+
return;
1958+
1959+
foreach ($this->_join as $data) {
1960+
list ($joinType, $joinTable, $joinCondition) = $data;
1961+
1962+
if (is_object ($joinTable))
1963+
$joinStr = $this->_buildPair ("", $joinTable);
1964+
else
1965+
$joinStr = $joinTable;
1966+
1967+
$this->_query .= " " . $joinType. " JOIN " . $joinStr ." on " . $joinCondition;
1968+
1969+
// Add join and query
1970+
if (!empty($this->_joinAnd) && isset($this->_joinAnd[$joinStr])) {
1971+
foreach($this->_joinAnd[$joinStr] as $join_and_cond) {
1972+
list ($concat, $varName, $operator, $val) = $join_and_cond;
1973+
$this->_query .= " " . $concat ." " . $varName;
1974+
$this->conditionToSql($operator, $val);
1975+
}
1976+
}
1977+
}
1978+
}
1979+
1980+
/**
1981+
* Convert a condition and value into the sql string
1982+
* @param String $operator The where constraint operator
1983+
* @param String $val The where constraint value
1984+
*/
1985+
private function conditionToSql($operator, $val) {
1986+
switch (strtolower ($operator)) {
1987+
case 'not in':
1988+
case 'in':
1989+
$comparison = ' ' . $operator. ' (';
1990+
if (is_object ($val)) {
1991+
$comparison .= $this->_buildPair ("", $val);
1992+
} else {
1993+
foreach ($val as $v) {
1994+
$comparison .= ' ?,';
1995+
$this->_bindParam ($v);
1996+
}
1997+
}
1998+
$this->_query .= rtrim($comparison, ',').' ) ';
1999+
break;
2000+
case 'not between':
2001+
case 'between':
2002+
$this->_query .= " $operator ? AND ? ";
2003+
$this->_bindParams ($val);
2004+
break;
2005+
case 'not exists':
2006+
case 'exists':
2007+
$this->_query.= $operator . $this->_buildPair ("", $val);
2008+
break;
2009+
default:
2010+
if (is_array ($val))
2011+
$this->_bindParams ($val);
2012+
else if ($val === null)
2013+
$this->_query .= $operator . " NULL";
2014+
else if ($val != 'DBNULL' || $val == '0')
2015+
$this->_query .= $this->_buildPair ($operator, $val);
2016+
}
2017+
}
19092018
}
19102019

19112020
// END class

readme.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,24 @@ $products = $db->get ("products p", null, "u.name, p.productName");
500500
print_r ($products);
501501
```
502502

503+
### Join Conditions
504+
Add AND condition to join statement
505+
```php
506+
$db->join("users u", "p.tenantID=u.tenantID", "LEFT");
507+
$db->joinWhere("users u", "u.tenantID", 5);
508+
$products = $db->get ("products p", null, "u.name, p.productName");
509+
print_r ($products);
510+
// Gives: SELECT u.login, p.productName FROM products p LEFT JOIN users u ON (p.tenantID=u.tenantID AND u.tenantID = 5)
511+
```
512+
Add OR condition to join statement
513+
```php
514+
$db->join("users u", "p.tenantID=u.tenantID", "LEFT");
515+
$db->joinOrWhere("users u", "u.tenantID", 5);
516+
$products = $db->get ("products p", null, "u.name, p.productName");
517+
print_r ($products);
518+
// Gives: SELECT u.login, p.productName FROM products p LEFT JOIN users u ON (p.tenantID=u.tenantID OR u.tenantID = 5)
519+
```
520+
503521
### Properties sharing
504522
Its is also possible to copy properties
505523

tests/mysqliDbTests.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
require_once ("../MysqliDb.php");
33
error_reporting(E_ALL);
44

5+
function pretty_print($array) {
6+
echo '<pre>';
7+
print_r($array);
8+
echo '</pre>';
9+
}
10+
511
$prefix = 't_';
612
$db = new Mysqlidb('localhost', 'root', '', 'testdb');
713
if(!$db) die("Database error");
@@ -322,6 +328,34 @@ function createTable ($name, $data) {
322328
exit;
323329
}
324330
///
331+
$db->join("users u", "p.userId=u.id", "LEFT");
332+
$db->joinWhere('t_users u', 'u.id', 'non existant value');
333+
$products = $db->get ("products p", null, "u.login, p.productName");
334+
if ($db->count != 5) {
335+
echo 'Invalid product count on joinWhere';
336+
exit;
337+
}
338+
foreach($products as $product) {
339+
if ($product['login']) {
340+
echo 'Invalid login result on joinWhere';
341+
exit;
342+
}
343+
}
344+
///
345+
$db->join("users u", "p.userId=u.id", "LEFT");
346+
$db->joinOrWhere('t_users u', 'u.id', 'non existant value');
347+
$products = $db->get ("products p", null, "u.login, p.productName");
348+
if ($db->count != 5) {
349+
echo 'Invalid product count on joinOrWhere';
350+
exit;
351+
}
352+
foreach($products as $product) {
353+
if (!$product['login']) {
354+
echo 'Invalid login result on joinWhere';
355+
exit;
356+
}
357+
}
358+
///
325359
$db->where("id = ? or id = ?", Array(1,2));
326360
$res = $db->get ("users");
327361
if ($db->count != 2) {
@@ -422,7 +456,10 @@ function createTable ($name, $data) {
422456

423457
//print_r($db->rawQuery("CALL simpleproc(?)",Array("test")));
424458

425-
print_r ($db->trace);
459+
echo '<pre>';
460+
pretty_print($db->trace);
461+
echo '</pre>';
426462
echo "All done\n";
427463
echo "Memory usage: ".memory_get_peak_usage()."\n";
464+
428465
?>

0 commit comments

Comments
 (0)