source: trunk/include/dblayer/functions_pgsql.inc.php @ 6666

Last change on this file since 6666 was 6666, checked in by nikrou, 14 years ago

Bug 1766 fixed : [PostgreSQL] unkown database function UNIX_TIMESTAMP()
Add a new function pwg_db_date_to_ts() to calculate a timestamp from a date

File size: 13.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
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
24define('REQUIRED_PGSQL_VERSION', '8.0');
25define('DB_ENGINE', 'PostgreSQL');
26
27define('DB_REGEX_OPERATOR', '~');
28define('DB_RANDOM_FUNCTION', 'RANDOM');
29
30/**
31 *
32 * simple functions
33 *
34 */
35
36function pwg_db_connect($host, $user, $password, $database)
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);
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  }
61}
62
63function 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
78function pwg_db_check_charset() 
79{
80  return true;
81}
82
83function 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
90function pwg_query($query)
91{
92  global $conf,$page,$debug,$t2;
93
94  $replace_pattern = '`REPLACE INTO\s(\S*)\s*([^)]*\))\s*VALUES\(([^,]*),(.*)\)\s*`mi'; 
95
96  $start = get_moment();
97
98  if (preg_match($replace_pattern, $query, $matches)
99      && $matches[1]==SESSIONS_TABLE)
100  {
101    $select_query = '
102SELECT 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 = '
108UPDATE '.$matches[1].'
109  SET expiration=now()
110  WHERE id='.$matches[3];
111    }
112    else
113    {
114      $query = '
115INSERT 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
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
166function pwg_db_nextval($column, $table)
167{
168  $query = '
169SELECT 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
181function pwg_db_changes($result) 
182{
183  return pg_affected_rows($result);
184}
185
186function pwg_db_num_rows($result) 
187{
188  return pg_num_rows($result);
189}
190
191function pwg_db_fetch_assoc($result)
192{
193  return pg_fetch_assoc($result);
194}
195
196function pwg_db_fetch_row($result)
197{
198  return pg_fetch_row($result);
199}
200
201function pwg_db_fetch_object($result)
202{
203  return pg_fetch_object($result);
204}
205
206function pwg_db_free_result($result) 
207{
208  return pg_free_result($result);
209}
210
211function pwg_db_real_escape_string($s)
212{
213  return pg_escape_string($s);
214}
215
216function pwg_db_insert_id($table=null, $column='id')
217{
218  $sequence = sprintf('%s_%s_seq', strtolower($table), $column);
219  $query = '
220SELECT CURRVAL(\''.$sequence.'\');';
221
222  list($id) = pwg_db_fetch_row(pwg_query($query));
223
224  return $id;
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 */
241function 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
254define('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 */
264function mass_updates($tablename, $dbfields, $datas, $flags=0)
265{
266  if (count($datas) == 0)
267    return;
268
269  if (count($datas) < 10)
270  {
271    foreach ($datas as $data)
272    {
273      $query = '
274UPDATE '.$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
317  } 
318  else
319  {
320    $all_fields = array_merge($dbfields['primary'], $dbfields['update']);
321    $temporary_tablename = $tablename.'_'.micro_seconds();
322    $query = '
323CREATE 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 )
329      $func_set = create_function('$s', 'return "$s = NULLIF(t2.$s, '.$tablename.'.$s)";');
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 = '
335UPDATE '.$tablename.'
336  SET '.
337      implode(
338        "\n    , ",
339        array_map($func_set, $dbfields['update'])
340        ).'
341FROM '.$temporary_tablename.' AS t2
342  WHERE '.
343      implode(
344        "\n    AND ",
345        array_map(
346          create_function('$s', 'return "'.$tablename.'.$s = t2.$s";'),
347          $dbfields['primary']
348          )
349        );
350    pwg_query($query);
351    $query = '
352DROP 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
367function 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 = '
388INSERT 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 */
427function do_maintenance_all_tables()
428{
429  global $prefixeTable, $page;
430
431  $all_tables = array();
432
433  // List all tables
434  $query = 'SELECT tablename FROM pg_tables
435WHERE tablename like \''.$prefixeTable.'%\'';
436
437  $all_tables = array_from_query($query, 'tablename');
438
439  // Optimize all tables
440  foreach ($all_tables as $table)
441  {
442    $query = 'VACUUM FULL '.$table;
443    pwg_query($query);
444  }
445  array_push($page['infos'],
446             l10n('All optimizations have been successfully completed.')
447             );
448}
449
450function pwg_db_concat($array)
451{
452  return implode($array, ' || ');
453}
454
455function pwg_db_concat_ws($array, $separator)
456{
457  return implode($array, ' || \''.$separator.'\' || ');
458}
459
460function pwg_db_cast_to_text($string)
461{
462  return 'CAST('.$string.' AS TEXT)';
463}
464
465/**
466 * returns an array containing the possible values of an enum field
467 *
468 * @param string tablename
469 * @param string fieldname
470 */
471function get_enums($table, $field)
472{
473  $typname = preg_replace('/'.$GLOBALS['prefixeTable'].'/', '', $table); 
474  $typname .= '_' . $field;
475
476  $query = 'SELECT
477enumlabel FROM pg_enum JOIN pg_type
478  ON pg_enum.enumtypid=pg_type.oid
479  WHERE typname=\''.$typname.'\'
480';
481  $result = pwg_query($query);
482  while ($row = pwg_db_fetch_assoc($result))
483  {
484    $options[] = $row['enumlabel'];
485  }
486
487  return $options;
488}
489
490// get_boolean transforms a string to a boolean value. If the string is
491// "false" (case insensitive), then the boolean value false is returned. In
492// any other case, true is returned.
493function get_boolean( $string )
494{
495  $boolean = true;
496  if ('f' == $string || 'false' == $string)
497  {
498    $boolean = false;
499  }
500  return $boolean;
501}
502
503/**
504 * returns boolean string 'true' or 'false' if the given var is boolean
505 *
506 * @param mixed $var
507 * @return mixed
508 */
509function boolean_to_string($var)
510{
511  if (is_bool($var))
512  {
513    return $var ? 'true' : 'false';
514  }
515  elseif ($var=='t' || $var=='f')
516  {
517    return ($var=='t') ? 'true' : 'false';
518  }
519  else
520  {
521    return $var;
522  }
523}
524
525/**
526 *
527 * interval and date functions
528 *
529 */
530
531function pwg_db_get_recent_period_expression($period, $date='CURRENT_DATE')
532{
533  if ($date!='CURRENT_DATE')
534  {
535    $date = '\''.$date.'\'::date';
536  }
537
538  return '('.$date.' - \''.$period.' DAY\'::interval)::date';
539}
540
541function pwg_db_get_recent_period($period, $date='CURRENT_DATE')
542{
543  $query = 'select '.pwg_db_get_recent_period_expression($period, $date);
544  list($d) = pwg_db_fetch_row(pwg_query($query));
545
546  return $d;
547}
548
549function pwg_db_get_flood_period_expression($seconds)
550{
551  return 'now() - \''.$seconds.' SECOND\'::interval';
552}
553
554function pwg_db_get_hour($date)
555{
556  return 'EXTRACT(HOUR FROM '.$date.')';
557}
558
559function pwg_db_get_date_YYYYMM($date)
560{
561  return 'TO_CHAR('.$date.', \'YYYYMM\')';
562}
563
564function pwg_db_get_date_MMDD($date)
565{
566  return 'TO_CHAR('.$date.', \'MMDD\')';
567}
568
569function pwg_db_get_year($date)
570{
571  return 'EXTRACT(YEAR FROM '.$date.')';
572}
573
574function pwg_db_get_month($date)
575{
576  return 'EXTRACT(MONTH FROM '.$date.')';
577}
578
579function pwg_db_get_week($date, $mode=null)
580{
581  return 'EXTRACT(WEEK FROM '.$date.')';
582}
583
584function pwg_db_get_dayofmonth($date)
585{
586  return 'EXTRACT(DAY FROM '.$date.')';
587}
588
589function pwg_db_get_dayofweek($date)
590{
591  return 'EXTRACT(DOW FROM '.$date.')::INTEGER - 1';
592}
593
594function pwg_db_get_weekday($date)
595{
596  return 'EXTRACT(ISODOW FROM '.$date.')::INTEGER - 1';
597}
598
599function pwg_db_date_to_ts($date) 
600{
601  return 'EXTRACT(EPOCH FROM '.$date.')';
602}
603
604// my_error returns (or send to standard output) the message concerning the
605// error occured for the last pgsql query.
606function my_error($header, $die)
607{
608  $error = '[pgsql error]'.pg_last_error()."\n";
609  $error .= $header;
610
611  if ($die)
612  {
613    fatal_error($error);
614  }
615  echo("<pre>");
616  trigger_error($error, E_USER_WARNING);
617  echo("</pre>");
618}
619
620
621?>
Note: See TracBrowser for help on using the repository browser.