class SqlsrvDateSql in Drupal driver for SQL Server and SQL Azure 4.0.x
Same name and namespace in other branches
- 8 src/Plugin/views/query/SqlsrvDateSql.php \Drupal\sqlsrv\Plugin\views\query\SqlsrvDateSql
- 4.2.x src/Plugin/views/query/SqlsrvDateSql.php \Drupal\sqlsrv\Plugin\views\query\SqlsrvDateSql
- 3.0.x src/Plugin/views/query/SqlsrvDateSql.php \Drupal\sqlsrv\Plugin\views\query\SqlsrvDateSql
- 3.1.x src/Plugin/views/query/SqlsrvDateSql.php \Drupal\sqlsrv\Plugin\views\query\SqlsrvDateSql
- 4.1.x src/Plugin/views/query/SqlsrvDateSql.php \Drupal\sqlsrv\Plugin\views\query\SqlsrvDateSql
MSSQL-specific date handling.
@internal This class should only be used by the Views SQL query plugin.
Hierarchy
- class \Drupal\sqlsrv\Plugin\views\query\SqlsrvDateSql implements DateSqlInterface
Expanded class hierarchy of SqlsrvDateSql
See also
\Drupal\views\Plugin\views\query\Sql
1 string reference to 'SqlsrvDateSql'
1 service uses SqlsrvDateSql
File
- src/
Plugin/ views/ query/ SqlsrvDateSql.php, line 15
Namespace
Drupal\sqlsrv\Plugin\views\queryView source
class SqlsrvDateSql implements DateSqlInterface {
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* An array of PHP-to-MSSQL replacement patterns.
*
* @var array
*/
protected static $replace = [
'Y' => 'yyyy',
'y' => 'yy',
'M' => 'MMM',
'm' => 'MM',
'n' => 'M',
'F' => 'MMMM',
'D' => 'ddd',
'd' => 'dd',
'l' => 'ddd',
'j' => 'd',
// No week number format.
'H' => 'HH',
'h' => 'hh',
'i' => 'mm',
's' => 'ss',
'A' => 'tt',
];
/**
* Constructs the MSSQL-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 "CONVERT(datetime2, {$field}, 127)";
}
// Base date field storage is timestamp, so the date to be returned here is
// epoch + stored value (seconds from epoch).
return "DATEADD(second, {$field}, '19700101')";
}
/**
* {@inheritdoc}
*/
public function getDateFormat($field, $format) {
$format = strtr($format, static::$replace);
if ($format === 'W') {
return "DATEPART(iso_week, {$field})";
}
return "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 = "DATEADD(second, {$offset}, {$field})";
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SqlsrvDateSql:: |
protected | property | The database connection. | |
SqlsrvDateSql:: |
protected static | property | An array of PHP-to-MSSQL replacement patterns. | |
SqlsrvDateSql:: |
public | function |
Returns a native database expression for a given field. Overrides DateSqlInterface:: |
|
SqlsrvDateSql:: |
public | function |
Creates a native database date formatting. Overrides DateSqlInterface:: |
|
SqlsrvDateSql:: |
public | function |
Applies the given offset to the given field. Overrides DateSqlInterface:: |
|
SqlsrvDateSql:: |
public | function |
Set the database to the given timezone. Overrides DateSqlInterface:: |
|
SqlsrvDateSql:: |
public | function | Constructs the MSSQL-specific date sql class. |