Révision 599a39cd
Ajouté par Assos Assos il y a environ 3 ans
drupal7/sites/all/modules/date/date_api/date_api_sql.inc | ||
---|---|---|
4 | 4 |
* @file |
5 | 5 |
* SQL helper for Date API. |
6 | 6 |
* |
7 |
* @TODO
|
|
7 |
* @todo
|
|
8 | 8 |
* Add experimental support for sqlite: http://www.sqlite.org/lang_datefunc.html |
9 | 9 |
* and Oracle (http://psoug.org/reference/date_func.html and |
10 | 10 |
* http://psoug.org/reference/datatypes.html) date/time functions. |
... | ... | |
19 | 19 |
* @return string |
20 | 20 |
* Correct sql string for database type. |
21 | 21 |
*/ |
22 |
function date_sql_concat($array) { |
|
22 |
function date_sql_concat(array $array) {
|
|
23 | 23 |
switch (Database::getConnection()->databaseType()) { |
24 | 24 |
case 'mysql': |
25 | 25 |
return "CONCAT(" . implode(",", $array) . ")"; |
... | ... | |
38 | 38 |
* @return string |
39 | 39 |
* SQL statement to return the first non-NULL value in the list. |
40 | 40 |
*/ |
41 |
function date_sql_coalesce($array) { |
|
41 |
function date_sql_coalesce(array $array) {
|
|
42 | 42 |
switch (Database::getConnection()->databaseType()) { |
43 | 43 |
case 'mysql': |
44 | 44 |
case 'pgsql': |
... | ... | |
50 | 50 |
* A helper function to do cross-database padding of date parts. |
51 | 51 |
* |
52 | 52 |
* @param string $str |
53 |
* A string to apply padding to |
|
53 |
* A string to apply padding to.
|
|
54 | 54 |
* @param int $size |
55 |
* The size the final string should be |
|
55 |
* The size the final string should be.
|
|
56 | 56 |
* @param string $pad |
57 |
* The value to pad the string with |
|
57 |
* The value to pad the string with.
|
|
58 | 58 |
* @param string $side |
59 |
* The side of the string to pad |
|
59 |
* The side of the string to pad.
|
|
60 | 60 |
*/ |
61 | 61 |
function date_sql_pad($str, $size = 2, $pad = '0', $side = 'l') { |
62 | 62 |
switch ($side) { |
... | ... | |
71 | 71 |
/** |
72 | 72 |
* A class to manipulate date SQL. |
73 | 73 |
*/ |
74 |
// @codingStandardsIgnoreStart |
|
75 | 74 |
class date_sql_handler { |
76 | 75 |
var $db_type = NULL; |
77 | 76 |
var $date_type = DATE_DATETIME; |
... | ... | |
113 | 112 |
break; |
114 | 113 |
|
115 | 114 |
case 'pgsql': |
116 |
$test = db_query("SELECT '2008-02-15 12:00:00 UTC' AT TIME ZONE 'US/Central'")->fetchField(); |
|
117 |
if ($test == '2008-02-15 06:00:00') { |
|
118 |
$has_support = TRUE; |
|
115 |
// PostgreSQL doesn't always have timezone support enabled, so catch |
|
116 |
// exceptions so they don't break the site. This is safe to do as it |
|
117 |
// is only checking to see if timezones are actually supported. |
|
118 |
try { |
|
119 |
$test = db_query("SELECT '2008-02-15 12:00:00 UTC' AT TIME ZONE 'US/Central'")->fetchField(); |
|
120 |
if ($test == '2008-02-15 06:00:00') { |
|
121 |
$has_support = TRUE; |
|
122 |
} |
|
123 |
} |
|
124 |
catch (PDOException $e) { |
|
125 |
// No support. |
|
119 | 126 |
} |
120 | 127 |
break; |
121 | 128 |
} |
... | ... | |
218 | 225 |
case 'pgsql': |
219 | 226 |
switch ($this->date_type) { |
220 | 227 |
case DATE_UNIX: |
221 |
$field = "$field::ABSTIME";
|
|
228 |
$field = "TO_TIMESTAMP($field)";
|
|
222 | 229 |
break; |
223 | 230 |
|
224 | 231 |
case DATE_ISO: |
... | ... | |
294 | 301 |
* @param int $count |
295 | 302 |
* The number of values to adjust. |
296 | 303 |
* @param string $granularity |
297 |
* The granularity of the adjustment, should be singular, |
|
298 |
* like SECOND, MINUTE, DAY, HOUR.
|
|
304 |
* The granularity of the adjustment, should be singular, like SECOND,
|
|
305 |
* MINUTE, DAY, HOUR. |
|
299 | 306 |
*/ |
300 | 307 |
function sql_date_math($field, $direction, $count, $granularity) { |
301 | 308 |
$granularity = strtoupper($granularity); |
... | ... | |
303 | 310 |
case 'mysql': |
304 | 311 |
switch ($direction) { |
305 | 312 |
case 'ADD': |
306 |
return "DATE_ADD($field, INTERVAL $count $granularity)";
|
|
313 |
return "DATE_ADD(CAST($field AS DATETIME), INTERVAL $count $granularity)";
|
|
307 | 314 |
|
308 | 315 |
case 'SUB': |
309 |
return "DATE_SUB($field, INTERVAL $count $granularity)";
|
|
316 |
return "DATE_SUB(CAST($field AS DATETIME), INTERVAL $count $granularity)";
|
|
310 | 317 |
} |
311 | 318 |
|
312 | 319 |
case 'pgsql': |
... | ... | |
332 | 339 |
} |
333 | 340 |
|
334 | 341 |
/** |
335 |
* Select a date value from the database, adjusting the value |
|
336 |
* for the timezone. |
|
342 |
* Select a date value from the database, adjusting for the timezone. |
|
337 | 343 |
* |
338 |
* Check whether database timezone conversion is supported in |
|
339 |
* this system and use it if possible, otherwise use an |
|
340 |
* offset. |
|
344 |
* Check whether database timezone conversion is supported in this system and |
|
345 |
* use it if possible, otherwise use an offset. |
|
341 | 346 |
* |
342 | 347 |
* @param string $field |
343 | 348 |
* The field to be adjusted. |
344 | 349 |
* @param bool $offset |
345 |
* Set a fixed offset or offset field to use for the date. |
|
346 |
* If set, no timezone conversion will be done and the |
|
347 |
* offset will be used. |
|
350 |
* Set a fixed offset or offset field to use for the date. If set, no |
|
351 |
* timezone conversion will be done and the offset will be used. |
|
348 | 352 |
*/ |
349 | 353 |
function sql_tz($field, $offset = NULL, $comp_date = NULL) { |
350 |
// If the timezones are values they need to be quoted, but |
|
351 |
// if they are field names they do not.
|
|
354 |
// If the timezones are values they need to be quoted, but if they are
|
|
355 |
// field names they do not. |
|
352 | 356 |
$db_zone = !empty($this->db_timezone_field) ? $this->db_timezone_field : "'{$this->db_timezone}'"; |
353 | 357 |
$localzone = !empty($this->local_timezone_field) ? $this->local_timezone_field : "'{$this->local_timezone}'"; |
354 | 358 |
// If a fixed offset is required, use it. |
... | ... | |
359 | 363 |
elseif ($db_zone == $localzone) { |
360 | 364 |
return $this->sql_offset($field, 0); |
361 | 365 |
} |
362 |
// If the db has no timezone support, adjust by the offset, |
|
363 |
// could be either a field name or a value.
|
|
366 |
// If the db has no timezone support, adjust by the offset, could be either
|
|
367 |
// a field name or a value. |
|
364 | 368 |
elseif (!$this->db_tz_support() || empty($localzone)) { |
365 | 369 |
if (!empty($this->offset_field)) { |
366 | 370 |
return $this->sql_offset($field, $this->offset_field); |
... | ... | |
441 | 445 |
's' => 'SS', |
442 | 446 |
'A' => 'AM', |
443 | 447 |
'\T' => '"T"', |
444 |
// '\W' => // TODO, what should this be?
|
|
448 |
// '\W' => // @todo what should this be?
|
|
445 | 449 |
); |
446 | 450 |
$format = strtr($format, $replace); |
447 | 451 |
return "TO_CHAR($field, '$format')"; |
... | ... | |
584 | 588 |
case 'pgsql': |
585 | 589 |
return "EXTRACT(WEEK FROM($field))"; |
586 | 590 |
} |
591 |
|
|
587 | 592 |
case 'DOW': |
588 | 593 |
switch ($this->db_type) { |
589 | 594 |
case 'mysql': |
... | ... | |
594 | 599 |
case 'pgsql': |
595 | 600 |
return "EXTRACT(DOW FROM($field))"; |
596 | 601 |
} |
602 |
|
|
597 | 603 |
case 'DOY': |
598 | 604 |
switch ($this->db_type) { |
599 | 605 |
case 'mysql': |
... | ... | |
636 | 642 |
date_modify($date, $adjustment . ' seconds'); |
637 | 643 |
} |
638 | 644 |
// When comparing a field to a date we can avoid doing timezone |
639 |
// conversion by altering the comparison date to the db timezone. |
|
640 |
// This won't work if the timezone is a field instead of a value.
|
|
645 |
// conversion by altering the comparison date to the db timezone. This
|
|
646 |
// won't work if the timezone is a field instead of a value. |
|
641 | 647 |
if (empty($this->db_timezone_field) && empty($this->local_timezone_field) && $this->db_timezone_field != $this->local_timezone_field) { |
642 | 648 |
$date->setTimezone(timezone_open($this->db_timezone)); |
643 | 649 |
$this->local_timezone = $this->db_timezone; |
... | ... | |
706 | 712 |
|
707 | 713 |
/** |
708 | 714 |
* An array of all date parts, |
709 |
* optionally limited to an array of allowed parts. |
|
715 |
* |
|
716 |
* @param bool $limit |
|
717 |
* Limit to an array of allowed parts. |
|
710 | 718 |
*/ |
711 | 719 |
function date_parts($limit = NULL) { |
712 | 720 |
$parts = array( |
... | ... | |
738 | 746 |
* 'min', 'max', 'format', 'sep', 'empty_now', 'empty_min', 'empty_max' . |
739 | 747 |
* Returns all info if empty. |
740 | 748 |
* @param string $part |
741 |
* 'year', 'month', 'day', 'hour', 'minute', or 'second. |
|
742 |
* returns info for all parts if empty.
|
|
749 |
* 'year', 'month', 'day', 'hour', 'minute', or 'second. Returns info for
|
|
750 |
* all parts if empty. |
|
743 | 751 |
*/ |
744 | 752 |
function part_info($op = NULL, $part = NULL) { |
745 | 753 |
$info = array(); |
... | ... | |
813 | 821 |
/** |
814 | 822 |
* Create a complete date/time value out of an incomplete array of values. |
815 | 823 |
* |
816 |
* For example, array('year' => 2008, 'month' => 05) will fill |
|
817 |
* in the day, hour, minute and second with the earliest possible
|
|
818 |
* values if type = 'min', the latest possible values if type = 'max',
|
|
819 |
* and the current values if type = 'now' .
|
|
824 |
* For example, array('year' => 2008, 'month' => 05) will fill in the day,
|
|
825 |
* hour, minute and second with the earliest possible values if type = 'min',
|
|
826 |
* the latest possible values if type = 'max', and the current values if type
|
|
827 |
* = 'now'.
|
|
820 | 828 |
*/ |
821 | 829 |
function complete_date($selected, $type = 'now') { |
822 | 830 |
if (empty($selected)) { |
... | ... | |
1119 | 1127 |
/** |
1120 | 1128 |
* Granularity arguments handler. |
1121 | 1129 |
* |
1122 |
* Use the parsed values from the ISO argument |
|
1123 |
* to determine the granularity of this period.
|
|
1130 |
* Use the parsed values from the ISO argument to determine the granularity
|
|
1131 |
* of this period. |
|
1124 | 1132 |
*/ |
1125 | 1133 |
function arg_granularity($arg) { |
1126 | 1134 |
$granularity = ''; |
... | ... | |
1133 | 1141 |
} |
1134 | 1142 |
|
1135 | 1143 |
/** |
1136 |
* Use the parsed values from the ISO argument to determine the |
|
1137 |
* min and max date for this period. |
|
1144 |
* Determine the min and max date for this period. |
|
1145 |
* |
|
1146 |
* Uses the parsed values from the ISO argument. |
|
1138 | 1147 |
*/ |
1139 | 1148 |
function arg_range($arg) { |
1140 | 1149 |
// Parse the argument to get its parts. |
... | ... | |
1143 | 1152 |
// Build a range from a period-only argument (assumes the min date is now.) |
1144 | 1153 |
if (empty($parts[0]['date']) && !empty($parts[0]['period']) && (empty($parts[1]))) { |
1145 | 1154 |
$min_date = date_now(); |
1146 |
$max_date = clone($min_date);
|
|
1155 |
$max_date = clone $min_date;
|
|
1147 | 1156 |
foreach ($parts[0]['period'] as $part => $value) { |
1148 | 1157 |
date_modify($max_date, "+$value $part"); |
1149 | 1158 |
} |
... | ... | |
1153 | 1162 |
// Build a range from a period to period argument. |
1154 | 1163 |
if (empty($parts[0]['date']) && !empty($parts[0]['period']) && !empty($parts[1]['period'])) { |
1155 | 1164 |
$min_date = date_now(); |
1156 |
$max_date = clone($min_date);
|
|
1165 |
$max_date = clone $min_date;
|
|
1157 | 1166 |
foreach ($parts[0]['period'] as $part => $value) { |
1158 | 1167 |
date_modify($min_date, "+$value $part"); |
1159 | 1168 |
} |
... | ... | |
1176 | 1185 |
// Build a range from start date + period. |
1177 | 1186 |
elseif (!empty($parts[1]['period'])) { |
1178 | 1187 |
foreach ($parts[1]['period'] as $part => $value) { |
1179 |
$max_date = clone($min_date);
|
|
1188 |
$max_date = clone $min_date;
|
|
1180 | 1189 |
date_modify($max_date, "+$value $part"); |
1181 | 1190 |
} |
1182 | 1191 |
date_modify($max_date, '-1 second'); |
... | ... | |
1204 | 1213 |
return array($now, $now); |
1205 | 1214 |
} |
1206 | 1215 |
} |
1207 |
// @codingStandardsIgnoreEnd |
Formats disponibles : Unified diff
Weekly update of contrib modules