@@ -974,15 +974,15 @@ protected function _replace($data)
974974 $ rows = $ this ->parsedQuery ['extensions ' ]['in ' ];
975975 foreach ((array )$ rows as $ row ) {
976976 if (!in_array ($ row , $ data ['prototype ' ], FALSE )) {
977- throw new Exception ("JSONDB Error: Can't insert data in the table \"{$ this ->table }\". The column \"{$ row }\" doesn't exist. " );
977+ throw new Exception ("JSONDB Error: Can't replace data in the table \"{$ this ->table }\". The column \"{$ row }\" doesn't exist. " );
978978 }
979979 }
980980 }
981981
982982 $ values_nb = count ($ this ->parsedQuery ['parameters ' ]);
983983 $ rows_nb = count ($ rows );
984984 if ($ values_nb !== $ rows_nb ) {
985- throw new Exception ("JSONDB Error: Can't insert data in the table \"{$ this ->table }\". Invalid number of parameters (given \"{$ values_nb }\" values to insert in \"{$ rows_nb }\" columns). " );
985+ throw new Exception ("JSONDB Error: Can't replace data in the table \"{$ this ->table }\". Invalid number of parameters (given \"{$ values_nb }\" values to insert in \"{$ rows_nb }\" columns). " );
986986 }
987987 $ current_data = $ data ['data ' ];
988988 $ insert = array ();
@@ -1008,34 +1008,45 @@ protected function _replace($data)
10081008 }
10091009 }
10101010
1011- $ insert = array_replace_recursive ($ current_data , $ insert );
1011+ $ i = 0 ;
1012+ foreach ($ current_data as &$ array_data ) {
1013+ $ array_data = array_key_exists ($ i , $ insert ) ? array_replace ($ array_data , $ insert [$ i ]) : $ array_data ;
1014+ $ i ++;
1015+ }
1016+ unset($ array_data );
1017+ $ insert = $ current_data ;
10121018
10131019 $ pk_error = FALSE ;
10141020 $ non_pk = array_flip (array_diff ($ data ['prototype ' ], $ data ['properties ' ]['primary_keys ' ]));
1015- foreach ((array )$ insert as $ index => $ array_data ) {
1021+ $ i = 0 ;
1022+ foreach ($ insert as $ array_data ) {
10161023 $ array_data = array_diff_key ($ array_data , $ non_pk );
1017- foreach (array_slice ($ insert , $ index + 1 ) as $ item ) {
1018- $ item = array_diff_key ($ item , $ non_pk );
1019- $ pk_error = $ pk_error || ($ item === $ array_data );
1024+ foreach (array_slice ($ insert , $ i + 1 ) as $ value ) {
1025+ $ value = array_diff_key ($ value , $ non_pk );
1026+ $ pk_error = $ pk_error || ($ value === $ array_data );
10201027 if ($ pk_error ) {
1021- $ values = implode (', ' , $ item );
1028+ exit ;
1029+ $ values = implode (', ' , $ value );
10221030 $ keys = implode (', ' , $ data ['properties ' ]['primary_keys ' ]);
10231031 throw new Exception ("JSONDB Error: Can't replace value. Duplicate values \"{$ values }\" for primary keys \"{$ keys }\". " );
10241032 }
10251033 }
1034+ $ i ++;
10261035 }
10271036
10281037 $ uk_error = FALSE ;
1038+ $ i = 0 ;
10291039 foreach ((array )$ data ['properties ' ]['unique_keys ' ] as $ uk ) {
1030- foreach ($ insert as $ index => $ array_data ) {
1040+ foreach ($ insert as $ array_data ) {
10311041 $ array_data = array_intersect_key ($ array_data , array ($ uk => $ uk ));
1032- foreach (array_slice ($ insert , $ index + 1 ) as $ item ) {
1033- $ item = array_intersect_key ($ item , array ($ uk => $ uk ));
1034- $ uk_error = $ uk_error || (!empty ($ item [$ uk ]) && ($ item === $ array_data ));
1042+ foreach (array_slice ($ insert , $ i + 1 ) as $ value ) {
1043+ $ value = array_intersect_key ($ value , array ($ uk => $ uk ));
1044+ $ uk_error = $ uk_error || (!empty ($ item [$ uk ]) && ($ value === $ array_data ));
10351045 if ($ uk_error ) {
1036- throw new Exception ("JSONDB Error: Can't replace value. Duplicate values \"{$ item [$ uk ]}\" for unique key \"{$ uk }\". " );
1046+ throw new Exception ("JSONDB Error: Can't replace value. Duplicate values \"{$ value [$ uk ]}\" for unique key \"{$ uk }\". " );
10371047 }
10381048 }
1049+ $ i ++;
10391050 }
10401051 }
10411052
@@ -1046,8 +1057,8 @@ protected function _replace($data)
10461057 }
10471058 unset($ line );
10481059
1049- usort ($ insert , function ($ after , $ now ) {
1050- return $ now ['#rowid ' ] < $ after ['#rowid ' ];
1060+ uksort ($ insert , function ($ after , $ now) use ( $ insert ) {
1061+ return $ insert [ $ now] ['#rowid ' ] < $ insert [ $ after] ['#rowid ' ];
10511062 });
10521063
10531064 $ data ['data ' ] = $ insert ;
@@ -1076,31 +1087,25 @@ protected function _delete($data)
10761087 $ to_delete = $ out ;
10771088 }
10781089
1079- $ insert = array ();
10801090 foreach ($ to_delete as $ array ) {
10811091 if (in_array ($ array , $ current_data , TRUE )) {
1082- $ current_data [array_search ($ array , $ current_data , TRUE )] = NULL ;
1083- }
1084- }
1085- foreach ($ current_data as $ array ) {
1086- if (NULL !== $ array ) {
1087- $ insert [] = $ array ;
1092+ unset($ current_data [array_search ($ array , $ current_data , TRUE )]);
10881093 }
10891094 }
10901095
1091- foreach ($ insert as $ key => &$ line ) {
1096+ foreach ($ current_data as $ key => &$ line ) {
10921097 uksort ($ line , function ($ after , $ now ) use ($ data ) {
10931098 return array_search ($ now , $ data ['prototype ' ], TRUE ) < array_search ($ after , $ data ['prototype ' ], TRUE );
10941099 });
10951100 }
10961101 unset($ line );
10971102
1098- usort ( $ insert , function ($ after , $ now ) {
1099- return $ now ['#rowid ' ] < $ after ['#rowid ' ];
1103+ uksort ( $ current_data , function ($ after , $ now) use ( $ current_data ) {
1104+ return $ current_data [ $ now] ['#rowid ' ] < $ current_data [ $ after] ['#rowid ' ];
11001105 });
11011106
1102- $ data ['data ' ] = $ insert ;
1103- $ data ['properties ' ]['last_valid_row_id ' ] = $ this ->_getLastValidRowID ($ to_delete ) - 1 ;
1107+ $ data ['data ' ] = $ current_data ;
1108+ ( count ( $ to_delete ) > 0 ) ? $ data ['properties ' ]['last_valid_row_id ' ] = $ this ->_getLastValidRowID ($ to_delete ) - 1 : NULL ;
11041109
11051110 $ this ->cache ->update ($ this ->_getTablePath (), $ data );
11061111
@@ -1180,8 +1185,8 @@ protected function _update($data)
11801185 }
11811186 unset($ line );
11821187
1183- usort ($ data ['data ' ], function ($ after , $ now ) {
1184- return $ now ['#rowid ' ] < $ after ['#rowid ' ];
1188+ uksort ($ data ['data ' ], function ($ after , $ now) use ( $ data ) {
1189+ return $ data [ ' data ' ][ $ now] ['#rowid ' ] < $ data [ ' data ' ][ $ after] ['#rowid ' ];
11851190 });
11861191
11871192 $ this ->cache ->update ($ this ->_getTablePath (), $ data );
0 commit comments