You are here

class MysqlDateSql in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views/src/Plugin/views/query/MysqlDateSql.php \Drupal\views\Plugin\views\query\MysqlDateSql

MySQL-specific date handling.

@internal This class should only be used by the Views SQL query plugin.

Hierarchy

Expanded class hierarchy of MysqlDateSql

See also

\Drupal\views\Plugin\views\query\Sql

1 file declares its use of MysqlDateSql
MysqlDateSqlTest.php in core/modules/views/tests/src/Unit/Plugin/views/query/MysqlDateSqlTest.php
1 string reference to 'MysqlDateSql'
views.services.yml in core/modules/views/views.services.yml
core/modules/views/views.services.yml
1 service uses MysqlDateSql
views.date_sql in core/modules/views/views.services.yml
Drupal\views\Plugin\views\query\MysqlDateSql

File

core/modules/views/src/Plugin/views/query/MysqlDateSql.php, line 15

Namespace

Drupal\views\Plugin\views\query
View source
class MysqlDateSql implements DateSqlInterface {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * An array of PHP-to-MySQL replacement patterns.
   */
  protected static $replace = [
    'Y' => '%Y',
    'y' => '%y',
    'M' => '%b',
    'm' => '%m',
    'n' => '%c',
    'F' => '%M',
    'D' => '%a',
    'd' => '%d',
    'l' => '%W',
    'j' => '%e',
    'W' => '%v',
    'H' => '%H',
    'h' => '%h',
    'i' => '%i',
    's' => '%s',
    'A' => '%p',
  ];

  /**
   * Constructs the MySQL-specific date sql class.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection.
   */
  public function __construct(Connection $database) {
    $this->database = $database;
  }

  /**
   * {@inheritdoc}
   */
  public function getDateField($field, $string_date) {
    if ($string_date) {
      return $field;
    }

    // Base date field storage is timestamp, so the date to be returned here is
    // epoch + stored value (seconds from epoch).
    return "DATE_ADD('19700101', INTERVAL {$field} SECOND)";
  }

  /**
   * {@inheritdoc}
   */
  public function getDateFormat($field, $format) {
    $format = strtr($format, static::$replace);
    return "DATE_FORMAT({$field}, '{$format}')";
  }

  /**
   * {@inheritdoc}
   */
  public function setTimezoneOffset($offset) {
    $this->database
      ->query("SET @@session.time_zone = '{$offset}'");
  }

  /**
   * {@inheritdoc}
   */
  public function setFieldTimezoneOffset(&$field, $offset) {
    if (!empty($offset)) {
      $field = "({$field} + INTERVAL {$offset} SECOND)";
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MysqlDateSql::$database protected property The database connection.
MysqlDateSql::$replace protected static property An array of PHP-to-MySQL replacement patterns.
MysqlDateSql::getDateField public function Returns a native database expression for a given field. Overrides DateSqlInterface::getDateField
MysqlDateSql::getDateFormat public function Creates a native database date formatting. Overrides DateSqlInterface::getDateFormat
MysqlDateSql::setFieldTimezoneOffset public function Applies the given offset to the given field. Overrides DateSqlInterface::setFieldTimezoneOffset
MysqlDateSql::setTimezoneOffset public function Set the database to the given timezone. Overrides DateSqlInterface::setTimezoneOffset
MysqlDateSql::__construct public function Constructs the MySQL-specific date sql class.