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

Last change on this file since 5196 was 5196, checked in by plg, 14 years ago

increase copyright year to 2010

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