TemporaryTableMapping.php 1.3 KB

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