You are here

function schema_unprefix_table in Schema 8

Same name and namespace in other branches
  1. 5 schema.module \schema_unprefix_table()
  2. 6 schema.module \schema_unprefix_table()
  3. 7 schema.module \schema_unprefix_table()

Unprefix a table name.

This is pretty much the converse of DatabaseConnection::prefixTables().

Parameters

string $name: The prefixed table name.

DatabaseConnection $connection: An optional database connection object.

Return value

string The unprefixed table name.

3 calls to schema_unprefix_table()
SchemaDatabaseSchema_mysql::inspect in src/SchemaDatabaseSchema_mysql.php
SchemaDatabaseSchema_pgsql::inspect in src/SchemaDatabaseSchema_pgsql.php
SchemaUnitTestCase::testUnprefixTable in src/Tests/SchemaUnitTestCase.php

File

./schema.module, line 240
The Schema module provides functionality built on the Schema API.

Code

function schema_unprefix_table($name, $connection = NULL) {
  $prefixes =& drupal_static(__FUNCTION__, array());
  if (!isset($connection)) {
    $connection = Database::getConnection();
  }
  $key = $connection
    ->getKey();
  if (!isset($prefixes[$key])) {
    $prefixes[$key] = array();
    $info = $connection
      ->getConnectionOptions();
    if (isset($info['prefix'])) {
      if (is_array($info['prefix'])) {
        $info['prefix'] = $info['prefix'] + array(
          'default' => '',
        );
      }
      else {
        $info['prefix'] = array(
          'default' => $info['prefix'],
        );
      }
      foreach ($info['prefix'] as $table => $prefix) {
        if ($table != 'default') {
          $prefixes[$key][$prefix . $table] = $table;
        }
        elseif ($prefix !== '') {
          $prefixes[$key][$prefix] = '';
        }
      }
    }
  }
  return !empty($prefixes[$key]) ? strtr($name, $prefixes[$key]) : $name;
}