You are here

function apdqc_fast_prefix_tables in Asynchronous Prefetch Database Query Cache 7

Append a database prefix to all tables in a query.

Queries sent to Drupal should wrap all table names in curly brackets. This function searches for this syntax and adds Drupal's table prefix to all tables, allowing Drupal to coexist with other systems in the same database if necessary.

Parameters

string $sql: A string containing a partial or entire SQL query.

Return value

string The properly-prefixed string.

4 calls to apdqc_fast_prefix_tables()
apdqc_async_data in ./apdqc.mysql.inc
Used to get & parse async cached data.
apdqc_boot in ./apdqc.module
Implements hook_boot().
apdqc_requirements in ./apdqc.install
Implements hook_requirements().
apdqc_run_prefetch_array in ./apdqc.cache.inc
Prefetch data from a cache table.

File

./apdqc.cache.inc, line 160
Extends Drupal's default database cache so async queries happen.

Code

function apdqc_fast_prefix_tables($sql) {
  $db_prefix = isset($GLOBALS['databases']['default']['default']['prefix']) ? $GLOBALS['databases']['default']['default']['prefix'] : '';
  if (is_array($db_prefix)) {
    if (array_key_exists('default', $db_prefix)) {
      $tmp = $db_prefix;
      unset($tmp['default']);
      foreach ($tmp as $key => $val) {
        $sql = strtr($sql, array(
          "{{$key}}" => "`{$val}{$key}`",
        ));
      }
      return strtr($sql, array(
        '{' => "`{$db_prefix['default']}",
        '}' => '`',
      ));
    }
    else {
      foreach ($db_prefix as $key => $val) {
        $sql = strtr($sql, array(
          "{{$key}}" => "`{$val}{$key}`",
        ));
      }
      return strtr($sql, array(
        '{' => '`',
        '}' => '`',
      ));
    }
  }
  else {
    return strtr($sql, array(
      '{' => "`{$db_prefix}",
      '}' => '`',
    ));
  }
}