3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2017 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
14 * Ce fichier déclare des fonctions étendant les fonctions natives de SQLite
16 * @package SPIP\Core\SQL\SQLite\Fonctions
19 if (!defined('_ECRIRE_INC_VERSION')) {
24 * Déclarer à SQLite des fonctions spécifiques utilisables dans les requêtes SQL
26 * SQLite ne supporte nativement que certaines fonctions dans les requêtes SQL.
27 * Cependant, il permet d'étendre très facilement celles-ci en déclarant de
28 * nouvelles fonctions.
30 * C'est ce qui est fait ici, en ajoutant des fonctions qui existent aussi
31 * dans d'autres moteurs, notamment en MySQL.
33 * @link http://www.sqlite.org/lang_corefunc.html Liste des fonctions natives
34 * @link http://sqlite.org/changes.html Liste des évolutions
36 * @param PDO|resource $sqlite Représente la connexion Sqlite
38 function _sqlite_init_functions(&$sqlite) {
47 'ACOS' => array('acos', 1),
48 'ASIN' => array('asin', 1),
49 'ATAN' => array('atan', 1), // mysql accepte 2 params comme atan2… hum ?
50 'ATAN2' => array('atan2', 2),
53 'CEIL' => array('_sqlite_func_ceil', 1),
54 'CONCAT' => array('_sqlite_func_concat', -1),
55 'COS' => array('cos', 1),
58 'DATE_FORMAT' => array('_sqlite_func_strftime', 2),
59 'DAYOFMONTH' => array('_sqlite_func_dayofmonth', 1),
60 'DEGREES' => array('rad2deg', 1),
63 'EXTRAIRE_MULTI' => array('_sqlite_func_extraire_multi', 2), // specifique a SPIP/sql_multi()
64 'EXP' => array('exp', 1),
67 'FIND_IN_SET' => array('_sqlite_func_find_in_set', 2),
68 'FLOOR' => array('_sqlite_func_floor', 1),
71 'IF' => array('_sqlite_func_if', 3),
72 'INSERT' => array('_sqlite_func_insert', 4),
73 'INSTR' => array('_sqlite_func_instr', 2),
76 'LEAST' => array('_sqlite_func_least', 3),
77 '_LEFT' => array('_sqlite_func_left', 2),
78 # 'LENGTH' => array('strlen', 1), // present v1.0.4
79 # 'LOWER' => array('strtolower', 1), // present v2.4
80 # 'LTRIM' => array('ltrim', 1), // present
83 'NOW' => array('_sqlite_func_now', 0),
86 'MD5' => array('md5', 1),
87 'MONTH' => array('_sqlite_func_month', 1),
90 'PREG_REPLACE' => array('_sqlite_func_preg_replace', 3),
93 'RADIANS' => array('deg2rad', 1),
94 'RAND' => array('_sqlite_func_rand', 0), // sinon random() v2.4
95 'REGEXP' => array('_sqlite_func_regexp_match', 2), // critere REGEXP supporte a partir de v3.3.2
96 'RIGHT' => array('_sqlite_func_right', 2),
97 # 'RTRIM' => array('rtrim', 1), // present
100 'SETTYPE' => array('settype', 2), // CAST present en v3.2.3
101 'SIN' => array('sin', 1),
102 'SQRT' => array('sqrt', 1),
103 'SUBSTRING' => array('_sqlite_func_substring' /*, 3*/), // peut etre appelee avec 2 ou 3 arguments, index base 1 et non 0
106 'TAN' => array('tan', 1),
107 'TIMESTAMPDIFF' => array('_sqlite_timestampdiff' /*, 3*/),
108 'TO_DAYS' => array('_sqlite_func_to_days', 1),
109 # 'TRIM' => array('trim', 1), // present
112 'UNIX_TIMESTAMP' => array('_sqlite_func_unix_timestamp', 1),
113 # 'UPPER' => array('strtoupper', 1), // present v2.4
116 'VIDE' => array('_sqlite_func_vide', 0), // du vide pour SELECT 0 as x ... ORDER BY x -> ORDER BY vide()
119 'YEAR' => array('_sqlite_func_year', 1)
123 foreach ($fonctions as $f => $r) {
124 _sqlite_add_function($sqlite, $f, $r);
127 #spip_log('functions sqlite chargees ','sqlite.'._LOG_DEBUG);
132 * Déclare une fonction à SQLite
135 * Permet au besoin de charger des fonctions
136 * ailleurs par _sqlite_init_functions();
138 * @uses _sqlite_is_version()
140 * @param PDO|resource $sqlite Représente la connexion Sqlite
141 * @param string $f Nom de la fonction à créer
142 * @param array $r Tableau indiquant :
143 * - le nom de la fonction à appeler,
144 * - le nombre de paramètres attendus de la fonction (-1 = infini, par défaut)
147 function _sqlite_add_function(&$sqlite, &$f, &$r) {
148 if (_sqlite_is_version(3, $sqlite)) {
150 ?
$sqlite->sqliteCreateFunction($f, $r[0], $r[1])
151 : $sqlite->sqliteCreateFunction($f, $r[0]);
154 ?
sqlite_create_function($sqlite, $f, $r[0], $r[1])
155 : sqlite_create_function($sqlite, $f, $r[0]);
160 // SQLite : fonctions sqlite -> php
161 // entre autre auteurs : mlebas
164 function _sqlite_func_ceil($a) {
168 // http://code.spip.net/@_sqlite_func_concat
169 function _sqlite_func_concat() {
170 $args = func_get_args();
172 return join('', $args);
176 // http://code.spip.net/@_sqlite_func_dayofmonth
177 function _sqlite_func_dayofmonth($d) {
178 return _sqlite_func_date("d", $d);
182 // http://code.spip.net/@_sqlite_func_find_in_set
183 function _sqlite_func_find_in_set($num, $set) {
185 foreach (explode(",", $set) as $v) {
195 function _sqlite_func_floor($a) {
199 // http://code.spip.net/@_sqlite_func_if
200 function _sqlite_func_if($bool, $oui, $non) {
201 return ($bool) ?
$oui : $non;
206 * INSERT(chaine, index, longueur, chaine) MySQL
207 * Retourne une chaine de caracteres a partir d'une chaine dans laquelle "sschaine"
208 * a ete inseree a la position "index" en remplacant "longueur" caracteres.
210 // http://code.spip.net/@_sqlite_func_insert
211 function _sqlite_func_insert($s, $index, $longueur, $chaine) {
213 substr($s, 0, $index)
215 . substr(substr($s, $index), $longueur);
219 // http://code.spip.net/@_sqlite_func_instr
220 function _sqlite_func_instr($s, $search) {
221 return strpos($s, $search);
225 // http://code.spip.net/@_sqlite_func_least
226 function _sqlite_func_least() {
227 $arg_list = func_get_args();
228 $least = min($arg_list);
230 #spip_log("Passage avec LEAST : $least",'sqlite.'._LOG_DEBUG);
235 // http://code.spip.net/@_sqlite_func_left
236 function _sqlite_func_left($s, $lenght) {
237 return substr($s, $lenght);
241 // http://code.spip.net/@_sqlite_func_now
242 function _sqlite_func_now() {
245 $now = date("Y-m-d H:i:s");
248 #spip_log("Passage avec NOW : $now",'sqlite.'._LOG_DEBUG);
253 // http://code.spip.net/@_sqlite_func_month
254 function _sqlite_func_month($d) {
255 return _sqlite_func_date("m", $d);
259 // http://code.spip.net/@_sqlite_func_preg_replace
260 function _sqlite_func_preg_replace($quoi, $cherche, $remplace) {
261 $return = preg_replace('%' . $cherche . '%', $remplace, $quoi);
263 #spip_log("preg_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG);
268 * Extrait une langue d'un texte <multi>[fr] xxx [en] yyy</multi>
270 * @param string $quoi le texte contenant ou non un multi
271 * @param string $lang la langue a extraire
272 * @return string, l'extrait trouve.
274 function _sqlite_func_extraire_multi($quoi, $lang) {
275 if (!defined('_EXTRAIRE_MULTI')) {
276 include_spip('inc/filtres');
278 if (!function_exists('approcher_langue')) {
279 include_spip('inc/lang');
281 if (preg_match_all(_EXTRAIRE_MULTI
, $quoi, $regs, PREG_SET_ORDER
)) {
282 foreach ($regs as $reg) {
283 // chercher la version de la langue courante
284 $trads = extraire_trads($reg[1]);
285 if ($l = approcher_langue($trads, $lang)) {
288 $trad = reset($trads);
290 $quoi = str_replace($reg[0], $trad, $quoi);
298 // http://code.spip.net/@_sqlite_func_rand
299 function _sqlite_func_rand() {
304 // http://code.spip.net/@_sqlite_func_right
305 function _sqlite_func_right($s, $length) {
306 return substr($s, 0 - $length);
310 // http://code.spip.net/@_sqlite_func_regexp_match
311 function _sqlite_func_regexp_match($cherche, $quoi) {
312 // optimiser un cas tres courant avec les requetes en base
313 if (!$quoi and !strlen($quoi)) {
316 $u = isset($GLOBALS['meta']['pcre_u']) ?
$GLOBALS['meta']['pcre_u'] : 'u';
317 $return = preg_match('%' . $cherche . '%imsS' . $u, $quoi);
319 #spip_log("regexp_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG);
323 // http://code.spip.net/@_sqlite_func_strftime
324 function _sqlite_func_strftime($date, $conv) {
325 return strftime($conv, is_int($date) ?
$date : strtotime($date));
329 * Nombre de jour entre 0000-00-00 et $d
331 * @link http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_to-days
336 function _sqlite_func_to_days($d) {
337 static $offset = 719528; // nb de jour entre 0000-00-00 et timestamp 0=1970-01-01
338 $result = $offset +
(int)ceil(_sqlite_func_unix_timestamp($d) / (24 * 3600));
340 #spip_log("Passage avec TO_DAYS : $d, $result",'sqlite.'._LOG_DEBUG);
344 function _sqlite_func_substring($string, $start, $len = null) {
345 // SQL compte a partir de 1, php a partir de 0
346 $start = ($start > 0) ?
$start - 1 : $start;
348 return substr($string, $start);
350 return substr($string, $start, $len);
355 * Calcul de la difference entre 2 timestamp, exprimes dans l'unite fournie en premier argument
357 * @link https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timestampdiff
359 * @param string $unit
360 * @param string $date1
361 * @param string $date2
364 function _sqlite_timestampdiff($unit, $date1, $date2) {
366 if (function_exists("date_diff")) {
367 $d1 = date_create($date1);
368 $d2 = date_create($date2);
369 $diff = date_diff($d1, $d2);
370 $inv = $diff->invert ?
-1 : 1;
373 return $inv * $diff->y
;
375 return $inv * (4 * $diff->y +
intval(floor($diff->m
/ 3)));
377 return $inv * (12 * $diff->y +
$diff->m
);
379 return $inv * intval(floor($diff->days
/ 7));
381 #var_dump($inv*$diff->days);
382 return $inv * $diff->days
;
384 return $inv * (24 * $diff->days +
$diff->h
);
386 return $inv * ((24 * $diff->days +
$diff->h
) * 60 +
$diff->i
);
388 return $inv * (((24 * $diff->days +
$diff->h
) * 60 +
$diff->i
) * 60 +
$diff->s
);
390 return $inv * (((24 * $diff->days +
$diff->h
) * 60 +
$diff->i
) * 60 +
$diff->s
) * 1000000;
396 $d1 = strtotime($date1);
397 $d2 = strtotime($date2);
399 $sign = ($diff < 0 ?
-1 : 1);
400 $diff = $sign * $diff;
405 return $sign * (date('Y', abs($diff)) - date('Y', 0));
407 return $sign * (4 * (date('Y', abs($diff)) - date('Y', 0)) +
intval(floor((date('m', $diff) - 1) / 3)));
409 return $sign * ((date('Y', $diff) - date('Y', 0)) * 12 +
date('m', $diff) - 1);
411 return intval(floor(($d2 - $d1) / 3600 / 7));
413 return intval(floor(($d2 - $d1) / 3600 / 24));
415 return intval(floor(($d2 - $d1) / 3600));
417 return intval(floor(($d2 - $d1) / 60));
421 return $d2 - $d1 * 1000000;
426 // http://code.spip.net/@_sqlite_func_unix_timestamp
427 function _sqlite_func_unix_timestamp($d) {
428 static $mem = array();
430 if (isset($mem[$d])) {
438 //2005-12-02 20:53:53
439 #spip_log("Passage avec UNIX_TIMESTAMP : $d",'sqlite.'._LOG_DEBUG);
441 return $mem[$d] = mktime();
444 // une pile plus grosse n'accelere pas le calcul
445 return $mem[$d] = strtotime($d);
449 // http://code.spip.net/@_sqlite_func_year
450 function _sqlite_func_year($d) {
451 return _sqlite_func_date("Y", $d);
455 * version optimisee et memoizee de date() utilisee par
456 * _sqlite_func_year, _sqlite_func_month, _sqlite_func_dayofmonth
458 * @param string $quoi
459 * format : Y, m, ou d
464 function _sqlite_func_date($quoi, $d) {
465 static $mem = array();
467 if (isset($mem[$d])) {
468 return $mem[$d][$quoi];
475 $dec = date("Y-m-d", _sqlite_func_unix_timestamp($d));
476 $mem[$d] = array("Y" => substr($dec, 0, 4), "m" => substr($dec, 5, 2), "d" => substr($dec, 8, 2));
478 return $mem[$d][$quoi];
481 // http://code.spip.net/@_sqlite_func_vide
482 function _sqlite_func_vide() {