You are here

function date_sql_handler::sql_field in Date 6.2

Same name and namespace in other branches
  1. 5.2 date_api_sql.inc \date_sql_handler::sql_field()
  2. 6 date_api_sql.inc \date_sql_handler::sql_field()
  3. 7.3 date_api/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

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

$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

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_sql.inc
Create a where clause to compare a complete date field to a complete date value.
date_sql_handler::sql_where_extract in ./date_api_sql.inc
Create a where clause to compare an extracted part of a field to an integer value.
date_sql_handler::sql_where_format in ./date_api_sql.inc
Create a where clause to compare a formated field to a formated value.

File

./date_api_sql.inc, line 165
SQL date functions.

Class

date_sql_handler
A class to manipulate date SQL.

Code

function sql_field($field, $offset = NULL) {
  if (drupal_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':
    case 'mysqli':
      switch ($this->date_type) {
        case DATE_UNIX:
          $field = "FROM_UNIXTIME({$field})";
          break;
        case DATE_ISO:
          if (version_compare(db_version(), '4.1.1', '>=')) {
            $field = "STR_TO_DATE({$field}, '%Y-%m-%%dT%T')";
          }
          else {
            $field = "REPLACE({$field}, 'T', ' ')";
          }
          break;
        case DATE_DATETIME:
          break;
      }
      break;
    case 'pgsql':
      switch ($this->date_type) {
        case DATE_UNIX:
          $field = "{$field}::ABSTIME";
          break;
        case DATE_ISO:
          $field = "TO_DATE({$field}, 'FMYYYY-FMMM-FMDDTFMHH24:FMMI:FMSS')";
          break;
        case DATE_DATETIME:
          break;
      }
      break;
  }

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