@@ -828,9 +828,19 @@ protected function _insert($data)
828828 return $ insert [$ now ]['#rowid ' ] < $ insert [$ after ]['#rowid ' ];
829829 });
830830
831+ $ last_ai = 0 ;
832+ foreach ($ data ['properties ' ] as $ field => $ property ) {
833+ if (is_array ($ property ) && array_key_exists ('auto_increment ' , $ property ) && TRUE === $ property ['auto_increment ' ]) {
834+ for ($ insert as $ lid => $ array_data ) {
835+ $ last_ai = max ($ insert [$ lid ][$ field ], $ last_ai );
836+ }
837+ break ;
838+ }
839+ }
840+
831841 $ data ['data'] = $ insert ;
832842 $ data ['properties ']['last_valid_row_id'] = $ this ->_getLastValidRowID ($ insert , FALSE );
833- $ data ['properties ' ]['last_insert_id ' ] = $ ai_id ;
843+ $ data ['properties ']['last_insert_id'] = $ last_ai ;
834844 $ data ['properties']['last_link_id'] = $ lk_id ;
835845
836846 $ this ->cache->update($ this ->_getTablePath (), $ data );
@@ -936,7 +946,18 @@ protected function _replace($data)
936946 return $ insert [$ now ]['#rowid ' ] < $ insert [$ after ]['#rowid ' ];
937947 });
938948
949+ $ last_ai = 0 ;
950+ foreach ($ data ['properties ' ] as $ field => $ property ) {
951+ if (is_array ($ property ) && array_key_exists ('auto_increment ' , $ property ) && TRUE === $ property ['auto_increment ' ]) {
952+ for ($ insert as $ lid => $ array_data ) {
953+ $ last_ai = max ($ insert [$ lid ][$ field ], $ last_ai );
954+ }
955+ break ;
956+ }
957+ }
958+
939959 $ data ['data ' ] = $ insert ;
960+ $ data ['properties ' ]['last_insert_id ' ] = $ last_ai ;
940961
941962 $ this ->cache ->update ($ this ->_getTablePath (), $ data );
942963
@@ -1064,25 +1085,17 @@ protected function _update($data)
10641085 return $ data ['data ' ][$ now ]['#rowid ' ] < $ data ['data ' ][$ after ]['#rowid ' ];
10651086 });
10661087
1067- $ auto_increment = NULL ;
1068- foreach ((array )$ data ['properties ' ] as $ column => $ prop ) {
1069- if (is_array ($ prop ) && array_key_exists ('auto_increment ' , $ prop ) && $ prop ['auto_increment ' ] === TRUE ) {
1070- $ auto_increment = $ column ;
1088+ $ last_ai = 0 ;
1089+ foreach ($ data ['properties ' ] as $ field => $ property ) {
1090+ if (is_array ($ property ) && array_key_exists ('auto_increment ' , $ property ) && TRUE === $ property ['auto_increment ' ]) {
1091+ for ($ data ['data ' ] as $ lid => $ array_data ) {
1092+ $ last_ai = max ($ data ['data ' ][$ lid ][$ field ], $ last_ai );
1093+ }
10711094 break ;
10721095 }
10731096 }
10741097
1075- if (NULL !== $ auto_increment ) {
1076- $ last_insert_id = 0 ;
1077- foreach ((array )$ data ['data ' ] as $ d ) {
1078- if ($ last_insert_id === 0 ) {
1079- $ last_insert_id = $ d [$ auto_increment ];
1080- } else {
1081- $ last_insert_id = max ($ last_insert_id , $ d [$ auto_increment ]);
1082- }
1083- }
1084- $ data ['properties ' ]['last_insert_id ' ] = $ last_insert_id ;
1085- }
1098+ $ data ['properties ' ]['last_insert_id ' ] = $ last_ai ;
10861099
10871100 $ this ->cache ->update ($ this ->_getTablePath (), $ data );
10881101
@@ -1108,7 +1121,7 @@ protected function _truncate($data)
11081121 /**
11091122 * The count() query
11101123 * @param array $data
1111- * @return array
1124+ * @return QueryResult
11121125 * @throws Exception
11131126 */
11141127 protected function _count ($ data )
@@ -1163,9 +1176,11 @@ protected function _count($data)
11631176 } else {
11641177 $ result ['count( ' . implode (', ' , $ this ->parsedQuery ['parameters ' ]) . ') ' ] = $ count ;
11651178 }
1179+
1180+ $ result = array ($ result );
11661181 }
11671182
1168- return $ result ;
1183+ return new QueryResult ( $ result, $ this ) ;
11691184 }
11701185
11711186 /**
0 commit comments