3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2011 *
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 $numargs = func_num_args();
171 $arg_list = func_get_args();
173 for ($i = 0; $i < $numargs; $i++
) {
174 if ($arg_list[$i] < $least) $least=$arg_list[$i];
176 #spip_log("Passage avec LEAST : $least",'debug');
181 // http://doc.spip.org/@_sqlite_func_left
182 function _sqlite_func_left ($s, $lenght) {
183 return substr($s,$lenght);
187 // http://doc.spip.org/@_sqlite_func_now
188 function _sqlite_func_now(){
189 $result = date("Y-m-d H:i:s", strtotime("now"));
190 #spip_log("Passage avec NOW : $result",'debug');
195 // http://doc.spip.org/@_sqlite_func_month
196 function _sqlite_func_month ($d) {
197 #spip_log("Passage avec MONTH : $d",'debug');
198 if (!$d) return date("n");
199 preg_match(";^([0-9]{4})-([0-9]+).*$;", $d, $f);
205 // http://doc.spip.org/@_sqlite_func_preg_replace
206 function _sqlite_func_preg_replace($quoi, $cherche, $remplace) {
207 $return = preg_replace('%'.$cherche.'%', $remplace, $quoi);
208 #spip_log("preg_replace : $quoi, $cherche, $remplace, $return",'debug');
213 // http://doc.spip.org/@_sqlite_func_rand
214 function _sqlite_func_rand() {
219 // http://doc.spip.org/@_sqlite_func_right
220 function _sqlite_func_right ($s, $lenght) {
221 return substr($s,0 - $lenght);
225 // http://doc.spip.org/@_sqlite_func_regexp_match
226 function _sqlite_func_regexp_match($cherche, $quoi) {
227 $return = preg_match('%'.$cherche.'%', $quoi);
228 #spip_log("regexp_replace : $quoi, $cherche, $remplace, $return",'debug');
232 // http://doc.spip.org/@_sqlite_func_strftime
233 function _sqlite_func_strftime($date, $conv){
234 return strftime($conv, $date);
237 // http://doc.spip.org/@_sqlite_func_to_days
238 function _sqlite_func_to_days ($d) {
239 $result = date("z", _sqlite_func_unix_timestamp($d));
240 #spip_log("Passage avec TO_DAYS : $d, $result",'debug');
245 // http://doc.spip.org/@_sqlite_func_unix_timestamp
246 function _sqlite_func_unix_timestamp($d) {
247 //2005-12-02 20:53:53
248 #spip_log("Passage avec UNIX_TIMESTAMP : $d",'debug');
249 // mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] )
250 if (!$d) return mktime();
251 return strtotime($d);
252 #preg_match(";^([0-9]{4})-([0-9]+)-([0-9]+)\s*(?:([0-9]+)(?::([0-9]+)(?::([0-9]+))?)?)?;", $d, $f);
253 #return mktime($f[4],$f[5],$f[6],$f[2],$f[3],$f[1]);
257 // http://doc.spip.org/@_sqlite_func_year
258 function _sqlite_func_year ($d) {
262 preg_match(";^([0-9]{4}).*$;", $d, $f);
265 spip_log("Passage avec YEAR : $d, $result",'debug');
270 // http://doc.spip.org/@_sqlite_func_vide
271 function _sqlite_func_vide(){