[SPIP] +2.1.12
[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-2011 *
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 $numargs = func_num_args();
171 $arg_list = func_get_args();
172 $least=$arg_list[0];
173 for ($i = 0; $i < $numargs; $i++) {
174 if ($arg_list[$i] < $least) $least=$arg_list[$i];
175 }
176 #spip_log("Passage avec LEAST : $least",'debug');
177 return $least;
178 }
179
180
181 // http://doc.spip.org/@_sqlite_func_left
182 function _sqlite_func_left ($s, $lenght) {
183 return substr($s,$lenght);
184 }
185
186
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');
191 return $result;
192 }
193
194
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);
200 return $f[2];
201 }
202
203
204
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');
209 return $return;
210 }
211
212
213 // http://doc.spip.org/@_sqlite_func_rand
214 function _sqlite_func_rand() {
215 return rand();
216 }
217
218
219 // http://doc.spip.org/@_sqlite_func_right
220 function _sqlite_func_right ($s, $lenght) {
221 return substr($s,0 - $lenght);
222 }
223
224
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');
229 return $return;
230 }
231
232 // http://doc.spip.org/@_sqlite_func_strftime
233 function _sqlite_func_strftime($date, $conv){
234 return strftime($conv, $date);
235 }
236
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');
241 return $result;
242 }
243
244
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]);
254 }
255
256
257 // http://doc.spip.org/@_sqlite_func_year
258 function _sqlite_func_year ($d) {
259 if (!$d){
260 $result = date("Y");
261 } else {
262 preg_match(";^([0-9]{4}).*$;", $d, $f);
263 $result = $f[1];
264 }
265 spip_log("Passage avec YEAR : $d, $result",'debug');
266 return $result;
267 }
268
269
270 // http://doc.spip.org/@_sqlite_func_vide
271 function _sqlite_func_vide(){
272 return;
273 }
274
275
276
277 ?>