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

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

Feature 1255 :

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