You are here

public function date_sql_handler::sql_field in Date 7.3

Same name and namespace in other branches
  1. 5.2 date_api_sql.inc \date_sql_handler::sql_field()
  2. 6.2 date_api_sql.inc \date_sql_handler::sql_field()
  3. 6 date_api_sql.inc \date_sql_handler::sql_field()
  4. 7 date_api/date_api_sql.inc \date_sql_handler::sql_field()
  5. 7.2 date_api/date_api_sql.inc \date_sql_handler::sql_field()

Helper function to create cross-database SQL dates.

Parameters

string $field: The real table and field name, like 'tablename.fieldname' .

string $offset: The name of a field that holds the timezone offset or an offset value. If NULL, the normal Drupal timezone handling will be used, if $offset = 0 no adjustment will be made.

Return value

string An appropriate SQL string for the db type and field type.

3 calls to date_sql_handler::sql_field()
date_sql_handler::sql_where_date in date_api/date_api_sql.inc
Creates a where clause to compare a complete date field to a date value.
date_sql_handler::sql_where_extract in date_api/date_api_sql.inc
Creates a where clause comparing an extracted date part to an integer.
date_sql_handler::sql_where_format in date_api/date_api_sql.inc
Create a where clause to compare a formated field to a formated value.

File

date_api/date_api_sql.inc, line 222
SQL helper for Date API.

Class

date_sql_handler
A class to manipulate date SQL.

Code

public function sql_field($field, $offset = NULL, $comp_date = NULL) {
  if (strtoupper($field) == 'NOW') {

    // NOW() will be in UTC since that is what we set the db timezone to.
    $this->local_timezone = 'UTC';
    return $this
      ->sql_offset('NOW()', $offset);
  }
  switch ($this->db_type) {
    case 'mysql':
      switch ($this->date_type) {
        case DATE_UNIX:
          $field = "FROM_UNIXTIME({$field})";
          break;
        case DATE_ISO:
          $field = "STR_TO_DATE({$field}, '%Y-%m-%dT%T')";
          break;
        case DATE_DATETIME:
          break;
      }
      break;
    case 'pgsql':
      switch ($this->date_type) {
        case DATE_UNIX:
          $field = "TO_TIMESTAMP({$field})";
          break;
        case DATE_ISO:
          $field = "TO_DATE({$field}, 'FMYYYY-FMMM-FMDDTFMHH24:FMMI:FMSS')";
          break;
        case DATE_DATETIME:
          break;
      }
      break;
    case 'sqlite':
      switch ($this->date_type) {
        case DATE_UNIX:
          $field = "datetime({$field}, 'unixepoch')";
          break;
        case DATE_ISO:
        case DATE_DATETIME:
          $field = "datetime({$field})";
          break;
      }
      break;
    case 'sqlsrv':
      switch ($this->date_type) {
        case DATE_UNIX:
          $field = "DATEADD(s, {$field}, '19700101 00:00:00:000')";
          break;
        case DATE_ISO:
        case DATE_DATETIME:
          $field = "CAST({$field} as smalldatetime)";
          break;
      }
      break;
      break;
  }

  // Adjust the resulting value to the right timezone/offset.
  return $this
    ->sql_tz($field, $offset, $comp_date);
}