@@ -61,7 +61,7 @@ trait WP_PDO_Synthetic_Statement_PHP_Compat {
6161 */
6262 #[ReturnTypeWillChange]
6363 public function setFetchMode ( $ mode , ...$ args ): bool {
64- return $ this ->setDefaultFetchMode ( $ mode , $ args );
64+ return $ this ->setDefaultFetchMode ( $ mode , ... $ args );
6565 }
6666
6767 /**
@@ -159,23 +159,19 @@ class WP_PDO_Synthetic_Statement extends PDOStatement {
159159 */
160160 private $ attributes = array ();
161161
162+ private $ stmt ;
163+
162164 /**
163165 * Constructor.
164166 *
165- * @param PDO $pdo The PDO connection.
166- * @param array $columns Basic column metadata (containing at least name, table name, and native type).
167- * @param array $rows Rows of the result set.
168- * @param int $affected_rows The number of affected rows.
167+ * @param PDOStatement $stmt The original PDO statement.
168+ * @param int $affected_rows The number of affected rows.
169169 */
170170 public function __construct (
171- PDO $ pdo ,
172- array $ columns ,
173- array $ rows ,
171+ PDOStatement $ stmt ,
174172 int $ affected_rows
175173 ) {
176- $ this ->pdo = $ pdo ;
177- $ this ->columns = $ columns ;
178- $ this ->rows = $ rows ;
174+ $ this ->stmt = $ stmt ;
179175 $ this ->affected_rows = $ affected_rows ;
180176 }
181177
@@ -186,7 +182,7 @@ public function __construct(
186182 * @return bool True on success, false on failure.
187183 */
188184 public function execute ( $ params = null ): bool {
189- throw new RuntimeException ( ' Not implemented ' );
185+ return $ this -> stmt -> execute ( $ params );
190186 }
191187
192188 /**
@@ -195,7 +191,7 @@ public function execute( $params = null ): bool {
195191 * @return int The number of columns in the result set.
196192 */
197193 public function columnCount (): int {
198- return count ( $ this ->columns );
194+ return $ this ->stmt -> columnCount ( );
199195 }
200196
201197 /**
@@ -225,91 +221,7 @@ public function fetch(
225221 $ cursorOrientation = 0 ,
226222 $ cursorOffset = 0
227223 ) {
228- if ( 0 === $ mode || null === $ mode ) {
229- $ mode = $ this ->fetch_mode ;
230- }
231- if ( null === $ cursorOrientation ) {
232- $ cursorOrientation = PDO ::FETCH_ORI_NEXT ;
233- }
234- if ( null === $ cursorOffset ) {
235- $ cursorOffset = 0 ;
236- }
237-
238- if ( ! array_key_exists ( $ this ->cursor_offset , $ this ->rows ) ) {
239- return false ;
240- }
241-
242- // Get current row data and column names.
243- $ row = $ this ->rows [ $ this ->cursor_offset ];
244- $ column_names = array_column ( $ this ->columns , 'name ' );
245-
246- // Advance the cursor to the next row.
247- $ this ->cursor_offset += 1 ;
248-
249- /*
250- * TODO: Support scrollable cursor ($cursorOrientation and $cursorOffset).
251- * This only has works for with statements that were prepared with
252- * the PDO::ATTR_CURSOR attribute set to PDO::CURSOR_SCROLL value.
253- * Without it, these parameters have no effect.
254- */
255-
256- /**
257- * With PHP < 8.1, the "PDO::ATTR_STRINGIFY_FETCHES" value of "false"
258- * is not working correctly with the PDO SQLite driver. In such case,
259- * we need to manually convert the row values to the correct types.
260- */
261- if ( PHP_VERSION_ID < 80100 && ! $ this ->getAttribute ( PDO ::ATTR_STRINGIFY_FETCHES ) ) {
262- foreach ( $ row as $ i => $ value ) {
263- $ type = $ this ->columns [ $ i ]['native_type ' ];
264- if ( 'integer ' === $ type ) {
265- $ row [ $ i ] = (int ) $ value ;
266- } elseif ( 'float ' === $ type ) {
267- $ row [ $ i ] = (float ) $ value ;
268- }
269- }
270- }
271-
272- switch ( $ mode ) {
273- case PDO ::FETCH_BOTH :
274- $ values = array ();
275- foreach ( $ row as $ i => $ value ) {
276- $ name = $ column_names [ $ i ];
277- $ values [ $ name ] = $ value ;
278- if ( ! array_key_exists ( $ i , $ values ) ) {
279- $ values [ $ i ] = $ value ;
280- }
281- }
282- return $ values ;
283- case PDO ::FETCH_NUM :
284- return $ row ;
285- case PDO ::FETCH_ASSOC :
286- return array_combine ( $ column_names , $ row );
287- case PDO ::FETCH_NAMED :
288- $ values = array ();
289- foreach ( $ row as $ i => $ value ) {
290- $ name = $ column_names [ $ i ];
291- if ( is_array ( $ values [ $ name ] ?? null ) ) {
292- $ values [ $ name ][] = $ value ;
293- } elseif ( array_key_exists ( $ name , $ values ) ) {
294- $ values [ $ name ] = array ( $ values [ $ name ], $ value );
295- } else {
296- $ values [ $ name ] = $ value ;
297- }
298- }
299- return $ values ;
300- case PDO ::FETCH_OBJ :
301- return (object ) array_combine ( $ column_names , $ row );
302- case PDO ::FETCH_CLASS :
303- throw new RuntimeException ( "'PDO::FETCH_CLASS' mode is not supported " );
304- case PDO ::FETCH_INTO :
305- throw new RuntimeException ( "'PDO::FETCH_INTO' mode is not supported " );
306- case PDO ::FETCH_LAZY :
307- throw new RuntimeException ( "'PDO::FETCH_LAZY' mode is not supported " );
308- case PDO ::FETCH_BOUND :
309- throw new RuntimeException ( "'PDO::FETCH_BOUND' mode is not supported " );
310- default :
311- throw new ValueError ( sprintf ( 'PDOStatement::fetch(): Argument #1 ($mode) must be a bitmask of PDO::FETCH_* constants ' , $ mode ) );
312- }
224+ return $ this ->stmt ->fetch ( $ mode , $ cursorOrientation , $ cursorOffset );
313225 }
314226
315227 /**
@@ -480,15 +392,7 @@ public function debugDumpParams(): ?bool {
480392 * @return array The result set as an array of rows.
481393 */
482394 private function fetchAllRows ( $ mode = null , ...$ args ): array {
483- if ( null === $ mode || 0 === $ mode ) {
484- $ mode = $ this ->fetch_mode ;
485- }
486-
487- $ rows = array ();
488- while ( $ row = $ this ->fetch ( $ mode , ...$ args ) ) {
489- $ rows [] = $ row ;
490- }
491- return $ rows ;
395+ return $ this ->stmt ->fetchAll ( $ mode , ...$ args );
492396 }
493397
494398 /**
0 commit comments