@@ -21,7 +21,7 @@ class MysqliDb
2121 protected static $ _instance ;
2222 /**
2323 * Table prefix
24- *
24+ *
2525 * @var string
2626 */
2727 public static $ prefix = '' ;
@@ -54,7 +54,7 @@ class MysqliDb
5454 *
5555 * @var array
5656 */
57- protected $ _join = array ();
57+ protected $ _join = array ();
5858 /**
5959 * An array that holds where conditions 'fieldname' => 'value'
6060 *
@@ -64,11 +64,11 @@ class MysqliDb
6464 /**
6565 * Dynamic type list for order by condition value
6666 */
67- protected $ _orderBy = array ();
67+ protected $ _orderBy = array ();
6868 /**
6969 * Dynamic type list for group by condition value
7070 */
71- protected $ _groupBy = array ();
71+ protected $ _groupBy = array ();
7272 /**
7373 * Dynamic array that holds a combination of where condition/table data value types and parameter references
7474 *
@@ -79,13 +79,13 @@ class MysqliDb
7979 * Variable which holds an amount of returned rows during get/getOne/select queries
8080 *
8181 * @var string
82- */
82+ */
8383 public $ count = 0 ;
8484 /**
8585 * Variable which holds an amount of returned rows during get/getOne/select queries with withTotalCount()
8686 *
8787 * @var string
88- */
88+ */
8989 public $ totalCount = 0 ;
9090 /**
9191 * Variable which holds last statement error
@@ -113,7 +113,7 @@ class MysqliDb
113113 protected $ isSubQuery = false ;
114114
115115 /**
116- * Name of the auto increment column
116+ * Name of the auto increment column
117117 *
118118 */
119119 protected $ _lastInsertId = null ;
@@ -240,7 +240,7 @@ protected function reset()
240240 $ this ->_where = array ();
241241 $ this ->_join = array ();
242242 $ this ->_orderBy = array ();
243- $ this ->_groupBy = array ();
243+ $ this ->_groupBy = array ();
244244 $ this ->_bindParams = array ('' ); // Create the empty 0 index
245245 $ this ->_query = null ;
246246 $ this ->_queryOptions = array ();
@@ -281,10 +281,10 @@ public function ObjectBuilder () {
281281 $ this ->returnType = 'Object ' ;
282282 return $ this ;
283283 }
284-
284+
285285 /**
286286 * Method to set a prefix
287- *
287+ *
288288 * @param string $prefix Contains a tableprefix
289289 */
290290 public function setPrefix ($ prefix = '' )
@@ -399,7 +399,7 @@ public function get($tableName, $numRows = null, $columns = '*')
399399 if (empty ($ columns ))
400400 $ columns = '* ' ;
401401
402- $ column = is_array ($ columns ) ? implode (', ' , $ columns ) : $ columns ;
402+ $ column = is_array ($ columns ) ? implode (', ' , $ columns ) : $ columns ;
403403 $ this ->_tableName = self ::$ prefix . $ tableName ;
404404 $ this ->_query = 'SELECT ' . implode (' ' , $ this ->_queryOptions ) . ' ' .
405405 $ column . " FROM " . $ this ->_tableName ;
@@ -423,7 +423,7 @@ public function get($tableName, $numRows = null, $columns = '*')
423423 *
424424 * @return array Contains the returned rows from the select query.
425425 */
426- public function getOne ($ tableName , $ columns = '* ' )
426+ public function getOne ($ tableName , $ columns = '* ' )
427427 {
428428 $ res = $ this ->get ($ tableName , 1 , $ columns );
429429
@@ -444,7 +444,7 @@ public function getOne($tableName, $columns = '*')
444444 *
445445 * @return string Contains the value of a returned column.
446446 */
447- public function getValue ($ tableName , $ column )
447+ public function getValue ($ tableName , $ column )
448448 {
449449 $ res = $ this ->ArrayBuilder ()->get ($ tableName , 1 , "{$ column } as retval " );
450450
@@ -563,12 +563,12 @@ public function where($whereProp, $whereValue = 'DBNULL', $operator = '=', $cond
563563 return $ this ;
564564 }
565565
566- /**
567- * This function store update column's name and column name of the
566+ /**
567+ * This function store update column's name and column name of the
568568 * autoincrement column
569- *
569+ *
570570 * @param Array Variable with values
571- * @param String Variable value
571+ * @param String Variable value
572572 */
573573 public function onDuplicate ($ _updateColumns , $ _lastInsertId = null )
574574 {
@@ -632,7 +632,7 @@ public function orderBy($orderByField, $orderbyDirection = "DESC", $customFields
632632 $ orderbyDirection = strtoupper (trim ($ orderbyDirection ));
633633 $ orderByField = preg_replace ("/[^-a-z0-9\.\(\),_`]+/i " ,'' , $ orderByField );
634634
635- // Add table prefix to orderByField if needed.
635+ // Add table prefix to orderByField if needed.
636636 //FIXME: We are adding prefix only if table is enclosed into `` to distinguish aliases
637637 // from table names
638638 $ orderByField = preg_replace ('/(\`)([`a-zA-Z0-9_]*\.)/ ' , '\1 ' . self ::$ prefix . '\2 ' , $ orderByField );
@@ -650,7 +650,7 @@ public function orderBy($orderByField, $orderbyDirection = "DESC", $customFields
650650
651651 $ this ->_orderBy [$ orderByField ] = $ orderbyDirection ;
652652 return $ this ;
653- }
653+ }
654654
655655 /**
656656 * This method allows you to specify multiple (method chaining optional) GROUP BY statements for SQL queries.
@@ -667,7 +667,7 @@ public function groupBy($groupByField)
667667
668668 $ this ->_groupBy [] = $ groupByField ;
669669 return $ this ;
670- }
670+ }
671671
672672 /**
673673 * This methods returns the ID of the last inserted item
@@ -805,57 +805,6 @@ private function _buildInsert ($tableName, $insertData, $operation)
805805 return true ;
806806 }
807807
808- /**
809- * Helper function to add variables into the query statement
810- *
811- * @param Array Variable with values
812- */
813- protected function _buildDuplicate ($ tableData )
814- {
815- if (is_array ($ this ->_updateColumns ) && !empty ($ this ->_updateColumns )) {
816- $ this ->_query .= " on duplicate key update " ;
817- if ($ this ->_lastInsertId ) {
818- $ this ->_lastQuery .= $ this ->_lastInsertId ."=LAST_INSERT_ID( " .$ this ->_lastInsertId ."), " ;
819- $ this ->_lastInsertId = null ;
820- }
821-
822- foreach ($ this ->_updateColumns as $ column ) {
823- $ this ->_query .= "` " . $ column . "` = " ;
824-
825- // Simple value
826- if (!is_array ($ tableData [$ column ])) {
827- $ this ->_bindParam ($ tableData [$ column ]);
828- $ this ->_query .= '?, ' ;
829- continue ;
830- }
831-
832- // Function value
833- $ arr = $ tableData [$ column ];
834- $ key = key ($ arr );
835- $ val = $ arr [$ key ];
836- switch ($ key ) {
837- case '[I] ' :
838- $ this ->_query .= $ column . $ val . ", " ;
839- break ;
840- case '[F] ' :
841- $ this ->_query .= $ val [0 ] . ", " ;
842- if (!empty ($ val [1 ]))
843- $ this ->_bindParams ($ val [1 ]);
844- break ;
845- case '[N] ' :
846- if ($ val == null )
847- $ this ->_query .= "! " . $ column . ", " ;
848- else
849- $ this ->_query .= "! " . $ val . ", " ;
850- break ;
851- default :
852- die ("Wrong operation " );
853- }
854- }
855- $ this ->_query = rtrim ($ this ->_query , ', ' );
856- }
857- }
858-
859808 /**
860809 * Abstraction method that will compile the WHERE statement,
861810 * any passed update data, and the desired rows.
@@ -870,12 +819,12 @@ protected function _buildDuplicate($tableData)
870819 protected function _buildQuery ($ numRows = null , $ tableData = null )
871820 {
872821 $ this ->_buildJoin ();
873- $ this ->_buildTableData ($ tableData );
822+ $ this ->_buildInsertQuery ($ tableData );
874823 $ this ->_buildWhere ();
875824 $ this ->_buildGroupBy ();
876825 $ this ->_buildOrderBy ();
877826 $ this ->_buildLimit ($ numRows );
878- $ this ->_buildDuplicate ($ tableData );
827+ $ this ->_buildOnDuplicate ($ tableData );
879828
880829 $ this ->_lastQuery = $ this ->replacePlaceHolders ($ this ->_query , $ this ->_bindParams );
881830
@@ -912,7 +861,7 @@ protected function _dynamicBindResults(mysqli_stmt $stmt)
912861
913862 // if $meta is false yet sqlstate is true, there's no sql error but the query is
914863 // most likely an update/insert/delete which doesn't produce any results
915- if (!$ meta && $ stmt ->sqlstate ) {
864+ if (!$ meta && $ stmt ->sqlstate ) {
916865 return array ();
917866 }
918867
@@ -996,20 +945,9 @@ protected function _buildJoin () {
996945 }
997946 }
998947
999- /**
1000- * Abstraction method that will build an INSERT or UPDATE part of the query
1001- */
1002- protected function _buildTableData ($ tableData ) {
1003- if (!is_array ($ tableData ))
1004- return ;
1005-
1006- $ isInsert = preg_match ('/^[INSERT|REPLACE]/ ' , $ this ->_query );
1007- if ($ isInsert )
1008- $ this ->_query .= ' (` ' . implode (array_keys ($ tableData ), '`, ` ' ) . '`) VALUES ( ' ;
1009- else
1010- $ this ->_query .= " SET " ;
1011-
1012- foreach ($ tableData as $ column => $ value ) {
948+ public function _buildDataPairs ($ tableData , $ tableColumns , $ isInsert ) {
949+ foreach ($ tableColumns as $ column ) {
950+ $ value = $ tableData [$ column ];
1013951 if (!$ isInsert )
1014952 $ this ->_query .= "` " . $ column . "` = " ;
1015953
@@ -1021,7 +959,7 @@ protected function _buildTableData ($tableData) {
1021959
1022960 // Simple value
1023961 if (!is_array ($ value )) {
1024- $ this ->_bindParam ($ value );
962+ $ this ->_bindParam ($ value );
1025963 $ this ->_query .= '?, ' ;
1026964 continue ;
1027965 }
@@ -1030,25 +968,59 @@ protected function _buildTableData ($tableData) {
1030968 $ key = key ($ value );
1031969 $ val = $ value [$ key ];
1032970 switch ($ key ) {
1033- case '[I] ' :
1034- $ this ->_query .= $ column . $ val . ", " ;
1035- break ;
1036- case '[F] ' :
1037- $ this ->_query .= $ val [0 ] . ", " ;
1038- if (!empty ($ val [1 ]))
1039- $ this ->_bindParams ($ val [1 ]);
1040- break ;
1041- case '[N] ' :
1042- if ($ val == null )
1043- $ this ->_query .= "! " . $ column . ", " ;
1044- else
1045- $ this ->_query .= "! " . $ val . ", " ;
1046- break ;
1047- default :
1048- die ("Wrong operation " );
971+ case '[I] ' :
972+ $ this ->_query .= $ column . $ val . ", " ;
973+ break ;
974+ case '[F] ' :
975+ $ this ->_query .= $ val [0 ] . ", " ;
976+ if (!empty ($ val [1 ]))
977+ $ this ->_bindParams ($ val [1 ]);
978+ break ;
979+ case '[N] ' :
980+ if ($ val == null )
981+ $ this ->_query .= "! " . $ column . ", " ;
982+ else
983+ $ this ->_query .= "! " . $ val . ", " ;
984+ break ;
985+ default :
986+ die ("Wrong operation " );
1049987 }
1050988 }
1051- $ this ->_query = rtrim ($ this ->_query , ', ' );
989+ $ this ->_query = rtrim ($ this ->_query , ', ' );
990+ }
991+
992+ /**
993+ * Helper function to add variables into the query statement
994+ *
995+ * @param Array Variable with values
996+ */
997+ protected function _buildOnDuplicate ($ tableData )
998+ {
999+ if (is_array ($ this ->_updateColumns ) && !empty ($ this ->_updateColumns )) {
1000+ $ this ->_query .= " on duplicate key update " ;
1001+ if ($ this ->_lastInsertId )
1002+ $ this ->_query .= $ this ->_lastInsertId . "=LAST_INSERT_ID ( " .$ this ->_lastInsertId ."), " ;
1003+
1004+ $ this ->_buildDataPairs ($ tableData , $ this ->_updateColumns , false );
1005+ }
1006+ }
1007+
1008+ /**
1009+ * Abstraction method that will build an INSERT or UPDATE part of the query
1010+ */
1011+ protected function _buildInsertQuery ($ tableData ) {
1012+ if (!is_array ($ tableData ))
1013+ return ;
1014+
1015+ $ isInsert = preg_match ('/^[INSERT|REPLACE]/ ' , $ this ->_query );
1016+ $ dataColumns = array_keys ($ tableData );
1017+ if ($ isInsert )
1018+ $ this ->_query .= ' (` ' . implode ($ dataColumns , '`, ` ' ) . '`) VALUES ( ' ;
1019+ else
1020+ $ this ->_query .= " SET " ;
1021+
1022+ $ this ->_buildDataPairs ($ tableData , $ dataColumns , $ isInsert );
1023+
10521024 if ($ isInsert )
10531025 $ this ->_query .= ') ' ;
10541026 }
@@ -1234,7 +1206,7 @@ public function getLastQuery () {
12341206
12351207 /**
12361208 * Method returns mysql error
1237- *
1209+ *
12381210 * @return string
12391211 */
12401212 public function getLastError () {
@@ -1246,7 +1218,7 @@ public function getLastError () {
12461218 /**
12471219 * Mostly internal method to get query and its params out of subquery object
12481220 * after get() and getAll()
1249- *
1221+ *
12501222 * @return array
12511223 */
12521224 public function getSubQuery () {
@@ -1321,7 +1293,7 @@ public function inc($num = 1) {
13211293 public function dec ($ num = 1 ) {
13221294 return Array ("[I] " => "- " . (int )$ num );
13231295 }
1324-
1296+
13251297 /**
13261298 * Method generates change boolean function call
13271299 * @param string column name. null by default
0 commit comments