c03c416456a88a4d0eb8875c9d815ff105efef66
[yaffs-website] / web / core / modules / views / src / Plugin / views / query / PostgresqlDateSql.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\query;
4
5 use Drupal\Core\Database\Connection;
6
7 /**
8  * PostgreSQL-specific date handling.
9  *
10  * @internal
11  *   This class should only be used by the Views SQL query plugin.
12  *
13  * @see \Drupal\views\Plugin\views\query\Sql
14  */
15 class PostgresqlDateSql implements DateSqlInterface {
16
17   /**
18    * The database connection.
19    *
20    * @var \Drupal\Core\Database\Connection
21    */
22   protected $database;
23
24   /**
25    * An array of PHP-to-PostgreSQL replacement patterns.
26    *
27    * @var array
28    */
29   protected static $replace = [
30     'Y' => 'YYYY',
31     'y' => 'YY',
32     'M' => 'Mon',
33     'm' => 'MM',
34     // No format for Numeric representation of a month, without leading zeros.
35     'n' => 'MM',
36     'F' => 'Month',
37     'D' => 'Dy',
38     'd' => 'DD',
39     'l' => 'Day',
40     // No format for Day of the month without leading zeros.
41     'j' => 'DD',
42     'W' => 'IW',
43     'H' => 'HH24',
44     'h' => 'HH12',
45     'i' => 'MI',
46     's' => 'SS',
47     'A' => 'AM',
48   ];
49
50   /**
51    * Constructs the PostgreSQL-specific date sql class.
52    *
53    * @param \Drupal\Core\Database\Connection $database
54    *   The database connection.
55    */
56   public function __construct(Connection $database) {
57     $this->database = $database;
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function getDateField($field, $string_date) {
64     if ($string_date) {
65       // Ensures compatibility with field offset operation below.
66       return "TO_TIMESTAMP($field, 'YYYY-MM-DD HH24:MI:SS')";
67     }
68     return "TO_TIMESTAMP($field)";
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function getDateFormat($field, $format) {
75     $format = strtr($format, static::$replace);
76     return "TO_CHAR($field, '$format')";
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public function setFieldTimezoneOffset(&$field, $offset) {
83     $field = "($field + INTERVAL '$offset SECONDS')";
84   }
85
86   /**
87    * {@inheritdoc}
88    */
89   public function setTimezoneOffset($offset) {
90     $this->database->query("SET TIME ZONE INTERVAL '$offset' HOUR TO MINUTE");
91   }
92
93 }