3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2014 *
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 \***************************************************************************/
13 if (!defined('_ECRIRE_INC_VERSION')) return;
16 * Des fonctions pour les requetes SQL
18 * Voir la liste des fonctions natives : http://www.sqlite.org/lang_corefunc.html
19 * Et la liste des evolutions pour : http://sqlite.org/changes.html
22 // http://doc.spip.org/@_sqlite_init_functions
23 function _sqlite_init_functions(&$sqlite){
25 if (!$sqlite) return false;
29 'CONCAT' => array( '_sqlite_func_concat' ,2),
30 'CEIL' => array( '_sqlite_func_ceil', 1), // absent de sqlite2
32 'DATE_FORMAT' => array( '_sqlite_func_strftime' ,2),
33 'DAYOFMONTH' => array( '_sqlite_func_dayofmonth' ,1),
35 'EXP' => array( 'exp' ,1),//exponentielle
36 'FIND_IN_SET' => array( '_sqlite_func_find_in_set' ,2),
37 'FLOOR' => array( '_sqlite_func_floor', 1), // absent de sqlite2
39 'IF' => array( '_sqlite_func_if' ,3),
40 'INSERT' => array( '_sqlite_func_insert' ,4),
41 'INSTR' => array( '_sqlite_func_instr' ,2),
43 'LEAST' => array( '_sqlite_func_least' ,3),
44 'LEFT' => array( '_sqlite_func_left' ,2),
45 # 'LENGTH' => array( 'strlen' ,1), // present v1.0.4
46 # 'LOWER' => array( 'strtolower' ,1), // present v2.4
47 # 'LTRIM' => array( 'ltrim' ,1), // present en theorie
49 'NOW' => array( '_sqlite_func_now' ,0),
51 'MD5' => array( 'md5' ,1),
52 'MONTH' => array( '_sqlite_func_month' ,1),
54 'PREG_REPLACE' => array( '_sqlite_func_preg_replace' ,3),
56 'RAND' => array( '_sqlite_func_rand' ,0), // sinon random() v2.4
57 'REGEXP' => array( '_sqlite_func_regexp_match' ,2), // critere REGEXP supporte a partir de v3.3.2
58 //'REGEXP_MATCH' => array( '_sqlite_func_regexp_match' ,2), // critere REGEXP supporte a partir de v3.3.2
60 'RIGHT' => array( '_sqlite_func_right' ,2),
61 # 'RTRIM' => array( 'rtrim' ,1), // present en theorie
63 'SETTYPE' => array( 'settype' ,2), // CAST present en v3.2.3
64 'SQRT' => array( 'sqrt' ,1),
65 'SUBSTRING' => array( 'substr' ,3),
67 'TO_DAYS' => array( '_sqlite_func_to_days' ,1),
68 # 'TRIM' => array( 'trim' ,1), // present en theorie
70 'UNIX_TIMESTAMP'=> array( '_sqlite_func_unix_timestamp' ,1),
71 # 'UPPER' => array( 'strtoupper' ,1), // present v2.4
73 'VIDE' => array( '_sqlite_func_vide' ,0), // du vide pour SELECT 0 as x ... ORDER BY x -> ORDER BY vide()
75 'YEAR' => array( '_sqlite_func_year' ,1)
79 foreach ($fonctions as $f=>$r){
80 _sqlite_add_function($sqlite, $f, $r);
83 #spip_log('functions sqlite chargees ');
86 // permet au besoin de charger des fonctions ailleurs par _sqlite_init_functions();
87 // http://doc.spip.org/@_sqlite_add_function
88 function _sqlite_add_function(&$sqlite, &$f, &$r){
89 if (_sqlite_is_version(3, $sqlite)){
91 ?
$sqlite->sqliteCreateFunction($f, $r[0], $r[1])
92 :$sqlite->sqliteCreateFunction($f, $r[0]);
95 ?
sqlite_create_function($sqlite, $f, $r[0], $r[1])
96 :sqlite_create_function($sqlite, $f, $r[0]);
101 // SQLite : fonctions sqlite -> php
102 // entre autre auteurs : mlebas
105 function _sqlite_func_ceil($a) {
109 // http://doc.spip.org/@_sqlite_func_concat
110 function _sqlite_func_concat ($a, $b) {
115 // http://doc.spip.org/@_sqlite_func_dayofmonth
116 function _sqlite_func_dayofmonth ($d) {
120 preg_match(";^([0-9]{4})-([0-9]+)-([0-9]+) .*$;", $d, $f);
123 #spip_log("Passage avec DAYOFMONTH : $d, $result",'debug');
128 // http://doc.spip.org/@_sqlite_func_find_in_set
129 function _sqlite_func_find_in_set($num, $set) {
131 foreach (explode(",",$set) as $v) {
132 if ($v == $num) return (++
$rank);
138 function _sqlite_func_floor($a) {
142 // http://doc.spip.org/@_sqlite_func_if
143 function _sqlite_func_if ($bool, $oui, $non) {
144 return ($bool)?
$oui:$non;
149 * INSERT(chaine, index, longueur, chaine) MySQL
150 * Retourne une chaine de caracteres a partir d'une chaine dans laquelle "sschaine"
151 * a ete inseree a la position "index" en remplacant "longueur" caracteres.
153 // http://doc.spip.org/@_sqlite_func_insert
154 function _sqlite_func_insert ($s, $index, $longueur, $chaine) {
158 . substr(substr($s, $index), $longueur);
162 // http://doc.spip.org/@_sqlite_func_instr
163 function _sqlite_func_instr ($s, $search) {
164 return strpos($s,$search);
168 // http://doc.spip.org/@_sqlite_func_least
169 function _sqlite_func_least () {
170 $arg_list = func_get_args();
171 $least = min($arg_list);
172 #spip_log("Passage avec LEAST : $least",'debug');
177 // http://doc.spip.org/@_sqlite_func_left
178 function _sqlite_func_left ($s, $lenght) {
179 return substr($s,$lenght);
183 // http://doc.spip.org/@_sqlite_func_now
184 function _sqlite_func_now(){
185 $result = date("Y-m-d H:i:s", strtotime("now"));
186 #spip_log("Passage avec NOW : $result",'debug');
191 // http://doc.spip.org/@_sqlite_func_month
192 function _sqlite_func_month ($d) {
193 #spip_log("Passage avec MONTH : $d",'debug');
194 if (!$d) return date("n");
195 preg_match(";^([0-9]{4})-([0-9]+).*$;", $d, $f);
201 // http://doc.spip.org/@_sqlite_func_preg_replace
202 function _sqlite_func_preg_replace($quoi, $cherche, $remplace) {
203 $return = preg_replace('%'.$cherche.'%', $remplace, $quoi);
204 #spip_log("preg_replace : $quoi, $cherche, $remplace, $return",'debug');
209 // http://doc.spip.org/@_sqlite_func_rand
210 function _sqlite_func_rand() {
215 // http://doc.spip.org/@_sqlite_func_right
216 function _sqlite_func_right ($s, $lenght) {
217 return substr($s,0 - $lenght);
221 // http://doc.spip.org/@_sqlite_func_regexp_match
222 function _sqlite_func_regexp_match($cherche, $quoi) {
223 $return = preg_match('%'.$cherche.'%', $quoi);
224 #spip_log("regexp_replace : $quoi, $cherche, $remplace, $return",'debug');
228 // http://doc.spip.org/@_sqlite_func_strftime
229 function _sqlite_func_strftime($date, $conv){
230 return strftime($conv, $date);
234 * Nombre de jour entre 0000-00-00 et $d
235 * http://doc.spip.org/@_sqlite_func_to_days
236 * cf http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_to-days
240 function _sqlite_func_to_days ($d) {
241 $offset = 719528; // nb de jour entre 0000-00-00 et timestamp 0=1970-01-01
242 $result = $offset+
(int)ceil(_sqlite_func_unix_timestamp($d)/(24*3600));
243 #spip_log("Passage avec TO_DAYS : $d, $result",'debug');
248 // http://doc.spip.org/@_sqlite_func_unix_timestamp
249 function _sqlite_func_unix_timestamp($d) {
250 //2005-12-02 20:53:53
251 #spip_log("Passage avec UNIX_TIMESTAMP : $d",'debug');
252 // mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] )
253 if (!$d) return mktime();
254 return strtotime($d);
255 #preg_match(";^([0-9]{4})-([0-9]+)-([0-9]+)\s*(?:([0-9]+)(?::([0-9]+)(?::([0-9]+))?)?)?;", $d, $f);
256 #return mktime($f[4],$f[5],$f[6],$f[2],$f[3],$f[1]);
260 // http://doc.spip.org/@_sqlite_func_year
261 function _sqlite_func_year ($d) {
265 preg_match(";^([0-9]{4}).*$;", $d, $f);
268 spip_log("Passage avec YEAR : $d, $result",'debug');
273 // http://doc.spip.org/@_sqlite_func_vide
274 function _sqlite_func_vide(){