You are here

class PostgresqlDateSql in Drupal 10

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

PostgreSQL-specific date handling.

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

Hierarchy

Expanded class hierarchy of PostgresqlDateSql

See also

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

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

File

core/modules/views/src/Plugin/views/query/PostgresqlDateSql.php, line 16

Namespace

Drupal\views\Plugin\views\query
View source
class PostgresqlDateSql implements DateSqlInterface {
  use DependencySerializationTrait;

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

  /**
   * An array of PHP-to-PostgreSQL replacement patterns.
   *
   * @var array
   */
  protected static $replace = [
    'Y' => 'YYYY',
    'y' => 'YY',
    'M' => 'Mon',
    'm' => 'MM',
    // No format for Numeric representation of a month, without leading zeros.
    'n' => 'MM',
    'F' => 'Month',
    'D' => 'Dy',
    'd' => 'DD',
    'l' => 'Day',
    // No format for Day of the month without leading zeros.
    'j' => 'DD',
    'W' => 'IW',
    'H' => 'HH24',
    'h' => 'HH12',
    'i' => 'MI',
    's' => 'SS',
    'A' => 'AM',
  ];

  /**
   * Constructs the PostgreSQL-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) {

      // Ensures compatibility with field offset operation below.
      return "TO_TIMESTAMP({$field}, 'YYYY-MM-DD\"T\"HH24:MI:SS')";
    }
    return "TO_TIMESTAMP({$field})";
  }

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

  /**
   * {@inheritdoc}
   */
  public function setFieldTimezoneOffset(&$field, $offset) {
    $field = "({$field} + INTERVAL '{$offset} SECONDS')";
  }

  /**
   * {@inheritdoc}
   */
  public function setTimezoneOffset($offset) {
    $this->database
      ->query("SET TIME ZONE INTERVAL '{$offset}' HOUR TO MINUTE");
  }

}

Members

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