@@ -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
0 commit comments