givewp icon indicating copy to clipboard operation
givewp copied to clipboard

Feature: add givewp_db_pre_query hook

Open jonwaldstein opened this issue 2 years ago • 1 comments

Resolves #6527

Description

This adds an action givewp_db_pre_query to our DB class for optimizing mysql before running data retrieval queries.

There is also an easy way to set mysql big selects using the DB class DB::enableBigSelects().

With this action people can do things like this:

add_action('givewp_db_pre_query', function() {
    static $bigSelects = false;

    if (!$bigSelects) {
        global $wpdb;

        $wpdb->query('SET SESSION SQL_BIG_SELECTS=1;');

        $bigSelects = true;
    }
});

or with our helper


add_action('givewp_db_pre_query', function() {
    \Give\Framework\Database\DB::enableBigSelects();
});

or target more specific queries

use Give\Framework\Database\DB;
use Give\Framework\Support\Facades\Str;

add_action('givewp_db_pre_query', function($sql) {
    if (Str::contains($sql, 'LEFT JOIN')){
        DB::enableBigSelects();
    }
});

Affects

Our database queries using the query builder.

Visuals

Testing Instructions

  • Set your mysql to max_join_size = 1500000000
  • Try to view the donations list table (should not load)
  • add snippet below and view donations list table (should load now)
add_action('givewp_db_pre_query', function() {
    \Give\Framework\Database\DB::enableBigSelects();
});

Pre-review Checklist

  • [ ] Acceptance criteria satisfied and marked in related issue
  • [ ] Relevant @unreleased tags included in DocBlocks
  • [ ] Includes unit tests
  • [ ] Reviewed by the designer (if follows a design)
  • [ ] Self Review of code and UX completed

jonwaldstein avatar Aug 09 '22 19:08 jonwaldstein

@JasonTheAdams this is ready for review. As we discussed, i'd like to put this to the test in different real-world situations before introducing a more permanent GiveWP setting.

jonwaldstein avatar Aug 10 '22 15:08 jonwaldstein