Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support ALTER TABLE AUTO_INCREMENT #35

Open
batonac opened this issue May 5, 2023 · 2 comments
Open

support ALTER TABLE AUTO_INCREMENT #35

batonac opened this issue May 5, 2023 · 2 comments

Comments

@batonac
Copy link

batonac commented May 5, 2023

The query in this function (edit: example from a third-party plugin) produces an error (with the latest trunk):

public function reset_auto_increment_availability_table()
{
    global $wpdb;
    $tablename      = $wpdb->prefix.$this->availability_tablename;
    $row            = $wpdb->get_row( "SELECT Max(sno) as max_sno FROM $tablename" );
    
    $auto_increment = (int) $row->max_sno + 1;
    // error_log('auto_increment : '.print_r($auto_increment,1));
    $prepare_values = array( $auto_increment );
    $query 			= $wpdb->prepare( "ALTER TABLE $tablename AUTO_INCREMENT = %d", $prepare_values);
    $status         = $wpdb->query($query);
}

Here's the temporary workaround I used (thanks to ChatGPT):

public function reset_auto_increment_availability_table()
{
    global $wpdb;
    $tablename      = $wpdb->prefix.$this->availability_tablename;
    $row            = $wpdb->get_row( "SELECT Max(sno) as max_sno FROM $tablename" );

    $auto_increment = (int) $row->max_sno + 1;
    // error_log('auto_increment : '.print_r($auto_increment,1));
    $prepare_values = array( $auto_increment );
    
    // Check if the database is SQLite
    if ($wpdb instanceof WP_SQLite_DB) {
        $query = $wpdb->prepare( "UPDATE sqlite_sequence SET seq = %d WHERE name = %s", array($auto_increment, $tablename));
    } else {
        $query = $wpdb->prepare( "ALTER TABLE $tablename AUTO_INCREMENT = %d", $prepare_values);
    }

    $status = $wpdb->query($query);
}
@aristath
Copy link
Member

aristath commented May 8, 2023

Hey there @batonac

This function produces an error (with the latest trunk):

I don't understand... I'm missing context here. Where is the reset_auto_increment_availability_table function coming from?

@batonac
Copy link
Author

batonac commented May 8, 2023

Sorry I wasn't clear. The function is from a third party plugin, and a bit of a distraction in and of itself.

I was merely trying to point out a query incompatibility that will need to be resolved to achieve compatibility with all the existing plugins being used.

I was hoping the workaround could help to point in the right direction for implementing a translation layer for this particular query.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants