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