Plugin Directory

Changeset 2352101


Ignore:
Timestamp:
08/04/2020 03:52:48 AM (6 years ago)
Author:
pressmaninc
Message:

v2.0.0

Location:
beyond-wpdb
Files:
18 added
10 edited
1 copied

Legend:

Unmodified
Added
Removed
  • beyond-wpdb/tags/2.0.0/beyond-wpdb.php

    r2337801 r2352101  
    33Plugin Name: Beyond Wpdb
    44Plugin URI:
    5 Description: Create a new table and register the meta data in a single row of JSON columns.Registration is done by mysql's trigger function.The data in WordPress is not affected.Convert sql statement, when searching for meta data.
    6 Version: 1.1.0
     5Description: Speed up your WordPress database by making use of JSON type columns in MySQL.
     6Version: 2.0.0
    77Author: PRESSMAN
    88Author URI: https://www.pressman.ne.jp/
     
    4545// Deifne Beyond_table name.
    4646foreach( array_keys( BEYOND_WPDB_PRIMARYS ) as $primary ) {
    47     define( beyond_wpdb_get_define_table_name( $primary ), $wpdb->prefix . $primary . 'meta_json' );
     47    define( beyond_wpdb_get_define_table_name( $primary ), $wpdb->prefix . $primary . 'meta_beyond' );
    4848}
    4949
    5050// Require files.
    51 require_once( plugin_dir_path( __FILE__ ) . 'class/register-hook.php' );
     51require_once( plugin_dir_path( __FILE__ ) . 'class/register.php' );
     52require_once( plugin_dir_path( __FILE__ ) . 'class/information.php' );
    5253require_once( plugin_dir_path( __FILE__ ) . 'class/sql.php' );
    5354require_once( plugin_dir_path( __FILE__ ) . 'class/meta-query.php' );
    5455require_once( plugin_dir_path( __FILE__ ) . 'class/wp-orderby.php' );
    55 require_once( plugin_dir_path( __FILE__ ) . 'class/column.php' );
    5656require_once( plugin_dir_path( __FILE__ ) . 'class/options.php' );
    57 
    58 // Plugin activation hook.
    59 register_activation_hook( __FILE__, ['Beyond_Wpdb_Register_Hook', 'activation'] );
    60 register_deactivation_hook( __FILE__, ['Beyond_Wpdb_Register_Hook', 'deactivation'] );
    61 register_uninstall_hook( __FILE__, ['Beyond_Wpdb_Register_Hook', 'uninstall'] );
     57require_once( plugin_dir_path( __FILE__ ) . 'class/admin-ajax.php' );
  • beyond-wpdb/tags/2.0.0/class/meta-query.php

    r2337801 r2352101  
    467467     */
    468468    public function virtual_column_exists( $key, $type ) {
    469         $beyond_wpdb_column = new Beyond_Wpdb_Column();
    470         $beyond_wpdb_column->set_columns();
    471         $virtual_columns = $beyond_wpdb_column->get_columns();
    472 
    473         return in_array( $key, $virtual_columns[$type] );
     469        $beyond_wpdb_info = new Beyond_Wpdb_Information();
     470        $beyond_wpdb_info->set_columns();
     471        $virtual_columns = $beyond_wpdb_info->get_columns();
     472        $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $type ) ) );
     473
     474        return in_array( $key, $virtual_columns[$table_name] );
    474475    }
    475476
  • beyond-wpdb/tags/2.0.0/class/options.php

    r2337802 r2352101  
    99 */
    1010class Beyond_Wpdb_Settings_page {
    11     private $columns = array();
     11    private $privilege_error = False;
    1212
    1313    /**
     
    1616    public function __construct()
    1717    {
     18        load_theme_textdomain( 'beyond-wpdb', plugin_dir_path( __FILE__ ) . '../lang' );
     19        add_action( 'admin_notices', array( $this, 'notice__warnig_or_error' ) );
    1820        add_action( 'admin_menu', array( $this, 'add_beyond_wpdb_settings_page' ) );
    1921        add_action( 'admin_init', array( $this, 'page_init' ) );
    20         $beyond_wpdb_column = new Beyond_Wpdb_Column();
    21         $beyond_wpdb_column->set_columns();
    22         $this->columns = $beyond_wpdb_column->get_columns();
    23 
    24         // add option beyond_wpdb_virtual_column_name
    25         add_action( 'add_option_beyond_wpdb_virtual_column_name', array( $this, 'do_after_add_virtual_column' ) ,10, 2 );
    26         // add option beyond_wpdb_data_init_name
    27         add_action( 'add_option_beyond_wpdb_data_init_name', array( $this, 'do_after_add_data_init' ) ,10, 2 );
    28         // update option beyond_wpdb_virtual_column_name
    29         add_action( 'update_option_beyond_wpdb_virtual_column_name', array( $this, 'do_after_update_virtual_column' ) ,10, 3 );
    30         // update option beyond_wpdb_data_init_name
    31         add_action( 'update_option_beyond_wpdb_data_init_name', array ( $this, 'do_after_update_data_init' ) ,10, 3 );
    32 
     22
     23    }
     24
     25    /**
     26     * Display warnig or error
     27     */
     28    function notice__warnig_or_error() {
     29        global $wpdb, $pagenow;
     30
     31        $current_db = $wpdb->get_var( "SELECT DATABASE()" );
     32        $current_user = explode('@', $wpdb->get_var( "SELECT USER()" ));
     33        $grants = $wpdb->get_results( "SHOW GRANTS FOR '{$current_user[0]}'@'{$current_user[1]}'", ARRAY_A );
     34        $grant_for_mysql_db = False;
     35        $current_privileges = array();
     36
     37        foreach ( $grants as $value ) {
     38            $value = array_values( $value )[0];
     39            if ( strpos( $value, "ON `mysql`." ) || strpos( $value, "root" ) ) {
     40                $grant_for_mysql_db = True;
     41            }
     42        }
     43
     44        if ( $grant_for_mysql_db ) {
     45            $result_privileges = $wpdb->get_results( "SELECT * FROM mysql.db WHERE Db = '{$current_db}' AND User = '{$current_user[0]}'", ARRAY_A );
     46            foreach ( $result_privileges as $val ) {
     47                $keys = array_keys( $val );
     48                $values = array_values( $val );
     49
     50                // If $v is Y, $keys[$idx] is assumed to be authorized.
     51                foreach ( $values as $idx => $v ) {
     52                    if ( $v === 'Y' ) {
     53                        array_push( $current_privileges, $keys[$idx] );
     54                    }
     55                }
     56            }
     57        }
     58
     59        $privileges_error = '';
     60        $expected_privileges = array(
     61            'Trigger_priv',
     62            'Select_priv',
     63            'Insert_priv',
     64            'Update_priv',
     65            'Delete_priv',
     66            'Create_priv',
     67            'Drop_priv'
     68        );
     69        if ( count( $current_privileges ) > 0 ) {
     70            foreach ( $expected_privileges as $privilege ) {
     71                if ( ! in_array( $privilege, $current_privileges ) ) {
     72                    switch ( $privilege ) {
     73                        case 'Trigger_priv':
     74                            $privileges_error .= "<p>" . __("You do not have permission to create triggers.", "beyond-wpdb") . "</p>";
     75                            break;
     76                        case 'Select_priv':
     77                            $privileges_error .= "<p>" . __("You don't have SELECT permission.", "beyond-wpdb") . "</p>";
     78                            break;
     79                        case 'Insert_priv':
     80                            $privileges_error .= "<p>" . __("You don't have INSERT permission.", "beyond-wpdb") . "</p>";
     81                            break;
     82                        case 'Update_priv':
     83                            $privileges_error .= "<p>" . __("You don't have UPDATE permission.", "beyond-wpdb") . "</p>";
     84                            break;
     85                        case 'Delete_priv':
     86                            $privileges_error .= "<p>" . __("You don't have DELETE permission.", "beyond-wpdb") . "</p>";
     87                            break;
     88                        case 'Create_priv':
     89                            $privileges_error .= "<p>" . __("You don't have CREATE permission.", "beyond-wpdb") . "</p>";
     90                            break;
     91                        default :
     92                            $privileges_error .= "<p>" . __("You don't have DROP permission.", "beyond-wpdb") . "</p>";
     93                            break;
     94                    }
     95                    $this->privilege_error = True;
     96                }
     97            }
     98        }
     99
     100        if ( isset( $_GET['page'] ) && ! $_GET['page'] && $pagenow === 'options-general.php' ) {
     101            if ( ! $grant_for_mysql_db ) {
     102                $grants_message = __("You do not have permission to the mysql database.<br>We access the db table in the mysql database to find out what permissions we have to use each feature.<br>There is a possibility that each function will not be available.", "beyond-wpdb" );
     103                print "
     104                <div class='notice notice-warning is-dismissible'>
     105                    <p>
     106                    {$grants_message}
     107                    </p>
     108                </div>
     109                ";
     110            }
     111
     112            if ( $privileges_error ) {
     113                $priviles_message = __( "You do not have the following permissions.Please use each function after granting permission.", "beyond-wpdb" );
     114                print "
     115                <div class='notice notice-error is-dismissible'>
     116                    <p>{$priviles_message}</p>
     117                    {$privileges_error}
     118                </div>";
     119            }
     120        }
    33121    }
    34122
     
    52140    public function create_beyond_wpdb_settings_page()
    53141    {
     142        $disabled = $this->checkDisabled() ? '' : 'disabled';
     143        $features = __( "Speed up database loading by creating your own tables that aggregate meta information.<br>This is especially useful when you have a large number of records and for complex meta-query data calls.", "beyond-wpdb" );
    54144?>
     145        <style>
     146            .d-block {
     147                display: block;
     148            }
     149            .d-inline-block {
     150                display: inline-block;
     151            }
     152            .d-none {
     153                display: none;
     154            }
     155            .table_not_exists {
     156                opacity:0.3;
     157                pointer-envet:none;
     158            }
     159        </style>
    55160        <div class='beyond-wpdb-settings-wrap'>
    56161            <!-- title -->
    57             <h1 style="margin-bottom: 30px;">Beyond WPDB Settings</h1>
    58             <!-- form -->
    59             <form action='options.php' method='post'>
    60                 <!-- Group Concat -->
    61                 <div>
    62                     <?php settings_fields( 'beyond_wpdb_group' ); ?>
    63                     <?php do_settings_sections( 'croup_concat_section' ); ?>
    64                 </div>
    65                 <div>
    66                     <?php settings_fields( 'beyond_wpdb_group' ); ?>
    67                     <?php do_settings_sections( 'data_init_section' ); ?>
    68                 </div>
    69                 <!-- Virtual Column Settings -->
    70                 <div>
    71                     <?php settings_fields( 'beyond_wpdb_group' ); ?>
    72                     <?php do_settings_sections( 'virtual_columns_section' ); ?>
    73                 </div>
     162            <div style="margin-bottom: 30px;">
     163                <h1 style="margin-bottom: 30px;">Beyond WPDB Settings</h1>
     164                <p>
     165                    <?php echo $features; ?>
     166                </p>
     167                <h2><?php echo __( "Settings", "beyond-wpdb" ) ?></h2>
     168            </div>
     169            <!-- data init -->
     170            <div class="data_init_section">
     171                <?php settings_fields( 'beyond_wpdb_group' ); ?>
     172                <?php do_settings_sections( 'data_init_section' ); ?>
    74173
    75174                <!-- Submit Button -->
    76                 <?php submit_button(); ?>
    77             </form>
     175                <p class="submit">
     176                    <button id="beyond-wpdb-init-btn" class="button button-primary" <?php echo $disabled; ?>>Update</button>
     177                </p>
     178            </>
     179            <!-- Virtual Column Settings -->
     180            <div>
     181                <?php settings_fields( 'beyond_wpdb_group' ); ?>
     182                <?php do_settings_sections( 'virtual_columns_section' ); ?>
     183
     184                <!-- Submit Button -->
     185                <p class="submit">
     186                    <button id="beyond-wpdb-virtual-columns-btn" class="button button-primary" <?php echo $disabled; ?>>Update</button>
     187                </p>
     188            </div>
     189            <!-- notice -->
     190            <div>
     191                <h2><?php echo __( "NOTICE", "beyond-wpdb" ) ?></h2>
     192                <p><?php echo __( "This plugin sets 4294967295 to group_concat_max_len when it createa an original table with JSON type column.<br>You can change the number by using filter 'beyond_group_concat_max_len'.", "beyond-wpdb" ) ?></p>
     193            </div>
    78194        </div>
    79195<?php
     
    85201    public function page_init()
    86202    {
    87         // group concat
    88         add_settings_section(
    89             'setting_section_croup_concat',
    90             '<h2>Group Concat</h2>',
    91             array( $this, 'print_group_concat_section_info' ),
    92             'croup_concat_section'
    93         );
    94 
    95         add_settings_field(
    96             '',
    97             "group_concat_max_len",
    98             array( $this, 'print_croup_concat_field' ),
    99             'croup_concat_section',
    100             'setting_section_croup_concat'
    101         );
    102 
    103         register_setting(
    104             'beyond_wpdb_group',
    105             '',
    106             'sanitize_text_field'
    107         );
    108 
     203        global $wpdb;
    109204        // data init
    110205        add_settings_section(
    111206            'setting_section_data_init',
    112             '<h2>Data Init</h2>',
     207            '<h2>' . __( "Creating and deleting custom metatables", "beyond-wpdb" ) . '</h2>',
    113208            array( $this, 'print_data_init_section_info' ),
    114209            'data_init_section'
     
    116211
    117212        add_settings_field(
    118             'data_init',
    119             "Data Init",
     213            'data_init_postmeta_json',
     214            __( "Table for postmeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: postmeta_beyond</span>",
    120215            array( $this, 'print_data_init_field' ),
    121216            'data_init_section',
    122             'setting_section_data_init'
     217            'setting_section_data_init',
     218            array(
     219                'id' => "data_init_{$wpdb->prefix}postmeta_beyond",
     220                'class' => "{$wpdb->prefix}postmeta_beyond"
     221            )
     222        );
     223
     224        add_settings_field(
     225            'data_init_usermeta_json',
     226            __( "Table for usermeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: usermeta_beyond</span>",
     227            array( $this, 'print_data_init_field' ),
     228            'data_init_section',
     229            'setting_section_data_init',
     230            array(
     231                'id' => "data_init_{$wpdb->prefix}usermeta_beyond",
     232                'class' => "{$wpdb->prefix}usermeta_beyond"
     233            )
     234        );
     235
     236        add_settings_field(
     237            'data_init_commentmeta_json',
     238            __( "Table for commentmeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: commentmeta_beyond</span>",
     239            array( $this, 'print_data_init_field' ),
     240            'data_init_section',
     241            'setting_section_data_init',
     242            array(
     243                'id' => "data_init_{$wpdb->prefix}commentmeta_beyond",
     244                'class' => "{$wpdb->prefix}commentmeta_beyond"
     245            )
    123246        );
    124247
     
    126249            'beyond_wpdb_group',
    127250            'beyond_wpdb_data_init_name',
    128             array( $this, 'sanitize_input_columns' )
     251            array()
    129252        );
    130253
     
    132255        add_settings_section(
    133256            'virtual_column_section_id',
    134             '<h2>Virtual Column Settings</h2>',
     257            '<h2>' . __( "Virtual Column Settings", "beyond-wpdb" ) . '</h2>',
    135258            array( $this, 'print_virtual_column_section_info' ),
    136259            'virtual_columns_section'
     
    139262        add_settings_field(
    140263            'postmeta_json', // id
    141             "postmeta_json", // title
     264            __( "Table for postmeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: postmeta_beyond</span>", // title
    142265            array( $this, 'print_postmeta_json_field' ), // callback
    143266            'virtual_columns_section', // section page
    144             'virtual_column_section_id' // section id
     267            'virtual_column_section_id', // section id,
     268            array(
     269                'id' => "virtual_column_{$wpdb->prefix}postmeta_beyond",
     270                'class' => "virtual_column_{$wpdb->prefix}postmeta_beyond"
     271            )
    145272        );
    146273
    147274        add_settings_field(
    148275            'usermeta_json',
    149             "usermeta_json",
     276            __( "Table for usermeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: usermeta_beyond</span>",
    150277            array( $this, 'print_usermeta_json_field' ),
    151278            'virtual_columns_section', // section page
    152             'virtual_column_section_id' // section id
     279            'virtual_column_section_id', // section id
     280            array(
     281                'id' => "virtual_column_{$wpdb->prefix}usermeta_beyond",
     282                'class' => "virtual_column_{$wpdb->prefix}usermeta_beyond"
     283            )
    153284        );
    154285
    155286        add_settings_field(
    156287            'commentmeta_json', // id
    157             "commentmeta_json", // title
     288            __( "Table for commentmeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: commentmeta_beyond</span>", // title
    158289            array( $this, 'print_commentmeta_json_field' ), // callback
    159290            'virtual_columns_section', // section id
    160             'virtual_column_section_id'
     291            'virtual_column_section_id',
     292            array(
     293                'id' => "virtual_column_{$wpdb->prefix}commentmeta_beyond",
     294                'class' => "virtual_column_{$wpdb->prefix}commentmeta_beyond"
     295            )
    161296        );
    162297
     
    164299            'beyond_wpdb_group',
    165300            'beyond_wpdb_virtual_column_name',
    166             array( $this, 'sanitize_input_columns' )
    167         );
    168     }
    169 
    170     /**
    171      * @param $option
    172      * @param $value
    173      */
    174     public function do_after_add_virtual_column( $option, $value ) {
    175         $this->do_virtual_column_processing( $option );
    176     }
    177 
    178     /**
    179      * @param $option
    180      * @param $value
    181      */
    182     public function do_after_add_data_init( $option, $value ) {
    183         $this->do_data_init_processing( $option );
    184     }
    185 
    186     /**
    187      * @param $old
    188      * @param $value
    189      * @param $option
    190      */
    191     public function do_after_update_virtual_column( $old, $value, $option ) {
    192         $this->do_virtual_column_processing( $option );
    193     }
    194 
    195     /**
    196      * @param $old
    197      * @param $value
    198      * @param $option
    199      */
    200     public function do_after_update_data_init( $old, $value, $option ) {
    201         $this->do_data_init_processing( $option );
    202     }
    203 
    204 
    205     /**
    206      * @param $option
    207      * create virtual column and delete virtual column and delete option
    208      */
    209     public function do_virtual_column_processing( $option ) {
    210         $this->create_virtual_column_and_index( get_option( $option ) );
    211         $this->delete_virtual_column( get_option( $option ) );
    212         delete_option( $option );
    213     }
    214 
    215     /**
    216      * @param $option
    217      * data init
    218      */
    219     public function do_data_init_processing( $option ) {
    220         $this->data_init( get_option($option) );
    221         delete_option( $option );
    222     }
    223 
    224     /**
    225      * @param $input
    226      *
    227      * @return array
    228      */
    229     public function sanitize_input_columns( $input )
    230     {
    231         $new_input = array();
    232         if( isset( $input['postmeta_json'] ) ) {
    233             $input_columns = explode( PHP_EOL, $input['postmeta_json'] );
    234             $input_columns = $this->sanitize( $input_columns );
    235             $new_input['postmeta_json'] = implode( PHP_EOL, $input_columns );
    236         }
    237 
    238         if( isset( $input['usermeta_json'] ) ) {
    239             $input_columns = explode( PHP_EOL, $input['usermeta_json'] );
    240             $input_columns = $this->sanitize( $input_columns );
    241             $new_input['usermeta_json'] = implode( PHP_EOL, $input_columns );
    242         }
    243 
    244         if( isset( $input['commentmeta_json'] ) ) {
    245             $input_columns = explode( PHP_EOL, $input['commentmeta_json'] );
    246             $input_columns = $this->sanitize( $input_columns );
    247             $new_input['commentmeta_json'] = implode( PHP_EOL, $input_columns );
    248         }
    249 
    250         if( isset( $input['data_init'] ) ) {
    251             $input_columns = explode( PHP_EOL, $input['data_init'] );
    252             $input_columns = $this->sanitize( $input_columns );
    253             $new_input['data_init'] = implode( PHP_EOL, $input_columns );
    254         }
    255 
    256         return $new_input;
    257     }
    258 
    259     /**
    260      * @param $option
    261      * data init
    262      */
    263     public function data_init( $option )
    264     {
    265         if ( isset( $option['data_init'] ) && $option['data_init'] === '1' ) {
    266             $beyond_wpdb_sql = new Beyond_Wpdb_Sql();
    267             $beyond_wpdb_sql->data_init();
    268         }
    269     }
    270 
    271     /**
    272      * @param $input_columns
    273      *
    274      * @return string
    275      */
    276     public function sanitize( $input_columns )
    277     {
    278         foreach ( $input_columns as $key => $value ) {
    279             $input_columns[$key] = sanitize_text_field( $value );
    280         }
    281         return $input_columns;
    282     }
    283 
    284     /**
    285      * @param $options
    286      * create virtual column
    287      */
    288     public function create_virtual_column_and_index( $options ) {
    289         global $wpdb;
    290 
    291         if ( is_array( $options ) ) {
    292             foreach ( $options as $key => $option ) {
    293 
    294                 if ( 'postmeta_json' === $key ) {
    295                     $type = 'post';
    296                 } elseif ( 'usermeta_json' === $key ) {
    297                     $type = 'user';
    298                 } else {
    299                     $type = 'comment';
    300                 }
    301 
    302                 $table_name = $this->get_json_table_name( $type );
    303                 $exist_columns = $this->columns[$type];
    304                 $option = explode( PHP_EOL, $option );
    305 
    306                 foreach ( $option as $value ) {
    307 
    308                     $value = esc_sql( $value );
    309 
    310                     // If $value already exists, continue
    311                     if ( in_array( $value, $exist_columns ) ) {
    312                         continue;
    313                     }
    314 
    315                     $json_key = '$.' . $value;
    316 
    317                     // create virtual column
    318                     $sql = "ALTER TABLE {$table_name} ADD {$value} VARCHAR(255) GENERATED ALWAYS AS ( JSON_UNQUOTE( JSON_EXTRACT( json, '$json_key' ) ) )";
    319                     $wpdb->query( $sql );
    320 
    321                     // create index
    322                     $sql = "ALTER TABLE {$table_name} ADD INDEX ({$value})";
    323                     $wpdb->query( $sql );
    324                 }
    325             }
    326         }
    327     }
    328 
    329     /**
    330      * @param $options
    331      * delete virtual columns
    332      * @return mixed
    333      */
    334     public function delete_virtual_column( $options ) {
    335         global $wpdb;
    336 
    337         if ( is_array( $options ) ) {
    338             foreach ( $options as $key => $option ) {
    339                 if ( 'postmeta_json' === $key ) {
    340                     $type = 'post';
    341                 } elseif ( 'usermeta_json' === $key ) {
    342                     $type = 'user';
    343                 } else {
    344                     $type = 'comment';
    345                 }
    346 
    347                 $table_name = $this->get_json_table_name( $type );
    348                 $exist_columns = $this->columns[$type];
    349                 $option = explode( PHP_EOL, $option );
    350 
    351                 foreach ( $exist_columns as $column ) {
    352                     if ( ! in_array( $column, $option ) ) {
    353                         $sql = "ALTER TABLE {$table_name} DROP COLUMN {$column}";
    354                         $wpdb->query( $sql );
    355                     }
    356                 }
    357             }
    358         }
    359     }
    360 
    361 
    362     public function get_json_table_name( $type ) {
    363         return esc_sql( constant( beyond_wpdb_get_define_table_name( $type ) ) );
     301            array()
     302        );
    364303    }
    365304
     
    369308    public function print_virtual_column_section_info()
    370309    {
    371         print '<p>
    372                  Enter a list metakeys for which you want to create a virtual column.Each metakey should be separated by return enter key.<br>
    373                  The virtual column is indexed and the search uses the virtual column, which makes it faster.
    374                </p>';
     310        $info = __( "When you enter a key of meta information separated by a new line,<br>the specified meta key from the JSON type column is set as a virtual column and the index is pasted.<br>You can further speed up data calls by specifying the most commonly used meta keys for meta queries.", "beyond-wpdb" );
     311        print "<p>
     312                 {$info}
     313               </p>";
    375314    }
    376315
     
    378317     * print postmeta json field
    379318     */
    380     public function print_postmeta_json_field()
    381     {
    382         $value = implode( PHP_EOL, $this->columns['post'] );
    383         printf(
    384             '<textarea rows="3" cols="40" id="postmeta_json" name="beyond_wpdb_virtual_column_name[postmeta_json]">%s</textarea>',
    385             $value
    386         );
     319    public function print_postmeta_json_field( $args )
     320    {
     321        $id = $args['id'];
     322        $disabled = $this->checkDisabled() ? '' : 'disabled';
     323        print "
     324        <textarea class='create_virtualColumns_text-area d-none' rows='3' cols='40' id='{$id}_textarea' name='{$id}' {$disabled}></textarea>
     325        <span class='create_{$id} d-none' style='margin-left: 20px;'>Processing...</span>
     326        <span class='data-init-input-loading' style='margin-left: 20px;'>Loading...</span>
     327        ";
     328
    387329    }
    388330
     
    390332     * print usermeta json field
    391333     */
    392     public function print_usermeta_json_field()
    393     {
    394         $value = implode( PHP_EOL, $this->columns['user'] );
    395         printf(
    396             '<textarea rows="3" cols="40" id="usermeta_json" name="beyond_wpdb_virtual_column_name[usermeta_json]">%s</textarea>',
    397             $value
    398         );
     334    public function print_usermeta_json_field( $args )
     335    {
     336        $id = $args['id'];
     337        $disabled = $this->checkDisabled() ? '' : 'disabled';
     338        print "
     339        <textarea class='create_virtualColumns_text-area d-none' rows='3' cols='40' id='{$id}_textarea' name='{$id}' {$disabled}></textarea>
     340        <span class='create_{$id} d-none' style='margin-left: 20px;'>Processing...</span>
     341        <span class='data-init-input-loading' style='margin-left: 20px;'>Loading...</span>
     342        ";
    399343    }
    400344
     
    402346     * print commentmeta json field
    403347     */
    404     public function print_commentmeta_json_field()
    405     {
    406         $value = implode( PHP_EOL, $this->columns['comment'] );
    407         printf(
    408             '<textarea rows="3" cols="40" id="commentmeta_json" name="beyond_wpdb_virtual_column_name[commentmeta_json]">%s</textarea>',
    409             $value
    410         );
    411     }
    412 
    413     /**
    414      * print group concat section info
    415      */
    416     public function print_group_concat_section_info()
    417     {
    418         print '<p>Use GROUP_CONCAT in data init.If the json string consisting of all metas is longer than this value, an error occurs.</p>';
    419     }
    420 
    421     /**
    422      * print croup_concat field
    423      */
    424     public function print_croup_concat_field()
    425     {
    426         global $wpdb;
    427         $group_concat_max_len = $wpdb->get_results( "show variables like 'group_concat_max_len'" );
    428         printf(
    429             '<p>%s</p>',
    430             $group_concat_max_len[0]->Value
    431         );
     348    public function print_commentmeta_json_field( $args )
     349    {
     350        $id = $args['id'];
     351        $disabled = $this->checkDisabled() ? '' : 'disabled';
     352        print "
     353        <textarea class='create_virtualColumns_text-area d-none' rows='3' cols='40' id='{$id}_textarea' name='{$id}' {$disabled}></textarea>
     354        <span class='create_{$id} d-none' style='margin-left: 20px;'>Processing...</span>
     355        <span class='data-init-input-loading' style='margin-left: 20px;'>Loading...</span>
     356        ";
    432357    }
    433358
     
    437362    public function print_data_init_section_info()
    438363    {
    439         print '<p>Collect all meta information and re-register it in the json table.</p>';
    440     }
    441 
    442     /**
    443      *  print data_init field
    444      */
    445     public function print_data_init_field()
    446     {
    447         print '<input type="checkbox" id="data_init" name="beyond_wpdb_data_init_name[data_init]" value="1">';
     364        $info = __( "Create and delete your own tables that aggregate the metadata. (Enabled to create, disabled to delete)<br>As long as it is enabled, all meta information will continue to be automatically registered, updated, and deleted in the JSON type columns of the relevant table.<br>When enabled, a new table will be created or initialized, which will take some time. When disabled, the table will be deleted.", "beyond-wpdb" );
     365        print $info;
     366    }
     367
     368    /**
     369     * Check to see if each feature is available
     370     * @return bool
     371     */
     372    public function checkDisabled() {
     373        return ! $this->privilege_error;
     374    }
     375
     376    /**
     377     * @param $args
     378     * print data_init field
     379     */
     380    public function print_data_init_field( $args )
     381    {
     382        $id = $args['id'];
     383        $disabled = $this->checkDisabled() ? '' : 'disabled';
     384        print "
     385        <div class='data-init-input-radio d-none'><input type='radio' id='{$id}_active' name='name_{$id}' value='1' {$disabled}>" . "<label for='{$id}_active'>" . __( "activation", "beyond-wpdb" ) . "</label>" .
     386        "<input type='radio' id='{$id}_deactive' name='name_{$id}' value='0' {$disabled} style='margin-left: 25px;'>" . "<label for='{$id}_deactive'>" . __( "deactivation", "beyond-wpdb" ) . "</label>" .
     387        "<span class='activate_{$id} d-none' style='margin-left: 20px;'>Processing...</span>
     388         <span class='success_{$id}' style='margin-left: 20px; display: none;'>Success.</span></div><span class='data-init-input-loading' style='margin-left: 20px;'>Loading...</span>
     389        ";
    448390    }
    449391}
  • beyond-wpdb/tags/2.0.0/class/sql.php

    r2330366 r2352101  
    88 */
    99class Beyond_Wpdb_Sql {
     10
    1011    function __construct() {
    1112        global $wpdb;
    1213
    1314        foreach( array_keys( BEYOND_WPDB_PRIMARYS ) as $primary ) {
     15            $this->triggers[$primary] = array();
     16
    1417            $insert_trigger = 'insert_' . $primary . '_trigger';
    1518            $this->$insert_trigger = esc_sql( $wpdb->prefix . 'insert_' . $primary . '_trigger' );
     19            array_push( $this->triggers[$primary], $this->$insert_trigger );
    1620
    1721            $delete_trigger = 'delete_' . $primary . '_trigger';
    1822            $this->$delete_trigger = esc_sql( $wpdb->prefix . 'delete_' . $primary . '_trigger' );
     23            array_push( $this->triggers[$primary], $this->$delete_trigger );
    1924
    2025            $insert_meta_trigger = 'insert_' . $primary . 'meta_trigger';
    2126            $this->$insert_meta_trigger = esc_sql( $wpdb->prefix . 'insert_' . $primary . 'meta_trigger' );
     27            array_push( $this->triggers[$primary], $this->$insert_meta_trigger );
    2228
    2329            $update_meta_trigger = 'update_' . $primary . 'meta_trigger';
    2430            $this->$update_meta_trigger = esc_sql( $wpdb->prefix . 'update_' . $primary . 'meta_trigger' );
     31            array_push( $this->triggers[$primary], $this->$update_meta_trigger );
    2532
    2633            $delete_meta_trigger = 'delete_' . $primary . 'meta_trigger';
    2734            $this->$delete_meta_trigger = esc_sql( $wpdb->prefix . 'delete_' . $primary . 'meta_trigger' );
     35            array_push( $this->triggers[$primary], $this->$delete_meta_trigger );
    2836        }
    2937    }
     
    3240     * Create table
    3341     *
    34      * @return void
    35      */
    36     function create_table() : void {
    37         foreach( BEYOND_WPDB_PRIMARYS as $primary => $values ) {
    38             $this->create_table_sql( $primary, $values['meta_table_key'] );
    39         }
     42     * @param string $type
     43     * @return void
     44     */
     45    function create_table( $primary ) : void {
     46        $values = BEYOND_WPDB_PRIMARYS[$primary];
     47        $this->create_table_sql( $primary, $values['meta_table_key'] );
    4048    }
    4149
     
    6674     * Drop table
    6775     *
    68      * @return void
    69      */
    70     function drop_table() {
    71         foreach( array_keys( BEYOND_WPDB_PRIMARYS ) as $primary ) {
    72             $this->drop_table_sql( $primary );
    73         }
     76     * @param $primary
     77     * @return void
     78     * @throws Exception
     79     */
     80    function drop_table( $primary ) {
     81        $this->drop_table_sql( $primary );
    7482    }
    7583
     
    7987     * @param string $primary
    8088     * @return void
     89     * @throws Exception
    8190     */
    8291    protected function drop_table_sql( $primary ) {
    8392        global $wpdb;
    84         $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) );
    85 
    86         $sql = 'DROP TABLE ' . $table_name;
    87         $wpdb->query( $sql );
     93
     94        $exist_tables = $this->get_exist_json_tables();
     95        $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) );
     96
     97        if ( in_array( $table_name, $exist_tables ) ) {
     98            $sql = 'DROP TABLE ' . $table_name;
     99            $wpdb->query( $sql );
     100
     101            if ( $wpdb->last_error ) {
     102                throw new Exception( $wpdb->last_error );
     103            }
     104        }
    88105    }
    89106
     
    93110     * Insert/Update/Delete postmeta trigger
    94111     *
    95      * @return void
    96      */
    97     function create_trigger() {
    98         foreach( BEYOND_WPDB_PRIMARYS as $primary => $values ) {
    99             $this->insert_primary_trigger( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_key'] );
    100             $this->delete_primary_trigger( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_key'] );
    101             $this->insert_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] );
    102             $this->update_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] );
    103             $this->delete_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] );
    104 
    105         }
     112     * @param string $primary
     113     * @return void
     114     * @throws Exception
     115     */
     116    function create_trigger( $primary ) {
     117        $values = BEYOND_WPDB_PRIMARYS[$primary];
     118        $this->insert_primary_trigger( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_key'] );
     119        $this->delete_primary_trigger( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_key'] );
     120        $this->insert_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] );
     121        $this->update_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] );
     122        $this->delete_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] );
    106123    }
    107124
     
    114131     * @param string $meta_table_key
    115132     * @return void
     133     * @throws Exception
    116134     */
    117135    protected function insert_primary_trigger( $primary, $primaty_table_name, $primary_table_key, $meta_table_key ) {
     
    119137
    120138        $insert_trigger = 'insert_' . $primary . '_trigger';
     139        $exist_triggers = $this->get_exist_triggers();
    121140        $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) );
    122141        $primaty_table_name = esc_sql( $primaty_table_name );
     
    124143        $primary_table_key = esc_sql( $primary_table_key );
    125144
    126         $sql = 'CREATE TRIGGER ' . $this->$insert_trigger . ' AFTER
     145        if ( ! in_array( $this->$insert_trigger, $exist_triggers ) ) {
     146            $sql = 'CREATE TRIGGER ' . $this->$insert_trigger . ' AFTER
    127147                INSERT ON
    128148                ' . $primaty_table_name . '
     
    134154                VALUES
    135155                    (NEW.' . $primary_table_key . ', "{}" )';
    136         $wpdb->query( $sql );
     156            $wpdb->query( $sql );
     157
     158            if ( $wpdb->last_error ) {
     159                throw new Exception( $wpdb->last_error );
     160            }
     161        }
    137162    }
    138163
     
    145170     * @param string $meta_table_key
    146171     * @return void
     172     * @throws Exception
    147173     */
    148174    protected function delete_primary_trigger( $primary, $primaty_table_name, $primary_table_key, $meta_table_key ) {
     
    150176
    151177        $delete_trigger = 'delete_' . $primary . '_trigger';
     178        $exist_triggers = $this->get_exist_triggers();
    152179        $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) );
    153180        $primaty_table_name = esc_sql( $primaty_table_name );
     
    155182        $primary_table_key = esc_sql( $primary_table_key );
    156183
    157         $sql = 'CREATE TRIGGER ' . $this->$delete_trigger . ' BEFORE
    158         DELETE ON
    159         ' . $primaty_table_name . '
    160         FOR
    161         EACH
    162         ROW
    163         DELETE FROM ' . $table_name . '
    164         WHERE ' . $meta_table_key . ' = OLD.' . $primary_table_key;
    165         $wpdb->query( $sql );
     184        if ( ! in_array( $this->$delete_trigger, $exist_triggers ) ) {
     185            $sql = 'CREATE TRIGGER ' . $this->$delete_trigger . ' BEFORE
     186            DELETE ON
     187            ' . $primaty_table_name . '
     188            FOR
     189            EACH
     190            ROW
     191            DELETE FROM ' . $table_name . '
     192            WHERE ' . $meta_table_key . ' = OLD.' . $primary_table_key;
     193            $wpdb->query( $sql );
     194
     195            if ( $wpdb->last_error ) {
     196                throw new Exception( $wpdb->last_error );
     197            }
     198        }
    166199    }
    167200
     
    173206     * @param string $meta_table_key
    174207     * @return void
     208     * @throws Exception
    175209     */
    176210    protected function insert_meta_trigger( $primary, $meta_table_name, $meta_table_key ) {
    177211        global $wpdb;
    178212
     213        $exist_triggers = $this->get_exist_triggers();
    179214        $insert_meta_trigger = 'insert_' . $primary . 'meta_trigger';
    180215        $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) );
     
    182217        $meta_table_key = esc_sql( $meta_table_key );
    183218
    184         $sql = 'CREATE TRIGGER ' . $this->$insert_meta_trigger . ' AFTER
     219        if ( ! in_array( $this->$insert_meta_trigger, $exist_triggers ) ) {
     220            $sql = 'CREATE TRIGGER ' . $this->$insert_meta_trigger . ' AFTER
    185221                INSERT ON
    186222                ' . $meta_table_name . '
     
    193229                (`json`, CONCAT
    194230                ("$.",NEW.meta_key), NEW.meta_value) WHERE ' . $meta_table_key . ' = NEW.'.$meta_table_key;
    195         $wpdb->query( $sql );
     231            $wpdb->query( $sql );
     232
     233            if ( $wpdb->last_error ) {
     234                throw new Exception( $wpdb->last_error );
     235            }
     236        }
    196237    }
    197238
     
    203244     * @param string $meta_table_key
    204245     * @return void
     246     * @throws Exception
    205247     */
    206248    protected function update_meta_trigger( $primary, $meta_table_name, $meta_table_key ) {
    207249        global $wpdb;
    208250
     251        $exist_triggers = $this->get_exist_triggers();
    209252        $update_meta_trigger = 'update_' . $primary . 'meta_trigger';
    210253        $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) );
     
    212255        $meta_table_key = esc_sql( $meta_table_key );
    213256
    214         $sql = 'CREATE TRIGGER ' . $this->$update_meta_trigger . ' AFTER
     257        if ( ! in_array( $this->$update_meta_trigger, $exist_triggers ) ) {
     258            $sql = 'CREATE TRIGGER ' . $this->$update_meta_trigger . ' AFTER
    215259                UPDATE ON
    216260                ' . $meta_table_name . '
     
    223267                (`json`, CONCAT
    224268                ("$.",NEW.meta_key), NEW.meta_value) WHERE ' . $meta_table_key . ' = NEW.' . $meta_table_key;
    225         $wpdb->query( $sql );
     269            $wpdb->query( $sql );
     270
     271            if ( $wpdb->last_error ) {
     272                throw new Exception( $wpdb->last_error );
     273            }
     274        }
    226275    }
    227276
     
    233282     * @param string $meta_table_key
    234283     * @return void
     284     * @throws Exception
    235285     */
    236286    protected function delete_meta_trigger( $primary, $meta_table_name, $meta_table_key ) {
    237287        global $wpdb;
    238288
     289        $exist_triggers = $this->get_exist_triggers();
    239290        $delete_meta_trigger = 'delete_' . $primary . 'meta_trigger';
    240291        $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) );
     
    242293        $meta_table_key = esc_sql( $meta_table_key );
    243294
    244         $sql = 'CREATE TRIGGER ' . $this->$delete_meta_trigger . ' AFTER
     295        if ( ! in_array( $this->$delete_meta_trigger, $exist_triggers ) ) {
     296            $sql = 'CREATE TRIGGER ' . $this->$delete_meta_trigger . ' AFTER
    245297                DELETE ON
    246298                ' . $meta_table_name . '
     
    252304                `json` = JSON_REMOVE(`json`, CONCAT
    253305                ("$.",OLD.meta_key)) WHERE ' . $meta_table_key . ' = OLD.'. $meta_table_key;
     306            $wpdb->query( $sql );
     307
     308            if ( $wpdb->last_error ) {
     309                throw new Exception( $wpdb->last_error );
     310            }
     311        }
     312    }
     313
     314    /**
     315     * Drop triggers
     316     *
     317     * @param $primary
     318     * @return void
     319     * @throws Exception
     320     */
     321    function drop_triggers( $primary ) {
     322        global $wpdb;
     323
     324        $exist_triggers = $this->get_exist_triggers();
     325
     326        foreach ( $this->triggers[$primary] as $value ) {
     327            if ( in_array( $value, $exist_triggers ) ) {
     328                $sql = 'DROP TRIGGER ' . esc_sql( $value );
     329                $wpdb->query( $sql );
     330            }
     331        }
     332
     333        if ( $wpdb->last_error ) {
     334            throw new Exception( $wpdb->last_error );
     335        }
     336    }
     337
     338    /**
     339     * Update table from post, postmeta table.
     340     *
     341     * @param $primary
     342     *
     343     * @throws Exception
     344     */
     345    function data_init( $primary ) {
     346        global $wpdb;
     347        $values = BEYOND_WPDB_PRIMARYS[$primary];
     348        $maxlen = 4294967295;
     349
     350        $maxlen = apply_filters( 'beyond_group_concat_max_len', $maxlen );
     351        $sql = 'SET SESSION group_concat_max_len = ' . $maxlen;
    254352        $wpdb->query( $sql );
    255     }
    256 
    257     /**
    258      * Drop triggers
    259      *
    260      * @return void
    261      */
    262     function drop_triggers() {
    263         global $wpdb;
    264 
    265         foreach( get_object_vars( $this ) as $value ) {
    266             $sql = 'DROP TRIGGER ' . esc_sql( $value );
    267             $wpdb->query( $sql );
    268         }
    269     }
    270 
    271     /**
    272      * Update table from post, postmeta table.
    273      *
    274      * @return void
    275      */
    276     function data_init() {
    277         foreach( BEYOND_WPDB_PRIMARYS as $primary => $values ) {
    278             $this->data_init_sql( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_name'], $values['meta_table_key'] );
    279             $this->delete_non_existent_data_from_json( $primary, $values['primary_table_name'], $values['primary_table_key'] );
    280         }
     353
     354        $this->data_init_sql( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_name'], $values['meta_table_key'] );
     355        $this->delete_non_existent_data_from_json( $primary, $values['primary_table_name'], $values['primary_table_key'] );
    281356    }
    282357
     
    284359     * data init sql
    285360     *
    286      * @param string $primary
    287      * @param string $primary_table_name
    288      * @param string $primary_table_key
    289      * @param string $meta_table_name
    290      * @param string $meta_table_key
    291      * @return void
     361     * @param $primary
     362     * @param $primary_table_name
     363     * @param $primary_table_key
     364     * @param $meta_table_name
     365     * @param $meta_table_key
     366     *
     367     * @throws Exception
    292368     */
    293369    protected function data_init_sql( $primary, $primary_table_name, $primary_table_key, $meta_table_name, $meta_table_key ) {
     
    311387                UPDATE json = VALUES(json)';
    312388        $wpdb->query( $sql );
     389
     390        if ( $wpdb->last_error ) {
     391            throw new Exception( $wpdb->last_error );
     392        }
    313393    }
    314394
     
    320400     * @param $primary_table_key
    321401     * @return void
     402     * @throws Exception
    322403     */
    323404    protected function delete_non_existent_data_from_json( $primary, $primary_table_name, $primary_table_key ) {
     
    334415
    335416        $wpdb->query( $sql );
     417
     418        if ( $wpdb->last_error ) {
     419            throw new Exception( $wpdb->last_error );
     420        }
     421    }
     422
     423    /**
     424     * Get exist virtual columns
     425     * @return array
     426     */
     427    protected function get_exist_triggers() {
     428        $beyond_wpdb_info = new Beyond_Wpdb_Information();
     429        $beyond_wpdb_info->set_triggers();
     430        return $beyond_wpdb_info->get_triggers();
     431    }
     432
     433    /**
     434     * Get exist json tables
     435     * @return array
     436     */
     437    protected function get_exist_json_tables(){
     438        $beyond_wpdb_info = new Beyond_Wpdb_Information();
     439        $beyond_wpdb_info->set_tables();
     440        return $beyond_wpdb_info->get_tables();
    336441    }
    337442}
  • beyond-wpdb/tags/2.0.0/readme.txt

    r2337807 r2352101  
    22Contributors: pressmaninc,kazunao,hiroshisekiguchi,hommakoharu,pmhirotaka
    33Tags: pressman,pressmaninc,json,meta_query,wpdb,database,fast,speed,speed up,sql,replace sql
    4 Stable tag: 1.1.0
     4Stable tag: 2.0.0
    55License: GPLv2 or later
    66License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    1010Requires MariaDB: 10.2
    1111Tested up to: 5.4
     12
     13※ Table names have been changed since v2.0.Please delete the meta_json table and reactivate it in the options screen.
    1214
    1315Are you having trouble with slow queries due to WordPress table structure?
     
    2325
    2426== Actual Behavior ==
     27* You can create and delete json tables and triggers and data for each json table on the configuration page.
     28* Create virtual columns and indexes for the json table you created in the configuration page.
     29* If you add a virtual column to the json table in the configuration page, the SQL is converted to face the virtual column.
    2530* Converts the SQL that WordPress auto-generates when using the default APIs of get_posts, Wp_Query, and etc. into a simple SQL that combines the posts/users/comment table with its own meta table.
    2631* The custom meta table automatically stores data using MySQL and MariaDB triggers.
     
    3237
    3338== Custom meta tables generated by plugin ==
    34 * postmeta_json (for posts table)
    35 * usermeta_json (for users table)
    36 * commentmeta_json (for comment table)
     39The actual table names are prefixed with $table_prefix.
     40* postmeta_beyond (for posts table)
     41* usermeta_beyond (for users table)
     42* commentmeta_beyond (for comment table)
    3743
    3844== Columns in each table ==
    39 * postmeta_json
     45The actual table names are prefixed with $table_prefix.
     46* postmeta_beyond
    4047    * post_id(int)
    4148    * json(json)
    42 * usermeta_json
     49* usermeta_beyond
    4350    * post_id(int)
    4451    * json(json)
    45 * commentmeta_json
     52* commentmeta_beyond
    4653    * post_id(int)
    4754    * json(json)
     
    5360ex1 ) Create 100 posts and 50 meta data for each post. Combine them 9 times.
    5461
    55 * postmeta_json
     62* postmeta_beyond
    5663    * Equal:0.04
    5764    * IN:0.09
     
    6168    * BETWEEN:0.04
    6269    * NOT BETWEEN:0.04
     70
    6371* postmeta
    6472    * Equal:4.72
     
    7078    * NOT BETWEEN:0.63
    7179
    72 ex2 ) 10000 posts, 1 meta(category) per post.If you add a virtual column and put an index in Category, or if you add a virtual column and put an index in Category
    73 * postmeta_json(without virtual column)
     80ex2 ) 10,000 posts, 1 meta per post. meta_key is category.
     81      Add a virtual column(category) to postmeta_beyond and put an index,
     82      and compare it to the case without adding a virtual column.
     83
     84* postmeta_beyond(with virtual column)
     85    * Equal:0.02
     86    * IN:0.35
     87    * BETWEEN:0.12
     88    * NOT BETWEEN:0.09
     89
     90* postmeta_beyond(without virtual column)
    7491    * Equal:0.15
    7592    * IN:1.92
    7693    * BETWEEN:0.37
    7794    * NOT BETWEEN:0.29
    78 
    79 * postmeta_json(with virtual column)
    80     * Equal:0.02
    81     * IN:0.35
    82     * BETWEEN:0.12
    83     * NOT BETWEEN:0.09
    8495
    8596== Installation ==
     
    91102* You'll need TRIGGER, SELECT, and UPDATE permissions for the target table and its columns to use the trigger feature of MySQL and MariaDB.
    92103* It makes changes to SQL only if `suppress_filters` is false.
     104* This plugin sets 4294967295 to group_concat_max_len when it create an original table with JSON type column. You can change the number by using filter 'beyond_group_concat_max_len'.
  • beyond-wpdb/trunk/beyond-wpdb.php

    r2337801 r2352101  
    33Plugin Name: Beyond Wpdb
    44Plugin URI:
    5 Description: Create a new table and register the meta data in a single row of JSON columns.Registration is done by mysql's trigger function.The data in WordPress is not affected.Convert sql statement, when searching for meta data.
    6 Version: 1.1.0
     5Description: Speed up your WordPress database by making use of JSON type columns in MySQL.
     6Version: 2.0.0
    77Author: PRESSMAN
    88Author URI: https://www.pressman.ne.jp/
     
    4545// Deifne Beyond_table name.
    4646foreach( array_keys( BEYOND_WPDB_PRIMARYS ) as $primary ) {
    47     define( beyond_wpdb_get_define_table_name( $primary ), $wpdb->prefix . $primary . 'meta_json' );
     47    define( beyond_wpdb_get_define_table_name( $primary ), $wpdb->prefix . $primary . 'meta_beyond' );
    4848}
    4949
    5050// Require files.
    51 require_once( plugin_dir_path( __FILE__ ) . 'class/register-hook.php' );
     51require_once( plugin_dir_path( __FILE__ ) . 'class/register.php' );
     52require_once( plugin_dir_path( __FILE__ ) . 'class/information.php' );
    5253require_once( plugin_dir_path( __FILE__ ) . 'class/sql.php' );
    5354require_once( plugin_dir_path( __FILE__ ) . 'class/meta-query.php' );
    5455require_once( plugin_dir_path( __FILE__ ) . 'class/wp-orderby.php' );
    55 require_once( plugin_dir_path( __FILE__ ) . 'class/column.php' );
    5656require_once( plugin_dir_path( __FILE__ ) . 'class/options.php' );
    57 
    58 // Plugin activation hook.
    59 register_activation_hook( __FILE__, ['Beyond_Wpdb_Register_Hook', 'activation'] );
    60 register_deactivation_hook( __FILE__, ['Beyond_Wpdb_Register_Hook', 'deactivation'] );
    61 register_uninstall_hook( __FILE__, ['Beyond_Wpdb_Register_Hook', 'uninstall'] );
     57require_once( plugin_dir_path( __FILE__ ) . 'class/admin-ajax.php' );
  • beyond-wpdb/trunk/class/meta-query.php

    r2337801 r2352101  
    467467     */
    468468    public function virtual_column_exists( $key, $type ) {
    469         $beyond_wpdb_column = new Beyond_Wpdb_Column();
    470         $beyond_wpdb_column->set_columns();
    471         $virtual_columns = $beyond_wpdb_column->get_columns();
    472 
    473         return in_array( $key, $virtual_columns[$type] );
     469        $beyond_wpdb_info = new Beyond_Wpdb_Information();
     470        $beyond_wpdb_info->set_columns();
     471        $virtual_columns = $beyond_wpdb_info->get_columns();
     472        $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $type ) ) );
     473
     474        return in_array( $key, $virtual_columns[$table_name] );
    474475    }
    475476
  • beyond-wpdb/trunk/class/options.php

    r2337802 r2352101  
    99 */
    1010class Beyond_Wpdb_Settings_page {
    11     private $columns = array();
     11    private $privilege_error = False;
    1212
    1313    /**
     
    1616    public function __construct()
    1717    {
     18        load_theme_textdomain( 'beyond-wpdb', plugin_dir_path( __FILE__ ) . '../lang' );
     19        add_action( 'admin_notices', array( $this, 'notice__warnig_or_error' ) );
    1820        add_action( 'admin_menu', array( $this, 'add_beyond_wpdb_settings_page' ) );
    1921        add_action( 'admin_init', array( $this, 'page_init' ) );
    20         $beyond_wpdb_column = new Beyond_Wpdb_Column();
    21         $beyond_wpdb_column->set_columns();
    22         $this->columns = $beyond_wpdb_column->get_columns();
    23 
    24         // add option beyond_wpdb_virtual_column_name
    25         add_action( 'add_option_beyond_wpdb_virtual_column_name', array( $this, 'do_after_add_virtual_column' ) ,10, 2 );
    26         // add option beyond_wpdb_data_init_name
    27         add_action( 'add_option_beyond_wpdb_data_init_name', array( $this, 'do_after_add_data_init' ) ,10, 2 );
    28         // update option beyond_wpdb_virtual_column_name
    29         add_action( 'update_option_beyond_wpdb_virtual_column_name', array( $this, 'do_after_update_virtual_column' ) ,10, 3 );
    30         // update option beyond_wpdb_data_init_name
    31         add_action( 'update_option_beyond_wpdb_data_init_name', array ( $this, 'do_after_update_data_init' ) ,10, 3 );
    32 
     22
     23    }
     24
     25    /**
     26     * Display warnig or error
     27     */
     28    function notice__warnig_or_error() {
     29        global $wpdb, $pagenow;
     30
     31        $current_db = $wpdb->get_var( "SELECT DATABASE()" );
     32        $current_user = explode('@', $wpdb->get_var( "SELECT USER()" ));
     33        $grants = $wpdb->get_results( "SHOW GRANTS FOR '{$current_user[0]}'@'{$current_user[1]}'", ARRAY_A );
     34        $grant_for_mysql_db = False;
     35        $current_privileges = array();
     36
     37        foreach ( $grants as $value ) {
     38            $value = array_values( $value )[0];
     39            if ( strpos( $value, "ON `mysql`." ) || strpos( $value, "root" ) ) {
     40                $grant_for_mysql_db = True;
     41            }
     42        }
     43
     44        if ( $grant_for_mysql_db ) {
     45            $result_privileges = $wpdb->get_results( "SELECT * FROM mysql.db WHERE Db = '{$current_db}' AND User = '{$current_user[0]}'", ARRAY_A );
     46            foreach ( $result_privileges as $val ) {
     47                $keys = array_keys( $val );
     48                $values = array_values( $val );
     49
     50                // If $v is Y, $keys[$idx] is assumed to be authorized.
     51                foreach ( $values as $idx => $v ) {
     52                    if ( $v === 'Y' ) {
     53                        array_push( $current_privileges, $keys[$idx] );
     54                    }
     55                }
     56            }
     57        }
     58
     59        $privileges_error = '';
     60        $expected_privileges = array(
     61            'Trigger_priv',
     62            'Select_priv',
     63            'Insert_priv',
     64            'Update_priv',
     65            'Delete_priv',
     66            'Create_priv',
     67            'Drop_priv'
     68        );
     69        if ( count( $current_privileges ) > 0 ) {
     70            foreach ( $expected_privileges as $privilege ) {
     71                if ( ! in_array( $privilege, $current_privileges ) ) {
     72                    switch ( $privilege ) {
     73                        case 'Trigger_priv':
     74                            $privileges_error .= "<p>" . __("You do not have permission to create triggers.", "beyond-wpdb") . "</p>";
     75                            break;
     76                        case 'Select_priv':
     77                            $privileges_error .= "<p>" . __("You don't have SELECT permission.", "beyond-wpdb") . "</p>";
     78                            break;
     79                        case 'Insert_priv':
     80                            $privileges_error .= "<p>" . __("You don't have INSERT permission.", "beyond-wpdb") . "</p>";
     81                            break;
     82                        case 'Update_priv':
     83                            $privileges_error .= "<p>" . __("You don't have UPDATE permission.", "beyond-wpdb") . "</p>";
     84                            break;
     85                        case 'Delete_priv':
     86                            $privileges_error .= "<p>" . __("You don't have DELETE permission.", "beyond-wpdb") . "</p>";
     87                            break;
     88                        case 'Create_priv':
     89                            $privileges_error .= "<p>" . __("You don't have CREATE permission.", "beyond-wpdb") . "</p>";
     90                            break;
     91                        default :
     92                            $privileges_error .= "<p>" . __("You don't have DROP permission.", "beyond-wpdb") . "</p>";
     93                            break;
     94                    }
     95                    $this->privilege_error = True;
     96                }
     97            }
     98        }
     99
     100        if ( isset( $_GET['page'] ) && ! $_GET['page'] && $pagenow === 'options-general.php' ) {
     101            if ( ! $grant_for_mysql_db ) {
     102                $grants_message = __("You do not have permission to the mysql database.<br>We access the db table in the mysql database to find out what permissions we have to use each feature.<br>There is a possibility that each function will not be available.", "beyond-wpdb" );
     103                print "
     104                <div class='notice notice-warning is-dismissible'>
     105                    <p>
     106                    {$grants_message}
     107                    </p>
     108                </div>
     109                ";
     110            }
     111
     112            if ( $privileges_error ) {
     113                $priviles_message = __( "You do not have the following permissions.Please use each function after granting permission.", "beyond-wpdb" );
     114                print "
     115                <div class='notice notice-error is-dismissible'>
     116                    <p>{$priviles_message}</p>
     117                    {$privileges_error}
     118                </div>";
     119            }
     120        }
    33121    }
    34122
     
    52140    public function create_beyond_wpdb_settings_page()
    53141    {
     142        $disabled = $this->checkDisabled() ? '' : 'disabled';
     143        $features = __( "Speed up database loading by creating your own tables that aggregate meta information.<br>This is especially useful when you have a large number of records and for complex meta-query data calls.", "beyond-wpdb" );
    54144?>
     145        <style>
     146            .d-block {
     147                display: block;
     148            }
     149            .d-inline-block {
     150                display: inline-block;
     151            }
     152            .d-none {
     153                display: none;
     154            }
     155            .table_not_exists {
     156                opacity:0.3;
     157                pointer-envet:none;
     158            }
     159        </style>
    55160        <div class='beyond-wpdb-settings-wrap'>
    56161            <!-- title -->
    57             <h1 style="margin-bottom: 30px;">Beyond WPDB Settings</h1>
    58             <!-- form -->
    59             <form action='options.php' method='post'>
    60                 <!-- Group Concat -->
    61                 <div>
    62                     <?php settings_fields( 'beyond_wpdb_group' ); ?>
    63                     <?php do_settings_sections( 'croup_concat_section' ); ?>
    64                 </div>
    65                 <div>
    66                     <?php settings_fields( 'beyond_wpdb_group' ); ?>
    67                     <?php do_settings_sections( 'data_init_section' ); ?>
    68                 </div>
    69                 <!-- Virtual Column Settings -->
    70                 <div>
    71                     <?php settings_fields( 'beyond_wpdb_group' ); ?>
    72                     <?php do_settings_sections( 'virtual_columns_section' ); ?>
    73                 </div>
     162            <div style="margin-bottom: 30px;">
     163                <h1 style="margin-bottom: 30px;">Beyond WPDB Settings</h1>
     164                <p>
     165                    <?php echo $features; ?>
     166                </p>
     167                <h2><?php echo __( "Settings", "beyond-wpdb" ) ?></h2>
     168            </div>
     169            <!-- data init -->
     170            <div class="data_init_section">
     171                <?php settings_fields( 'beyond_wpdb_group' ); ?>
     172                <?php do_settings_sections( 'data_init_section' ); ?>
    74173
    75174                <!-- Submit Button -->
    76                 <?php submit_button(); ?>
    77             </form>
     175                <p class="submit">
     176                    <button id="beyond-wpdb-init-btn" class="button button-primary" <?php echo $disabled; ?>>Update</button>
     177                </p>
     178            </>
     179            <!-- Virtual Column Settings -->
     180            <div>
     181                <?php settings_fields( 'beyond_wpdb_group' ); ?>
     182                <?php do_settings_sections( 'virtual_columns_section' ); ?>
     183
     184                <!-- Submit Button -->
     185                <p class="submit">
     186                    <button id="beyond-wpdb-virtual-columns-btn" class="button button-primary" <?php echo $disabled; ?>>Update</button>
     187                </p>
     188            </div>
     189            <!-- notice -->
     190            <div>
     191                <h2><?php echo __( "NOTICE", "beyond-wpdb" ) ?></h2>
     192                <p><?php echo __( "This plugin sets 4294967295 to group_concat_max_len when it createa an original table with JSON type column.<br>You can change the number by using filter 'beyond_group_concat_max_len'.", "beyond-wpdb" ) ?></p>
     193            </div>
    78194        </div>
    79195<?php
     
    85201    public function page_init()
    86202    {
    87         // group concat
    88         add_settings_section(
    89             'setting_section_croup_concat',
    90             '<h2>Group Concat</h2>',
    91             array( $this, 'print_group_concat_section_info' ),
    92             'croup_concat_section'
    93         );
    94 
    95         add_settings_field(
    96             '',
    97             "group_concat_max_len",
    98             array( $this, 'print_croup_concat_field' ),
    99             'croup_concat_section',
    100             'setting_section_croup_concat'
    101         );
    102 
    103         register_setting(
    104             'beyond_wpdb_group',
    105             '',
    106             'sanitize_text_field'
    107         );
    108 
     203        global $wpdb;
    109204        // data init
    110205        add_settings_section(
    111206            'setting_section_data_init',
    112             '<h2>Data Init</h2>',
     207            '<h2>' . __( "Creating and deleting custom metatables", "beyond-wpdb" ) . '</h2>',
    113208            array( $this, 'print_data_init_section_info' ),
    114209            'data_init_section'
     
    116211
    117212        add_settings_field(
    118             'data_init',
    119             "Data Init",
     213            'data_init_postmeta_json',
     214            __( "Table for postmeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: postmeta_beyond</span>",
    120215            array( $this, 'print_data_init_field' ),
    121216            'data_init_section',
    122             'setting_section_data_init'
     217            'setting_section_data_init',
     218            array(
     219                'id' => "data_init_{$wpdb->prefix}postmeta_beyond",
     220                'class' => "{$wpdb->prefix}postmeta_beyond"
     221            )
     222        );
     223
     224        add_settings_field(
     225            'data_init_usermeta_json',
     226            __( "Table for usermeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: usermeta_beyond</span>",
     227            array( $this, 'print_data_init_field' ),
     228            'data_init_section',
     229            'setting_section_data_init',
     230            array(
     231                'id' => "data_init_{$wpdb->prefix}usermeta_beyond",
     232                'class' => "{$wpdb->prefix}usermeta_beyond"
     233            )
     234        );
     235
     236        add_settings_field(
     237            'data_init_commentmeta_json',
     238            __( "Table for commentmeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: commentmeta_beyond</span>",
     239            array( $this, 'print_data_init_field' ),
     240            'data_init_section',
     241            'setting_section_data_init',
     242            array(
     243                'id' => "data_init_{$wpdb->prefix}commentmeta_beyond",
     244                'class' => "{$wpdb->prefix}commentmeta_beyond"
     245            )
    123246        );
    124247
     
    126249            'beyond_wpdb_group',
    127250            'beyond_wpdb_data_init_name',
    128             array( $this, 'sanitize_input_columns' )
     251            array()
    129252        );
    130253
     
    132255        add_settings_section(
    133256            'virtual_column_section_id',
    134             '<h2>Virtual Column Settings</h2>',
     257            '<h2>' . __( "Virtual Column Settings", "beyond-wpdb" ) . '</h2>',
    135258            array( $this, 'print_virtual_column_section_info' ),
    136259            'virtual_columns_section'
     
    139262        add_settings_field(
    140263            'postmeta_json', // id
    141             "postmeta_json", // title
     264            __( "Table for postmeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: postmeta_beyond</span>", // title
    142265            array( $this, 'print_postmeta_json_field' ), // callback
    143266            'virtual_columns_section', // section page
    144             'virtual_column_section_id' // section id
     267            'virtual_column_section_id', // section id,
     268            array(
     269                'id' => "virtual_column_{$wpdb->prefix}postmeta_beyond",
     270                'class' => "virtual_column_{$wpdb->prefix}postmeta_beyond"
     271            )
    145272        );
    146273
    147274        add_settings_field(
    148275            'usermeta_json',
    149             "usermeta_json",
     276            __( "Table for usermeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: usermeta_beyond</span>",
    150277            array( $this, 'print_usermeta_json_field' ),
    151278            'virtual_columns_section', // section page
    152             'virtual_column_section_id' // section id
     279            'virtual_column_section_id', // section id
     280            array(
     281                'id' => "virtual_column_{$wpdb->prefix}usermeta_beyond",
     282                'class' => "virtual_column_{$wpdb->prefix}usermeta_beyond"
     283            )
    153284        );
    154285
    155286        add_settings_field(
    156287            'commentmeta_json', // id
    157             "commentmeta_json", // title
     288            __( "Table for commentmeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: commentmeta_beyond</span>", // title
    158289            array( $this, 'print_commentmeta_json_field' ), // callback
    159290            'virtual_columns_section', // section id
    160             'virtual_column_section_id'
     291            'virtual_column_section_id',
     292            array(
     293                'id' => "virtual_column_{$wpdb->prefix}commentmeta_beyond",
     294                'class' => "virtual_column_{$wpdb->prefix}commentmeta_beyond"
     295            )
    161296        );
    162297
     
    164299            'beyond_wpdb_group',
    165300            'beyond_wpdb_virtual_column_name',
    166             array( $this, 'sanitize_input_columns' )
    167         );
    168     }
    169 
    170     /**
    171      * @param $option
    172      * @param $value
    173      */
    174     public function do_after_add_virtual_column( $option, $value ) {
    175         $this->do_virtual_column_processing( $option );
    176     }
    177 
    178     /**
    179      * @param $option
    180      * @param $value
    181      */
    182     public function do_after_add_data_init( $option, $value ) {
    183         $this->do_data_init_processing( $option );
    184     }
    185 
    186     /**
    187      * @param $old
    188      * @param $value
    189      * @param $option
    190      */
    191     public function do_after_update_virtual_column( $old, $value, $option ) {
    192         $this->do_virtual_column_processing( $option );
    193     }
    194 
    195     /**
    196      * @param $old
    197      * @param $value
    198      * @param $option
    199      */
    200     public function do_after_update_data_init( $old, $value, $option ) {
    201         $this->do_data_init_processing( $option );
    202     }
    203 
    204 
    205     /**
    206      * @param $option
    207      * create virtual column and delete virtual column and delete option
    208      */
    209     public function do_virtual_column_processing( $option ) {
    210         $this->create_virtual_column_and_index( get_option( $option ) );
    211         $this->delete_virtual_column( get_option( $option ) );
    212         delete_option( $option );
    213     }
    214 
    215     /**
    216      * @param $option
    217      * data init
    218      */
    219     public function do_data_init_processing( $option ) {
    220         $this->data_init( get_option($option) );
    221         delete_option( $option );
    222     }
    223 
    224     /**
    225      * @param $input
    226      *
    227      * @return array
    228      */
    229     public function sanitize_input_columns( $input )
    230     {
    231         $new_input = array();
    232         if( isset( $input['postmeta_json'] ) ) {
    233             $input_columns = explode( PHP_EOL, $input['postmeta_json'] );
    234             $input_columns = $this->sanitize( $input_columns );
    235             $new_input['postmeta_json'] = implode( PHP_EOL, $input_columns );
    236         }
    237 
    238         if( isset( $input['usermeta_json'] ) ) {
    239             $input_columns = explode( PHP_EOL, $input['usermeta_json'] );
    240             $input_columns = $this->sanitize( $input_columns );
    241             $new_input['usermeta_json'] = implode( PHP_EOL, $input_columns );
    242         }
    243 
    244         if( isset( $input['commentmeta_json'] ) ) {
    245             $input_columns = explode( PHP_EOL, $input['commentmeta_json'] );
    246             $input_columns = $this->sanitize( $input_columns );
    247             $new_input['commentmeta_json'] = implode( PHP_EOL, $input_columns );
    248         }
    249 
    250         if( isset( $input['data_init'] ) ) {
    251             $input_columns = explode( PHP_EOL, $input['data_init'] );
    252             $input_columns = $this->sanitize( $input_columns );
    253             $new_input['data_init'] = implode( PHP_EOL, $input_columns );
    254         }
    255 
    256         return $new_input;
    257     }
    258 
    259     /**
    260      * @param $option
    261      * data init
    262      */
    263     public function data_init( $option )
    264     {
    265         if ( isset( $option['data_init'] ) && $option['data_init'] === '1' ) {
    266             $beyond_wpdb_sql = new Beyond_Wpdb_Sql();
    267             $beyond_wpdb_sql->data_init();
    268         }
    269     }
    270 
    271     /**
    272      * @param $input_columns
    273      *
    274      * @return string
    275      */
    276     public function sanitize( $input_columns )
    277     {
    278         foreach ( $input_columns as $key => $value ) {
    279             $input_columns[$key] = sanitize_text_field( $value );
    280         }
    281         return $input_columns;
    282     }
    283 
    284     /**
    285      * @param $options
    286      * create virtual column
    287      */
    288     public function create_virtual_column_and_index( $options ) {
    289         global $wpdb;
    290 
    291         if ( is_array( $options ) ) {
    292             foreach ( $options as $key => $option ) {
    293 
    294                 if ( 'postmeta_json' === $key ) {
    295                     $type = 'post';
    296                 } elseif ( 'usermeta_json' === $key ) {
    297                     $type = 'user';
    298                 } else {
    299                     $type = 'comment';
    300                 }
    301 
    302                 $table_name = $this->get_json_table_name( $type );
    303                 $exist_columns = $this->columns[$type];
    304                 $option = explode( PHP_EOL, $option );
    305 
    306                 foreach ( $option as $value ) {
    307 
    308                     $value = esc_sql( $value );
    309 
    310                     // If $value already exists, continue
    311                     if ( in_array( $value, $exist_columns ) ) {
    312                         continue;
    313                     }
    314 
    315                     $json_key = '$.' . $value;
    316 
    317                     // create virtual column
    318                     $sql = "ALTER TABLE {$table_name} ADD {$value} VARCHAR(255) GENERATED ALWAYS AS ( JSON_UNQUOTE( JSON_EXTRACT( json, '$json_key' ) ) )";
    319                     $wpdb->query( $sql );
    320 
    321                     // create index
    322                     $sql = "ALTER TABLE {$table_name} ADD INDEX ({$value})";
    323                     $wpdb->query( $sql );
    324                 }
    325             }
    326         }
    327     }
    328 
    329     /**
    330      * @param $options
    331      * delete virtual columns
    332      * @return mixed
    333      */
    334     public function delete_virtual_column( $options ) {
    335         global $wpdb;
    336 
    337         if ( is_array( $options ) ) {
    338             foreach ( $options as $key => $option ) {
    339                 if ( 'postmeta_json' === $key ) {
    340                     $type = 'post';
    341                 } elseif ( 'usermeta_json' === $key ) {
    342                     $type = 'user';
    343                 } else {
    344                     $type = 'comment';
    345                 }
    346 
    347                 $table_name = $this->get_json_table_name( $type );
    348                 $exist_columns = $this->columns[$type];
    349                 $option = explode( PHP_EOL, $option );
    350 
    351                 foreach ( $exist_columns as $column ) {
    352                     if ( ! in_array( $column, $option ) ) {
    353                         $sql = "ALTER TABLE {$table_name} DROP COLUMN {$column}";
    354                         $wpdb->query( $sql );
    355                     }
    356                 }
    357             }
    358         }
    359     }
    360 
    361 
    362     public function get_json_table_name( $type ) {
    363         return esc_sql( constant( beyond_wpdb_get_define_table_name( $type ) ) );
     301            array()
     302        );
    364303    }
    365304
     
    369308    public function print_virtual_column_section_info()
    370309    {
    371         print '<p>
    372                  Enter a list metakeys for which you want to create a virtual column.Each metakey should be separated by return enter key.<br>
    373                  The virtual column is indexed and the search uses the virtual column, which makes it faster.
    374                </p>';
     310        $info = __( "When you enter a key of meta information separated by a new line,<br>the specified meta key from the JSON type column is set as a virtual column and the index is pasted.<br>You can further speed up data calls by specifying the most commonly used meta keys for meta queries.", "beyond-wpdb" );
     311        print "<p>
     312                 {$info}
     313               </p>";
    375314    }
    376315
     
    378317     * print postmeta json field
    379318     */
    380     public function print_postmeta_json_field()
    381     {
    382         $value = implode( PHP_EOL, $this->columns['post'] );
    383         printf(
    384             '<textarea rows="3" cols="40" id="postmeta_json" name="beyond_wpdb_virtual_column_name[postmeta_json]">%s</textarea>',
    385             $value
    386         );
     319    public function print_postmeta_json_field( $args )
     320    {
     321        $id = $args['id'];
     322        $disabled = $this->checkDisabled() ? '' : 'disabled';
     323        print "
     324        <textarea class='create_virtualColumns_text-area d-none' rows='3' cols='40' id='{$id}_textarea' name='{$id}' {$disabled}></textarea>
     325        <span class='create_{$id} d-none' style='margin-left: 20px;'>Processing...</span>
     326        <span class='data-init-input-loading' style='margin-left: 20px;'>Loading...</span>
     327        ";
     328
    387329    }
    388330
     
    390332     * print usermeta json field
    391333     */
    392     public function print_usermeta_json_field()
    393     {
    394         $value = implode( PHP_EOL, $this->columns['user'] );
    395         printf(
    396             '<textarea rows="3" cols="40" id="usermeta_json" name="beyond_wpdb_virtual_column_name[usermeta_json]">%s</textarea>',
    397             $value
    398         );
     334    public function print_usermeta_json_field( $args )
     335    {
     336        $id = $args['id'];
     337        $disabled = $this->checkDisabled() ? '' : 'disabled';
     338        print "
     339        <textarea class='create_virtualColumns_text-area d-none' rows='3' cols='40' id='{$id}_textarea' name='{$id}' {$disabled}></textarea>
     340        <span class='create_{$id} d-none' style='margin-left: 20px;'>Processing...</span>
     341        <span class='data-init-input-loading' style='margin-left: 20px;'>Loading...</span>
     342        ";
    399343    }
    400344
     
    402346     * print commentmeta json field
    403347     */
    404     public function print_commentmeta_json_field()
    405     {
    406         $value = implode( PHP_EOL, $this->columns['comment'] );
    407         printf(
    408             '<textarea rows="3" cols="40" id="commentmeta_json" name="beyond_wpdb_virtual_column_name[commentmeta_json]">%s</textarea>',
    409             $value
    410         );
    411     }
    412 
    413     /**
    414      * print group concat section info
    415      */
    416     public function print_group_concat_section_info()
    417     {
    418         print '<p>Use GROUP_CONCAT in data init.If the json string consisting of all metas is longer than this value, an error occurs.</p>';
    419     }
    420 
    421     /**
    422      * print croup_concat field
    423      */
    424     public function print_croup_concat_field()
    425     {
    426         global $wpdb;
    427         $group_concat_max_len = $wpdb->get_results( "show variables like 'group_concat_max_len'" );
    428         printf(
    429             '<p>%s</p>',
    430             $group_concat_max_len[0]->Value
    431         );
     348    public function print_commentmeta_json_field( $args )
     349    {
     350        $id = $args['id'];
     351        $disabled = $this->checkDisabled() ? '' : 'disabled';
     352        print "
     353        <textarea class='create_virtualColumns_text-area d-none' rows='3' cols='40' id='{$id}_textarea' name='{$id}' {$disabled}></textarea>
     354        <span class='create_{$id} d-none' style='margin-left: 20px;'>Processing...</span>
     355        <span class='data-init-input-loading' style='margin-left: 20px;'>Loading...</span>
     356        ";
    432357    }
    433358
     
    437362    public function print_data_init_section_info()
    438363    {
    439         print '<p>Collect all meta information and re-register it in the json table.</p>';
    440     }
    441 
    442     /**
    443      *  print data_init field
    444      */
    445     public function print_data_init_field()
    446     {
    447         print '<input type="checkbox" id="data_init" name="beyond_wpdb_data_init_name[data_init]" value="1">';
     364        $info = __( "Create and delete your own tables that aggregate the metadata. (Enabled to create, disabled to delete)<br>As long as it is enabled, all meta information will continue to be automatically registered, updated, and deleted in the JSON type columns of the relevant table.<br>When enabled, a new table will be created or initialized, which will take some time. When disabled, the table will be deleted.", "beyond-wpdb" );
     365        print $info;
     366    }
     367
     368    /**
     369     * Check to see if each feature is available
     370     * @return bool
     371     */
     372    public function checkDisabled() {
     373        return ! $this->privilege_error;
     374    }
     375
     376    /**
     377     * @param $args
     378     * print data_init field
     379     */
     380    public function print_data_init_field( $args )
     381    {
     382        $id = $args['id'];
     383        $disabled = $this->checkDisabled() ? '' : 'disabled';
     384        print "
     385        <div class='data-init-input-radio d-none'><input type='radio' id='{$id}_active' name='name_{$id}' value='1' {$disabled}>" . "<label for='{$id}_active'>" . __( "activation", "beyond-wpdb" ) . "</label>" .
     386        "<input type='radio' id='{$id}_deactive' name='name_{$id}' value='0' {$disabled} style='margin-left: 25px;'>" . "<label for='{$id}_deactive'>" . __( "deactivation", "beyond-wpdb" ) . "</label>" .
     387        "<span class='activate_{$id} d-none' style='margin-left: 20px;'>Processing...</span>
     388         <span class='success_{$id}' style='margin-left: 20px; display: none;'>Success.</span></div><span class='data-init-input-loading' style='margin-left: 20px;'>Loading...</span>
     389        ";
    448390    }
    449391}
  • beyond-wpdb/trunk/class/sql.php

    r2330366 r2352101  
    88 */
    99class Beyond_Wpdb_Sql {
     10
    1011    function __construct() {
    1112        global $wpdb;
    1213
    1314        foreach( array_keys( BEYOND_WPDB_PRIMARYS ) as $primary ) {
     15            $this->triggers[$primary] = array();
     16
    1417            $insert_trigger = 'insert_' . $primary . '_trigger';
    1518            $this->$insert_trigger = esc_sql( $wpdb->prefix . 'insert_' . $primary . '_trigger' );
     19            array_push( $this->triggers[$primary], $this->$insert_trigger );
    1620
    1721            $delete_trigger = 'delete_' . $primary . '_trigger';
    1822            $this->$delete_trigger = esc_sql( $wpdb->prefix . 'delete_' . $primary . '_trigger' );
     23            array_push( $this->triggers[$primary], $this->$delete_trigger );
    1924
    2025            $insert_meta_trigger = 'insert_' . $primary . 'meta_trigger';
    2126            $this->$insert_meta_trigger = esc_sql( $wpdb->prefix . 'insert_' . $primary . 'meta_trigger' );
     27            array_push( $this->triggers[$primary], $this->$insert_meta_trigger );
    2228
    2329            $update_meta_trigger = 'update_' . $primary . 'meta_trigger';
    2430            $this->$update_meta_trigger = esc_sql( $wpdb->prefix . 'update_' . $primary . 'meta_trigger' );
     31            array_push( $this->triggers[$primary], $this->$update_meta_trigger );
    2532
    2633            $delete_meta_trigger = 'delete_' . $primary . 'meta_trigger';
    2734            $this->$delete_meta_trigger = esc_sql( $wpdb->prefix . 'delete_' . $primary . 'meta_trigger' );
     35            array_push( $this->triggers[$primary], $this->$delete_meta_trigger );
    2836        }
    2937    }
     
    3240     * Create table
    3341     *
    34      * @return void
    35      */
    36     function create_table() : void {
    37         foreach( BEYOND_WPDB_PRIMARYS as $primary => $values ) {
    38             $this->create_table_sql( $primary, $values['meta_table_key'] );
    39         }
     42     * @param string $type
     43     * @return void
     44     */
     45    function create_table( $primary ) : void {
     46        $values = BEYOND_WPDB_PRIMARYS[$primary];
     47        $this->create_table_sql( $primary, $values['meta_table_key'] );
    4048    }
    4149
     
    6674     * Drop table
    6775     *
    68      * @return void
    69      */
    70     function drop_table() {
    71         foreach( array_keys( BEYOND_WPDB_PRIMARYS ) as $primary ) {
    72             $this->drop_table_sql( $primary );
    73         }
     76     * @param $primary
     77     * @return void
     78     * @throws Exception
     79     */
     80    function drop_table( $primary ) {
     81        $this->drop_table_sql( $primary );
    7482    }
    7583
     
    7987     * @param string $primary
    8088     * @return void
     89     * @throws Exception
    8190     */
    8291    protected function drop_table_sql( $primary ) {
    8392        global $wpdb;
    84         $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) );
    85 
    86         $sql = 'DROP TABLE ' . $table_name;
    87         $wpdb->query( $sql );
     93
     94        $exist_tables = $this->get_exist_json_tables();
     95        $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) );
     96
     97        if ( in_array( $table_name, $exist_tables ) ) {
     98            $sql = 'DROP TABLE ' . $table_name;
     99            $wpdb->query( $sql );
     100
     101            if ( $wpdb->last_error ) {
     102                throw new Exception( $wpdb->last_error );
     103            }
     104        }
    88105    }
    89106
     
    93110     * Insert/Update/Delete postmeta trigger
    94111     *
    95      * @return void
    96      */
    97     function create_trigger() {
    98         foreach( BEYOND_WPDB_PRIMARYS as $primary => $values ) {
    99             $this->insert_primary_trigger( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_key'] );
    100             $this->delete_primary_trigger( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_key'] );
    101             $this->insert_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] );
    102             $this->update_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] );
    103             $this->delete_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] );
    104 
    105         }
     112     * @param string $primary
     113     * @return void
     114     * @throws Exception
     115     */
     116    function create_trigger( $primary ) {
     117        $values = BEYOND_WPDB_PRIMARYS[$primary];
     118        $this->insert_primary_trigger( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_key'] );
     119        $this->delete_primary_trigger( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_key'] );
     120        $this->insert_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] );
     121        $this->update_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] );
     122        $this->delete_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] );
    106123    }
    107124
     
    114131     * @param string $meta_table_key
    115132     * @return void
     133     * @throws Exception
    116134     */
    117135    protected function insert_primary_trigger( $primary, $primaty_table_name, $primary_table_key, $meta_table_key ) {
     
    119137
    120138        $insert_trigger = 'insert_' . $primary . '_trigger';
     139        $exist_triggers = $this->get_exist_triggers();
    121140        $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) );
    122141        $primaty_table_name = esc_sql( $primaty_table_name );
     
    124143        $primary_table_key = esc_sql( $primary_table_key );
    125144
    126         $sql = 'CREATE TRIGGER ' . $this->$insert_trigger . ' AFTER
     145        if ( ! in_array( $this->$insert_trigger, $exist_triggers ) ) {
     146            $sql = 'CREATE TRIGGER ' . $this->$insert_trigger . ' AFTER
    127147                INSERT ON
    128148                ' . $primaty_table_name . '
     
    134154                VALUES
    135155                    (NEW.' . $primary_table_key . ', "{}" )';
    136         $wpdb->query( $sql );
     156            $wpdb->query( $sql );
     157
     158            if ( $wpdb->last_error ) {
     159                throw new Exception( $wpdb->last_error );
     160            }
     161        }
    137162    }
    138163
     
    145170     * @param string $meta_table_key
    146171     * @return void
     172     * @throws Exception
    147173     */
    148174    protected function delete_primary_trigger( $primary, $primaty_table_name, $primary_table_key, $meta_table_key ) {
     
    150176
    151177        $delete_trigger = 'delete_' . $primary . '_trigger';
     178        $exist_triggers = $this->get_exist_triggers();
    152179        $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) );
    153180        $primaty_table_name = esc_sql( $primaty_table_name );
     
    155182        $primary_table_key = esc_sql( $primary_table_key );
    156183
    157         $sql = 'CREATE TRIGGER ' . $this->$delete_trigger . ' BEFORE
    158         DELETE ON
    159         ' . $primaty_table_name . '
    160         FOR
    161         EACH
    162         ROW
    163         DELETE FROM ' . $table_name . '
    164         WHERE ' . $meta_table_key . ' = OLD.' . $primary_table_key;
    165         $wpdb->query( $sql );
     184        if ( ! in_array( $this->$delete_trigger, $exist_triggers ) ) {
     185            $sql = 'CREATE TRIGGER ' . $this->$delete_trigger . ' BEFORE
     186            DELETE ON
     187            ' . $primaty_table_name . '
     188            FOR
     189            EACH
     190            ROW
     191            DELETE FROM ' . $table_name . '
     192            WHERE ' . $meta_table_key . ' = OLD.' . $primary_table_key;
     193            $wpdb->query( $sql );
     194
     195            if ( $wpdb->last_error ) {
     196                throw new Exception( $wpdb->last_error );
     197            }
     198        }
    166199    }
    167200
     
    173206     * @param string $meta_table_key
    174207     * @return void
     208     * @throws Exception
    175209     */
    176210    protected function insert_meta_trigger( $primary, $meta_table_name, $meta_table_key ) {
    177211        global $wpdb;
    178212
     213        $exist_triggers = $this->get_exist_triggers();
    179214        $insert_meta_trigger = 'insert_' . $primary . 'meta_trigger';
    180215        $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) );
     
    182217        $meta_table_key = esc_sql( $meta_table_key );
    183218
    184         $sql = 'CREATE TRIGGER ' . $this->$insert_meta_trigger . ' AFTER
     219        if ( ! in_array( $this->$insert_meta_trigger, $exist_triggers ) ) {
     220            $sql = 'CREATE TRIGGER ' . $this->$insert_meta_trigger . ' AFTER
    185221                INSERT ON
    186222                ' . $meta_table_name . '
     
    193229                (`json`, CONCAT
    194230                ("$.",NEW.meta_key), NEW.meta_value) WHERE ' . $meta_table_key . ' = NEW.'.$meta_table_key;
    195         $wpdb->query( $sql );
     231            $wpdb->query( $sql );
     232
     233            if ( $wpdb->last_error ) {
     234                throw new Exception( $wpdb->last_error );
     235            }
     236        }
    196237    }
    197238
     
    203244     * @param string $meta_table_key
    204245     * @return void
     246     * @throws Exception
    205247     */
    206248    protected function update_meta_trigger( $primary, $meta_table_name, $meta_table_key ) {
    207249        global $wpdb;
    208250
     251        $exist_triggers = $this->get_exist_triggers();
    209252        $update_meta_trigger = 'update_' . $primary . 'meta_trigger';
    210253        $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) );
     
    212255        $meta_table_key = esc_sql( $meta_table_key );
    213256
    214         $sql = 'CREATE TRIGGER ' . $this->$update_meta_trigger . ' AFTER
     257        if ( ! in_array( $this->$update_meta_trigger, $exist_triggers ) ) {
     258            $sql = 'CREATE TRIGGER ' . $this->$update_meta_trigger . ' AFTER
    215259                UPDATE ON
    216260                ' . $meta_table_name . '
     
    223267                (`json`, CONCAT
    224268                ("$.",NEW.meta_key), NEW.meta_value) WHERE ' . $meta_table_key . ' = NEW.' . $meta_table_key;
    225         $wpdb->query( $sql );
     269            $wpdb->query( $sql );
     270
     271            if ( $wpdb->last_error ) {
     272                throw new Exception( $wpdb->last_error );
     273            }
     274        }
    226275    }
    227276
     
    233282     * @param string $meta_table_key
    234283     * @return void
     284     * @throws Exception
    235285     */
    236286    protected function delete_meta_trigger( $primary, $meta_table_name, $meta_table_key ) {
    237287        global $wpdb;
    238288
     289        $exist_triggers = $this->get_exist_triggers();
    239290        $delete_meta_trigger = 'delete_' . $primary . 'meta_trigger';
    240291        $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) );
     
    242293        $meta_table_key = esc_sql( $meta_table_key );
    243294
    244         $sql = 'CREATE TRIGGER ' . $this->$delete_meta_trigger . ' AFTER
     295        if ( ! in_array( $this->$delete_meta_trigger, $exist_triggers ) ) {
     296            $sql = 'CREATE TRIGGER ' . $this->$delete_meta_trigger . ' AFTER
    245297                DELETE ON
    246298                ' . $meta_table_name . '
     
    252304                `json` = JSON_REMOVE(`json`, CONCAT
    253305                ("$.",OLD.meta_key)) WHERE ' . $meta_table_key . ' = OLD.'. $meta_table_key;
     306            $wpdb->query( $sql );
     307
     308            if ( $wpdb->last_error ) {
     309                throw new Exception( $wpdb->last_error );
     310            }
     311        }
     312    }
     313
     314    /**
     315     * Drop triggers
     316     *
     317     * @param $primary
     318     * @return void
     319     * @throws Exception
     320     */
     321    function drop_triggers( $primary ) {
     322        global $wpdb;
     323
     324        $exist_triggers = $this->get_exist_triggers();
     325
     326        foreach ( $this->triggers[$primary] as $value ) {
     327            if ( in_array( $value, $exist_triggers ) ) {
     328                $sql = 'DROP TRIGGER ' . esc_sql( $value );
     329                $wpdb->query( $sql );
     330            }
     331        }
     332
     333        if ( $wpdb->last_error ) {
     334            throw new Exception( $wpdb->last_error );
     335        }
     336    }
     337
     338    /**
     339     * Update table from post, postmeta table.
     340     *
     341     * @param $primary
     342     *
     343     * @throws Exception
     344     */
     345    function data_init( $primary ) {
     346        global $wpdb;
     347        $values = BEYOND_WPDB_PRIMARYS[$primary];
     348        $maxlen = 4294967295;
     349
     350        $maxlen = apply_filters( 'beyond_group_concat_max_len', $maxlen );
     351        $sql = 'SET SESSION group_concat_max_len = ' . $maxlen;
    254352        $wpdb->query( $sql );
    255     }
    256 
    257     /**
    258      * Drop triggers
    259      *
    260      * @return void
    261      */
    262     function drop_triggers() {
    263         global $wpdb;
    264 
    265         foreach( get_object_vars( $this ) as $value ) {
    266             $sql = 'DROP TRIGGER ' . esc_sql( $value );
    267             $wpdb->query( $sql );
    268         }
    269     }
    270 
    271     /**
    272      * Update table from post, postmeta table.
    273      *
    274      * @return void
    275      */
    276     function data_init() {
    277         foreach( BEYOND_WPDB_PRIMARYS as $primary => $values ) {
    278             $this->data_init_sql( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_name'], $values['meta_table_key'] );
    279             $this->delete_non_existent_data_from_json( $primary, $values['primary_table_name'], $values['primary_table_key'] );
    280         }
     353
     354        $this->data_init_sql( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_name'], $values['meta_table_key'] );
     355        $this->delete_non_existent_data_from_json( $primary, $values['primary_table_name'], $values['primary_table_key'] );
    281356    }
    282357
     
    284359     * data init sql
    285360     *
    286      * @param string $primary
    287      * @param string $primary_table_name
    288      * @param string $primary_table_key
    289      * @param string $meta_table_name
    290      * @param string $meta_table_key
    291      * @return void
     361     * @param $primary
     362     * @param $primary_table_name
     363     * @param $primary_table_key
     364     * @param $meta_table_name
     365     * @param $meta_table_key
     366     *
     367     * @throws Exception
    292368     */
    293369    protected function data_init_sql( $primary, $primary_table_name, $primary_table_key, $meta_table_name, $meta_table_key ) {
     
    311387                UPDATE json = VALUES(json)';
    312388        $wpdb->query( $sql );
     389
     390        if ( $wpdb->last_error ) {
     391            throw new Exception( $wpdb->last_error );
     392        }
    313393    }
    314394
     
    320400     * @param $primary_table_key
    321401     * @return void
     402     * @throws Exception
    322403     */
    323404    protected function delete_non_existent_data_from_json( $primary, $primary_table_name, $primary_table_key ) {
     
    334415
    335416        $wpdb->query( $sql );
     417
     418        if ( $wpdb->last_error ) {
     419            throw new Exception( $wpdb->last_error );
     420        }
     421    }
     422
     423    /**
     424     * Get exist virtual columns
     425     * @return array
     426     */
     427    protected function get_exist_triggers() {
     428        $beyond_wpdb_info = new Beyond_Wpdb_Information();
     429        $beyond_wpdb_info->set_triggers();
     430        return $beyond_wpdb_info->get_triggers();
     431    }
     432
     433    /**
     434     * Get exist json tables
     435     * @return array
     436     */
     437    protected function get_exist_json_tables(){
     438        $beyond_wpdb_info = new Beyond_Wpdb_Information();
     439        $beyond_wpdb_info->set_tables();
     440        return $beyond_wpdb_info->get_tables();
    336441    }
    337442}
  • beyond-wpdb/trunk/readme.txt

    r2337807 r2352101  
    22Contributors: pressmaninc,kazunao,hiroshisekiguchi,hommakoharu,pmhirotaka
    33Tags: pressman,pressmaninc,json,meta_query,wpdb,database,fast,speed,speed up,sql,replace sql
    4 Stable tag: 1.1.0
     4Stable tag: 2.0.0
    55License: GPLv2 or later
    66License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    1010Requires MariaDB: 10.2
    1111Tested up to: 5.4
     12
     13※ Table names have been changed since v2.0.Please delete the meta_json table and reactivate it in the options screen.
    1214
    1315Are you having trouble with slow queries due to WordPress table structure?
     
    2325
    2426== Actual Behavior ==
     27* You can create and delete json tables and triggers and data for each json table on the configuration page.
     28* Create virtual columns and indexes for the json table you created in the configuration page.
     29* If you add a virtual column to the json table in the configuration page, the SQL is converted to face the virtual column.
    2530* Converts the SQL that WordPress auto-generates when using the default APIs of get_posts, Wp_Query, and etc. into a simple SQL that combines the posts/users/comment table with its own meta table.
    2631* The custom meta table automatically stores data using MySQL and MariaDB triggers.
     
    3237
    3338== Custom meta tables generated by plugin ==
    34 * postmeta_json (for posts table)
    35 * usermeta_json (for users table)
    36 * commentmeta_json (for comment table)
     39The actual table names are prefixed with $table_prefix.
     40* postmeta_beyond (for posts table)
     41* usermeta_beyond (for users table)
     42* commentmeta_beyond (for comment table)
    3743
    3844== Columns in each table ==
    39 * postmeta_json
     45The actual table names are prefixed with $table_prefix.
     46* postmeta_beyond
    4047    * post_id(int)
    4148    * json(json)
    42 * usermeta_json
     49* usermeta_beyond
    4350    * post_id(int)
    4451    * json(json)
    45 * commentmeta_json
     52* commentmeta_beyond
    4653    * post_id(int)
    4754    * json(json)
     
    5360ex1 ) Create 100 posts and 50 meta data for each post. Combine them 9 times.
    5461
    55 * postmeta_json
     62* postmeta_beyond
    5663    * Equal:0.04
    5764    * IN:0.09
     
    6168    * BETWEEN:0.04
    6269    * NOT BETWEEN:0.04
     70
    6371* postmeta
    6472    * Equal:4.72
     
    7078    * NOT BETWEEN:0.63
    7179
    72 ex2 ) 10000 posts, 1 meta(category) per post.If you add a virtual column and put an index in Category, or if you add a virtual column and put an index in Category
    73 * postmeta_json(without virtual column)
     80ex2 ) 10,000 posts, 1 meta per post. meta_key is category.
     81      Add a virtual column(category) to postmeta_beyond and put an index,
     82      and compare it to the case without adding a virtual column.
     83
     84* postmeta_beyond(with virtual column)
     85    * Equal:0.02
     86    * IN:0.35
     87    * BETWEEN:0.12
     88    * NOT BETWEEN:0.09
     89
     90* postmeta_beyond(without virtual column)
    7491    * Equal:0.15
    7592    * IN:1.92
    7693    * BETWEEN:0.37
    7794    * NOT BETWEEN:0.29
    78 
    79 * postmeta_json(with virtual column)
    80     * Equal:0.02
    81     * IN:0.35
    82     * BETWEEN:0.12
    83     * NOT BETWEEN:0.09
    8495
    8596== Installation ==
     
    91102* You'll need TRIGGER, SELECT, and UPDATE permissions for the target table and its columns to use the trigger feature of MySQL and MariaDB.
    92103* It makes changes to SQL only if `suppress_filters` is false.
     104* This plugin sets 4294967295 to group_concat_max_len when it create an original table with JSON type column. You can change the number by using filter 'beyond_group_concat_max_len'.
Note: See TracChangeset for help on using the changeset viewer.