You are here

class SqlsrvDateSql in Drupal driver for SQL Server and SQL Azure 8

Same name and namespace in other branches
  1. 4.2.x src/Plugin/views/query/SqlsrvDateSql.php \Drupal\sqlsrv\Plugin\views\query\SqlsrvDateSql
  2. 3.0.x src/Plugin/views/query/SqlsrvDateSql.php \Drupal\sqlsrv\Plugin\views\query\SqlsrvDateSql
  3. 3.1.x src/Plugin/views/query/SqlsrvDateSql.php \Drupal\sqlsrv\Plugin\views\query\SqlsrvDateSql
  4. 4.0.x src/Plugin/views/query/SqlsrvDateSql.php \Drupal\sqlsrv\Plugin\views\query\SqlsrvDateSql
  5. 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

Expanded class hierarchy of SqlsrvDateSql

See also

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

1 string reference to 'SqlsrvDateSql'
sqlsrv.services.yml in ./sqlsrv.services.yml
sqlsrv.services.yml
1 service uses SqlsrvDateSql
sqlsrv.views.date_sql in ./sqlsrv.services.yml
Drupal\sqlsrv\Plugin\views\query\SqlsrvDateSql

File

src/Plugin/views/query/SqlsrvDateSql.php, line 15

Namespace

Drupal\sqlsrv\Plugin\views\query
View 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);

    // MS SQL does not have a ISO week substitution string, so it needs special
    // handling.
    // @see http://wikipedia.org/wiki/ISO_week_date#Calculation
    // @see http://stackoverflow.com/a/15511864/1499564
    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

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