[SPIP] ~2.1.12 -->2.1.25
[velocampus/web/www.git] / www / ecrire / req / sqlite_fonctions.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2014 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
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 \***************************************************************************/
12
13 if (!defined('_ECRIRE_INC_VERSION')) return;
14
15 /*
16 * Des fonctions pour les requetes SQL
17 *
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
20 *
21 */
22 // http://doc.spip.org/@_sqlite_init_functions
23 function _sqlite_init_functions(&$sqlite){
24
25 if (!$sqlite) return false;
26
27
28 $fonctions = array(
29 'CONCAT' => array( '_sqlite_func_concat' ,2),
30 'CEIL' => array( '_sqlite_func_ceil', 1), // absent de sqlite2
31
32 'DATE_FORMAT' => array( '_sqlite_func_strftime' ,2),
33 'DAYOFMONTH' => array( '_sqlite_func_dayofmonth' ,1),
34
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
38
39 'IF' => array( '_sqlite_func_if' ,3),
40 'INSERT' => array( '_sqlite_func_insert' ,4),
41 'INSTR' => array( '_sqlite_func_instr' ,2),
42
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
48
49 'NOW' => array( '_sqlite_func_now' ,0),
50
51 'MD5' => array( 'md5' ,1),
52 'MONTH' => array( '_sqlite_func_month' ,1),
53
54 'PREG_REPLACE' => array( '_sqlite_func_preg_replace' ,3),
55
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
59
60 'RIGHT' => array( '_sqlite_func_right' ,2),
61 # 'RTRIM' => array( 'rtrim' ,1), // present en theorie
62
63 'SETTYPE' => array( 'settype' ,2), // CAST present en v3.2.3
64 'SQRT' => array( 'sqrt' ,1),
65 'SUBSTRING' => array( 'substr' ,3),
66
67 'TO_DAYS' => array( '_sqlite_func_to_days' ,1),
68 # 'TRIM' => array( 'trim' ,1), // present en theorie
69
70 'UNIX_TIMESTAMP'=> array( '_sqlite_func_unix_timestamp' ,1),
71 # 'UPPER' => array( 'strtoupper' ,1), // present v2.4
72
73 'VIDE' => array( '_sqlite_func_vide' ,0), // du vide pour SELECT 0 as x ... ORDER BY x -> ORDER BY vide()
74
75 'YEAR' => array( '_sqlite_func_year' ,1)
76 );
77
78
79 foreach ($fonctions as $f=>$r){
80 _sqlite_add_function($sqlite, $f, $r);
81 }
82
83 #spip_log('functions sqlite chargees ');
84 }
85
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)){
90 isset($r[1])
91 ?$sqlite->sqliteCreateFunction($f, $r[0], $r[1])
92 :$sqlite->sqliteCreateFunction($f, $r[0]);
93 } else {
94 isset($r[1])
95 ?sqlite_create_function($sqlite, $f, $r[0], $r[1])
96 :sqlite_create_function($sqlite, $f, $r[0]);
97 }
98 }
99
100 //
101 // SQLite : fonctions sqlite -> php
102 // entre autre auteurs : mlebas
103 //
104
105 function _sqlite_func_ceil($a) {
106 return ceil($a);
107 }
108
109 // http://doc.spip.org/@_sqlite_func_concat
110 function _sqlite_func_concat ($a, $b) {
111 return $a.$b;
112 }
113
114
115 // http://doc.spip.org/@_sqlite_func_dayofmonth
116 function _sqlite_func_dayofmonth ($d) {
117 if (!$d){
118 $result = date("j");
119 } else {
120 preg_match(";^([0-9]{4})-([0-9]+)-([0-9]+) .*$;", $d, $f);
121 $result = $f[3];
122 }
123 #spip_log("Passage avec DAYOFMONTH : $d, $result",'debug');
124 return $result;
125 }
126
127
128 // http://doc.spip.org/@_sqlite_func_find_in_set
129 function _sqlite_func_find_in_set($num, $set) {
130 $rank=0;
131 foreach (explode(",",$set) as $v) {
132 if ($v == $num) return (++$rank);
133 $rank++;
134 }
135 return 0;
136 }
137
138 function _sqlite_func_floor($a) {
139 return floor($a);
140 }
141
142 // http://doc.spip.org/@_sqlite_func_if
143 function _sqlite_func_if ($bool, $oui, $non) {
144 return ($bool)?$oui:$non;
145 }
146
147
148 /*
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.
152 */
153 // http://doc.spip.org/@_sqlite_func_insert
154 function _sqlite_func_insert ($s, $index, $longueur, $chaine) {
155 return
156 substr($s,0, $index)
157 . $chaine
158 . substr(substr($s, $index), $longueur);
159 }
160
161
162 // http://doc.spip.org/@_sqlite_func_instr
163 function _sqlite_func_instr ($s, $search) {
164 return strpos($s,$search);
165 }
166
167
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');
173 return $least;
174 }
175
176
177 // http://doc.spip.org/@_sqlite_func_left
178 function _sqlite_func_left ($s, $lenght) {
179 return substr($s,$lenght);
180 }
181
182
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');
187 return $result;
188 }
189
190
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);
196 return $f[2];
197 }
198
199
200
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');
205 return $return;
206 }
207
208
209 // http://doc.spip.org/@_sqlite_func_rand
210 function _sqlite_func_rand() {
211 return rand();
212 }
213
214
215 // http://doc.spip.org/@_sqlite_func_right
216 function _sqlite_func_right ($s, $lenght) {
217 return substr($s,0 - $lenght);
218 }
219
220
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');
225 return $return;
226 }
227
228 // http://doc.spip.org/@_sqlite_func_strftime
229 function _sqlite_func_strftime($date, $conv){
230 return strftime($conv, $date);
231 }
232
233 /**
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
237 * @param string $d
238 * @return int
239 */
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');
244 return $result;
245 }
246
247
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]);
257 }
258
259
260 // http://doc.spip.org/@_sqlite_func_year
261 function _sqlite_func_year ($d) {
262 if (!$d){
263 $result = date("Y");
264 } else {
265 preg_match(";^([0-9]{4}).*$;", $d, $f);
266 $result = $f[1];
267 }
268 spip_log("Passage avec YEAR : $d, $result",'debug');
269 return $result;
270 }
271
272
273 // http://doc.spip.org/@_sqlite_func_vide
274 function _sqlite_func_vide(){
275 return;
276 }
277
278
279
280 ?>