You are here

protected function Connection::setPrefix in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::setPrefix()

Set the list of prefixes used by this database connection.

Parameters

array|string $prefix: Either a single prefix, or an array of prefixes, in any of the multiple forms documented in default.settings.php.

3 calls to Connection::setPrefix()
Connection::queryTemporary in core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php
Runs a SELECT query and stores its results in a temporary table.
Connection::__construct in core/lib/Drupal/Core/Database/Connection.php
Constructs a Connection object.
Connection::__construct in core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php
Constructs a \Drupal\Core\Database\Driver\sqlite\Connection object.

File

core/lib/Drupal/Core/Database/Connection.php, line 296

Class

Connection
Base Database API class.

Namespace

Drupal\Core\Database

Code

protected function setPrefix($prefix) {
  if (is_array($prefix)) {
    $this->prefixes = $prefix + [
      'default' => '',
    ];
  }
  else {
    $this->prefixes = [
      'default' => $prefix,
    ];
  }

  // Set up variables for use in prefixTables(). Replace table-specific
  // prefixes first.
  $this->prefixSearch = [];
  $this->prefixReplace = [];
  foreach ($this->prefixes as $key => $val) {
    if ($key != 'default') {
      $this->prefixSearch[] = '{' . $key . '}';
      $this->prefixReplace[] = $val . $key;
    }
  }

  // Then replace remaining tables with the default prefix.
  $this->prefixSearch[] = '{';
  $this->prefixReplace[] = $this->prefixes['default'];
  $this->prefixSearch[] = '}';
  $this->prefixReplace[] = '';

  // Set up a map of prefixed => un-prefixed tables.
  foreach ($this->prefixes as $table_name => $prefix) {
    if ($table_name !== 'default') {
      $this->unprefixedTablesMap[$prefix . $table_name] = $table_name;
    }
  }
}