diff --git a/CHANGELOG.md b/CHANGELOG.md index 1949a10..67a64e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ All notable changes to this project will be documented in this file. This project adhere to the [Semantic Versioning](http://semver.org/) standard. -## [1.0.8] TBD +## [1.0.8] 2024-02-23 * Feat - Add the `DB::generate_results` and `DB::generate_col` methods to the `DB` class to fetch all results matching an unbounded query with a set of bounded queries. diff --git a/README.md b/README.md index 696ff70..b2de1ae 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,9 @@ composer require stellarwp/db - [Inherited from `$wpdb`](#inherited-from-wpdb) - [`get_var()`](#get_var) - [`get_col()`](#get_col) + - [`generate_col()`](#get_col) + - [`get_results()`](#get_results) + - [`generate_results()`](#generate_results) - [`esc_like()`](#esc_like) - [`remove_placeholder_escape()`](#remove_placeholder_escape) @@ -930,6 +933,55 @@ $meta_values = DB::get_col( ); ``` +### `generate_col()` + +Returns a Generator that will produce an array of values for the column for the given query. +Differrently from the `get_col()` method, this method will run unbounded queries (queries without a `LIMIT` set) in batches, ensuring as-efficient-as-possible use of the database and memory. + +```php +$meta_values = DB::generate_col( + DB::table( 'postmeta' ) + ->select( 'meta_value' ) + ->where( 'meta_key', 'some_key' ) + ->getSQL() +); + +foreach( $meta_values as $meta_value ) { + // Do something with the meta value. +} +``` + +### `get_results()` + +Returns an array of rows for for the given query. + +```php +$private_posts = DB::get_results( + DB::table( 'posts' ) + ->select( '*' ) + ->where( 'post_status', 'private' ) + ->getSQL() +); +``` + +### `generate_results()` + +Returns a Generator that will produce an array of rows for the given query. +Differrently from the `get_results()` method, this method will run unbounded queries (queries without a `LIMIT` set) in batches, ensuring as-efficient-as-possible use of the database and memory. + +```php +$private_posts = DB::generate_results( + DB::table( 'posts' ) + ->select( '*' ) + ->where( 'post_status', 'private' ) + ->getSQL() +); + +foreach( $private_posts as $private_post ) { + // Do something with the post. +} +``` + ### `esc_like()` Escapes a string with a percent sign in it so it can be safely used with [Where LIKE](#where-like-clauses) without the percent sign being interpreted as a wildcard character.