3 namespace Drupal\views\Plugin\views\query;
5 use Drupal\Core\Database\Connection;
8 * PostgreSQL-specific date handling.
11 * This class should only be used by the Views SQL query plugin.
13 * @see \Drupal\views\Plugin\views\query\Sql
15 class PostgresqlDateSql implements DateSqlInterface {
18 * The database connection.
20 * @var \Drupal\Core\Database\Connection
25 * An array of PHP-to-PostgreSQL replacement patterns.
29 protected static $replace = [
34 // No format for Numeric representation of a month, without leading zeros.
40 // No format for Day of the month without leading zeros.
51 * Constructs the PostgreSQL-specific date sql class.
53 * @param \Drupal\Core\Database\Connection $database
54 * The database connection.
56 public function __construct(Connection $database) {
57 $this->database = $database;
63 public function getDateField($field, $string_date) {
65 // Ensures compatibility with field offset operation below.
66 return "TO_TIMESTAMP($field, 'YYYY-MM-DD HH24:MI:SS')";
68 return "TO_TIMESTAMP($field)";
74 public function getDateFormat($field, $format) {
75 $format = strtr($format, static::$replace);
76 return "TO_CHAR($field, '$format')";
82 public function setFieldTimezoneOffset(&$field, $offset) {
83 $field = "($field + INTERVAL '$offset SECONDS')";
89 public function setTimezoneOffset($offset) {
90 $this->database->query("SET TIME ZONE INTERVAL '$offset' HOUR TO MINUTE");