c53e660cb16562e77eb961f62a3e25ee335897ad
[yaffs-website] / web / core / lib / Drupal / Core / Entity / Sql / TemporaryTableMapping.php
1 <?php
2
3 namespace Drupal\Core\Entity\Sql;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6
7 /**
8  * Defines a temporary table mapping class.
9  */
10 class TemporaryTableMapping extends DefaultTableMapping {
11
12   /**
13    * {@inheritdoc}
14    */
15   protected function generateFieldTableName(FieldStorageDefinitionInterface $storage_definition, $revision) {
16     return static::getTempTableName(parent::generateFieldTableName($storage_definition, $revision));
17   }
18
19   /**
20    * Generates a temporary table name.
21    *
22    * The method accounts for a maximum table name length of 64 characters.
23    *
24    * @param string $table_name
25    *   The initial table name.
26    * @param string $prefix
27    *   (optional) The prefix to use for the new table name. Defaults to 'tmp_'.
28    *
29    * @return string
30    *   The final table name.
31    */
32   public static function getTempTableName($table_name, $prefix = 'tmp_') {
33     $tmp_table_name = $prefix . $table_name;
34
35     // Limit the string to 48 characters, keeping a 16 characters margin for db
36     // prefixes.
37     if (strlen($table_name) > 48) {
38       $short_table_name = substr($table_name, 0, 34);
39       $table_hash = substr(hash('sha256', $table_name), 0, 10);
40
41       $tmp_table_name = $prefix . $short_table_name . $table_hash;
42     }
43     return $tmp_table_name;
44   }
45
46 }