source: trunk/include/dblayer/functions_sqlite.inc.php @ 4781

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

Feature 511 : add support for sqlite database engine

Using session_write_close function when session handler use database because write is called after object destruction.

File size: 11.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_SQLITE_VERSION', '3.0.0');
25define('DB_ENGINE', 'SQLite');
26
27define('DB_REGEX_OPERATOR', 'REGEXP');
28define('DB_RANDOM_FUNCTION', 'RANDOM');
29
30/**
31 *
32 * simple functions
33 *
34 */
35
36function pwg_db_connect($host, $user, $password, $database)
37{
38  global $conf;
39
40  $db_file = sprintf('%s/%s.db', $conf['local_data_dir'], $database);
41
42  $link = new SQLite3($db_file);
43  $link->createFunction('now', 'pwg_now', 0);
44  $link->createFunction('md5', 'md5', 1);
45
46  $link->createAggregate('std', 'pwg_std_step', 'pwg_std_finalize');
47  $link->createFunction('regexp', 'pwg_regexp', 2);
48
49  return $link;
50}
51
52function pwg_db_check_charset() 
53{
54  return true;
55}
56
57function pwg_get_db_version() 
58{
59  global $pwg_db_link;
60
61  $versionInfos = $pwg_db_link->version();
62  return $versionInfos['versionString'];
63}
64
65function pwg_query($query)
66{
67  global $conf,$page,$debug,$t2,$pwg_db_link;
68
69  $start = get_moment();
70
71  $truncate_pattern = '`truncate(.*)`i';
72  $insert_pattern = '`(INSERT INTO [^)]*\)\s*VALUES)(\([^)]*\))\s*,\s*(.*)`mi'; 
73
74  if (preg_match($truncate_pattern, $query, $matches))
75  {
76    $query = str_replace('TRUNCATE TABLE', 'DELETE FROM', $query);
77    $truncate_query = true;
78    ($result = $pwg_db_link->exec($query)) or die($query."\n<br>".$pwg_db_link->lastErrorMsg());
79  }
80  elseif (preg_match($insert_pattern, $query, $matches))
81  {
82    $base_query = substr($query, 0, strlen($matches[1])+1);
83    $values_pattern = '`\)\s*,\s*\(`';
84    $values = preg_split($values_pattern, substr($query, strlen($matches[1])+1));
85    $values[0] = substr($values[0], 1);
86    $values[count($values)-1] = substr($values[count($values)-1], 
87                                     0, 
88                                     strlen($values[count($values)-1])-1
89                                     );
90    for ($n=0;$n<count($values);$n++)
91    {
92      $query = $base_query . '('. $values[$n] . ")\n;";
93      ($result = $pwg_db_link->query($query)) 
94        or die($query."\n<br>".$pwg_db_link->lastErrorMsg());
95    }
96  }
97  else 
98  {
99    ($result = $pwg_db_link->query($query)) 
100      or die($query."\n<br>".$pwg_db_link->lastErrorMsg());
101  }
102
103  $time = get_moment() - $start;
104
105  if (!isset($page['count_queries']))
106  {
107    $page['count_queries'] = 0;
108    $page['queries_time'] = 0;
109  }
110
111  $page['count_queries']++;
112  $page['queries_time']+= $time;
113
114  if ($conf['show_queries'])
115  {
116    $output = '';
117    $output.= '<pre>['.$page['count_queries'].'] ';
118    $output.= "\n".$query;
119    $output.= "\n".'(this query time : ';
120    $output.= '<b>'.number_format($time, 3, '.', ' ').' s)</b>';
121    $output.= "\n".'(total SQL time  : ';
122    $output.= number_format($page['queries_time'], 3, '.', ' ').' s)';
123    $output.= "\n".'(total time      : ';
124    $output.= number_format( ($time+$start-$t2), 3, '.', ' ').' s)';
125    if ( $result!=null and preg_match('/\s*SELECT\s+/i',$query) )
126    {
127      $output.= "\n".'(num rows        : ';
128      $output.= pwg_db_num_rows($result).' )';
129    }
130    elseif ( $result!=null
131      and preg_match('/\s*INSERT|UPDATE|REPLACE|DELETE\s+/i',$query) 
132      and !isset($truncate_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 MAX('.$column.')+1
149  FROM '.$table;
150  list($next) = pwg_db_fetch_row(pwg_query($query));
151  if (is_null($next))
152  {
153    $next = 1;
154  }
155  return $next;
156}
157
158/**
159 *
160 * complex functions
161 *
162 */
163
164function pwg_db_changes(SQLite3Result $result=null) 
165{
166  global $pwg_db_link;
167
168  return $pwg_db_link->changes();
169}
170
171function pwg_db_num_rows($result) 
172{ 
173  return $result->numColumns();
174}
175
176function pwg_db_fetch_assoc($result)
177{
178  return $result->fetchArray(SQLITE3_ASSOC);
179}
180
181function pwg_db_fetch_row($result)
182{
183  return $result->fetchArray(SQLITE3_NUM);
184}
185
186function pwg_db_fetch_object($result)
187{
188  return $result;
189}
190
191function pwg_db_free_result($result) 
192{
193}
194
195function pwg_db_real_escape_string($s)
196{
197  global $pwg_db_link;
198
199  return $pwg_db_link->escapeString($s);
200}
201
202function pwg_db_insert_id()
203{
204  global $pwg_db_link;
205
206  return $pwg_db_link->lastInsertRowID();
207}
208
209/**
210 *
211 * complex functions
212 *
213 */
214
215/**
216 * creates an array based on a query, this function is a very common pattern
217 * used here
218 *
219 * @param string $query
220 * @param string $fieldname
221 * @return array
222 */
223function array_from_query($query, $fieldname)
224{
225  $array = array();
226
227  $result = pwg_query($query);
228  while ($row = pwg_db_fetch_assoc($result))
229  {
230    array_push($array, $row[$fieldname]);
231  }
232
233  return $array;
234}
235
236define('MASS_UPDATES_SKIP_EMPTY', 1);
237/**
238 * updates multiple lines in a table
239 *
240 * @param string table_name
241 * @param array dbfields
242 * @param array datas
243 * @param int flags - if MASS_UPDATES_SKIP_EMPTY - empty values do not overwrite existing ones
244 * @return void
245 */
246function mass_updates($tablename, $dbfields, $datas, $flags=0)
247{
248  if (count($datas) == 0)
249    return;
250
251  foreach ($datas as $data)
252  {
253    $query = '
254UPDATE '.$tablename.'
255  SET ';
256    $is_first = true;
257    foreach ($dbfields['update'] as $key)
258    {
259      $separator = $is_first ? '' : ",\n    ";
260     
261      if (isset($data[$key]) and $data[$key] != '')
262      {
263        $query.= $separator.$key.' = \''.$data[$key].'\'';
264      }
265      else
266      {
267        if ($flags & MASS_UPDATES_SKIP_EMPTY )
268          continue; // next field
269        $query.= "$separator$key = NULL";
270      }
271      $is_first = false;
272    }
273    if (!$is_first)
274    {// only if one field at least updated
275      $query.= '
276  WHERE ';
277      $is_first = true;
278      foreach ($dbfields['primary'] as $key)
279      {
280        if (!$is_first)
281        {
282          $query.= ' AND ';
283        }
284        if ( isset($data[$key]) )
285        {
286          $query.= $key.' = \''.$data[$key].'\'';
287        }
288        else
289        {
290          $query.= $key.' IS NULL';
291        }
292        $is_first = false;
293      }
294      pwg_query($query);
295    }
296  }
297}
298
299
300/**
301 * inserts multiple lines in a table
302 *
303 * @param string table_name
304 * @param array dbfields
305 * @param array inserts
306 * @return void
307 */
308
309function mass_inserts($table_name, $dbfields, $datas)
310{
311  if (count($datas) != 0)
312  {
313    $first = true;
314
315    $packet_size = 16777216;
316    $packet_size = $packet_size - 2000; // The last list of values MUST not exceed 2000 character*/
317    $query = '';
318
319    foreach ($datas as $insert)
320    {
321      if (strlen($query) >= $packet_size)
322      {
323        pwg_query($query);
324        $first = true;
325      }
326
327      if ($first)
328      {
329        $query = '
330INSERT INTO '.$table_name.'
331  ('.implode(',', $dbfields).')
332  VALUES';
333        $first = false;
334      }
335      else
336      {
337        $query .= '
338  , ';
339      }
340
341      $query .= '(';
342      foreach ($dbfields as $field_id => $dbfield)
343      {
344        if ($field_id > 0)
345        {
346          $query .= ',';
347        }
348
349        if (!isset($insert[$dbfield]) or $insert[$dbfield] === '')
350        {
351          $query .= 'NULL';
352        }
353        else
354        {
355          $query .= "'".$insert[$dbfield]."'";
356        }
357      }
358      $query .= ')';
359    }
360    pwg_query($query);
361  }
362}
363
364/**
365 * Do maintenance on all PWG tables
366 *
367 * @return none
368 */
369function do_maintenance_all_tables()
370{
371  global $prefixeTable, $page;
372
373  $all_tables = array();
374
375  // List all tables
376  $query = 'SELECT name FROM SQLITE_MASTER
377WHERE name LIKE \''.$prefixeTable.'%\'';
378
379  $all_tables = array_from_query($query, 'name');
380  foreach ($all_tables as $table_name)
381  {
382    $query = 'VACUUM '.$table_name.';';
383    $result = pwg_query($query);
384  }
385 
386  array_push($page['infos'],
387             l10n('Optimizations completed')
388             );
389}
390
391function pwg_db_concat_ws($array, $separator)
392{
393  $glue = sprintf(' || \'%s\' || ', $separator);
394
395  return implode($array, $glue);
396}
397
398function pwg_db_cast_to_text($string)
399{
400  return $string;
401}
402
403/**
404 * returns an array containing the possible values of an enum field
405 *
406 * @param string tablename
407 * @param string fieldname
408 */
409function get_enums($table, $field)
410{
411  return array();
412}
413
414// get_boolean transforms a string to a boolean value. If the string is
415// "false" (case insensitive), then the boolean value false is returned. In
416// any other case, true is returned.
417function get_boolean( $string )
418{
419  $boolean = true;
420  if ('f' == $string || 'false' == $string)
421  {
422    $boolean = false;
423  }
424  return $boolean;
425}
426
427/**
428 * returns boolean string 'true' or 'false' if the given var is boolean
429 *
430 * @param mixed $var
431 * @return mixed
432 */
433function boolean_to_string($var)
434{
435  if (!empty($var) && ($var == 't'))
436  {
437    return 'true';
438  }
439  else
440  {
441    return 'false';
442  }
443}
444
445/**
446 *
447 * interval and date functions
448 *
449 */
450
451function pwg_db_get_recent_period_expression($period, $date='CURRENT_DATE')
452{
453  if ($date!='CURRENT_DATE')
454  {
455    $date = '\''.$date.'\'';
456  }
457
458  return 'date('.$date.',\''.$period.' DAY\')';
459}
460
461function pwg_db_get_recent_period($period, $date='CURRENT_DATE')
462{
463  $query = 'select '.pwg_db_get_recent_period_expression($period, $date);
464  list($d) = pwg_db_fetch_row(pwg_query($query));
465
466  return $d;
467}
468
469function pwg_db_get_date_YYYYMM($date)
470{
471  return 'strftime(\'%Y%m\','.$date.')';
472}
473
474function pwg_db_get_date_MMDD($date)
475{
476  return 'strftime(\'%m%d\','.$date.')';
477}
478
479function pwg_db_get_year($date)
480{
481  return 'strftime(\'%Y\','.$date.')';
482}
483
484function pwg_db_get_month($date)
485{
486  return 'strftime(\'%m\','.$date.')';
487}
488
489function pwg_db_get_week($date, $mode=null)
490{
491  return 'strftime(\'%W\','.$date.')';
492}
493
494function pwg_db_get_dayofmonth($date)
495{
496  return 'strftime(\'%d\','.$date.')';
497}
498
499function pwg_db_get_dayofweek($date)
500{
501  return 'strftime(\'%w\','.$date.')+1';
502}
503
504function pwg_db_get_weekday($date)
505{
506  return 'strftime(\'%w\','.$date.')';
507}
508
509// my_error returns (or send to standard output) the message concerning the
510// error occured for the last mysql query.
511function my_error($header, $die)
512{
513  global $pwg_db_link;
514
515  $error = '[sqlite error]'.$pwg_db_link->lastErrorMsg()."\n";
516  $error .= $header;
517
518  if ($die)
519  {
520    fatal_error($error);
521  }
522  echo("<pre>");
523  trigger_error($error, E_USER_WARNING);
524  echo("</pre>");
525}
526
527// sqlite create functions
528function pwg_now()
529{
530  return date('Y-m-d H:i:s');
531}
532
533function pwg_regexp($pattern, $string)
534{
535  $pattern = sprintf('`%s`', $pattern);
536  return preg_match($pattern, $string);
537}
538
539function pwg_std_step(&$values, $rownumber, $value) 
540{
541  $values[] = $value;
542
543  return $values;
544}
545
546function pwg_std_finalize(&$values, $rownumber) 
547{
548  if (count($values)<=1)
549  {
550    return 0;
551  }
552
553  $total = 0;
554  $total_square = 0;
555  foreach ($values as $value)
556  {
557    $total += $value;
558    $total_square += pow($value, 2);
559  }
560
561  $mean = $total/count($values);
562  $var = $total_square/count($values) - pow($mean, 2);
563 
564  return sqrt($var);
565}
566?>
Note: See TracBrowser for help on using the repository browser.