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

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

Feature 1255 : bug in pwg_session_write() with postgresql.
replace into doesn't exists. Must do two queries

File size: 12.7 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2009 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', true); 
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()
196{
197  // select currval('piwigo_user_id_seq');
198}
199
200/**
201 *
202 * complex functions
203 *
204 */
205
206/**
207 * creates an array based on a query, this function is a very common pattern
208 * used here
209 *
210 * @param string $query
211 * @param string $fieldname
212 * @return array
213 */
214function array_from_query($query, $fieldname)
215{
216  $array = array();
217
218  $result = pwg_query($query);
219  while ($row = pg_fetch_assoc($result))
220  {
221    array_push($array, $row[$fieldname]);
222  }
223
224  return $array;
225}
226
227define('MASS_UPDATES_SKIP_EMPTY', 1);
228/**
229 * updates multiple lines in a table
230 *
231 * @param string table_name
232 * @param array dbfields
233 * @param array datas
234 * @param int flags - if MASS_UPDATES_SKIP_EMPTY - empty values do not overwrite existing ones
235 * @return void
236 */
237function mass_updates($tablename, $dbfields, $datas, $flags=0)
238{
239  if (count($datas) == 0)
240    return;
241
242  if (count($datas) < 10)
243  {
244    foreach ($datas as $data)
245    {
246      $query = '
247UPDATE '.$tablename.'
248  SET ';
249      $is_first = true;
250      foreach ($dbfields['update'] as $key)
251      {
252        $separator = $is_first ? '' : ",\n    ";
253
254        if (isset($data[$key]) and $data[$key] != '')
255        {
256          $query.= $separator.$key.' = \''.$data[$key].'\'';
257        }
258        else
259        {
260          if ($flags & MASS_UPDATES_SKIP_EMPTY )
261            continue; // next field
262          $query.= "$separator$key = NULL";
263        }
264        $is_first = false;
265      }
266      if (!$is_first)
267      {// only if one field at least updated
268        $query.= '
269  WHERE ';
270        $is_first = true;
271        foreach ($dbfields['primary'] as $key)
272        {
273          if (!$is_first)
274          {
275            $query.= ' AND ';
276          }
277          if ( isset($data[$key]) )
278          {
279            $query.= $key.' = \''.$data[$key].'\'';
280          }
281          else
282          {
283            $query.= $key.' IS NULL';
284          }
285          $is_first = false;
286        }
287        pwg_query($query);
288      }
289    } // foreach update
290  } // if mysql_ver or count<X
291  else
292  {
293    $all_fields = array_merge($dbfields['primary'], $dbfields['update']);
294    $temporary_tablename = $tablename.'_'.micro_seconds();
295    $query = '
296CREATE TABLE '.$temporary_tablename.'
297  AS SELECT * FROM '.$tablename.' WHERE 1=2';
298
299    pwg_query($query);
300    mass_inserts($temporary_tablename, $all_fields, $datas);
301    if ( $flags & MASS_UPDATES_SKIP_EMPTY )
302      $func_set = create_function('$s, $t', 'return "$s = IFNULL(t2.$s, '.$tablename.'.$s)";');
303    else
304      $func_set = create_function('$s', 'return "$s = t2.$s";');
305
306    // update of images table by joining with temporary table
307    $query = '
308UPDATE '.$tablename.'
309  SET '.
310      implode(
311        "\n    , ",
312        array_map($func_set, $dbfields['update'])
313        ).'
314FROM '.$temporary_tablename.' AS t2
315  WHERE '.
316      implode(
317        "\n    AND ",
318        array_map(
319          create_function('$s, $t', 'return "'.$tablename.'.$s = t2.$s";'),
320          $dbfields['primary']
321          )
322        );
323    pwg_query($query);
324    $query = '
325DROP TABLE '.$temporary_tablename;
326    pwg_query($query);
327  }
328}
329
330
331/**
332 * inserts multiple lines in a table
333 *
334 * @param string table_name
335 * @param array dbfields
336 * @param array inserts
337 * @return void
338 */
339
340function mass_inserts($table_name, $dbfields, $datas)
341{
342  if (count($datas) != 0)
343  {
344    $first = true;
345
346    $packet_size = 16777216;
347    $packet_size = $packet_size - 2000; // The last list of values MUST not exceed 2000 character*/
348    $query = '';
349
350    foreach ($datas as $insert)
351    {
352      if (strlen($query) >= $packet_size)
353      {
354        pwg_query($query);
355        $first = true;
356      }
357
358      if ($first)
359      {
360        $query = '
361INSERT INTO '.$table_name.'
362  ('.implode(',', $dbfields).')
363  VALUES';
364        $first = false;
365      }
366      else
367      {
368        $query .= '
369  , ';
370      }
371
372      $query .= '(';
373      foreach ($dbfields as $field_id => $dbfield)
374      {
375        if ($field_id > 0)
376        {
377          $query .= ',';
378        }
379
380        if (!isset($insert[$dbfield]) or $insert[$dbfield] === '')
381        {
382          $query .= 'NULL';
383        }
384        else
385        {
386          $query .= "'".$insert[$dbfield]."'";
387        }
388      }
389      $query .= ')';
390    }
391    pwg_query($query);
392  }
393}
394
395/**
396 * Do maintenance on all PWG tables
397 *
398 * @return none
399 */
400function do_maintenance_all_tables()
401{
402  global $prefixeTable, $page;
403
404  $all_tables = array();
405
406  // List all tables
407  $query = 'SELECT tablename FROM pg_tables
408WHERE tablename like \''.$prefixeTable.'%\'';
409
410  $all_tables = array_from_query($query, 'tablename');
411
412  // Optimize all tables
413  foreach ($all_tables as $table)
414  {
415    $query = 'VACUUM FULL '.$table;
416    pwg_query($query);
417  }
418  array_push($page['infos'],
419             l10n('Optimizations completed')
420             );
421}
422
423function pwg_db_concat($array)
424{
425  $string = implode($array, ',');
426  return 'ARRAY_TO_STRING(ARRAY['.$string.'])';
427}
428
429function pwg_db_concat_ws($array, $separator)
430{
431  $string = implode($array, ',');
432  return 'ARRAY_TO_STRING(ARRAY['.$string.'],\''.$separator.'\')';
433}
434
435function pwg_db_cast_to_text($string)
436{
437  return 'CAST('.$string.' AS TEXT)';
438}
439
440/**
441 * returns an array containing the possible values of an enum field
442 *
443 * @param string tablename
444 * @param string fieldname
445 */
446function get_enums($table, $field)
447{
448  $typname = preg_replace('/'.$GLOBALS['prefixeTable'].'/', '', $table); 
449  $typname .= '_' . $field;
450
451  $query = 'SELECT
452enumlabel FROM pg_enum JOIN pg_type
453  ON pg_enum.enumtypid=pg_type.oid
454  WHERE typname=\''.$typname.'\'
455';
456  $result = pwg_query($query);
457  while ($row = pwg_db_fetch_assoc($result))
458  {
459    $options[] = $row['enumlabel'];
460  }
461
462  return $options;
463}
464
465// get_boolean transforms a string to a boolean value. If the string is
466// "false" (case insensitive), then the boolean value false is returned. In
467// any other case, true is returned.
468function get_boolean( $string )
469{
470  $boolean = true;
471  if ('f' == $string || 'false' == $string)
472  {
473    $boolean = false;
474  }
475  return $boolean;
476}
477
478/**
479 * returns boolean string 'true' or 'false' if the given var is boolean
480 *
481 * @param mixed $var
482 * @return mixed
483 */
484function boolean_to_string($var)
485{
486  if (!empty($var) && ($var == 't'))
487  {
488    return 'true';
489  }
490  else
491  {
492    return 'false';
493  }
494}
495
496/**
497 *
498 * interval and date functions
499 *
500 */
501
502function pwg_db_get_recent_period_expression($period, $date='CURRENT_DATE')
503{
504  if ($date!='CURRENT_DATE')
505  {
506    $date = '\''.$date.'\'::date';
507  }
508
509  return '('.$date.' - \''.$period.' DAY\'::interval)::date';
510}
511
512function pwg_db_get_recent_period($period, $date='CURRENT_DATE')
513{
514  $query = 'select '.pwg_db_get_recent_period_expression($period, $date);
515  list($d) = pwg_db_fetch_row(pwg_query($query));
516
517  return $d;
518}
519
520function pwg_db_get_date_YYYYMM($date)
521{
522  return 'TO_CHAR('.$date.', \'YYYYMM\')';
523}
524
525function pwg_db_get_date_MMDD($date)
526{
527  return 'TO_CHAR('.$date.', \'MMDD\')';
528}
529
530function pwg_db_get_year($date)
531{
532  return 'EXTRACT(YEAR FROM '.$date.')';
533}
534
535function pwg_db_get_month($date)
536{
537  return 'EXTRACT(MONTH FROM '.$date.')';
538}
539
540function pwg_db_get_week($date, $mode=null)
541{
542  return 'EXTRACT(WEEK FROM '.$date.')';
543}
544
545function pwg_db_get_dayofmonth($date)
546{
547  return 'EXTRACT(DAY FROM '.$date.')';
548}
549
550function pwg_db_get_dayofweek($date)
551{
552  return 'EXTRACT(DOW FROM '.$date.')::INTEGER - 1';
553}
554
555function pwg_db_get_weekday($date)
556{
557  return 'EXTRACT(ISODOW FROM '.$date.')::INTEGER - 1';
558}
559
560// my_error returns (or send to standard output) the message concerning the
561// error occured for the last mysql query.
562function my_error($header, $die)
563{
564  $error = '[pgsql error]'.pg_last_error()."\n";
565  $error .= $header;
566
567  if ($die)
568  {
569    fatal_error($error);
570  }
571  echo("<pre>");
572  trigger_error($error, E_USER_WARNING);
573  echo("</pre>");
574}
575
576
577?>
Note: See TracBrowser for help on using the repository browser.