[4410] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
| 3 | // | Piwigo - a PHP based picture gallery | |
---|
| 4 | // +-----------------------------------------------------------------------+ |
---|
[5196] | 5 | // | Copyright(C) 2008-2010 Piwigo Team http://piwigo.org | |
---|
[4410] | 6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
| 7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
| 8 | // +-----------------------------------------------------------------------+ |
---|
| 9 | // | This program is free software; you can redistribute it and/or modify | |
---|
| 10 | // | it under the terms of the GNU General Public License as published by | |
---|
| 11 | // | the Free Software Foundation | |
---|
| 12 | // | | |
---|
| 13 | // | This program is distributed in the hope that it will be useful, but | |
---|
| 14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 16 | // | General Public License for more details. | |
---|
| 17 | // | | |
---|
| 18 | // | You should have received a copy of the GNU General Public License | |
---|
| 19 | // | along with this program; if not, write to the Free Software | |
---|
| 20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 21 | // | USA. | |
---|
| 22 | // +-----------------------------------------------------------------------+ |
---|
| 23 | |
---|
| 24 | define('REQUIRED_PGSQL_VERSION', '8.0'); |
---|
| 25 | define('DB_ENGINE', 'PostgreSQL'); |
---|
| 26 | |
---|
| 27 | define('DB_REGEX_OPERATOR', '~'); |
---|
| 28 | define('DB_RANDOM_FUNCTION', 'RANDOM'); |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * |
---|
| 32 | * simple functions |
---|
| 33 | * |
---|
| 34 | */ |
---|
| 35 | |
---|
[5236] | 36 | function pwg_db_connect($host, $user, $password, $database) |
---|
[4410] | 37 | { |
---|
| 38 | $connection_string = ''; |
---|
| 39 | if (strpos($host,':') !== false) |
---|
| 40 | { |
---|
| 41 | list($host, $port) = explode(':', $host); |
---|
| 42 | } |
---|
| 43 | $connection_string = sprintf('host=%s', $host); |
---|
| 44 | if (!empty($port)) |
---|
| 45 | { |
---|
| 46 | $connection_string .= sprintf(' port=%d', $port); |
---|
| 47 | } |
---|
| 48 | $connection_string .= sprintf(' user=%s password=%s dbname=%s', |
---|
| 49 | $user, |
---|
| 50 | $password, |
---|
| 51 | $database); |
---|
[5267] | 52 | $link = @pg_connect($connection_string); |
---|
[5236] | 53 | if (!$link) |
---|
| 54 | { |
---|
| 55 | throw new Exception("Can't connect to server"); |
---|
| 56 | } |
---|
| 57 | else |
---|
| 58 | { |
---|
| 59 | return $link; |
---|
| 60 | } |
---|
[4410] | 61 | } |
---|
| 62 | |
---|
[6090] | 63 | function pwg_db_check_version() |
---|
| 64 | { |
---|
| 65 | $current_version = pwg_get_db_version(); |
---|
| 66 | if (version_compare($current_version, REQUIRED_PGSQL_VERSION, '<')) |
---|
| 67 | { |
---|
| 68 | fatal_error( |
---|
| 69 | sprintf( |
---|
| 70 | 'your database version is too old, you have "%s" and you need at least "%s"', |
---|
| 71 | $current_version, |
---|
| 72 | REQUIRED_PGSQL_VERSION |
---|
| 73 | ) |
---|
| 74 | ); |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | |
---|
[4410] | 78 | function pwg_db_check_charset() |
---|
| 79 | { |
---|
| 80 | return true; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | function pwg_get_db_version() |
---|
| 84 | { |
---|
| 85 | list($pg_version) = pwg_db_fetch_row(pwg_query('SHOW SERVER_VERSION;')); |
---|
| 86 | |
---|
| 87 | return $pg_version; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | function pwg_query($query) |
---|
| 91 | { |
---|
| 92 | global $conf,$page,$debug,$t2; |
---|
| 93 | |
---|
[4886] | 94 | $replace_pattern = '`REPLACE INTO\s(\S*)\s*([^)]*\))\s*VALUES\(([^,]*),(.*)\)\s*`mi'; |
---|
| 95 | |
---|
[4410] | 96 | $start = get_moment(); |
---|
| 97 | |
---|
[4886] | 98 | if (preg_match($replace_pattern, $query, $matches) |
---|
| 99 | && $matches[1]==SESSIONS_TABLE) |
---|
| 100 | { |
---|
| 101 | $select_query = ' |
---|
| 102 | SELECT id FROM '.$matches[1].' |
---|
| 103 | WHERE id='.$matches[3]; |
---|
| 104 | ( $result = pg_query($select_query)) or die($query."\n<br>".pg_last_error()); |
---|
| 105 | if (pwg_db_num_rows($result)==1) |
---|
| 106 | { |
---|
| 107 | $query = ' |
---|
| 108 | UPDATE '.$matches[1].' |
---|
| 109 | SET expiration=now() |
---|
| 110 | WHERE id='.$matches[3]; |
---|
| 111 | } |
---|
| 112 | else |
---|
| 113 | { |
---|
| 114 | $query = ' |
---|
| 115 | INSERT INTO '.$matches[1].' |
---|
| 116 | '.$matches[2].' VALUES('.$matches[3].','.$matches[4].')'; |
---|
| 117 | } |
---|
| 118 | ( $result = pg_query($query)) or die($query."\n<br>".pg_last_error()); |
---|
| 119 | } |
---|
| 120 | else |
---|
| 121 | { |
---|
| 122 | ($result = pg_query($query)) or die($query."\n<br>".pg_last_error()); |
---|
| 123 | } |
---|
| 124 | |
---|
[4410] | 125 | $time = get_moment() - $start; |
---|
| 126 | |
---|
| 127 | if (!isset($page['count_queries'])) |
---|
| 128 | { |
---|
| 129 | $page['count_queries'] = 0; |
---|
| 130 | $page['queries_time'] = 0; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | $page['count_queries']++; |
---|
| 134 | $page['queries_time']+= $time; |
---|
| 135 | |
---|
| 136 | if ($conf['show_queries']) |
---|
| 137 | { |
---|
| 138 | $output = ''; |
---|
| 139 | $output.= '<pre>['.$page['count_queries'].'] '; |
---|
| 140 | $output.= "\n".$query; |
---|
| 141 | $output.= "\n".'(this query time : '; |
---|
| 142 | $output.= '<b>'.number_format($time, 3, '.', ' ').' s)</b>'; |
---|
| 143 | $output.= "\n".'(total SQL time : '; |
---|
| 144 | $output.= number_format($page['queries_time'], 3, '.', ' ').' s)'; |
---|
| 145 | $output.= "\n".'(total time : '; |
---|
| 146 | $output.= number_format( ($time+$start-$t2), 3, '.', ' ').' s)'; |
---|
| 147 | if ( $result!=null and preg_match('/\s*SELECT\s+/i',$query) ) |
---|
| 148 | { |
---|
| 149 | $output.= "\n".'(num rows : '; |
---|
| 150 | $output.= pwg_db_num_rows($result).' )'; |
---|
| 151 | } |
---|
| 152 | elseif ( $result!=null |
---|
| 153 | and preg_match('/\s*INSERT|UPDATE|REPLACE|DELETE\s+/i',$query) ) |
---|
| 154 | { |
---|
| 155 | $output.= "\n".'(affected rows : '; |
---|
| 156 | $output.= pwg_db_changes($result).' )'; |
---|
| 157 | } |
---|
| 158 | $output.= "</pre>\n"; |
---|
| 159 | |
---|
| 160 | $debug .= $output; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | return $result; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | function pwg_db_nextval($column, $table) |
---|
| 167 | { |
---|
| 168 | $query = ' |
---|
| 169 | SELECT nextval(\''.$table.'_'.$column.'_seq\')'; |
---|
| 170 | list($next) = pwg_db_fetch_row(pwg_query($query)); |
---|
| 171 | |
---|
| 172 | return $next; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | /** |
---|
| 176 | * |
---|
| 177 | * complex functions |
---|
| 178 | * |
---|
| 179 | */ |
---|
| 180 | |
---|
| 181 | function pwg_db_changes($result) |
---|
| 182 | { |
---|
| 183 | return pg_affected_rows($result); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | function pwg_db_num_rows($result) |
---|
| 187 | { |
---|
| 188 | return pg_num_rows($result); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | function pwg_db_fetch_assoc($result) |
---|
| 192 | { |
---|
| 193 | return pg_fetch_assoc($result); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | function pwg_db_fetch_row($result) |
---|
| 197 | { |
---|
| 198 | return pg_fetch_row($result); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | function pwg_db_fetch_object($result) |
---|
| 202 | { |
---|
| 203 | return pg_fetch_object($result); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | function pwg_db_free_result($result) |
---|
| 207 | { |
---|
| 208 | return pg_free_result($result); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | function pwg_db_real_escape_string($s) |
---|
| 212 | { |
---|
| 213 | return pg_escape_string($s); |
---|
| 214 | } |
---|
| 215 | |
---|
[4892] | 216 | function pwg_db_insert_id($table=null, $column='id') |
---|
[4410] | 217 | { |
---|
[4892] | 218 | $sequence = sprintf('%s_%s_seq', strtolower($table), $column); |
---|
| 219 | $query = ' |
---|
| 220 | SELECT CURRVAL(\''.$sequence.'\');'; |
---|
| 221 | |
---|
| 222 | list($id) = pwg_db_fetch_row(pwg_query($query)); |
---|
| 223 | |
---|
| 224 | return $id; |
---|
[4410] | 225 | } |
---|
| 226 | |
---|
| 227 | /** |
---|
| 228 | * |
---|
| 229 | * complex functions |
---|
| 230 | * |
---|
| 231 | */ |
---|
| 232 | |
---|
| 233 | /** |
---|
| 234 | * creates an array based on a query, this function is a very common pattern |
---|
| 235 | * used here |
---|
| 236 | * |
---|
| 237 | * @param string $query |
---|
| 238 | * @param string $fieldname |
---|
| 239 | * @return array |
---|
| 240 | */ |
---|
| 241 | function array_from_query($query, $fieldname) |
---|
| 242 | { |
---|
| 243 | $array = array(); |
---|
| 244 | |
---|
| 245 | $result = pwg_query($query); |
---|
| 246 | while ($row = pg_fetch_assoc($result)) |
---|
| 247 | { |
---|
| 248 | array_push($array, $row[$fieldname]); |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | return $array; |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | define('MASS_UPDATES_SKIP_EMPTY', 1); |
---|
| 255 | /** |
---|
| 256 | * updates multiple lines in a table |
---|
| 257 | * |
---|
| 258 | * @param string table_name |
---|
| 259 | * @param array dbfields |
---|
| 260 | * @param array datas |
---|
| 261 | * @param int flags - if MASS_UPDATES_SKIP_EMPTY - empty values do not overwrite existing ones |
---|
| 262 | * @return void |
---|
| 263 | */ |
---|
| 264 | function mass_updates($tablename, $dbfields, $datas, $flags=0) |
---|
| 265 | { |
---|
| 266 | if (count($datas) == 0) |
---|
| 267 | return; |
---|
[4781] | 268 | |
---|
[4410] | 269 | if (count($datas) < 10) |
---|
[4781] | 270 | { |
---|
[4410] | 271 | foreach ($datas as $data) |
---|
| 272 | { |
---|
| 273 | $query = ' |
---|
| 274 | UPDATE '.$tablename.' |
---|
| 275 | SET '; |
---|
| 276 | $is_first = true; |
---|
| 277 | foreach ($dbfields['update'] as $key) |
---|
| 278 | { |
---|
| 279 | $separator = $is_first ? '' : ",\n "; |
---|
| 280 | |
---|
| 281 | if (isset($data[$key]) and $data[$key] != '') |
---|
| 282 | { |
---|
| 283 | $query.= $separator.$key.' = \''.$data[$key].'\''; |
---|
| 284 | } |
---|
| 285 | else |
---|
| 286 | { |
---|
| 287 | if ($flags & MASS_UPDATES_SKIP_EMPTY ) |
---|
| 288 | continue; // next field |
---|
| 289 | $query.= "$separator$key = NULL"; |
---|
| 290 | } |
---|
| 291 | $is_first = false; |
---|
| 292 | } |
---|
| 293 | if (!$is_first) |
---|
| 294 | {// only if one field at least updated |
---|
| 295 | $query.= ' |
---|
| 296 | WHERE '; |
---|
| 297 | $is_first = true; |
---|
| 298 | foreach ($dbfields['primary'] as $key) |
---|
| 299 | { |
---|
| 300 | if (!$is_first) |
---|
| 301 | { |
---|
| 302 | $query.= ' AND '; |
---|
| 303 | } |
---|
| 304 | if ( isset($data[$key]) ) |
---|
| 305 | { |
---|
| 306 | $query.= $key.' = \''.$data[$key].'\''; |
---|
| 307 | } |
---|
| 308 | else |
---|
| 309 | { |
---|
| 310 | $query.= $key.' IS NULL'; |
---|
| 311 | } |
---|
| 312 | $is_first = false; |
---|
| 313 | } |
---|
| 314 | pwg_query($query); |
---|
| 315 | } |
---|
| 316 | } // foreach update |
---|
[6511] | 317 | } |
---|
[4410] | 318 | else |
---|
| 319 | { |
---|
| 320 | $all_fields = array_merge($dbfields['primary'], $dbfields['update']); |
---|
| 321 | $temporary_tablename = $tablename.'_'.micro_seconds(); |
---|
| 322 | $query = ' |
---|
| 323 | CREATE TABLE '.$temporary_tablename.' |
---|
| 324 | AS SELECT * FROM '.$tablename.' WHERE 1=2'; |
---|
| 325 | |
---|
| 326 | pwg_query($query); |
---|
| 327 | mass_inserts($temporary_tablename, $all_fields, $datas); |
---|
| 328 | if ( $flags & MASS_UPDATES_SKIP_EMPTY ) |
---|
[6511] | 329 | $func_set = create_function('$s', 'return "$s = NULLIF(t2.$s, '.$tablename.'.$s)";'); |
---|
[4410] | 330 | else |
---|
| 331 | $func_set = create_function('$s', 'return "$s = t2.$s";'); |
---|
| 332 | |
---|
| 333 | // update of images table by joining with temporary table |
---|
| 334 | $query = ' |
---|
| 335 | UPDATE '.$tablename.' |
---|
| 336 | SET '. |
---|
| 337 | implode( |
---|
| 338 | "\n , ", |
---|
| 339 | array_map($func_set, $dbfields['update']) |
---|
| 340 | ).' |
---|
| 341 | FROM '.$temporary_tablename.' AS t2 |
---|
| 342 | WHERE '. |
---|
| 343 | implode( |
---|
| 344 | "\n AND ", |
---|
| 345 | array_map( |
---|
[6511] | 346 | create_function('$s', 'return "'.$tablename.'.$s = t2.$s";'), |
---|
[4410] | 347 | $dbfields['primary'] |
---|
| 348 | ) |
---|
| 349 | ); |
---|
| 350 | pwg_query($query); |
---|
| 351 | $query = ' |
---|
| 352 | DROP TABLE '.$temporary_tablename; |
---|
| 353 | pwg_query($query); |
---|
| 354 | } |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | |
---|
| 358 | /** |
---|
| 359 | * inserts multiple lines in a table |
---|
| 360 | * |
---|
| 361 | * @param string table_name |
---|
| 362 | * @param array dbfields |
---|
| 363 | * @param array inserts |
---|
| 364 | * @return void |
---|
| 365 | */ |
---|
| 366 | |
---|
| 367 | function mass_inserts($table_name, $dbfields, $datas) |
---|
| 368 | { |
---|
| 369 | if (count($datas) != 0) |
---|
| 370 | { |
---|
| 371 | $first = true; |
---|
| 372 | |
---|
| 373 | $packet_size = 16777216; |
---|
| 374 | $packet_size = $packet_size - 2000; // The last list of values MUST not exceed 2000 character*/ |
---|
| 375 | $query = ''; |
---|
| 376 | |
---|
| 377 | foreach ($datas as $insert) |
---|
| 378 | { |
---|
| 379 | if (strlen($query) >= $packet_size) |
---|
| 380 | { |
---|
| 381 | pwg_query($query); |
---|
| 382 | $first = true; |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | if ($first) |
---|
| 386 | { |
---|
| 387 | $query = ' |
---|
| 388 | INSERT INTO '.$table_name.' |
---|
| 389 | ('.implode(',', $dbfields).') |
---|
| 390 | VALUES'; |
---|
| 391 | $first = false; |
---|
| 392 | } |
---|
| 393 | else |
---|
| 394 | { |
---|
| 395 | $query .= ' |
---|
| 396 | , '; |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | $query .= '('; |
---|
| 400 | foreach ($dbfields as $field_id => $dbfield) |
---|
| 401 | { |
---|
| 402 | if ($field_id > 0) |
---|
| 403 | { |
---|
| 404 | $query .= ','; |
---|
| 405 | } |
---|
| 406 | |
---|
| 407 | if (!isset($insert[$dbfield]) or $insert[$dbfield] === '') |
---|
| 408 | { |
---|
| 409 | $query .= 'NULL'; |
---|
| 410 | } |
---|
| 411 | else |
---|
| 412 | { |
---|
| 413 | $query .= "'".$insert[$dbfield]."'"; |
---|
| 414 | } |
---|
| 415 | } |
---|
| 416 | $query .= ')'; |
---|
| 417 | } |
---|
| 418 | pwg_query($query); |
---|
| 419 | } |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | /** |
---|
| 423 | * Do maintenance on all PWG tables |
---|
| 424 | * |
---|
| 425 | * @return none |
---|
| 426 | */ |
---|
| 427 | function do_maintenance_all_tables() |
---|
| 428 | { |
---|
| 429 | global $prefixeTable, $page; |
---|
| 430 | |
---|
| 431 | $all_tables = array(); |
---|
| 432 | |
---|
| 433 | // List all tables |
---|
[4781] | 434 | $query = 'SELECT tablename FROM pg_tables |
---|
| 435 | WHERE tablename like \''.$prefixeTable.'%\''; |
---|
[4410] | 436 | |
---|
[4781] | 437 | $all_tables = array_from_query($query, 'tablename'); |
---|
[4410] | 438 | |
---|
| 439 | // Optimize all tables |
---|
[4781] | 440 | foreach ($all_tables as $table) |
---|
[4410] | 441 | { |
---|
[4781] | 442 | $query = 'VACUUM FULL '.$table; |
---|
| 443 | pwg_query($query); |
---|
[4410] | 444 | } |
---|
[4781] | 445 | array_push($page['infos'], |
---|
[5036] | 446 | l10n('All optimizations have been successfully completed.') |
---|
[4781] | 447 | ); |
---|
[4410] | 448 | } |
---|
| 449 | |
---|
[4833] | 450 | function pwg_db_concat($array) |
---|
| 451 | { |
---|
| 452 | $string = implode($array, ','); |
---|
| 453 | return 'ARRAY_TO_STRING(ARRAY['.$string.'])'; |
---|
| 454 | } |
---|
| 455 | |
---|
[4781] | 456 | function pwg_db_concat_ws($array, $separator) |
---|
[4410] | 457 | { |
---|
[4781] | 458 | $string = implode($array, ','); |
---|
| 459 | return 'ARRAY_TO_STRING(ARRAY['.$string.'],\''.$separator.'\')'; |
---|
[4410] | 460 | } |
---|
| 461 | |
---|
| 462 | function pwg_db_cast_to_text($string) |
---|
| 463 | { |
---|
| 464 | return 'CAST('.$string.' AS TEXT)'; |
---|
| 465 | } |
---|
| 466 | |
---|
| 467 | /** |
---|
| 468 | * returns an array containing the possible values of an enum field |
---|
| 469 | * |
---|
| 470 | * @param string tablename |
---|
| 471 | * @param string fieldname |
---|
| 472 | */ |
---|
| 473 | function get_enums($table, $field) |
---|
| 474 | { |
---|
| 475 | $typname = preg_replace('/'.$GLOBALS['prefixeTable'].'/', '', $table); |
---|
| 476 | $typname .= '_' . $field; |
---|
| 477 | |
---|
| 478 | $query = 'SELECT |
---|
| 479 | enumlabel FROM pg_enum JOIN pg_type |
---|
| 480 | ON pg_enum.enumtypid=pg_type.oid |
---|
| 481 | WHERE typname=\''.$typname.'\' |
---|
| 482 | '; |
---|
| 483 | $result = pwg_query($query); |
---|
| 484 | while ($row = pwg_db_fetch_assoc($result)) |
---|
| 485 | { |
---|
| 486 | $options[] = $row['enumlabel']; |
---|
| 487 | } |
---|
| 488 | |
---|
| 489 | return $options; |
---|
| 490 | } |
---|
| 491 | |
---|
| 492 | // get_boolean transforms a string to a boolean value. If the string is |
---|
| 493 | // "false" (case insensitive), then the boolean value false is returned. In |
---|
| 494 | // any other case, true is returned. |
---|
| 495 | function get_boolean( $string ) |
---|
| 496 | { |
---|
| 497 | $boolean = true; |
---|
| 498 | if ('f' == $string || 'false' == $string) |
---|
| 499 | { |
---|
| 500 | $boolean = false; |
---|
| 501 | } |
---|
| 502 | return $boolean; |
---|
| 503 | } |
---|
| 504 | |
---|
| 505 | /** |
---|
| 506 | * returns boolean string 'true' or 'false' if the given var is boolean |
---|
| 507 | * |
---|
| 508 | * @param mixed $var |
---|
| 509 | * @return mixed |
---|
| 510 | */ |
---|
| 511 | function boolean_to_string($var) |
---|
| 512 | { |
---|
[6342] | 513 | if (is_bool($var)) |
---|
[4410] | 514 | { |
---|
[6342] | 515 | return $var ? 'true' : 'false'; |
---|
[4410] | 516 | } |
---|
| 517 | else |
---|
| 518 | { |
---|
[6342] | 519 | return $var; |
---|
[4410] | 520 | } |
---|
| 521 | } |
---|
| 522 | |
---|
| 523 | /** |
---|
| 524 | * |
---|
| 525 | * interval and date functions |
---|
| 526 | * |
---|
| 527 | */ |
---|
| 528 | |
---|
| 529 | function pwg_db_get_recent_period_expression($period, $date='CURRENT_DATE') |
---|
| 530 | { |
---|
| 531 | if ($date!='CURRENT_DATE') |
---|
| 532 | { |
---|
| 533 | $date = '\''.$date.'\'::date'; |
---|
| 534 | } |
---|
| 535 | |
---|
| 536 | return '('.$date.' - \''.$period.' DAY\'::interval)::date'; |
---|
| 537 | } |
---|
| 538 | |
---|
| 539 | function pwg_db_get_recent_period($period, $date='CURRENT_DATE') |
---|
| 540 | { |
---|
| 541 | $query = 'select '.pwg_db_get_recent_period_expression($period, $date); |
---|
| 542 | list($d) = pwg_db_fetch_row(pwg_query($query)); |
---|
| 543 | |
---|
| 544 | return $d; |
---|
| 545 | } |
---|
| 546 | |
---|
[6579] | 547 | function pwg_db_get_hour($date) |
---|
| 548 | { |
---|
| 549 | return 'EXTRACT(HOUR FROM '.$date.')'; |
---|
| 550 | } |
---|
| 551 | |
---|
[4410] | 552 | function pwg_db_get_date_YYYYMM($date) |
---|
| 553 | { |
---|
| 554 | return 'TO_CHAR('.$date.', \'YYYYMM\')'; |
---|
| 555 | } |
---|
| 556 | |
---|
| 557 | function pwg_db_get_date_MMDD($date) |
---|
| 558 | { |
---|
| 559 | return 'TO_CHAR('.$date.', \'MMDD\')'; |
---|
| 560 | } |
---|
| 561 | |
---|
| 562 | function pwg_db_get_year($date) |
---|
| 563 | { |
---|
| 564 | return 'EXTRACT(YEAR FROM '.$date.')'; |
---|
| 565 | } |
---|
| 566 | |
---|
| 567 | function pwg_db_get_month($date) |
---|
| 568 | { |
---|
| 569 | return 'EXTRACT(MONTH FROM '.$date.')'; |
---|
| 570 | } |
---|
| 571 | |
---|
| 572 | function pwg_db_get_week($date, $mode=null) |
---|
| 573 | { |
---|
| 574 | return 'EXTRACT(WEEK FROM '.$date.')'; |
---|
| 575 | } |
---|
| 576 | |
---|
| 577 | function pwg_db_get_dayofmonth($date) |
---|
| 578 | { |
---|
| 579 | return 'EXTRACT(DAY FROM '.$date.')'; |
---|
| 580 | } |
---|
| 581 | |
---|
| 582 | function pwg_db_get_dayofweek($date) |
---|
| 583 | { |
---|
| 584 | return 'EXTRACT(DOW FROM '.$date.')::INTEGER - 1'; |
---|
| 585 | } |
---|
| 586 | |
---|
| 587 | function pwg_db_get_weekday($date) |
---|
| 588 | { |
---|
| 589 | return 'EXTRACT(ISODOW FROM '.$date.')::INTEGER - 1'; |
---|
| 590 | } |
---|
| 591 | |
---|
| 592 | // my_error returns (or send to standard output) the message concerning the |
---|
[6511] | 593 | // error occured for the last pgsql query. |
---|
[4410] | 594 | function my_error($header, $die) |
---|
| 595 | { |
---|
| 596 | $error = '[pgsql error]'.pg_last_error()."\n"; |
---|
| 597 | $error .= $header; |
---|
| 598 | |
---|
| 599 | if ($die) |
---|
| 600 | { |
---|
| 601 | fatal_error($error); |
---|
| 602 | } |
---|
| 603 | echo("<pre>"); |
---|
| 604 | trigger_error($error, E_USER_WARNING); |
---|
| 605 | echo("</pre>"); |
---|
| 606 | } |
---|
| 607 | |
---|
| 608 | |
---|
| 609 | ?> |
---|