Skip to content

Commit 5cf4753

Browse files
committed
buildWhere() refactoring
1 parent 2cffff2 commit 5cf4753

File tree

2 files changed

+209
-151
lines changed

2 files changed

+209
-151
lines changed

MysqliDb.php

Lines changed: 201 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -522,25 +522,38 @@ protected function _determineType($item)
522522
*
523523
* @param string Variable value
524524
*/
525-
protected function _bindParam($value)
526-
{
525+
protected function _bindParam($value) {
527526
$this->_bindParams[0] .= $this->_determineType ($value);
528527
array_push ($this->_bindParams, $value);
529528
}
530529

530+
/**
531+
* Helper function to add variables into bind parameters array in bulk
532+
*
533+
* @param Array Variable with values
534+
*/
535+
protected function _bindParams ($values) {
536+
foreach ($values as $value)
537+
$this->_bindParam ($value);
538+
}
539+
540+
/**
541+
* Helper function to add variables into bind parameters array and will return
542+
* its SQL part of the query according to operator in ' $operator ?' or
543+
* ' $operator ($subquery) ' formats
544+
*
545+
* @param Array Variable with values
546+
*/
531547
protected function _buildPair ($operator, $value) {
532548
if (!is_object($value)) {
533549
$this->_bindParam ($value);
534-
$comparison = ' ' . $operator. ' ? ';
535-
return $comparison;
550+
return ' ' . $operator. ' ? ';
536551
}
537552

538-
$subQuery = $value->getSubQuery();
539-
$comparison = " " . $operator . " (" . $subQuery['query'] . ")";
540-
foreach ($subQuery['params'] as $v)
541-
$this->_bindParam ($v);
553+
$subQuery = $value->getSubQuery ();
554+
$this->_bindParams ($subQuery['params']);
542555

543-
return $comparison;
556+
return " " . $operator . " (" . $subQuery['query'] . ")";
544557
}
545558

546559
/**
@@ -555,148 +568,14 @@ protected function _buildPair ($operator, $value) {
555568
*/
556569
protected function _buildQuery($numRows = null, $tableData = null)
557570
{
558-
$hasTableData = is_array($tableData);
559-
$hasConditional = !empty($this->_where);
560-
561-
// Did the user call the "join" method?
562-
if (!empty($this->_join)) {
563-
foreach ($this->_join as $prop => $value) {
564-
$this->_query .= " " . $prop . " on " . $value;
565-
}
566-
}
571+
$this->_buildJoin();
572+
$this->_buildTableData ($tableData);
573+
$this->_buildWhere();
574+
$this->_buildGroupBy();
575+
$this->_buildOrderBy();
576+
$this->_buildLimit ($numRows);
567577

568-
// Determine INSERT or UPDATE query
569-
if ($hasTableData) {
570-
$isInsert = strpos ($this->_query, 'INSERT');
571-
$isUpdate = strpos ($this->_query, 'UPDATE');
572-
573-
if ($isInsert !== false) {
574-
//is insert statement
575-
$this->_query .= '(`' . implode(array_keys($tableData), '`, `') . '`)';
576-
$this->_query .= ' VALUES(';
577-
}
578-
579-
foreach ($tableData as $column => $value) {
580-
if ($isUpdate !== false)
581-
$this->_query .= "`" . $column . "` = ";
582-
583-
if (is_object ($value)) {
584-
$this->_query .= $this->_buildPair ("", $value) . ", ";
585-
} else if (!is_array ($value)) {
586-
$this->_bindParam ($value);
587-
$this->_query .= '?, ';
588-
} else {
589-
$key = key ($value);
590-
$val = $value[$key];
591-
switch ($key) {
592-
case '[I]':
593-
$this->_query .= $column . $val . ", ";
594-
break;
595-
case '[F]':
596-
$this->_query .= $val[0] . ", ";
597-
if (!empty ($val[1])) {
598-
foreach ($val[1] as $v)
599-
$this->_bindParam ($v);
600-
}
601-
break;
602-
case '[N]':
603-
if ($val == null)
604-
$this->_query .= "!" . $column . ", ";
605-
else
606-
$this->_query .= "!" . $val . ", ";
607-
break;
608-
default:
609-
die ("Wrong operation");
610-
}
611-
}
612-
}
613-
$this->_query = rtrim($this->_query, ', ');
614-
if ($isInsert !== false)
615-
$this->_query .= ')';
616-
}
617-
618-
// Did the user call the "where" method?
619-
if ($hasConditional) {
620-
//Prepair the where portion of the query
621-
$this->_query .= ' WHERE ';
622-
$i = 0;
623-
foreach ($this->_where as $cond) {
624-
list ($concat, $wValue, $wKey) = $cond;
625-
626-
// if its not a first condition insert its concatenator (AND or OR)
627-
if ($i++ != 0)
628-
$this->_query .= " $concat ";
629-
$this->_query .= $wKey;
630-
631-
if (is_array ($wValue)) {
632-
// if the value is an array, then this isn't a basic = comparison
633-
$key = key($wValue);
634-
$val = $wValue[$key];
635-
switch( strtolower($key) ) {
636-
case '0':
637-
foreach ($wValue as $v)
638-
$this->_bindParam ($v);
639-
break;
640-
case 'not in':
641-
case 'in':
642-
$comparison = ' ' . $key . ' (';
643-
if (is_object ($val)) {
644-
$comparison .= $this->_buildPair ("", $val);
645-
} else {
646-
foreach ($val as $v) {
647-
$comparison .= ' ?,';
648-
$this->_bindParam ($v);
649-
}
650-
}
651-
$this->_query .= rtrim($comparison, ',').' ) ';
652-
break;
653-
case 'not between':
654-
case 'between':
655-
$this->_query .= " $key ? AND ? ";
656-
$this->_bindParam ($val[0]);
657-
$this->_bindParam ($val[1]);
658-
break;
659-
default:
660-
// We are using a comparison operator with only one parameter after it
661-
$this->_query .= $this->_buildPair ($key, $val);
662-
}
663-
} else if ($wValue === null) {
664-
//
665-
} else {
666-
$this->_query .= $this->_buildPair ("=", $wValue);
667-
}
668-
}
669-
}
670-
671-
// Did the user call the "groupBy" method?
672-
if (!empty($this->_groupBy)) {
673-
$this->_query .= " GROUP BY ";
674-
foreach ($this->_groupBy as $key => $value) {
675-
// prepares the reset of the SQL query.
676-
$this->_query .= $value . ", ";
677-
}
678-
$this->_query = rtrim($this->_query, ', ') . " ";
679-
}
680-
681-
// Did the user call the "orderBy" method?
682-
if (!empty ($this->_orderBy)) {
683-
$this->_query .= " ORDER BY ";
684-
foreach ($this->_orderBy as $prop => $value) {
685-
// prepares the reset of the SQL query.
686-
$this->_query .= $prop . " " . $value . ", ";
687-
}
688-
$this->_query = rtrim ($this->_query, ', ') . " ";
689-
}
690-
691-
// Did the user set a limit
692-
if (isset($numRows)) {
693-
if (is_array ($numRows))
694-
$this->_query .= ' LIMIT ' . (int)$numRows[0] . ', ' . (int)$numRows[1];
695-
else
696-
$this->_query .= ' LIMIT ' . (int)$numRows;
697-
}
698-
699-
$this->_lastQuery = $this->replacePlaceHolders($this->_query, $this->_bindParams);
578+
$this->_lastQuery = $this->replacePlaceHolders ($this->_query, $this->_bindParams);
700579

701580
if ($this->isSubQuery)
702581
return;
@@ -757,6 +636,178 @@ protected function _dynamicBindResults(mysqli_stmt $stmt)
757636
return $results;
758637
}
759638

639+
640+
/**
641+
* Abstraction method that will build an JOIN part of the query
642+
*/
643+
protected function _buildJoin () {
644+
if (empty ($this->_join))
645+
return;
646+
647+
foreach ($this->_join as $prop => $value)
648+
$this->_query .= " " . $prop . " on " . $value;
649+
}
650+
651+
/**
652+
* Abstraction method that will build an INSERT or UPDATE part of the query
653+
*/
654+
protected function _buildTableData ($tableData) {
655+
if (!is_array ($tableData))
656+
return;
657+
658+
$isInsert = strpos ($this->_query, 'INSERT');
659+
$isUpdate = strpos ($this->_query, 'UPDATE');
660+
661+
if ($isInsert !== false) {
662+
$this->_query .= '(`' . implode(array_keys($tableData), '`, `') . '`)';
663+
$this->_query .= ' VALUES(';
664+
}
665+
666+
foreach ($tableData as $column => $value) {
667+
if ($isUpdate !== false)
668+
$this->_query .= "`" . $column . "` = ";
669+
670+
// Subquery value
671+
if (is_object ($value)) {
672+
$this->_query .= $this->_buildPair ("", $value) . ", ";
673+
continue;
674+
}
675+
676+
// Simple value
677+
if (!is_array ($value)) {
678+
$this->_bindParam ($value);
679+
$this->_query .= '?, ';
680+
continue;
681+
}
682+
683+
// Function value
684+
$key = key ($value);
685+
$val = $value[$key];
686+
switch ($key) {
687+
case '[I]':
688+
$this->_query .= $column . $val . ", ";
689+
break;
690+
case '[F]':
691+
$this->_query .= $val[0] . ", ";
692+
if (!empty ($val[1]))
693+
$this->_bindParams ($val[1]);
694+
break;
695+
case '[N]':
696+
if ($val == null)
697+
$this->_query .= "!" . $column . ", ";
698+
else
699+
$this->_query .= "!" . $val . ", ";
700+
break;
701+
default:
702+
die ("Wrong operation");
703+
}
704+
}
705+
$this->_query = rtrim($this->_query, ', ');
706+
if ($isInsert !== false)
707+
$this->_query .= ')';
708+
}
709+
710+
/**
711+
* Abstraction method that will build the part of the WHERE conditions
712+
*/
713+
protected function _buildWhere () {
714+
if (empty ($this->_where))
715+
return;
716+
717+
//Prepair the where portion of the query
718+
$this->_query .= ' WHERE ';
719+
720+
// Remove first AND/OR concatenator
721+
$this->_where[0][0] = '';
722+
foreach ($this->_where as $cond) {
723+
list ($concat, $wValue, $wKey) = $cond;
724+
725+
$this->_query .= " " . $concat ." " . $wKey;
726+
727+
// Empty value (raw where condition in wKey)
728+
if ($wValue === null)
729+
continue;
730+
731+
// Simple = comparison
732+
if (!is_array ($wValue))
733+
$wValue = Array ('=' => $wValue);
734+
735+
$key = key ($wValue);
736+
$val = $wValue[$key];
737+
switch (strtolower ($key)) {
738+
case '0':
739+
$this->_bindParams ($wValue);
740+
break;
741+
case 'not in':
742+
case 'in':
743+
$comparison = ' ' . $key . ' (';
744+
if (is_object ($val)) {
745+
$comparison .= $this->_buildPair ("", $val);
746+
} else {
747+
foreach ($val as $v) {
748+
$comparison .= ' ?,';
749+
$this->_bindParam ($v);
750+
}
751+
}
752+
$this->_query .= rtrim($comparison, ',').' ) ';
753+
break;
754+
case 'not between':
755+
case 'between':
756+
$this->_query .= " $key ? AND ? ";
757+
$this->_bindParams ($val);
758+
break;
759+
default:
760+
$this->_query .= $this->_buildPair ($key, $val);
761+
}
762+
}
763+
}
764+
765+
/**
766+
* Abstraction method that will build the GROUP BY part of the WHERE statement
767+
*
768+
*/
769+
protected function _buildGroupBy () {
770+
if (empty ($this->_groupBy))
771+
return;
772+
773+
$this->_query .= " GROUP BY ";
774+
foreach ($this->_groupBy as $key => $value)
775+
$this->_query .= $value . ", ";
776+
777+
$this->_query = rtrim($this->_query, ', ') . " ";
778+
}
779+
780+
/**
781+
* Abstraction method that will build the LIMIT part of the WHERE statement
782+
*
783+
* @param int $numRows The number of rows total to return.
784+
*/
785+
protected function _buildOrderBy () {
786+
if (empty ($this->_orderBy))
787+
return;
788+
789+
$this->_query .= " ORDER BY ";
790+
foreach ($this->_orderBy as $prop => $value)
791+
$this->_query .= $prop . " " . $value . ", ";
792+
793+
$this->_query = rtrim ($this->_query, ', ') . " ";
794+
}
795+
796+
/**
797+
* Abstraction method that will build the LIMIT part of the WHERE statement
798+
*
799+
* @param int $numRows The number of rows total to return.
800+
*/
801+
protected function _buildLimit ($numRows) {
802+
if (!isset ($numRows))
803+
return;
804+
805+
if (is_array ($numRows))
806+
$this->_query .= ' LIMIT ' . (int)$numRows[0] . ', ' . (int)$numRows[1];
807+
else
808+
$this->_query .= ' LIMIT ' . (int)$numRows;
809+
}
810+
760811
/**
761812
* Method attempts to prepare the SQL query
762813
* and throws an error if there was a problem.

0 commit comments

Comments
 (0)