Skip to content

Commit

Permalink
Make sure db prefix / suffix is consistently applied
Browse files Browse the repository at this point in the history
Modifies the db_get_table() and plugin_table() functions to ensure that
we build a correct table name based on the user's config settings, e.g.

- trim leading/trailing whitespace
- do not add underscores if already present

Fixes #17355, regression issue introduced by commit
5a84e9d where plugin_table() no longer
inserts an underscore after $g_db_table_plugin_prefix
  • Loading branch information
dregad committed Jun 2, 2014
1 parent 97f7ad7 commit fade884
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
18 changes: 11 additions & 7 deletions core/database_api.php
Expand Up @@ -959,16 +959,20 @@ function db_get_table( $p_name ) {
$t_table = $p_name;
}

$t_prefix = config_get_global( 'db_table_prefix' );
$t_suffix = config_get_global( 'db_table_suffix' );

if( $t_prefix ) {
$t_table = $t_prefix . '_' . $t_table;
# Determine table prefix including trailing '_'
$t_prefix = trim( config_get_global( 'db_table_prefix' ) );
if( !empty( $t_prefix ) && '_' != substr( $t_prefix, -1 ) ) {
$t_prefix .= '_';
}
# Determine table suffix including leading '_'
$t_suffix = trim( config_get_global( 'db_table_suffix' ) );
if( !empty( $t_suffix ) && '_' != substr( $t_suffix, 0, 1 ) ) {
$t_suffix = '_' . $t_suffix;
}
$t_table .= $t_suffix;

# Physical table name
$t_table = $t_prefix . $t_table . $t_suffix;
db_check_identifier_size( $t_table );

return $t_table;
}

Expand Down
13 changes: 5 additions & 8 deletions core/plugin_api.php
Expand Up @@ -233,16 +233,13 @@ function plugin_table( $p_name, $p_basename = null ) {
$t_current = $p_basename;
}

$t_table_name = config_get_global( 'db_table_prefix' );
if( !empty( $t_table_name ) ) {
$t_table_name .= '_';
# Determine plugin table prefix including trailing '_'
$t_prefix = trim( config_get_global( 'db_table_plugin_prefix' ) );
if( !empty( $t_prefix ) && '_' != substr( $t_prefix, -1 ) ) {
$t_prefix .= '_';
}
$t_table_name .=
config_get_global( 'db_table_plugin_prefix' ) .
$t_current . '_' . $p_name .
config_get_global( 'db_table_suffix' );

return $t_table_name;
return db_get_table( $t_prefix . $t_current . '_' . $p_name );
}

/**
Expand Down

0 comments on commit fade884

Please sign in to comment.