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