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

Fix https://github.com/stuttter/ludicrousdb/issues/188 #194

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions ludicrousdb/includes/class-ludicrousdb.php
Original file line number Diff line number Diff line change
Expand Up @@ -685,11 +685,20 @@
*
* @since 1.0.0
*
* @param bool $allow_bail Unused. For WP compat only.

Check failure on line 688 in ludicrousdb/includes/class-ludicrousdb.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 3 spaces after parameter type; 1 found
* @param string $query Query.
*
* @return resource MySQL database connection
*/
public function db_connect( $query = '' ) {
public function db_connect( $allow_bail = null, $query = '' ) {
if ( is_string( $allow_bail ) && $query === '' ) {
$query = $allow_bail;
// for reference only, not used, since this is generally not what we want here
// $allow_bail = $this->die_on_disconnect;
} elseif ( $allow_bail === null ) {

Check failure on line 698 in ludicrousdb/includes/class-ludicrousdb.php

View workflow job for this annotation

GitHub Actions / phpcs

Empty ELSEIF statement detected
// unlike WP core we can try another server if one fails
// $allow_bail = $this->die_on_disconnect;
}

// Bail if empty query
if ( empty( $query ) ) {
Expand Down Expand Up @@ -1412,14 +1421,20 @@
*
* @since 1.0.0
*
* @param string $db MySQL database name.
* @param false|string|mysqli|resource $dbh_or_table Optional. The database. One of:
* - the current database
* - the database housing the specified table
* - the database of the MySQL resource
* @param string $db MySQL database name.
* @param false|mysqli|resource $dbh Optional. The database. One of:
* - the current database

Check failure on line 1426 in ludicrousdb/includes/class-ludicrousdb.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 5 spaces before asterisk; 4 found
* - the database housing the specified table

Check failure on line 1427 in ludicrousdb/includes/class-ludicrousdb.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 5 spaces before asterisk; 4 found
* - the database of the MySQL resource

Check failure on line 1428 in ludicrousdb/includes/class-ludicrousdb.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 5 spaces before asterisk; 4 found
* @param false|string $table Optional. The table name. Only used if $dbh is false
*/
public function select( $db, $dbh_or_table = false ) {
$dbh = $this->get_db_object( $dbh_or_table );
public function select( $db, $dbh = false, $table = false ) {
if ( $dbh === false ) {
$dbh = $this->get_db_object( $table );
} elseif ( is_string( $dbh ) ) {
// backwards-compat of $dbh_or_table
$dbh = $this->get_db_object( $dbh );
}

if ( ! $this->dbh_type_check( $dbh ) ) {
return false;
Expand Down Expand Up @@ -1468,17 +1483,17 @@
* See set_charset().
*
* @since 1.0.0
* @param string $to_escape String to escape.
* @param string $data String to escape.
*/
public function _real_escape( $to_escape = '' ) { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
public function _real_escape( $data = '' ) { // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore

// Bail if not a scalar
if ( ! is_scalar( $to_escape ) ) {
if ( ! is_scalar( $data ) ) {
return '';
}

// Slash the query part
$escaped = addslashes( $to_escape );
$escaped = addslashes( $data );

// Maybe use WordPress core placeholder method
if ( method_exists( $this, 'add_placeholder_escape' ) ) {
Expand Down Expand Up @@ -1593,13 +1608,13 @@
*
* @since 1.0.0
*
* @param bool $die_on_disconnect Optional. Allows the function to die. Default true.
* @param bool $allow_bail Optional. Allows the function to die. Default true.
* @param bool $dbh_or_table Optional.
* @param string $query Optional. Query string passed db_connect
*
* @return bool|void True if the connection is up.
*/
public function check_connection( $die_on_disconnect = true, $dbh_or_table = false, $query = '' ) {
public function check_connection( $allow_bail = true, $dbh_or_table = false, $query = '' ) {
$dbh = $this->get_db_object( $dbh_or_table );

// Return true if ping is successful. This is the most common case.
Expand All @@ -1624,7 +1639,7 @@
for ( $tries = 1; $tries <= $this->reconnect_retries; $tries++ ) {

// Try to reconnect
$retry = $this->db_connect( $query );
$retry = $this->db_connect( false, $query );

// Return true if the connection is up
if ( false !== $retry ) {
Expand All @@ -1646,7 +1661,7 @@
}

// Bail here if not allowed to call $this->bail()
if ( false === $die_on_disconnect ) {
if ( false === $allow_bail ) {
return false;
}

Expand Down Expand Up @@ -1789,7 +1804,7 @@
$elapsed = 0;

} else {
$this->dbh = $this->db_connect( $query );
$this->dbh = $this->db_connect( $this->die_on_disconnect, $query );

if ( ! $this->dbh_type_check( $this->dbh ) ) {
$this->run_query_log_callbacks( $query, $retval );
Expand Down Expand Up @@ -2179,7 +2194,7 @@

// Table name
} elseif ( is_string( $dbh_or_table ) ) {
$dbh = $this->db_connect( "SELECT FROM {$dbh_or_table} {$this->users}" );
$dbh = $this->db_connect( $this->die_on_disconnect, "SELECT FROM {$dbh_or_table} {$this->users}" );
}

return $dbh;
Expand Down
Loading