@@ -29,209 +29,209 @@ |
||
| 29 | 29 | */ |
| 30 | 30 | class Multis extends AbstractCollecteur { |
| 31 | 31 | |
| 32 | - protected static string $markPrefix = 'MULTI'; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * La preg pour découper et collecter les modèles |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - protected string $preg_multi; |
|
| 39 | - |
|
| 40 | - public function __construct(?string $preg = null) { |
|
| 41 | - |
|
| 42 | - $this->preg_multi = ($preg ?: '@<multi>(.*?)</multi>@sS'); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Sanitizer une collection d'occurences de multi : on sanitize chaque texte de langue séparemment |
|
| 47 | - * |
|
| 48 | - * @param array $collection |
|
| 49 | - * @param string $sanitize_callback |
|
| 50 | - * @return array |
|
| 51 | - */ |
|
| 52 | - protected function sanitizer_collection(array $collection, string $sanitize_callback): array { |
|
| 53 | - |
|
| 54 | - foreach ($collection as &$multi) { |
|
| 55 | - $changed = false; |
|
| 56 | - foreach ($multi['trads'] as $lang => $trad) { |
|
| 57 | - $t = $sanitize_callback($trad); |
|
| 58 | - if ($t !== $trad) { |
|
| 59 | - $changed = true; |
|
| 60 | - $multi['trads'][$lang] = $t; |
|
| 61 | - } |
|
| 62 | - } |
|
| 63 | - if ($changed) { |
|
| 64 | - $texte = $this->agglomerer_trads($multi['trads']); |
|
| 65 | - $multi['raw'] = str_replace($multi['texte'], $texte, $multi['raw']); |
|
| 66 | - $multi['texte'] = $texte; |
|
| 67 | - } |
|
| 68 | - } |
|
| 69 | - return $collection; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Convertit le contenu d'une balise `<multi>` en un tableau |
|
| 75 | - * |
|
| 76 | - * Exemple de blocs. |
|
| 77 | - * - `texte par défaut [fr] en français [en] en anglais` |
|
| 78 | - * - `[fr] en français [en] en anglais` |
|
| 79 | - * |
|
| 80 | - * @param string $bloc |
|
| 81 | - * Le contenu intérieur d'un bloc multi |
|
| 82 | - * @return array [code de langue => texte] |
|
| 83 | - * Peut retourner un code de langue vide, lorsqu'un texte par défaut est indiqué. |
|
| 84 | - **/ |
|
| 85 | - protected function extraire_trads($bloc) { |
|
| 86 | - $trads = []; |
|
| 87 | - |
|
| 88 | - if (strlen($bloc)) { |
|
| 89 | - $langs = $this->collecteur($bloc, ']', '[', '@[\[]([a-z]{2,3}(_[a-z]{2,3})?(_[a-z]{2,3})?)[\]]@siS'); |
|
| 90 | - $lang = ''; |
|
| 91 | - $pos_prev = 0; |
|
| 92 | - foreach ($langs as $l) { |
|
| 93 | - $pos = $l['pos']; |
|
| 94 | - if ($lang or $pos > $pos_prev) { |
|
| 95 | - $trads[$lang] = substr($bloc, $pos_prev, $pos - $pos_prev); |
|
| 96 | - } |
|
| 97 | - $lang = $l['match'][1]; |
|
| 98 | - $pos_prev = $pos + $l['length']; |
|
| 99 | - } |
|
| 100 | - $trads[$lang] = substr($bloc, $pos_prev); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - return $trads; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Recoller ensemble les trads pour reconstituer le texte dans la balise <multi>...</multi> |
|
| 108 | - * @param $trads |
|
| 109 | - * @return string |
|
| 110 | - */ |
|
| 111 | - protected function agglomerer_trads($trads) { |
|
| 112 | - $texte = ''; |
|
| 113 | - foreach ($trads as $lang => $trad) { |
|
| 114 | - if ($texte or $lang) { |
|
| 115 | - $texte .= "[$lang]"; |
|
| 116 | - } |
|
| 117 | - $texte .= $trad; |
|
| 118 | - } |
|
| 119 | - return $texte; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * @param string $texte |
|
| 124 | - * @param array $options |
|
| 125 | - * bool $collecter_liens |
|
| 126 | - * @return array |
|
| 127 | - */ |
|
| 128 | - public function collecter(string $texte, array $options = []): array { |
|
| 129 | - if (!$texte) { |
|
| 130 | - return []; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - // collecter les matchs de la preg |
|
| 134 | - $multis = $this->collecteur($texte, '', '<multi', $this->preg_multi, empty($options['detecter_presence']) ? 0 : 1); |
|
| 135 | - // si on veut seulement detecter la présence, on peut retourner tel quel |
|
| 136 | - if (empty($options['detecter_presence'])) { |
|
| 137 | - foreach ($multis as $k => &$multi) { |
|
| 138 | - $multi['texte'] = $multi['match'][1]; |
|
| 139 | - // extraire les trads du texte |
|
| 140 | - $multi['trads'] = $this->extraire_trads($multi['texte']); |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - return $multis; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Traiter les multis d'un texte |
|
| 149 | - * |
|
| 150 | - * @uses approcher_langue() |
|
| 151 | - * @uses lang_typo() |
|
| 152 | - * @uses code_echappement() |
|
| 153 | - * @uses echappe_retour() |
|
| 154 | - * |
|
| 155 | - * @param string $texte |
|
| 156 | - * @param array $options |
|
| 157 | - * ?string $lang |
|
| 158 | - * ?string $lang_defaut |
|
| 159 | - * ?bool echappe_span |
|
| 160 | - * ?bool appliquer_typo |
|
| 161 | - * @return string |
|
| 162 | - */ |
|
| 163 | - public function traiter(string $texte, array $options) { |
|
| 164 | - if ($texte) { |
|
| 165 | - |
|
| 166 | - $multis = $this->collecter($texte); |
|
| 167 | - if (!empty($multis)) { |
|
| 168 | - $lang = $options['lang'] ?? $GLOBALS['spip_lang']; |
|
| 169 | - $lang_defaut = $options['lang_defaut'] ?? _LANGUE_PAR_DEFAUT; |
|
| 170 | - $echappe_span = $options['echappe_span'] ?? false; |
|
| 171 | - $appliquer_typo = $options['appliquer_typo'] ?? true; |
|
| 172 | - |
|
| 173 | - if (!function_exists('approcher_langue')) { |
|
| 174 | - include_spip('inc/lang'); |
|
| 175 | - } |
|
| 176 | - if (!function_exists('code_echappement')) { |
|
| 177 | - include_spip('inc/texte_mini'); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - $offset_pos = 0; |
|
| 181 | - foreach ($multis as $m) { |
|
| 182 | - |
|
| 183 | - // chercher la version de la langue courante |
|
| 184 | - $trads = $m['trads']; |
|
| 185 | - if (empty($trads)) { |
|
| 186 | - $trad = ''; |
|
| 187 | - } |
|
| 188 | - elseif ($l = approcher_langue($trads, $lang)) { |
|
| 189 | - $trad = $trads[$l]; |
|
| 190 | - } else { |
|
| 191 | - if ($lang_defaut == 'aucune') { |
|
| 192 | - $trad = ''; |
|
| 193 | - } else { |
|
| 194 | - // langue absente, prendre le fr ou une langue précisée (meme comportement que inc/traduire.php) |
|
| 195 | - // ou la premiere dispo |
|
| 196 | - if (!$l = approcher_langue($trads, $lang_defaut)) { |
|
| 197 | - $l = array_keys($trads); |
|
| 198 | - $l = reset($l); |
|
| 199 | - } |
|
| 200 | - $trad = $trads[$l]; |
|
| 201 | - |
|
| 202 | - // mais typographier le texte selon les regles de celle-ci |
|
| 203 | - // Attention aux blocs multi sur plusieurs lignes |
|
| 204 | - if ($appliquer_typo) { |
|
| 205 | - $typographie = charger_fonction(lang_typo($l), 'typographie'); |
|
| 206 | - $trad = $typographie($trad); |
|
| 207 | - |
|
| 208 | - // Tester si on echappe en span ou en div |
|
| 209 | - // il ne faut pas echapper en div si propre produit un seul paragraphe |
|
| 210 | - include_spip('inc/texte'); |
|
| 211 | - $trad_propre = preg_replace(',(^<p[^>]*>|</p>$),Uims', '', propre($trad)); |
|
| 212 | - $mode = preg_match(',</?(' . _BALISES_BLOCS . ')[>[:space:]],iS', $trad_propre) ? 'div' : 'span'; |
|
| 213 | - if ($mode === 'div') { |
|
| 214 | - $trad = rtrim($trad) . "\n\n"; |
|
| 215 | - } |
|
| 216 | - $trad = code_echappement($trad, 'multi', false, $mode); |
|
| 217 | - $trad = str_replace("'", '"', inserer_attribut($trad, 'lang', $l)); |
|
| 218 | - if (lang_dir($l) !== lang_dir($lang)) { |
|
| 219 | - $trad = str_replace("'", '"', inserer_attribut($trad, 'dir', lang_dir($l))); |
|
| 220 | - } |
|
| 221 | - if (!$echappe_span) { |
|
| 222 | - $trad = echappe_retour($trad, 'multi'); |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - $texte = substr_replace($texte, $trad, $m['pos'] + $offset_pos, $m['length']); |
|
| 229 | - $offset_pos += strlen($trad) - $m['length']; |
|
| 230 | - } |
|
| 231 | - } |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - return $texte; |
|
| 235 | - } |
|
| 32 | + protected static string $markPrefix = 'MULTI'; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * La preg pour découper et collecter les modèles |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + protected string $preg_multi; |
|
| 39 | + |
|
| 40 | + public function __construct(?string $preg = null) { |
|
| 41 | + |
|
| 42 | + $this->preg_multi = ($preg ?: '@<multi>(.*?)</multi>@sS'); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Sanitizer une collection d'occurences de multi : on sanitize chaque texte de langue séparemment |
|
| 47 | + * |
|
| 48 | + * @param array $collection |
|
| 49 | + * @param string $sanitize_callback |
|
| 50 | + * @return array |
|
| 51 | + */ |
|
| 52 | + protected function sanitizer_collection(array $collection, string $sanitize_callback): array { |
|
| 53 | + |
|
| 54 | + foreach ($collection as &$multi) { |
|
| 55 | + $changed = false; |
|
| 56 | + foreach ($multi['trads'] as $lang => $trad) { |
|
| 57 | + $t = $sanitize_callback($trad); |
|
| 58 | + if ($t !== $trad) { |
|
| 59 | + $changed = true; |
|
| 60 | + $multi['trads'][$lang] = $t; |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | + if ($changed) { |
|
| 64 | + $texte = $this->agglomerer_trads($multi['trads']); |
|
| 65 | + $multi['raw'] = str_replace($multi['texte'], $texte, $multi['raw']); |
|
| 66 | + $multi['texte'] = $texte; |
|
| 67 | + } |
|
| 68 | + } |
|
| 69 | + return $collection; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Convertit le contenu d'une balise `<multi>` en un tableau |
|
| 75 | + * |
|
| 76 | + * Exemple de blocs. |
|
| 77 | + * - `texte par défaut [fr] en français [en] en anglais` |
|
| 78 | + * - `[fr] en français [en] en anglais` |
|
| 79 | + * |
|
| 80 | + * @param string $bloc |
|
| 81 | + * Le contenu intérieur d'un bloc multi |
|
| 82 | + * @return array [code de langue => texte] |
|
| 83 | + * Peut retourner un code de langue vide, lorsqu'un texte par défaut est indiqué. |
|
| 84 | + **/ |
|
| 85 | + protected function extraire_trads($bloc) { |
|
| 86 | + $trads = []; |
|
| 87 | + |
|
| 88 | + if (strlen($bloc)) { |
|
| 89 | + $langs = $this->collecteur($bloc, ']', '[', '@[\[]([a-z]{2,3}(_[a-z]{2,3})?(_[a-z]{2,3})?)[\]]@siS'); |
|
| 90 | + $lang = ''; |
|
| 91 | + $pos_prev = 0; |
|
| 92 | + foreach ($langs as $l) { |
|
| 93 | + $pos = $l['pos']; |
|
| 94 | + if ($lang or $pos > $pos_prev) { |
|
| 95 | + $trads[$lang] = substr($bloc, $pos_prev, $pos - $pos_prev); |
|
| 96 | + } |
|
| 97 | + $lang = $l['match'][1]; |
|
| 98 | + $pos_prev = $pos + $l['length']; |
|
| 99 | + } |
|
| 100 | + $trads[$lang] = substr($bloc, $pos_prev); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + return $trads; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Recoller ensemble les trads pour reconstituer le texte dans la balise <multi>...</multi> |
|
| 108 | + * @param $trads |
|
| 109 | + * @return string |
|
| 110 | + */ |
|
| 111 | + protected function agglomerer_trads($trads) { |
|
| 112 | + $texte = ''; |
|
| 113 | + foreach ($trads as $lang => $trad) { |
|
| 114 | + if ($texte or $lang) { |
|
| 115 | + $texte .= "[$lang]"; |
|
| 116 | + } |
|
| 117 | + $texte .= $trad; |
|
| 118 | + } |
|
| 119 | + return $texte; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * @param string $texte |
|
| 124 | + * @param array $options |
|
| 125 | + * bool $collecter_liens |
|
| 126 | + * @return array |
|
| 127 | + */ |
|
| 128 | + public function collecter(string $texte, array $options = []): array { |
|
| 129 | + if (!$texte) { |
|
| 130 | + return []; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + // collecter les matchs de la preg |
|
| 134 | + $multis = $this->collecteur($texte, '', '<multi', $this->preg_multi, empty($options['detecter_presence']) ? 0 : 1); |
|
| 135 | + // si on veut seulement detecter la présence, on peut retourner tel quel |
|
| 136 | + if (empty($options['detecter_presence'])) { |
|
| 137 | + foreach ($multis as $k => &$multi) { |
|
| 138 | + $multi['texte'] = $multi['match'][1]; |
|
| 139 | + // extraire les trads du texte |
|
| 140 | + $multi['trads'] = $this->extraire_trads($multi['texte']); |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + return $multis; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Traiter les multis d'un texte |
|
| 149 | + * |
|
| 150 | + * @uses approcher_langue() |
|
| 151 | + * @uses lang_typo() |
|
| 152 | + * @uses code_echappement() |
|
| 153 | + * @uses echappe_retour() |
|
| 154 | + * |
|
| 155 | + * @param string $texte |
|
| 156 | + * @param array $options |
|
| 157 | + * ?string $lang |
|
| 158 | + * ?string $lang_defaut |
|
| 159 | + * ?bool echappe_span |
|
| 160 | + * ?bool appliquer_typo |
|
| 161 | + * @return string |
|
| 162 | + */ |
|
| 163 | + public function traiter(string $texte, array $options) { |
|
| 164 | + if ($texte) { |
|
| 165 | + |
|
| 166 | + $multis = $this->collecter($texte); |
|
| 167 | + if (!empty($multis)) { |
|
| 168 | + $lang = $options['lang'] ?? $GLOBALS['spip_lang']; |
|
| 169 | + $lang_defaut = $options['lang_defaut'] ?? _LANGUE_PAR_DEFAUT; |
|
| 170 | + $echappe_span = $options['echappe_span'] ?? false; |
|
| 171 | + $appliquer_typo = $options['appliquer_typo'] ?? true; |
|
| 172 | + |
|
| 173 | + if (!function_exists('approcher_langue')) { |
|
| 174 | + include_spip('inc/lang'); |
|
| 175 | + } |
|
| 176 | + if (!function_exists('code_echappement')) { |
|
| 177 | + include_spip('inc/texte_mini'); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + $offset_pos = 0; |
|
| 181 | + foreach ($multis as $m) { |
|
| 182 | + |
|
| 183 | + // chercher la version de la langue courante |
|
| 184 | + $trads = $m['trads']; |
|
| 185 | + if (empty($trads)) { |
|
| 186 | + $trad = ''; |
|
| 187 | + } |
|
| 188 | + elseif ($l = approcher_langue($trads, $lang)) { |
|
| 189 | + $trad = $trads[$l]; |
|
| 190 | + } else { |
|
| 191 | + if ($lang_defaut == 'aucune') { |
|
| 192 | + $trad = ''; |
|
| 193 | + } else { |
|
| 194 | + // langue absente, prendre le fr ou une langue précisée (meme comportement que inc/traduire.php) |
|
| 195 | + // ou la premiere dispo |
|
| 196 | + if (!$l = approcher_langue($trads, $lang_defaut)) { |
|
| 197 | + $l = array_keys($trads); |
|
| 198 | + $l = reset($l); |
|
| 199 | + } |
|
| 200 | + $trad = $trads[$l]; |
|
| 201 | + |
|
| 202 | + // mais typographier le texte selon les regles de celle-ci |
|
| 203 | + // Attention aux blocs multi sur plusieurs lignes |
|
| 204 | + if ($appliquer_typo) { |
|
| 205 | + $typographie = charger_fonction(lang_typo($l), 'typographie'); |
|
| 206 | + $trad = $typographie($trad); |
|
| 207 | + |
|
| 208 | + // Tester si on echappe en span ou en div |
|
| 209 | + // il ne faut pas echapper en div si propre produit un seul paragraphe |
|
| 210 | + include_spip('inc/texte'); |
|
| 211 | + $trad_propre = preg_replace(',(^<p[^>]*>|</p>$),Uims', '', propre($trad)); |
|
| 212 | + $mode = preg_match(',</?(' . _BALISES_BLOCS . ')[>[:space:]],iS', $trad_propre) ? 'div' : 'span'; |
|
| 213 | + if ($mode === 'div') { |
|
| 214 | + $trad = rtrim($trad) . "\n\n"; |
|
| 215 | + } |
|
| 216 | + $trad = code_echappement($trad, 'multi', false, $mode); |
|
| 217 | + $trad = str_replace("'", '"', inserer_attribut($trad, 'lang', $l)); |
|
| 218 | + if (lang_dir($l) !== lang_dir($lang)) { |
|
| 219 | + $trad = str_replace("'", '"', inserer_attribut($trad, 'dir', lang_dir($l))); |
|
| 220 | + } |
|
| 221 | + if (!$echappe_span) { |
|
| 222 | + $trad = echappe_retour($trad, 'multi'); |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + $texte = substr_replace($texte, $trad, $m['pos'] + $offset_pos, $m['length']); |
|
| 229 | + $offset_pos += strlen($trad) - $m['length']; |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + return $texte; |
|
| 235 | + } |
|
| 236 | 236 | |
| 237 | 237 | } |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | |
| 22 | 22 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 23 | - return; |
|
| 23 | + return; |
|
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | // TODO: get/set_caracteres ? |
@@ -44,91 +44,91 @@ discard block |
||
| 44 | 44 | */ |
| 45 | 45 | function req_sqlite_dist($addr, $port, $login, $pass, $db = '', $prefixe = '', $sqlite_version = '') |
| 46 | 46 | { |
| 47 | - static $last_connect = []; |
|
| 48 | - |
|
| 49 | - // si provient de selectdb |
|
| 50 | - // un code pour etre sur que l'on vient de select_db() |
|
| 51 | - if (strpos($db, $code = '@selectdb@') !== false) { |
|
| 52 | - foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 53 | - $$a = $last_connect[$a]; |
|
| 54 | - } |
|
| 55 | - $db = str_replace($code, '', $db); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /* |
|
| 47 | + static $last_connect = []; |
|
| 48 | + |
|
| 49 | + // si provient de selectdb |
|
| 50 | + // un code pour etre sur que l'on vient de select_db() |
|
| 51 | + if (strpos($db, $code = '@selectdb@') !== false) { |
|
| 52 | + foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 53 | + $$a = $last_connect[$a]; |
|
| 54 | + } |
|
| 55 | + $db = str_replace($code, '', $db); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /* |
|
| 59 | 59 | * En sqlite, seule l'adresse du fichier est importante. |
| 60 | 60 | * Ce sera $db le nom, |
| 61 | 61 | * le path est $addr |
| 62 | 62 | * (_DIR_DB si $addr est vide) |
| 63 | 63 | */ |
| 64 | - _sqlite_init(); |
|
| 65 | - |
|
| 66 | - // determiner le dossier de la base : $addr ou _DIR_DB |
|
| 67 | - $f = _DIR_DB; |
|
| 68 | - if ($addr and str_contains($addr, '/')) { |
|
| 69 | - $f = rtrim($addr, '/') . '/'; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - // un nom de base demande et impossible d'obtenir la base, on s'en va : |
|
| 73 | - // il faut que la base existe ou que le repertoire parent soit writable |
|
| 74 | - if ($db and !is_file($f .= $db . '.sqlite') and !is_writable(dirname($f))) { |
|
| 75 | - spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.' . _LOG_HS); |
|
| 76 | - |
|
| 77 | - return false; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - // charger les modules sqlite au besoin |
|
| 81 | - if (!_sqlite_charger_version($sqlite_version)) { |
|
| 82 | - spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.' . _LOG_HS); |
|
| 83 | - |
|
| 84 | - return false; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - // chargement des constantes |
|
| 88 | - // il ne faut pas definir les constantes avant d'avoir charge les modules sqlite |
|
| 89 | - $define = 'spip_sqlite' . $sqlite_version . '_constantes'; |
|
| 90 | - $define(); |
|
| 91 | - |
|
| 92 | - $ok = false; |
|
| 93 | - if (!$db) { |
|
| 94 | - // si pas de db -> |
|
| 95 | - // base temporaire tant qu'on ne connait pas son vrai nom |
|
| 96 | - // pour tester la connexion |
|
| 97 | - $db = '_sqlite' . $sqlite_version . '_install'; |
|
| 98 | - $tmp = _DIR_DB . $db . '.sqlite'; |
|
| 99 | - $ok = $link = new \PDO("sqlite:$tmp"); |
|
| 100 | - } else { |
|
| 101 | - // Ouvrir (eventuellement creer la base) |
|
| 102 | - $ok = $link = new \PDO("sqlite:$f"); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - if (!$ok) { |
|
| 106 | - $e = _sqlite_last_error_from_link($link); |
|
| 107 | - spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f : $e", 'sqlite.' . _LOG_HS); |
|
| 108 | - |
|
| 109 | - return false; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - if ($link) { |
|
| 113 | - $last_connect = [ |
|
| 114 | - 'addr' => $addr, |
|
| 115 | - 'port' => $port, |
|
| 116 | - 'login' => $login, |
|
| 117 | - 'pass' => $pass, |
|
| 118 | - 'db' => $db, |
|
| 119 | - 'prefixe' => $prefixe, |
|
| 120 | - ]; |
|
| 121 | - // etre sur qu'on definit bien les fonctions a chaque nouvelle connexion |
|
| 122 | - include_spip('req/sqlite_fonctions'); |
|
| 123 | - _sqlite_init_functions($link); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return [ |
|
| 127 | - 'db' => $db, |
|
| 128 | - 'prefixe' => $prefixe ? $prefixe : $db, |
|
| 129 | - 'link' => $link, |
|
| 130 | - 'total_requetes' => 0, |
|
| 131 | - ]; |
|
| 64 | + _sqlite_init(); |
|
| 65 | + |
|
| 66 | + // determiner le dossier de la base : $addr ou _DIR_DB |
|
| 67 | + $f = _DIR_DB; |
|
| 68 | + if ($addr and str_contains($addr, '/')) { |
|
| 69 | + $f = rtrim($addr, '/') . '/'; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + // un nom de base demande et impossible d'obtenir la base, on s'en va : |
|
| 73 | + // il faut que la base existe ou que le repertoire parent soit writable |
|
| 74 | + if ($db and !is_file($f .= $db . '.sqlite') and !is_writable(dirname($f))) { |
|
| 75 | + spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.' . _LOG_HS); |
|
| 76 | + |
|
| 77 | + return false; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + // charger les modules sqlite au besoin |
|
| 81 | + if (!_sqlite_charger_version($sqlite_version)) { |
|
| 82 | + spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.' . _LOG_HS); |
|
| 83 | + |
|
| 84 | + return false; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + // chargement des constantes |
|
| 88 | + // il ne faut pas definir les constantes avant d'avoir charge les modules sqlite |
|
| 89 | + $define = 'spip_sqlite' . $sqlite_version . '_constantes'; |
|
| 90 | + $define(); |
|
| 91 | + |
|
| 92 | + $ok = false; |
|
| 93 | + if (!$db) { |
|
| 94 | + // si pas de db -> |
|
| 95 | + // base temporaire tant qu'on ne connait pas son vrai nom |
|
| 96 | + // pour tester la connexion |
|
| 97 | + $db = '_sqlite' . $sqlite_version . '_install'; |
|
| 98 | + $tmp = _DIR_DB . $db . '.sqlite'; |
|
| 99 | + $ok = $link = new \PDO("sqlite:$tmp"); |
|
| 100 | + } else { |
|
| 101 | + // Ouvrir (eventuellement creer la base) |
|
| 102 | + $ok = $link = new \PDO("sqlite:$f"); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + if (!$ok) { |
|
| 106 | + $e = _sqlite_last_error_from_link($link); |
|
| 107 | + spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f : $e", 'sqlite.' . _LOG_HS); |
|
| 108 | + |
|
| 109 | + return false; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + if ($link) { |
|
| 113 | + $last_connect = [ |
|
| 114 | + 'addr' => $addr, |
|
| 115 | + 'port' => $port, |
|
| 116 | + 'login' => $login, |
|
| 117 | + 'pass' => $pass, |
|
| 118 | + 'db' => $db, |
|
| 119 | + 'prefixe' => $prefixe, |
|
| 120 | + ]; |
|
| 121 | + // etre sur qu'on definit bien les fonctions a chaque nouvelle connexion |
|
| 122 | + include_spip('req/sqlite_fonctions'); |
|
| 123 | + _sqlite_init_functions($link); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return [ |
|
| 127 | + 'db' => $db, |
|
| 128 | + 'prefixe' => $prefixe ? $prefixe : $db, |
|
| 129 | + 'link' => $link, |
|
| 130 | + 'total_requetes' => 0, |
|
| 131 | + ]; |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | |
@@ -148,14 +148,14 @@ discard block |
||
| 148 | 148 | */ |
| 149 | 149 | function spip_sqlite_query($query, $serveur = '', $requeter = true) |
| 150 | 150 | { |
| 151 | - #spip_log("spip_sqlite_query() > $query",'sqlite.'._LOG_DEBUG); |
|
| 152 | - #_sqlite_init(); // fait la premiere fois dans spip_sqlite |
|
| 153 | - $query = Sqlite::traduire_requete($query, $serveur); |
|
| 154 | - if (!$requeter) { |
|
| 155 | - return $query; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - return Sqlite::executer_requete($query, $serveur); |
|
| 151 | + #spip_log("spip_sqlite_query() > $query",'sqlite.'._LOG_DEBUG); |
|
| 152 | + #_sqlite_init(); // fait la premiere fois dans spip_sqlite |
|
| 153 | + $query = Sqlite::traduire_requete($query, $serveur); |
|
| 154 | + if (!$requeter) { |
|
| 155 | + return $query; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + return Sqlite::executer_requete($query, $serveur); |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | |
@@ -173,11 +173,11 @@ discard block |
||
| 173 | 173 | function spip_sqlite_alter($query, $serveur = '', $requeter = true) |
| 174 | 174 | { |
| 175 | 175 | |
| 176 | - $query = spip_sqlite_query("ALTER $query", $serveur, false); |
|
| 177 | - // traduire la requete pour recuperer les bons noms de table |
|
| 178 | - $query = Sqlite::traduire_requete($query, $serveur); |
|
| 176 | + $query = spip_sqlite_query("ALTER $query", $serveur, false); |
|
| 177 | + // traduire la requete pour recuperer les bons noms de table |
|
| 178 | + $query = Sqlite::traduire_requete($query, $serveur); |
|
| 179 | 179 | |
| 180 | - /* |
|
| 180 | + /* |
|
| 181 | 181 | * la il faut faire les transformations |
| 182 | 182 | * si ALTER TABLE x (DROP|CHANGE) y |
| 183 | 183 | * |
@@ -186,251 +186,251 @@ discard block |
||
| 186 | 186 | * 3) faire chaque requete independemment |
| 187 | 187 | */ |
| 188 | 188 | |
| 189 | - // 1 |
|
| 190 | - if (preg_match('/\s*(ALTER(\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 191 | - $debut = $regs[1]; |
|
| 192 | - $table = $regs[3]; |
|
| 193 | - $suite = $regs[4]; |
|
| 194 | - } else { |
|
| 195 | - spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.' . _LOG_ERREUR); |
|
| 196 | - |
|
| 197 | - return false; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - // 2 |
|
| 201 | - // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 202 | - // tout en cassant "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... en deux |
|
| 203 | - // ou revoir l'api de sql_alter en creant un |
|
| 204 | - // sql_alter_table($table,array($actions)); |
|
| 205 | - $todo = explode(',', $suite); |
|
| 206 | - |
|
| 207 | - // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 208 | - $todo2 = []; |
|
| 209 | - $i = 0; |
|
| 210 | - $ouverte = false; |
|
| 211 | - while ($do = array_shift($todo)) { |
|
| 212 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 213 | - $o = (str_contains($do, '(')); |
|
| 214 | - $f = (str_contains($do, ')')); |
|
| 215 | - if ($o and !$f) { |
|
| 216 | - $ouverte = true; |
|
| 217 | - } elseif ($f) { |
|
| 218 | - $ouverte = false; |
|
| 219 | - } |
|
| 220 | - if (!$ouverte) { |
|
| 221 | - $i++; |
|
| 222 | - } |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - // 3 |
|
| 226 | - $resultats = []; |
|
| 227 | - foreach ($todo2 as $do) { |
|
| 228 | - $do = trim($do); |
|
| 229 | - if ( |
|
| 230 | - !preg_match('/(DROP PRIMARY KEY|DROP KEY|DROP INDEX|DROP COLUMN|DROP' |
|
| 231 | - . '|CHANGE COLUMN|CHANGE|MODIFY|RENAME TO|RENAME' |
|
| 232 | - . '|ADD PRIMARY KEY|ADD KEY|ADD INDEX|ADD UNIQUE KEY|ADD UNIQUE' |
|
| 233 | - . '|ADD COLUMN|ADD' |
|
| 234 | - . ')\s*([^\s]*)\s*(.*)?/i', $do, $matches) |
|
| 235 | - ) { |
|
| 236 | - spip_log( |
|
| 237 | - "SQLite : Probleme de ALTER TABLE, utilisation non reconnue dans : $do \n(requete d'origine : $query)", |
|
| 238 | - 'sqlite.' . _LOG_ERREUR |
|
| 239 | - ); |
|
| 240 | - |
|
| 241 | - return false; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - $cle = strtoupper($matches[1]); |
|
| 245 | - $colonne_origine = $matches[2]; |
|
| 246 | - $colonne_destination = ''; |
|
| 247 | - |
|
| 248 | - $def = $matches[3]; |
|
| 249 | - |
|
| 250 | - // eluder une eventuelle clause before|after|first inutilisable |
|
| 251 | - $defr = rtrim(preg_replace('/(BEFORE|AFTER|FIRST)(.*)$/is', '', $def)); |
|
| 252 | - $defo = $defr; // garder la def d'origine pour certains cas |
|
| 253 | - // remplacer les definitions venant de mysql |
|
| 254 | - $defr = _sqlite_remplacements_definitions_table($defr); |
|
| 255 | - |
|
| 256 | - // reinjecter dans le do |
|
| 257 | - $do = str_replace($def, $defr, $do); |
|
| 258 | - $def = $defr; |
|
| 259 | - |
|
| 260 | - switch ($cle) { |
|
| 261 | - // suppression d'un index |
|
| 262 | - case 'DROP KEY': |
|
| 263 | - case 'DROP INDEX': |
|
| 264 | - $nom_index = $colonne_origine; |
|
| 265 | - spip_sqlite_drop_index($nom_index, $table, $serveur); |
|
| 266 | - break; |
|
| 267 | - |
|
| 268 | - // suppression d'une pk |
|
| 269 | - case 'DROP PRIMARY KEY': |
|
| 270 | - if ( |
|
| 271 | - !_sqlite_modifier_table( |
|
| 272 | - $table, |
|
| 273 | - $colonne_origine, |
|
| 274 | - ['key' => ['PRIMARY KEY' => '']], |
|
| 275 | - $serveur |
|
| 276 | - ) |
|
| 277 | - ) { |
|
| 278 | - return false; |
|
| 279 | - } |
|
| 280 | - break; |
|
| 281 | - // suppression d'une colonne |
|
| 282 | - case 'DROP COLUMN': |
|
| 283 | - case 'DROP': |
|
| 284 | - if ( |
|
| 285 | - !_sqlite_modifier_table( |
|
| 286 | - $table, |
|
| 287 | - [$colonne_origine => ''], |
|
| 288 | - [], |
|
| 289 | - $serveur |
|
| 290 | - ) |
|
| 291 | - ) { |
|
| 292 | - return false; |
|
| 293 | - } |
|
| 294 | - break; |
|
| 295 | - |
|
| 296 | - case 'CHANGE COLUMN': |
|
| 297 | - case 'CHANGE': |
|
| 298 | - // recuperer le nom de la future colonne |
|
| 299 | - // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 300 | - // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 301 | - $def = trim($defo); |
|
| 302 | - $colonne_destination = substr($def, 0, strpos($def, ' ')); |
|
| 303 | - $def = substr($def, strlen($colonne_destination) + 1); |
|
| 304 | - |
|
| 305 | - if ( |
|
| 306 | - !_sqlite_modifier_table( |
|
| 307 | - $table, |
|
| 308 | - [$colonne_origine => $colonne_destination], |
|
| 309 | - ['field' => [$colonne_destination => $def]], |
|
| 310 | - $serveur |
|
| 311 | - ) |
|
| 312 | - ) { |
|
| 313 | - return false; |
|
| 314 | - } |
|
| 315 | - break; |
|
| 316 | - |
|
| 317 | - case 'MODIFY': |
|
| 318 | - // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 319 | - // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 320 | - if ( |
|
| 321 | - !_sqlite_modifier_table( |
|
| 322 | - $table, |
|
| 323 | - $colonne_origine, |
|
| 324 | - ['field' => [$colonne_origine => $defo]], |
|
| 325 | - $serveur |
|
| 326 | - ) |
|
| 327 | - ) { |
|
| 328 | - return false; |
|
| 329 | - } |
|
| 330 | - break; |
|
| 331 | - |
|
| 332 | - // pas geres en sqlite2 |
|
| 333 | - case 'RENAME': |
|
| 334 | - $do = 'RENAME TO' . substr($do, 6); |
|
| 335 | - case 'RENAME TO': |
|
| 336 | - if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 337 | - spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 338 | - |
|
| 339 | - return false; |
|
| 340 | - } |
|
| 341 | - break; |
|
| 342 | - |
|
| 343 | - // ajout d'une pk |
|
| 344 | - case 'ADD PRIMARY KEY': |
|
| 345 | - $pk = trim(substr($do, 16)); |
|
| 346 | - $pk = ($pk[0] == '(') ? substr($pk, 1, -1) : $pk; |
|
| 347 | - if ( |
|
| 348 | - !_sqlite_modifier_table( |
|
| 349 | - $table, |
|
| 350 | - $colonne_origine, |
|
| 351 | - ['key' => ['PRIMARY KEY' => $pk]], |
|
| 352 | - $serveur |
|
| 353 | - ) |
|
| 354 | - ) { |
|
| 355 | - return false; |
|
| 356 | - } |
|
| 357 | - break; |
|
| 358 | - // ajout d'un index |
|
| 359 | - case 'ADD UNIQUE KEY': |
|
| 360 | - case 'ADD UNIQUE': |
|
| 361 | - $unique = true; |
|
| 362 | - case 'ADD INDEX': |
|
| 363 | - case 'ADD KEY': |
|
| 364 | - if (!isset($unique)) { |
|
| 365 | - $unique = false; |
|
| 366 | - } |
|
| 367 | - // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 368 | - // bug potentiel si qqn met "(colonne, colonne)" |
|
| 369 | - // |
|
| 370 | - // nom_index (colonnes) |
|
| 371 | - if ($def) { |
|
| 372 | - $colonnes = substr($def, 1, -1); |
|
| 373 | - $nom_index = $colonne_origine; |
|
| 374 | - } else { |
|
| 375 | - // (colonne) |
|
| 376 | - if ($colonne_origine[0] == '(') { |
|
| 377 | - $colonnes = substr($colonne_origine, 1, -1); |
|
| 378 | - if (str_contains(',', $colonnes)) { |
|
| 379 | - spip_log('SQLite : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 380 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite.' . _LOG_ERREUR); |
|
| 381 | - break; |
|
| 382 | - } else { |
|
| 383 | - $nom_index = $colonnes; |
|
| 384 | - } |
|
| 385 | - } // nom_index |
|
| 386 | - else { |
|
| 387 | - $nom_index = $colonnes = $colonne_origine; |
|
| 388 | - } |
|
| 389 | - } |
|
| 390 | - spip_sqlite_create_index($nom_index, $table, $colonnes, $unique, $serveur); |
|
| 391 | - break; |
|
| 392 | - |
|
| 393 | - // pas geres en sqlite2 |
|
| 394 | - case 'ADD COLUMN': |
|
| 395 | - $do = 'ADD' . substr($do, 10); |
|
| 396 | - case 'ADD': |
|
| 397 | - default: |
|
| 398 | - if (!preg_match(',primary\s+key,i', $do)) { |
|
| 399 | - if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 400 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 401 | - |
|
| 402 | - return false; |
|
| 403 | - } |
|
| 404 | - break; |
|
| 405 | - } |
|
| 406 | - // ou si la colonne est aussi primary key |
|
| 407 | - // cas du add id_truc int primary key |
|
| 408 | - // ajout d'une colonne qui passe en primary key directe |
|
| 409 | - else { |
|
| 410 | - $def = trim(substr($do, 3)); |
|
| 411 | - $colonne_ajoutee = substr($def, 0, strpos($def, ' ')); |
|
| 412 | - $def = substr($def, strlen($colonne_ajoutee) + 1); |
|
| 413 | - $opts = []; |
|
| 414 | - if (preg_match(',primary\s+key,i', $def)) { |
|
| 415 | - $opts['key'] = ['PRIMARY KEY' => $colonne_ajoutee]; |
|
| 416 | - $def = preg_replace(',primary\s+key,i', '', $def); |
|
| 417 | - } |
|
| 418 | - $opts['field'] = [$colonne_ajoutee => $def]; |
|
| 419 | - if (!_sqlite_modifier_table($table, [$colonne_ajoutee], $opts, $serveur)) { |
|
| 420 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 421 | - |
|
| 422 | - return false; |
|
| 423 | - } |
|
| 424 | - } |
|
| 425 | - break; |
|
| 426 | - } |
|
| 427 | - // tout est bon, ouf ! |
|
| 428 | - spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.' . _LOG_INFO); |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.' . _LOG_INFO); |
|
| 432 | - |
|
| 433 | - return true; |
|
| 189 | + // 1 |
|
| 190 | + if (preg_match('/\s*(ALTER(\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 191 | + $debut = $regs[1]; |
|
| 192 | + $table = $regs[3]; |
|
| 193 | + $suite = $regs[4]; |
|
| 194 | + } else { |
|
| 195 | + spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.' . _LOG_ERREUR); |
|
| 196 | + |
|
| 197 | + return false; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + // 2 |
|
| 201 | + // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 202 | + // tout en cassant "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... en deux |
|
| 203 | + // ou revoir l'api de sql_alter en creant un |
|
| 204 | + // sql_alter_table($table,array($actions)); |
|
| 205 | + $todo = explode(',', $suite); |
|
| 206 | + |
|
| 207 | + // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 208 | + $todo2 = []; |
|
| 209 | + $i = 0; |
|
| 210 | + $ouverte = false; |
|
| 211 | + while ($do = array_shift($todo)) { |
|
| 212 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 213 | + $o = (str_contains($do, '(')); |
|
| 214 | + $f = (str_contains($do, ')')); |
|
| 215 | + if ($o and !$f) { |
|
| 216 | + $ouverte = true; |
|
| 217 | + } elseif ($f) { |
|
| 218 | + $ouverte = false; |
|
| 219 | + } |
|
| 220 | + if (!$ouverte) { |
|
| 221 | + $i++; |
|
| 222 | + } |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + // 3 |
|
| 226 | + $resultats = []; |
|
| 227 | + foreach ($todo2 as $do) { |
|
| 228 | + $do = trim($do); |
|
| 229 | + if ( |
|
| 230 | + !preg_match('/(DROP PRIMARY KEY|DROP KEY|DROP INDEX|DROP COLUMN|DROP' |
|
| 231 | + . '|CHANGE COLUMN|CHANGE|MODIFY|RENAME TO|RENAME' |
|
| 232 | + . '|ADD PRIMARY KEY|ADD KEY|ADD INDEX|ADD UNIQUE KEY|ADD UNIQUE' |
|
| 233 | + . '|ADD COLUMN|ADD' |
|
| 234 | + . ')\s*([^\s]*)\s*(.*)?/i', $do, $matches) |
|
| 235 | + ) { |
|
| 236 | + spip_log( |
|
| 237 | + "SQLite : Probleme de ALTER TABLE, utilisation non reconnue dans : $do \n(requete d'origine : $query)", |
|
| 238 | + 'sqlite.' . _LOG_ERREUR |
|
| 239 | + ); |
|
| 240 | + |
|
| 241 | + return false; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + $cle = strtoupper($matches[1]); |
|
| 245 | + $colonne_origine = $matches[2]; |
|
| 246 | + $colonne_destination = ''; |
|
| 247 | + |
|
| 248 | + $def = $matches[3]; |
|
| 249 | + |
|
| 250 | + // eluder une eventuelle clause before|after|first inutilisable |
|
| 251 | + $defr = rtrim(preg_replace('/(BEFORE|AFTER|FIRST)(.*)$/is', '', $def)); |
|
| 252 | + $defo = $defr; // garder la def d'origine pour certains cas |
|
| 253 | + // remplacer les definitions venant de mysql |
|
| 254 | + $defr = _sqlite_remplacements_definitions_table($defr); |
|
| 255 | + |
|
| 256 | + // reinjecter dans le do |
|
| 257 | + $do = str_replace($def, $defr, $do); |
|
| 258 | + $def = $defr; |
|
| 259 | + |
|
| 260 | + switch ($cle) { |
|
| 261 | + // suppression d'un index |
|
| 262 | + case 'DROP KEY': |
|
| 263 | + case 'DROP INDEX': |
|
| 264 | + $nom_index = $colonne_origine; |
|
| 265 | + spip_sqlite_drop_index($nom_index, $table, $serveur); |
|
| 266 | + break; |
|
| 267 | + |
|
| 268 | + // suppression d'une pk |
|
| 269 | + case 'DROP PRIMARY KEY': |
|
| 270 | + if ( |
|
| 271 | + !_sqlite_modifier_table( |
|
| 272 | + $table, |
|
| 273 | + $colonne_origine, |
|
| 274 | + ['key' => ['PRIMARY KEY' => '']], |
|
| 275 | + $serveur |
|
| 276 | + ) |
|
| 277 | + ) { |
|
| 278 | + return false; |
|
| 279 | + } |
|
| 280 | + break; |
|
| 281 | + // suppression d'une colonne |
|
| 282 | + case 'DROP COLUMN': |
|
| 283 | + case 'DROP': |
|
| 284 | + if ( |
|
| 285 | + !_sqlite_modifier_table( |
|
| 286 | + $table, |
|
| 287 | + [$colonne_origine => ''], |
|
| 288 | + [], |
|
| 289 | + $serveur |
|
| 290 | + ) |
|
| 291 | + ) { |
|
| 292 | + return false; |
|
| 293 | + } |
|
| 294 | + break; |
|
| 295 | + |
|
| 296 | + case 'CHANGE COLUMN': |
|
| 297 | + case 'CHANGE': |
|
| 298 | + // recuperer le nom de la future colonne |
|
| 299 | + // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 300 | + // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 301 | + $def = trim($defo); |
|
| 302 | + $colonne_destination = substr($def, 0, strpos($def, ' ')); |
|
| 303 | + $def = substr($def, strlen($colonne_destination) + 1); |
|
| 304 | + |
|
| 305 | + if ( |
|
| 306 | + !_sqlite_modifier_table( |
|
| 307 | + $table, |
|
| 308 | + [$colonne_origine => $colonne_destination], |
|
| 309 | + ['field' => [$colonne_destination => $def]], |
|
| 310 | + $serveur |
|
| 311 | + ) |
|
| 312 | + ) { |
|
| 313 | + return false; |
|
| 314 | + } |
|
| 315 | + break; |
|
| 316 | + |
|
| 317 | + case 'MODIFY': |
|
| 318 | + // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 319 | + // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 320 | + if ( |
|
| 321 | + !_sqlite_modifier_table( |
|
| 322 | + $table, |
|
| 323 | + $colonne_origine, |
|
| 324 | + ['field' => [$colonne_origine => $defo]], |
|
| 325 | + $serveur |
|
| 326 | + ) |
|
| 327 | + ) { |
|
| 328 | + return false; |
|
| 329 | + } |
|
| 330 | + break; |
|
| 331 | + |
|
| 332 | + // pas geres en sqlite2 |
|
| 333 | + case 'RENAME': |
|
| 334 | + $do = 'RENAME TO' . substr($do, 6); |
|
| 335 | + case 'RENAME TO': |
|
| 336 | + if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 337 | + spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 338 | + |
|
| 339 | + return false; |
|
| 340 | + } |
|
| 341 | + break; |
|
| 342 | + |
|
| 343 | + // ajout d'une pk |
|
| 344 | + case 'ADD PRIMARY KEY': |
|
| 345 | + $pk = trim(substr($do, 16)); |
|
| 346 | + $pk = ($pk[0] == '(') ? substr($pk, 1, -1) : $pk; |
|
| 347 | + if ( |
|
| 348 | + !_sqlite_modifier_table( |
|
| 349 | + $table, |
|
| 350 | + $colonne_origine, |
|
| 351 | + ['key' => ['PRIMARY KEY' => $pk]], |
|
| 352 | + $serveur |
|
| 353 | + ) |
|
| 354 | + ) { |
|
| 355 | + return false; |
|
| 356 | + } |
|
| 357 | + break; |
|
| 358 | + // ajout d'un index |
|
| 359 | + case 'ADD UNIQUE KEY': |
|
| 360 | + case 'ADD UNIQUE': |
|
| 361 | + $unique = true; |
|
| 362 | + case 'ADD INDEX': |
|
| 363 | + case 'ADD KEY': |
|
| 364 | + if (!isset($unique)) { |
|
| 365 | + $unique = false; |
|
| 366 | + } |
|
| 367 | + // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 368 | + // bug potentiel si qqn met "(colonne, colonne)" |
|
| 369 | + // |
|
| 370 | + // nom_index (colonnes) |
|
| 371 | + if ($def) { |
|
| 372 | + $colonnes = substr($def, 1, -1); |
|
| 373 | + $nom_index = $colonne_origine; |
|
| 374 | + } else { |
|
| 375 | + // (colonne) |
|
| 376 | + if ($colonne_origine[0] == '(') { |
|
| 377 | + $colonnes = substr($colonne_origine, 1, -1); |
|
| 378 | + if (str_contains(',', $colonnes)) { |
|
| 379 | + spip_log('SQLite : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 380 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite.' . _LOG_ERREUR); |
|
| 381 | + break; |
|
| 382 | + } else { |
|
| 383 | + $nom_index = $colonnes; |
|
| 384 | + } |
|
| 385 | + } // nom_index |
|
| 386 | + else { |
|
| 387 | + $nom_index = $colonnes = $colonne_origine; |
|
| 388 | + } |
|
| 389 | + } |
|
| 390 | + spip_sqlite_create_index($nom_index, $table, $colonnes, $unique, $serveur); |
|
| 391 | + break; |
|
| 392 | + |
|
| 393 | + // pas geres en sqlite2 |
|
| 394 | + case 'ADD COLUMN': |
|
| 395 | + $do = 'ADD' . substr($do, 10); |
|
| 396 | + case 'ADD': |
|
| 397 | + default: |
|
| 398 | + if (!preg_match(',primary\s+key,i', $do)) { |
|
| 399 | + if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 400 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 401 | + |
|
| 402 | + return false; |
|
| 403 | + } |
|
| 404 | + break; |
|
| 405 | + } |
|
| 406 | + // ou si la colonne est aussi primary key |
|
| 407 | + // cas du add id_truc int primary key |
|
| 408 | + // ajout d'une colonne qui passe en primary key directe |
|
| 409 | + else { |
|
| 410 | + $def = trim(substr($do, 3)); |
|
| 411 | + $colonne_ajoutee = substr($def, 0, strpos($def, ' ')); |
|
| 412 | + $def = substr($def, strlen($colonne_ajoutee) + 1); |
|
| 413 | + $opts = []; |
|
| 414 | + if (preg_match(',primary\s+key,i', $def)) { |
|
| 415 | + $opts['key'] = ['PRIMARY KEY' => $colonne_ajoutee]; |
|
| 416 | + $def = preg_replace(',primary\s+key,i', '', $def); |
|
| 417 | + } |
|
| 418 | + $opts['field'] = [$colonne_ajoutee => $def]; |
|
| 419 | + if (!_sqlite_modifier_table($table, [$colonne_ajoutee], $opts, $serveur)) { |
|
| 420 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 421 | + |
|
| 422 | + return false; |
|
| 423 | + } |
|
| 424 | + } |
|
| 425 | + break; |
|
| 426 | + } |
|
| 427 | + // tout est bon, ouf ! |
|
| 428 | + spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.' . _LOG_INFO); |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.' . _LOG_INFO); |
|
| 432 | + |
|
| 433 | + return true; |
|
| 434 | 434 | } |
| 435 | 435 | |
| 436 | 436 | /** |
@@ -452,38 +452,38 @@ discard block |
||
| 452 | 452 | * - true si la requête réussie, false sinon. |
| 453 | 453 | */ |
| 454 | 454 | function spip_sqlite_create( |
| 455 | - $nom, |
|
| 456 | - $champs, |
|
| 457 | - $cles, |
|
| 458 | - $autoinc = false, |
|
| 459 | - $temporary = false, |
|
| 460 | - $serveur = '', |
|
| 461 | - $requeter = true |
|
| 455 | + $nom, |
|
| 456 | + $champs, |
|
| 457 | + $cles, |
|
| 458 | + $autoinc = false, |
|
| 459 | + $temporary = false, |
|
| 460 | + $serveur = '', |
|
| 461 | + $requeter = true |
|
| 462 | 462 | ) { |
| 463 | - $query = _sqlite_requete_create($nom, $champs, $cles, $autoinc, $temporary, $ifnotexists = true, $serveur, $requeter); |
|
| 464 | - if (!$query) { |
|
| 465 | - return false; |
|
| 466 | - } |
|
| 467 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 468 | - |
|
| 469 | - // SQLite ne cree pas les KEY sur les requetes CREATE TABLE |
|
| 470 | - // il faut donc les faire creer ensuite |
|
| 471 | - if (!$requeter) { |
|
| 472 | - return $res; |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - $ok = $res ? true : false; |
|
| 476 | - if ($ok) { |
|
| 477 | - foreach ($cles as $k => $v) { |
|
| 478 | - if (preg_match(',^(UNIQUE KEY|KEY|UNIQUE)\s,i', $k, $m)) { |
|
| 479 | - $index = trim(substr($k, strlen($m[1]))); |
|
| 480 | - $unique = (strlen($m[1]) > 3); |
|
| 481 | - $ok &= spip_sqlite_create_index($index, $nom, $v, $unique, $serveur); |
|
| 482 | - } |
|
| 483 | - } |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - return $ok ? true : false; |
|
| 463 | + $query = _sqlite_requete_create($nom, $champs, $cles, $autoinc, $temporary, $ifnotexists = true, $serveur, $requeter); |
|
| 464 | + if (!$query) { |
|
| 465 | + return false; |
|
| 466 | + } |
|
| 467 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 468 | + |
|
| 469 | + // SQLite ne cree pas les KEY sur les requetes CREATE TABLE |
|
| 470 | + // il faut donc les faire creer ensuite |
|
| 471 | + if (!$requeter) { |
|
| 472 | + return $res; |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + $ok = $res ? true : false; |
|
| 476 | + if ($ok) { |
|
| 477 | + foreach ($cles as $k => $v) { |
|
| 478 | + if (preg_match(',^(UNIQUE KEY|KEY|UNIQUE)\s,i', $k, $m)) { |
|
| 479 | + $index = trim(substr($k, strlen($m[1]))); |
|
| 480 | + $unique = (strlen($m[1]) > 3); |
|
| 481 | + $ok &= spip_sqlite_create_index($index, $nom, $v, $unique, $serveur); |
|
| 482 | + } |
|
| 483 | + } |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + return $ok ? true : false; |
|
| 487 | 487 | } |
| 488 | 488 | |
| 489 | 489 | /** |
@@ -497,21 +497,21 @@ discard block |
||
| 497 | 497 | **/ |
| 498 | 498 | function spip_sqlite_create_base($nom, $serveur = '', $option = true) |
| 499 | 499 | { |
| 500 | - $f = $nom . '.sqlite'; |
|
| 501 | - if (strpos($nom, '/') === false) { |
|
| 502 | - $f = _DIR_DB . $f; |
|
| 503 | - } |
|
| 500 | + $f = $nom . '.sqlite'; |
|
| 501 | + if (strpos($nom, '/') === false) { |
|
| 502 | + $f = _DIR_DB . $f; |
|
| 503 | + } |
|
| 504 | 504 | |
| 505 | - $ok = new \PDO("sqlite:$f"); |
|
| 505 | + $ok = new \PDO("sqlite:$f"); |
|
| 506 | 506 | |
| 507 | - if ($ok) { |
|
| 508 | - unset($ok); |
|
| 507 | + if ($ok) { |
|
| 508 | + unset($ok); |
|
| 509 | 509 | |
| 510 | - return true; |
|
| 511 | - } |
|
| 512 | - unset($ok); |
|
| 510 | + return true; |
|
| 511 | + } |
|
| 512 | + unset($ok); |
|
| 513 | 513 | |
| 514 | - return false; |
|
| 514 | + return false; |
|
| 515 | 515 | } |
| 516 | 516 | |
| 517 | 517 | |
@@ -533,22 +533,22 @@ discard block |
||
| 533 | 533 | */ |
| 534 | 534 | function spip_sqlite_create_view($nom, $query_select, $serveur = '', $requeter = true) |
| 535 | 535 | { |
| 536 | - if (!$query_select) { |
|
| 537 | - return false; |
|
| 538 | - } |
|
| 539 | - // vue deja presente |
|
| 540 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 541 | - spip_log( |
|
| 542 | - "Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", |
|
| 543 | - 'sqlite.' . _LOG_ERREUR |
|
| 544 | - ); |
|
| 545 | - |
|
| 546 | - return false; |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 550 | - |
|
| 551 | - return spip_sqlite_query($query, $serveur, $requeter); |
|
| 536 | + if (!$query_select) { |
|
| 537 | + return false; |
|
| 538 | + } |
|
| 539 | + // vue deja presente |
|
| 540 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 541 | + spip_log( |
|
| 542 | + "Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", |
|
| 543 | + 'sqlite.' . _LOG_ERREUR |
|
| 544 | + ); |
|
| 545 | + |
|
| 546 | + return false; |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 550 | + |
|
| 551 | + return spip_sqlite_query($query, $serveur, $requeter); |
|
| 552 | 552 | } |
| 553 | 553 | |
| 554 | 554 | /** |
@@ -571,54 +571,54 @@ discard block |
||
| 571 | 571 | */ |
| 572 | 572 | function spip_sqlite_create_index($nom, $table, $champs, $unique = '', $serveur = '', $requeter = true) |
| 573 | 573 | { |
| 574 | - if (!($nom or $table or $champs)) { |
|
| 575 | - spip_log( |
|
| 576 | - "Champ manquant pour creer un index sqlite ($nom, $table, (" . join(',', $champs) . '))', |
|
| 577 | - 'sqlite.' . _LOG_ERREUR |
|
| 578 | - ); |
|
| 579 | - |
|
| 580 | - return false; |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - // SQLite ne differentie pas noms des index en fonction des tables |
|
| 584 | - // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 585 | - $nom = $table . '_' . $nom; |
|
| 586 | - // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 587 | - if (!is_array($champs)) { |
|
| 588 | - if ($champs[0] == '(') { |
|
| 589 | - $champs = substr($champs, 1, -1); |
|
| 590 | - } |
|
| 591 | - $champs = [$champs]; |
|
| 592 | - // supprimer l'info de longueur d'index mysql en fin de champ |
|
| 593 | - $champs = preg_replace(',\(\d+\)$,', '', $champs); |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - $ifnotexists = ''; |
|
| 597 | - $version = spip_sqlite_fetch(spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), '', $serveur); |
|
| 598 | - if (!function_exists('spip_version_compare')) { |
|
| 599 | - include_spip('plugins/installer'); |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 603 | - $ifnotexists = ' IF NOT EXISTS'; |
|
| 604 | - } else { |
|
| 605 | - /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 606 | - $a = spip_sqlite_showtable($table, $serveur); |
|
| 607 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 608 | - return true; |
|
| 609 | - } |
|
| 610 | - } |
|
| 611 | - |
|
| 612 | - $query = 'CREATE ' . ($unique ? 'UNIQUE ' : '') . "INDEX$ifnotexists $nom ON $table (" . join(',', $champs) . ')'; |
|
| 613 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 614 | - if (!$requeter) { |
|
| 615 | - return $res; |
|
| 616 | - } |
|
| 617 | - if ($res) { |
|
| 618 | - return true; |
|
| 619 | - } else { |
|
| 620 | - return false; |
|
| 621 | - } |
|
| 574 | + if (!($nom or $table or $champs)) { |
|
| 575 | + spip_log( |
|
| 576 | + "Champ manquant pour creer un index sqlite ($nom, $table, (" . join(',', $champs) . '))', |
|
| 577 | + 'sqlite.' . _LOG_ERREUR |
|
| 578 | + ); |
|
| 579 | + |
|
| 580 | + return false; |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + // SQLite ne differentie pas noms des index en fonction des tables |
|
| 584 | + // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 585 | + $nom = $table . '_' . $nom; |
|
| 586 | + // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 587 | + if (!is_array($champs)) { |
|
| 588 | + if ($champs[0] == '(') { |
|
| 589 | + $champs = substr($champs, 1, -1); |
|
| 590 | + } |
|
| 591 | + $champs = [$champs]; |
|
| 592 | + // supprimer l'info de longueur d'index mysql en fin de champ |
|
| 593 | + $champs = preg_replace(',\(\d+\)$,', '', $champs); |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + $ifnotexists = ''; |
|
| 597 | + $version = spip_sqlite_fetch(spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), '', $serveur); |
|
| 598 | + if (!function_exists('spip_version_compare')) { |
|
| 599 | + include_spip('plugins/installer'); |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 603 | + $ifnotexists = ' IF NOT EXISTS'; |
|
| 604 | + } else { |
|
| 605 | + /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 606 | + $a = spip_sqlite_showtable($table, $serveur); |
|
| 607 | + if (isset($a['key']['KEY ' . $nom])) { |
|
| 608 | + return true; |
|
| 609 | + } |
|
| 610 | + } |
|
| 611 | + |
|
| 612 | + $query = 'CREATE ' . ($unique ? 'UNIQUE ' : '') . "INDEX$ifnotexists $nom ON $table (" . join(',', $champs) . ')'; |
|
| 613 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 614 | + if (!$requeter) { |
|
| 615 | + return $res; |
|
| 616 | + } |
|
| 617 | + if ($res) { |
|
| 618 | + return true; |
|
| 619 | + } else { |
|
| 620 | + return false; |
|
| 621 | + } |
|
| 622 | 622 | } |
| 623 | 623 | |
| 624 | 624 | /** |
@@ -636,31 +636,31 @@ discard block |
||
| 636 | 636 | */ |
| 637 | 637 | function spip_sqlite_count($r, $serveur = '', $requeter = true) |
| 638 | 638 | { |
| 639 | - if (!$r) { |
|
| 640 | - return 0; |
|
| 641 | - } |
|
| 642 | - |
|
| 643 | - // select ou autre (insert, update,...) ? |
|
| 644 | - // (link,requete) a compter |
|
| 645 | - if (is_array($r->spipSqliteRowCount)) { |
|
| 646 | - list($link, $query) = $r->spipSqliteRowCount; |
|
| 647 | - // amelioration possible a tester intensivement : pas de order by pour compter ! |
|
| 648 | - // $query = preg_replace(",ORDER BY .+(LIMIT\s|HAVING\s|GROUP BY\s|$),Uims","\\1",$query); |
|
| 649 | - $query = "SELECT count(*) as zzzzsqlitecount FROM ($query)"; |
|
| 650 | - $l = $link->query($query); |
|
| 651 | - $i = 0; |
|
| 652 | - if ($l and $z = $l->fetch()) { |
|
| 653 | - $i = (int) $z['zzzzsqlitecount']; |
|
| 654 | - } |
|
| 655 | - $r->spipSqliteRowCount = $i; |
|
| 656 | - } |
|
| 657 | - if (isset($r->spipSqliteRowCount)) { |
|
| 658 | - // Ce compte est faux s'il y a des limit dans la requete :( |
|
| 659 | - // il retourne le nombre d'enregistrements sans le limit |
|
| 660 | - return $r->spipSqliteRowCount; |
|
| 661 | - } else { |
|
| 662 | - return $r->rowCount(); |
|
| 663 | - } |
|
| 639 | + if (!$r) { |
|
| 640 | + return 0; |
|
| 641 | + } |
|
| 642 | + |
|
| 643 | + // select ou autre (insert, update,...) ? |
|
| 644 | + // (link,requete) a compter |
|
| 645 | + if (is_array($r->spipSqliteRowCount)) { |
|
| 646 | + list($link, $query) = $r->spipSqliteRowCount; |
|
| 647 | + // amelioration possible a tester intensivement : pas de order by pour compter ! |
|
| 648 | + // $query = preg_replace(",ORDER BY .+(LIMIT\s|HAVING\s|GROUP BY\s|$),Uims","\\1",$query); |
|
| 649 | + $query = "SELECT count(*) as zzzzsqlitecount FROM ($query)"; |
|
| 650 | + $l = $link->query($query); |
|
| 651 | + $i = 0; |
|
| 652 | + if ($l and $z = $l->fetch()) { |
|
| 653 | + $i = (int) $z['zzzzsqlitecount']; |
|
| 654 | + } |
|
| 655 | + $r->spipSqliteRowCount = $i; |
|
| 656 | + } |
|
| 657 | + if (isset($r->spipSqliteRowCount)) { |
|
| 658 | + // Ce compte est faux s'il y a des limit dans la requete :( |
|
| 659 | + // il retourne le nombre d'enregistrements sans le limit |
|
| 660 | + return $r->spipSqliteRowCount; |
|
| 661 | + } else { |
|
| 662 | + return $r->rowCount(); |
|
| 663 | + } |
|
| 664 | 664 | } |
| 665 | 665 | |
| 666 | 666 | |
@@ -679,31 +679,31 @@ discard block |
||
| 679 | 679 | * - false si la requête a échouée |
| 680 | 680 | **/ |
| 681 | 681 | function spip_sqlite_countsel( |
| 682 | - $from = [], |
|
| 683 | - $where = [], |
|
| 684 | - $groupby = '', |
|
| 685 | - $having = [], |
|
| 686 | - $serveur = '', |
|
| 687 | - $requeter = true |
|
| 682 | + $from = [], |
|
| 683 | + $where = [], |
|
| 684 | + $groupby = '', |
|
| 685 | + $having = [], |
|
| 686 | + $serveur = '', |
|
| 687 | + $requeter = true |
|
| 688 | 688 | ) { |
| 689 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 690 | - $r = spip_sqlite_select( |
|
| 691 | - "COUNT($c)", |
|
| 692 | - $from, |
|
| 693 | - $where, |
|
| 694 | - '', |
|
| 695 | - '', |
|
| 696 | - '', |
|
| 697 | - $having, |
|
| 698 | - $serveur, |
|
| 699 | - $requeter |
|
| 700 | - ); |
|
| 701 | - if ((is_resource($r) or is_object($r)) && $requeter) { // ressource : sqlite2, object : sqlite3 |
|
| 702 | - [$r] = spip_sqlite_fetch($r, SPIP_SQLITE3_NUM, $serveur); |
|
| 703 | - $r = (int) $r; |
|
| 704 | - } |
|
| 705 | - |
|
| 706 | - return $r; |
|
| 689 | + $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 690 | + $r = spip_sqlite_select( |
|
| 691 | + "COUNT($c)", |
|
| 692 | + $from, |
|
| 693 | + $where, |
|
| 694 | + '', |
|
| 695 | + '', |
|
| 696 | + '', |
|
| 697 | + $having, |
|
| 698 | + $serveur, |
|
| 699 | + $requeter |
|
| 700 | + ); |
|
| 701 | + if ((is_resource($r) or is_object($r)) && $requeter) { // ressource : sqlite2, object : sqlite3 |
|
| 702 | + [$r] = spip_sqlite_fetch($r, SPIP_SQLITE3_NUM, $serveur); |
|
| 703 | + $r = (int) $r; |
|
| 704 | + } |
|
| 705 | + |
|
| 706 | + return $r; |
|
| 707 | 707 | } |
| 708 | 708 | |
| 709 | 709 | |
@@ -721,24 +721,24 @@ discard block |
||
| 721 | 721 | **/ |
| 722 | 722 | function spip_sqlite_delete($table, $where = '', $serveur = '', $requeter = true) |
| 723 | 723 | { |
| 724 | - $res = spip_sqlite_query( |
|
| 725 | - _sqlite_calculer_expression('DELETE FROM', $table, ',') |
|
| 726 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 727 | - $serveur, |
|
| 728 | - $requeter |
|
| 729 | - ); |
|
| 730 | - |
|
| 731 | - // renvoyer la requete inerte si demandee |
|
| 732 | - if (!$requeter) { |
|
| 733 | - return $res; |
|
| 734 | - } |
|
| 735 | - |
|
| 736 | - if ($res) { |
|
| 737 | - $link = _sqlite_link($serveur); |
|
| 738 | - return $res->rowCount(); |
|
| 739 | - } else { |
|
| 740 | - return false; |
|
| 741 | - } |
|
| 724 | + $res = spip_sqlite_query( |
|
| 725 | + _sqlite_calculer_expression('DELETE FROM', $table, ',') |
|
| 726 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 727 | + $serveur, |
|
| 728 | + $requeter |
|
| 729 | + ); |
|
| 730 | + |
|
| 731 | + // renvoyer la requete inerte si demandee |
|
| 732 | + if (!$requeter) { |
|
| 733 | + return $res; |
|
| 734 | + } |
|
| 735 | + |
|
| 736 | + if ($res) { |
|
| 737 | + $link = _sqlite_link($serveur); |
|
| 738 | + return $res->rowCount(); |
|
| 739 | + } else { |
|
| 740 | + return false; |
|
| 741 | + } |
|
| 742 | 742 | } |
| 743 | 743 | |
| 744 | 744 | |
@@ -755,15 +755,15 @@ discard block |
||
| 755 | 755 | */ |
| 756 | 756 | function spip_sqlite_drop_table($table, $exist = '', $serveur = '', $requeter = true) |
| 757 | 757 | { |
| 758 | - if ($exist) { |
|
| 759 | - $exist = ' IF EXISTS'; |
|
| 760 | - } |
|
| 761 | - |
|
| 762 | - if (spip_sqlite_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 763 | - return true; |
|
| 764 | - } else { |
|
| 765 | - return false; |
|
| 766 | - } |
|
| 758 | + if ($exist) { |
|
| 759 | + $exist = ' IF EXISTS'; |
|
| 760 | + } |
|
| 761 | + |
|
| 762 | + if (spip_sqlite_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 763 | + return true; |
|
| 764 | + } else { |
|
| 765 | + return false; |
|
| 766 | + } |
|
| 767 | 767 | } |
| 768 | 768 | |
| 769 | 769 | |
@@ -780,11 +780,11 @@ discard block |
||
| 780 | 780 | */ |
| 781 | 781 | function spip_sqlite_drop_view($view, $exist = '', $serveur = '', $requeter = true) |
| 782 | 782 | { |
| 783 | - if ($exist) { |
|
| 784 | - $exist = ' IF EXISTS'; |
|
| 785 | - } |
|
| 783 | + if ($exist) { |
|
| 784 | + $exist = ' IF EXISTS'; |
|
| 785 | + } |
|
| 786 | 786 | |
| 787 | - return spip_sqlite_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 787 | + return spip_sqlite_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 788 | 788 | } |
| 789 | 789 | |
| 790 | 790 | /** |
@@ -799,20 +799,20 @@ discard block |
||
| 799 | 799 | */ |
| 800 | 800 | function spip_sqlite_drop_index($nom, $table, $serveur = '', $requeter = true) |
| 801 | 801 | { |
| 802 | - if (!($nom or $table)) { |
|
| 803 | - spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 802 | + if (!($nom or $table)) { |
|
| 803 | + spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 804 | 804 | |
| 805 | - return false; |
|
| 806 | - } |
|
| 805 | + return false; |
|
| 806 | + } |
|
| 807 | 807 | |
| 808 | - // SQLite ne differentie pas noms des index en fonction des tables |
|
| 809 | - // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 810 | - $index = $table . '_' . $nom; |
|
| 811 | - $exist = ' IF EXISTS'; |
|
| 808 | + // SQLite ne differentie pas noms des index en fonction des tables |
|
| 809 | + // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 810 | + $index = $table . '_' . $nom; |
|
| 811 | + $exist = ' IF EXISTS'; |
|
| 812 | 812 | |
| 813 | - $query = "DROP INDEX$exist $index"; |
|
| 813 | + $query = "DROP INDEX$exist $index"; |
|
| 814 | 814 | |
| 815 | - return spip_sqlite_query($query, $serveur, $requeter); |
|
| 815 | + return spip_sqlite_query($query, $serveur, $requeter); |
|
| 816 | 816 | } |
| 817 | 817 | |
| 818 | 818 | /** |
@@ -829,29 +829,29 @@ discard block |
||
| 829 | 829 | **/ |
| 830 | 830 | function spip_sqlite_error($query = '', $serveur = '') |
| 831 | 831 | { |
| 832 | - $link = _sqlite_link($serveur); |
|
| 833 | - |
|
| 834 | - if ($link) { |
|
| 835 | - $errs = $link->errorInfo(); |
|
| 836 | - $s = _sqlite_last_error_from_link($link); |
|
| 837 | - } else { |
|
| 838 | - $s = ': aucune ressource sqlite (link)'; |
|
| 839 | - } |
|
| 840 | - if ($s) { |
|
| 841 | - $trace = debug_backtrace(); |
|
| 842 | - if ($trace[0]['function'] != 'spip_sqlite_error') { |
|
| 843 | - spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 844 | - } |
|
| 845 | - } |
|
| 846 | - |
|
| 847 | - return $s; |
|
| 832 | + $link = _sqlite_link($serveur); |
|
| 833 | + |
|
| 834 | + if ($link) { |
|
| 835 | + $errs = $link->errorInfo(); |
|
| 836 | + $s = _sqlite_last_error_from_link($link); |
|
| 837 | + } else { |
|
| 838 | + $s = ': aucune ressource sqlite (link)'; |
|
| 839 | + } |
|
| 840 | + if ($s) { |
|
| 841 | + $trace = debug_backtrace(); |
|
| 842 | + if ($trace[0]['function'] != 'spip_sqlite_error') { |
|
| 843 | + spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 844 | + } |
|
| 845 | + } |
|
| 846 | + |
|
| 847 | + return $s; |
|
| 848 | 848 | } |
| 849 | 849 | |
| 850 | 850 | function _sqlite_last_error_from_link($link) |
| 851 | 851 | { |
| 852 | - if ($link) { |
|
| 853 | - $errs = $link->errorInfo(); |
|
| 854 | - /* |
|
| 852 | + if ($link) { |
|
| 853 | + $errs = $link->errorInfo(); |
|
| 854 | + /* |
|
| 855 | 855 | $errs[0] |
| 856 | 856 | numero SQLState ('HY000' souvent lors d'une erreur) |
| 857 | 857 | http://www.easysoft.com/developer/interfaces/odbc/sqlstate_status_return_codes.html |
@@ -861,11 +861,11 @@ discard block |
||
| 861 | 861 | $errs[2] |
| 862 | 862 | Le texte du message d'erreur |
| 863 | 863 | */ |
| 864 | - if (ltrim($errs[0], '0')) { // 00000 si pas d'erreur |
|
| 865 | - return "$errs[2]"; |
|
| 866 | - } |
|
| 867 | - } |
|
| 868 | - return ''; |
|
| 864 | + if (ltrim($errs[0], '0')) { // 00000 si pas d'erreur |
|
| 865 | + return "$errs[2]"; |
|
| 866 | + } |
|
| 867 | + } |
|
| 868 | + return ''; |
|
| 869 | 869 | } |
| 870 | 870 | |
| 871 | 871 | /** |
@@ -883,23 +883,23 @@ discard block |
||
| 883 | 883 | **/ |
| 884 | 884 | function spip_sqlite_errno($serveur = '') |
| 885 | 885 | { |
| 886 | - $link = _sqlite_link($serveur); |
|
| 887 | - |
|
| 888 | - if ($link) { |
|
| 889 | - $t = $link->errorInfo(); |
|
| 890 | - $s = ltrim($t[0], '0'); // 00000 si pas d'erreur |
|
| 891 | - if ($s) { |
|
| 892 | - $s .= ' / ' . $t[1]; |
|
| 893 | - } // ajoute l'erreur du moteur SQLite |
|
| 894 | - } else { |
|
| 895 | - $s = ': aucune ressource sqlite (link)'; |
|
| 896 | - } |
|
| 897 | - |
|
| 898 | - if ($s) { |
|
| 899 | - spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 900 | - } |
|
| 901 | - |
|
| 902 | - return $s ? $s : 0; |
|
| 886 | + $link = _sqlite_link($serveur); |
|
| 887 | + |
|
| 888 | + if ($link) { |
|
| 889 | + $t = $link->errorInfo(); |
|
| 890 | + $s = ltrim($t[0], '0'); // 00000 si pas d'erreur |
|
| 891 | + if ($s) { |
|
| 892 | + $s .= ' / ' . $t[1]; |
|
| 893 | + } // ajoute l'erreur du moteur SQLite |
|
| 894 | + } else { |
|
| 895 | + $s = ': aucune ressource sqlite (link)'; |
|
| 896 | + } |
|
| 897 | + |
|
| 898 | + if ($s) { |
|
| 899 | + spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 900 | + } |
|
| 901 | + |
|
| 902 | + return $s ? $s : 0; |
|
| 903 | 903 | } |
| 904 | 904 | |
| 905 | 905 | |
@@ -916,19 +916,19 @@ discard block |
||
| 916 | 916 | */ |
| 917 | 917 | function spip_sqlite_explain($query, $serveur = '', $requeter = true) |
| 918 | 918 | { |
| 919 | - if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 920 | - return []; |
|
| 921 | - } |
|
| 922 | - |
|
| 923 | - $query = Sqlite::traduire_requete($query, $serveur); |
|
| 924 | - $query = 'EXPLAIN ' . $query; |
|
| 925 | - if (!$requeter) { |
|
| 926 | - return $query; |
|
| 927 | - } |
|
| 928 | - // on ne trace pas ces requetes, sinon on obtient un tracage sans fin... |
|
| 929 | - $r = Sqlite::executer_requete($query, $serveur, false); |
|
| 930 | - |
|
| 931 | - return $r ? spip_sqlite_fetch($r, null, $serveur) : false; // hum ? etrange ca... a verifier |
|
| 919 | + if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 920 | + return []; |
|
| 921 | + } |
|
| 922 | + |
|
| 923 | + $query = Sqlite::traduire_requete($query, $serveur); |
|
| 924 | + $query = 'EXPLAIN ' . $query; |
|
| 925 | + if (!$requeter) { |
|
| 926 | + return $query; |
|
| 927 | + } |
|
| 928 | + // on ne trace pas ces requetes, sinon on obtient un tracage sans fin... |
|
| 929 | + $r = Sqlite::executer_requete($query, $serveur, false); |
|
| 930 | + |
|
| 931 | + return $r ? spip_sqlite_fetch($r, null, $serveur) : false; // hum ? etrange ca... a verifier |
|
| 932 | 932 | } |
| 933 | 933 | |
| 934 | 934 | |
@@ -949,35 +949,35 @@ discard block |
||
| 949 | 949 | function spip_sqlite_fetch($r, $t = '', $serveur = '', $requeter = true) |
| 950 | 950 | { |
| 951 | 951 | |
| 952 | - $link = _sqlite_link($serveur); |
|
| 953 | - $t = $t ? $t : SPIP_SQLITE3_ASSOC; |
|
| 954 | - |
|
| 955 | - if (!$r) { |
|
| 956 | - return false; |
|
| 957 | - } |
|
| 958 | - |
|
| 959 | - $retour = $r->fetch($t); |
|
| 960 | - |
|
| 961 | - if (!$retour) { |
|
| 962 | - if ($r->errorCode() === '00000') { |
|
| 963 | - return null; |
|
| 964 | - } |
|
| 965 | - return false; |
|
| 966 | - } |
|
| 967 | - |
|
| 968 | - // Renvoie des 'table.titre' au lieu de 'titre' tout court ! pff ! |
|
| 969 | - // suppression de 'table.' pour toutes les cles (c'est un peu violent !) |
|
| 970 | - // c'est couteux : on ne verifie que la premiere ligne pour voir si on le fait ou non |
|
| 971 | - if (str_contains(implode('', array_keys($retour)), '.')) { |
|
| 972 | - foreach ($retour as $cle => $val) { |
|
| 973 | - if (($pos = strpos($cle, '.')) !== false) { |
|
| 974 | - $retour[substr($cle, $pos + 1)] = &$retour[$cle]; |
|
| 975 | - unset($retour[$cle]); |
|
| 976 | - } |
|
| 977 | - } |
|
| 978 | - } |
|
| 979 | - |
|
| 980 | - return $retour; |
|
| 952 | + $link = _sqlite_link($serveur); |
|
| 953 | + $t = $t ? $t : SPIP_SQLITE3_ASSOC; |
|
| 954 | + |
|
| 955 | + if (!$r) { |
|
| 956 | + return false; |
|
| 957 | + } |
|
| 958 | + |
|
| 959 | + $retour = $r->fetch($t); |
|
| 960 | + |
|
| 961 | + if (!$retour) { |
|
| 962 | + if ($r->errorCode() === '00000') { |
|
| 963 | + return null; |
|
| 964 | + } |
|
| 965 | + return false; |
|
| 966 | + } |
|
| 967 | + |
|
| 968 | + // Renvoie des 'table.titre' au lieu de 'titre' tout court ! pff ! |
|
| 969 | + // suppression de 'table.' pour toutes les cles (c'est un peu violent !) |
|
| 970 | + // c'est couteux : on ne verifie que la premiere ligne pour voir si on le fait ou non |
|
| 971 | + if (str_contains(implode('', array_keys($retour)), '.')) { |
|
| 972 | + foreach ($retour as $cle => $val) { |
|
| 973 | + if (($pos = strpos($cle, '.')) !== false) { |
|
| 974 | + $retour[substr($cle, $pos + 1)] = &$retour[$cle]; |
|
| 975 | + unset($retour[$cle]); |
|
| 976 | + } |
|
| 977 | + } |
|
| 978 | + } |
|
| 979 | + |
|
| 980 | + return $retour; |
|
| 981 | 981 | } |
| 982 | 982 | |
| 983 | 983 | /** |
@@ -991,8 +991,8 @@ discard block |
||
| 991 | 991 | **/ |
| 992 | 992 | function spip_sqlite_seek($r, $row_number, $serveur = '', $requeter = true) |
| 993 | 993 | { |
| 994 | - // encore un truc de bien fichu : PDO ne PEUT PAS faire de seek ou de rewind... |
|
| 995 | - return false; |
|
| 994 | + // encore un truc de bien fichu : PDO ne PEUT PAS faire de seek ou de rewind... |
|
| 995 | + return false; |
|
| 996 | 996 | } |
| 997 | 997 | |
| 998 | 998 | |
@@ -1009,10 +1009,10 @@ discard block |
||
| 1009 | 1009 | */ |
| 1010 | 1010 | function spip_sqlite_free(&$r, $serveur = '', $requeter = true) |
| 1011 | 1011 | { |
| 1012 | - unset($r); |
|
| 1012 | + unset($r); |
|
| 1013 | 1013 | |
| 1014 | - return true; |
|
| 1015 | - //return sqlite_free_result($r); |
|
| 1014 | + return true; |
|
| 1015 | + //return sqlite_free_result($r); |
|
| 1016 | 1016 | } |
| 1017 | 1017 | |
| 1018 | 1018 | |
@@ -1028,8 +1028,8 @@ discard block |
||
| 1028 | 1028 | */ |
| 1029 | 1029 | function spip_sqlite_get_charset($charset = [], $serveur = '', $requeter = true) |
| 1030 | 1030 | { |
| 1031 | - //$c = !$charset ? '' : (" LIKE "._q($charset['charset'])); |
|
| 1032 | - //return spip_sqlite_fetch(sqlite_query(_sqlite_link($serveur), "SHOW CHARACTER SET$c"), NULL, $serveur); |
|
| 1031 | + //$c = !$charset ? '' : (" LIKE "._q($charset['charset'])); |
|
| 1032 | + //return spip_sqlite_fetch(sqlite_query(_sqlite_link($serveur), "SHOW CHARACTER SET$c"), NULL, $serveur); |
|
| 1033 | 1033 | } |
| 1034 | 1034 | |
| 1035 | 1035 | |
@@ -1045,7 +1045,7 @@ discard block |
||
| 1045 | 1045 | **/ |
| 1046 | 1046 | function spip_sqlite_hex($v) |
| 1047 | 1047 | { |
| 1048 | - return hexdec($v); |
|
| 1048 | + return hexdec($v); |
|
| 1049 | 1049 | } |
| 1050 | 1050 | |
| 1051 | 1051 | |
@@ -1068,7 +1068,7 @@ discard block |
||
| 1068 | 1068 | **/ |
| 1069 | 1069 | function spip_sqlite_in($val, $valeurs, $not = '', $serveur = '', $requeter = true) |
| 1070 | 1070 | { |
| 1071 | - return "($val $not IN ($valeurs))"; |
|
| 1071 | + return "($val $not IN ($valeurs))"; |
|
| 1072 | 1072 | } |
| 1073 | 1073 | |
| 1074 | 1074 | |
@@ -1097,20 +1097,20 @@ discard block |
||
| 1097 | 1097 | function spip_sqlite_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) |
| 1098 | 1098 | { |
| 1099 | 1099 | |
| 1100 | - $query = "INSERT INTO $table " . ($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1101 | - if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
|
| 1102 | - if (!$requeter) { |
|
| 1103 | - return $r; |
|
| 1104 | - } |
|
| 1105 | - $nb = Sqlite::last_insert_id($serveur); |
|
| 1106 | - } else { |
|
| 1107 | - $nb = false; |
|
| 1108 | - } |
|
| 1100 | + $query = "INSERT INTO $table " . ($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1101 | + if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
|
| 1102 | + if (!$requeter) { |
|
| 1103 | + return $r; |
|
| 1104 | + } |
|
| 1105 | + $nb = Sqlite::last_insert_id($serveur); |
|
| 1106 | + } else { |
|
| 1107 | + $nb = false; |
|
| 1108 | + } |
|
| 1109 | 1109 | |
| 1110 | - $err = spip_sqlite_error($query, $serveur); |
|
| 1110 | + $err = spip_sqlite_error($query, $serveur); |
|
| 1111 | 1111 | |
| 1112 | - // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1113 | - return isset($_GET['var_profile']) ? $r : $nb; |
|
| 1112 | + // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1113 | + return isset($_GET['var_profile']) ? $r : $nb; |
|
| 1114 | 1114 | } |
| 1115 | 1115 | |
| 1116 | 1116 | |
@@ -1136,28 +1136,28 @@ discard block |
||
| 1136 | 1136 | **/ |
| 1137 | 1137 | function spip_sqlite_insertq($table, $couples = [], $desc = [], $serveur = '', $requeter = true) |
| 1138 | 1138 | { |
| 1139 | - if (!$desc) { |
|
| 1140 | - $desc = description_table($table, $serveur); |
|
| 1141 | - } |
|
| 1142 | - if (!$desc) { |
|
| 1143 | - die("$table insertion sans description"); |
|
| 1144 | - } |
|
| 1145 | - $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1146 | - |
|
| 1147 | - foreach ($couples as $champ => $val) { |
|
| 1148 | - $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1149 | - } |
|
| 1150 | - |
|
| 1151 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1152 | - $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1153 | - |
|
| 1154 | - $cles = $valeurs = ''; |
|
| 1155 | - if (count($couples)) { |
|
| 1156 | - $cles = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1157 | - $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1158 | - } |
|
| 1159 | - |
|
| 1160 | - return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1139 | + if (!$desc) { |
|
| 1140 | + $desc = description_table($table, $serveur); |
|
| 1141 | + } |
|
| 1142 | + if (!$desc) { |
|
| 1143 | + die("$table insertion sans description"); |
|
| 1144 | + } |
|
| 1145 | + $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1146 | + |
|
| 1147 | + foreach ($couples as $champ => $val) { |
|
| 1148 | + $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1149 | + } |
|
| 1150 | + |
|
| 1151 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1152 | + $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1153 | + |
|
| 1154 | + $cles = $valeurs = ''; |
|
| 1155 | + if (count($couples)) { |
|
| 1156 | + $cles = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1157 | + $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1158 | + } |
|
| 1159 | + |
|
| 1160 | + return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1161 | 1161 | } |
| 1162 | 1162 | |
| 1163 | 1163 | |
@@ -1182,70 +1182,70 @@ discard block |
||
| 1182 | 1182 | **/ |
| 1183 | 1183 | function spip_sqlite_insertq_multi($table, $tab_couples = [], $desc = [], $serveur = '', $requeter = true) |
| 1184 | 1184 | { |
| 1185 | - if (!$desc) { |
|
| 1186 | - $desc = description_table($table, $serveur); |
|
| 1187 | - } |
|
| 1188 | - if (!$desc) { |
|
| 1189 | - die("$table insertion sans description"); |
|
| 1190 | - } |
|
| 1191 | - if (!isset($desc['field'])) { |
|
| 1192 | - $desc['field'] = []; |
|
| 1193 | - } |
|
| 1194 | - |
|
| 1195 | - // recuperer les champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1196 | - $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 1197 | - |
|
| 1198 | - // seul le nom de la table est a traduire ici : |
|
| 1199 | - // le faire une seule fois au debut |
|
| 1200 | - $query_start = "INSERT INTO $table "; |
|
| 1201 | - $query_start = Sqlite::traduire_requete($query_start, $serveur); |
|
| 1202 | - |
|
| 1203 | - // ouvrir une transaction |
|
| 1204 | - if ($requeter) { |
|
| 1205 | - Sqlite::demarrer_transaction($serveur); |
|
| 1206 | - } |
|
| 1207 | - |
|
| 1208 | - while ($couples = array_shift($tab_couples)) { |
|
| 1209 | - foreach ($couples as $champ => $val) { |
|
| 1210 | - $couples[$champ] = _sqlite_calculer_cite($val, $desc['field'][$champ]); |
|
| 1211 | - } |
|
| 1212 | - |
|
| 1213 | - // inserer les champs timestamp par defaut |
|
| 1214 | - $couples = array_merge($maj, $couples); |
|
| 1215 | - |
|
| 1216 | - $champs = $valeurs = ''; |
|
| 1217 | - if (count($couples)) { |
|
| 1218 | - $champs = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1219 | - $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1220 | - $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1221 | - } else { |
|
| 1222 | - $query = $query_start . 'DEFAULT VALUES'; |
|
| 1223 | - } |
|
| 1224 | - |
|
| 1225 | - if ($requeter) { |
|
| 1226 | - $retour = Sqlite::executer_requete($query, $serveur); |
|
| 1227 | - } |
|
| 1228 | - |
|
| 1229 | - // sur le dernier couple uniquement |
|
| 1230 | - if (!count($tab_couples)) { |
|
| 1231 | - $nb = 0; |
|
| 1232 | - if ($requeter) { |
|
| 1233 | - $nb = Sqlite::last_insert_id($serveur); |
|
| 1234 | - } else { |
|
| 1235 | - return $query; |
|
| 1236 | - } |
|
| 1237 | - } |
|
| 1238 | - |
|
| 1239 | - $err = spip_sqlite_error($query, $serveur); |
|
| 1240 | - } |
|
| 1241 | - |
|
| 1242 | - if ($requeter) { |
|
| 1243 | - Sqlite::finir_transaction($serveur); |
|
| 1244 | - } |
|
| 1245 | - |
|
| 1246 | - // renvoie le dernier id d'autoincrement ajoute |
|
| 1247 | - // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1248 | - return isset($_GET['var_profile']) ? $retour : $nb; |
|
| 1185 | + if (!$desc) { |
|
| 1186 | + $desc = description_table($table, $serveur); |
|
| 1187 | + } |
|
| 1188 | + if (!$desc) { |
|
| 1189 | + die("$table insertion sans description"); |
|
| 1190 | + } |
|
| 1191 | + if (!isset($desc['field'])) { |
|
| 1192 | + $desc['field'] = []; |
|
| 1193 | + } |
|
| 1194 | + |
|
| 1195 | + // recuperer les champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1196 | + $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 1197 | + |
|
| 1198 | + // seul le nom de la table est a traduire ici : |
|
| 1199 | + // le faire une seule fois au debut |
|
| 1200 | + $query_start = "INSERT INTO $table "; |
|
| 1201 | + $query_start = Sqlite::traduire_requete($query_start, $serveur); |
|
| 1202 | + |
|
| 1203 | + // ouvrir une transaction |
|
| 1204 | + if ($requeter) { |
|
| 1205 | + Sqlite::demarrer_transaction($serveur); |
|
| 1206 | + } |
|
| 1207 | + |
|
| 1208 | + while ($couples = array_shift($tab_couples)) { |
|
| 1209 | + foreach ($couples as $champ => $val) { |
|
| 1210 | + $couples[$champ] = _sqlite_calculer_cite($val, $desc['field'][$champ]); |
|
| 1211 | + } |
|
| 1212 | + |
|
| 1213 | + // inserer les champs timestamp par defaut |
|
| 1214 | + $couples = array_merge($maj, $couples); |
|
| 1215 | + |
|
| 1216 | + $champs = $valeurs = ''; |
|
| 1217 | + if (count($couples)) { |
|
| 1218 | + $champs = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1219 | + $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1220 | + $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1221 | + } else { |
|
| 1222 | + $query = $query_start . 'DEFAULT VALUES'; |
|
| 1223 | + } |
|
| 1224 | + |
|
| 1225 | + if ($requeter) { |
|
| 1226 | + $retour = Sqlite::executer_requete($query, $serveur); |
|
| 1227 | + } |
|
| 1228 | + |
|
| 1229 | + // sur le dernier couple uniquement |
|
| 1230 | + if (!count($tab_couples)) { |
|
| 1231 | + $nb = 0; |
|
| 1232 | + if ($requeter) { |
|
| 1233 | + $nb = Sqlite::last_insert_id($serveur); |
|
| 1234 | + } else { |
|
| 1235 | + return $query; |
|
| 1236 | + } |
|
| 1237 | + } |
|
| 1238 | + |
|
| 1239 | + $err = spip_sqlite_error($query, $serveur); |
|
| 1240 | + } |
|
| 1241 | + |
|
| 1242 | + if ($requeter) { |
|
| 1243 | + Sqlite::finir_transaction($serveur); |
|
| 1244 | + } |
|
| 1245 | + |
|
| 1246 | + // renvoie le dernier id d'autoincrement ajoute |
|
| 1247 | + // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1248 | + return isset($_GET['var_profile']) ? $retour : $nb; |
|
| 1249 | 1249 | } |
| 1250 | 1250 | |
| 1251 | 1251 | |
@@ -1261,7 +1261,7 @@ discard block |
||
| 1261 | 1261 | **/ |
| 1262 | 1262 | function spip_sqlite_preferer_transaction($serveur = '', $requeter = true) |
| 1263 | 1263 | { |
| 1264 | - return true; |
|
| 1264 | + return true; |
|
| 1265 | 1265 | } |
| 1266 | 1266 | |
| 1267 | 1267 | /** |
@@ -1279,12 +1279,12 @@ discard block |
||
| 1279 | 1279 | **/ |
| 1280 | 1280 | function spip_sqlite_demarrer_transaction($serveur = '', $requeter = true) |
| 1281 | 1281 | { |
| 1282 | - if (!$requeter) { |
|
| 1283 | - return 'BEGIN TRANSACTION'; |
|
| 1284 | - } |
|
| 1285 | - Sqlite::demarrer_transaction($serveur); |
|
| 1282 | + if (!$requeter) { |
|
| 1283 | + return 'BEGIN TRANSACTION'; |
|
| 1284 | + } |
|
| 1285 | + Sqlite::demarrer_transaction($serveur); |
|
| 1286 | 1286 | |
| 1287 | - return true; |
|
| 1287 | + return true; |
|
| 1288 | 1288 | } |
| 1289 | 1289 | |
| 1290 | 1290 | /** |
@@ -1299,12 +1299,12 @@ discard block |
||
| 1299 | 1299 | **/ |
| 1300 | 1300 | function spip_sqlite_terminer_transaction($serveur = '', $requeter = true) |
| 1301 | 1301 | { |
| 1302 | - if (!$requeter) { |
|
| 1303 | - return 'COMMIT'; |
|
| 1304 | - } |
|
| 1305 | - Sqlite::finir_transaction($serveur); |
|
| 1302 | + if (!$requeter) { |
|
| 1303 | + return 'COMMIT'; |
|
| 1304 | + } |
|
| 1305 | + Sqlite::finir_transaction($serveur); |
|
| 1306 | 1306 | |
| 1307 | - return true; |
|
| 1307 | + return true; |
|
| 1308 | 1308 | } |
| 1309 | 1309 | |
| 1310 | 1310 | |
@@ -1320,27 +1320,27 @@ discard block |
||
| 1320 | 1320 | **/ |
| 1321 | 1321 | function spip_sqlite_listdbs($serveur = '', $requeter = true) |
| 1322 | 1322 | { |
| 1323 | - _sqlite_init(); |
|
| 1324 | - |
|
| 1325 | - if (!is_dir($d = substr(_DIR_DB, 0, -1))) { |
|
| 1326 | - return []; |
|
| 1327 | - } |
|
| 1328 | - |
|
| 1329 | - include_spip('inc/flock'); |
|
| 1330 | - $bases = preg_files($d, $pattern = '(.*)\.sqlite$'); |
|
| 1331 | - $bds = []; |
|
| 1332 | - |
|
| 1333 | - foreach ($bases as $b) { |
|
| 1334 | - // pas de bases commencant pas sqlite |
|
| 1335 | - // (on s'en sert pour l'installation pour simuler la presence d'un serveur) |
|
| 1336 | - // les bases sont de la forme _sqliteX_tmp_spip_install.sqlite |
|
| 1337 | - if (strpos($b, '_sqlite')) { |
|
| 1338 | - continue; |
|
| 1339 | - } |
|
| 1340 | - $bds[] = preg_replace(";.*/$pattern;iS", '$1', $b); |
|
| 1341 | - } |
|
| 1342 | - |
|
| 1343 | - return $bds; |
|
| 1323 | + _sqlite_init(); |
|
| 1324 | + |
|
| 1325 | + if (!is_dir($d = substr(_DIR_DB, 0, -1))) { |
|
| 1326 | + return []; |
|
| 1327 | + } |
|
| 1328 | + |
|
| 1329 | + include_spip('inc/flock'); |
|
| 1330 | + $bases = preg_files($d, $pattern = '(.*)\.sqlite$'); |
|
| 1331 | + $bds = []; |
|
| 1332 | + |
|
| 1333 | + foreach ($bases as $b) { |
|
| 1334 | + // pas de bases commencant pas sqlite |
|
| 1335 | + // (on s'en sert pour l'installation pour simuler la presence d'un serveur) |
|
| 1336 | + // les bases sont de la forme _sqliteX_tmp_spip_install.sqlite |
|
| 1337 | + if (strpos($b, '_sqlite')) { |
|
| 1338 | + continue; |
|
| 1339 | + } |
|
| 1340 | + $bds[] = preg_replace(";.*/$pattern;iS", '$1', $b); |
|
| 1341 | + } |
|
| 1342 | + |
|
| 1343 | + return $bds; |
|
| 1344 | 1344 | } |
| 1345 | 1345 | |
| 1346 | 1346 | |
@@ -1356,9 +1356,9 @@ discard block |
||
| 1356 | 1356 | */ |
| 1357 | 1357 | function spip_sqlite_multi($objet, $lang) |
| 1358 | 1358 | { |
| 1359 | - $r = 'EXTRAIRE_MULTI(' . $objet . ", '" . $lang . "') AS multi"; |
|
| 1359 | + $r = 'EXTRAIRE_MULTI(' . $objet . ", '" . $lang . "') AS multi"; |
|
| 1360 | 1360 | |
| 1361 | - return $r; |
|
| 1361 | + return $r; |
|
| 1362 | 1362 | } |
| 1363 | 1363 | |
| 1364 | 1364 | |
@@ -1376,15 +1376,15 @@ discard block |
||
| 1376 | 1376 | **/ |
| 1377 | 1377 | function spip_sqlite_optimize($table, $serveur = '', $requeter = true) |
| 1378 | 1378 | { |
| 1379 | - static $do = false; |
|
| 1380 | - if ($requeter and $do) { |
|
| 1381 | - return true; |
|
| 1382 | - } |
|
| 1383 | - if ($requeter) { |
|
| 1384 | - $do = true; |
|
| 1385 | - } |
|
| 1386 | - |
|
| 1387 | - return spip_sqlite_query('VACUUM', $serveur, $requeter); |
|
| 1379 | + static $do = false; |
|
| 1380 | + if ($requeter and $do) { |
|
| 1381 | + return true; |
|
| 1382 | + } |
|
| 1383 | + if ($requeter) { |
|
| 1384 | + $do = true; |
|
| 1385 | + } |
|
| 1386 | + |
|
| 1387 | + return spip_sqlite_query('VACUUM', $serveur, $requeter); |
|
| 1388 | 1388 | } |
| 1389 | 1389 | |
| 1390 | 1390 | |
@@ -1402,15 +1402,15 @@ discard block |
||
| 1402 | 1402 | */ |
| 1403 | 1403 | function spip_sqlite_quote($v, $type = '') |
| 1404 | 1404 | { |
| 1405 | - if (!is_array($v)) { |
|
| 1406 | - return _sqlite_calculer_cite($v, $type); |
|
| 1407 | - } |
|
| 1408 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1409 | - foreach ($v as $k => $r) { |
|
| 1410 | - $v[$k] = spip_sqlite_quote($r, $type); |
|
| 1411 | - } |
|
| 1412 | - |
|
| 1413 | - return join(',', $v); |
|
| 1405 | + if (!is_array($v)) { |
|
| 1406 | + return _sqlite_calculer_cite($v, $type); |
|
| 1407 | + } |
|
| 1408 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1409 | + foreach ($v as $k => $r) { |
|
| 1410 | + $v[$k] = spip_sqlite_quote($r, $type); |
|
| 1411 | + } |
|
| 1412 | + |
|
| 1413 | + return join(',', $v); |
|
| 1414 | 1414 | } |
| 1415 | 1415 | |
| 1416 | 1416 | |
@@ -1428,9 +1428,9 @@ discard block |
||
| 1428 | 1428 | **/ |
| 1429 | 1429 | function spip_sqlite_date_proche($champ, $interval, $unite) |
| 1430 | 1430 | { |
| 1431 | - $op = (($interval <= 0) ? '>' : '<'); |
|
| 1431 | + $op = (($interval <= 0) ? '>' : '<'); |
|
| 1432 | 1432 | |
| 1433 | - return "($champ $op datetime('" . date('Y-m-d H:i:s') . "', '$interval $unite'))"; |
|
| 1433 | + return "($champ $op datetime('" . date('Y-m-d H:i:s') . "', '$interval $unite'))"; |
|
| 1434 | 1434 | } |
| 1435 | 1435 | |
| 1436 | 1436 | |
@@ -1449,48 +1449,48 @@ discard block |
||
| 1449 | 1449 | */ |
| 1450 | 1450 | function spip_sqlite_repair($table, $serveur = '', $requeter = true) |
| 1451 | 1451 | { |
| 1452 | - if ( |
|
| 1453 | - $desc = spip_sqlite_showtable($table, $serveur) |
|
| 1454 | - and isset($desc['field']) |
|
| 1455 | - and is_array($desc['field']) |
|
| 1456 | - ) { |
|
| 1457 | - foreach ($desc['field'] as $c => $d) { |
|
| 1458 | - if ( |
|
| 1459 | - preg_match(',^(tinytext|mediumtext|text|longtext|varchar|char),i', $d) |
|
| 1460 | - and stripos($d, 'NOT NULL') !== false |
|
| 1461 | - and stripos($d, 'DEFAULT') === false |
|
| 1462 | - /* pas touche aux cles primaires */ |
|
| 1463 | - and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1464 | - ) { |
|
| 1465 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
|
| 1466 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1467 | - } |
|
| 1468 | - if ( |
|
| 1469 | - preg_match(',^(INTEGER),i', $d) |
|
| 1470 | - and stripos($d, 'NOT NULL') !== false |
|
| 1471 | - and stripos($d, 'DEFAULT') === false |
|
| 1472 | - /* pas touche aux cles primaires */ |
|
| 1473 | - and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1474 | - ) { |
|
| 1475 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
|
| 1476 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1477 | - } |
|
| 1478 | - if ( |
|
| 1479 | - preg_match(',^(datetime),i', $d) |
|
| 1480 | - and stripos($d, 'NOT NULL') !== false |
|
| 1481 | - and stripos($d, 'DEFAULT') === false |
|
| 1482 | - /* pas touche aux cles primaires */ |
|
| 1483 | - and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1484 | - ) { |
|
| 1485 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0000-00-00 00:00:00'", $serveur); |
|
| 1486 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1487 | - } |
|
| 1488 | - } |
|
| 1489 | - |
|
| 1490 | - return [' OK ']; |
|
| 1491 | - } |
|
| 1492 | - |
|
| 1493 | - return [' ERROR ']; |
|
| 1452 | + if ( |
|
| 1453 | + $desc = spip_sqlite_showtable($table, $serveur) |
|
| 1454 | + and isset($desc['field']) |
|
| 1455 | + and is_array($desc['field']) |
|
| 1456 | + ) { |
|
| 1457 | + foreach ($desc['field'] as $c => $d) { |
|
| 1458 | + if ( |
|
| 1459 | + preg_match(',^(tinytext|mediumtext|text|longtext|varchar|char),i', $d) |
|
| 1460 | + and stripos($d, 'NOT NULL') !== false |
|
| 1461 | + and stripos($d, 'DEFAULT') === false |
|
| 1462 | + /* pas touche aux cles primaires */ |
|
| 1463 | + and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1464 | + ) { |
|
| 1465 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
|
| 1466 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1467 | + } |
|
| 1468 | + if ( |
|
| 1469 | + preg_match(',^(INTEGER),i', $d) |
|
| 1470 | + and stripos($d, 'NOT NULL') !== false |
|
| 1471 | + and stripos($d, 'DEFAULT') === false |
|
| 1472 | + /* pas touche aux cles primaires */ |
|
| 1473 | + and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1474 | + ) { |
|
| 1475 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
|
| 1476 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1477 | + } |
|
| 1478 | + if ( |
|
| 1479 | + preg_match(',^(datetime),i', $d) |
|
| 1480 | + and stripos($d, 'NOT NULL') !== false |
|
| 1481 | + and stripos($d, 'DEFAULT') === false |
|
| 1482 | + /* pas touche aux cles primaires */ |
|
| 1483 | + and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1484 | + ) { |
|
| 1485 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0000-00-00 00:00:00'", $serveur); |
|
| 1486 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1487 | + } |
|
| 1488 | + } |
|
| 1489 | + |
|
| 1490 | + return [' OK ']; |
|
| 1491 | + } |
|
| 1492 | + |
|
| 1493 | + return [' ERROR ']; |
|
| 1494 | 1494 | } |
| 1495 | 1495 | |
| 1496 | 1496 | |
@@ -1520,25 +1520,25 @@ discard block |
||
| 1520 | 1520 | **/ |
| 1521 | 1521 | function spip_sqlite_replace($table, $couples, $desc = [], $serveur = '', $requeter = true) |
| 1522 | 1522 | { |
| 1523 | - if (!$desc) { |
|
| 1524 | - $desc = description_table($table, $serveur); |
|
| 1525 | - } |
|
| 1526 | - if (!$desc) { |
|
| 1527 | - die("$table insertion sans description"); |
|
| 1528 | - } |
|
| 1529 | - $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1530 | - |
|
| 1531 | - foreach ($couples as $champ => $val) { |
|
| 1532 | - $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1533 | - } |
|
| 1534 | - |
|
| 1535 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1536 | - $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1537 | - |
|
| 1538 | - return spip_sqlite_query("REPLACE INTO $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1539 | - ',', |
|
| 1540 | - $couples |
|
| 1541 | - ) . ')', $serveur); |
|
| 1523 | + if (!$desc) { |
|
| 1524 | + $desc = description_table($table, $serveur); |
|
| 1525 | + } |
|
| 1526 | + if (!$desc) { |
|
| 1527 | + die("$table insertion sans description"); |
|
| 1528 | + } |
|
| 1529 | + $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1530 | + |
|
| 1531 | + foreach ($couples as $champ => $val) { |
|
| 1532 | + $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1533 | + } |
|
| 1534 | + |
|
| 1535 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1536 | + $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1537 | + |
|
| 1538 | + return spip_sqlite_query("REPLACE INTO $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1539 | + ',', |
|
| 1540 | + $couples |
|
| 1541 | + ) . ')', $serveur); |
|
| 1542 | 1542 | } |
| 1543 | 1543 | |
| 1544 | 1544 | |
@@ -1569,13 +1569,13 @@ discard block |
||
| 1569 | 1569 | function spip_sqlite_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $requeter = true) |
| 1570 | 1570 | { |
| 1571 | 1571 | |
| 1572 | - // boucler pour trainter chaque requete independemment |
|
| 1573 | - foreach ($tab_couples as $couples) { |
|
| 1574 | - $retour = spip_sqlite_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1575 | - } |
|
| 1572 | + // boucler pour trainter chaque requete independemment |
|
| 1573 | + foreach ($tab_couples as $couples) { |
|
| 1574 | + $retour = spip_sqlite_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1575 | + } |
|
| 1576 | 1576 | |
| 1577 | - // renvoie le dernier id |
|
| 1578 | - return $retour; |
|
| 1577 | + // renvoie le dernier id |
|
| 1578 | + return $retour; |
|
| 1579 | 1579 | } |
| 1580 | 1580 | |
| 1581 | 1581 | |
@@ -1602,44 +1602,44 @@ discard block |
||
| 1602 | 1602 | * - array : Tableau décrivant requête et temps d'exécution si var_profile actif pour tracer. |
| 1603 | 1603 | */ |
| 1604 | 1604 | function spip_sqlite_select( |
| 1605 | - $select, |
|
| 1606 | - $from, |
|
| 1607 | - $where = '', |
|
| 1608 | - $groupby = '', |
|
| 1609 | - $orderby = '', |
|
| 1610 | - $limit = '', |
|
| 1611 | - $having = '', |
|
| 1612 | - $serveur = '', |
|
| 1613 | - $requeter = true |
|
| 1605 | + $select, |
|
| 1606 | + $from, |
|
| 1607 | + $where = '', |
|
| 1608 | + $groupby = '', |
|
| 1609 | + $orderby = '', |
|
| 1610 | + $limit = '', |
|
| 1611 | + $having = '', |
|
| 1612 | + $serveur = '', |
|
| 1613 | + $requeter = true |
|
| 1614 | 1614 | ) { |
| 1615 | 1615 | |
| 1616 | - // version() n'est pas connu de sqlite |
|
| 1617 | - $select = str_replace('version()', 'sqlite_version()', $select); |
|
| 1618 | - |
|
| 1619 | - // recomposer from |
|
| 1620 | - $from = (!is_array($from) ? $from : _sqlite_calculer_select_as($from)); |
|
| 1621 | - |
|
| 1622 | - $query = |
|
| 1623 | - _sqlite_calculer_expression('SELECT', $select, ', ') |
|
| 1624 | - . _sqlite_calculer_expression('FROM', $from, ', ') |
|
| 1625 | - . _sqlite_calculer_expression('WHERE', $where) |
|
| 1626 | - . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
|
| 1627 | - . _sqlite_calculer_expression('HAVING', $having) |
|
| 1628 | - . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1629 | - . ($limit ? "\nLIMIT $limit" : ''); |
|
| 1630 | - |
|
| 1631 | - // dans un select, on doit renvoyer la requête en cas d'erreur |
|
| 1632 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1633 | - // texte de la requete demande ? |
|
| 1634 | - if (!$requeter) { |
|
| 1635 | - return $res; |
|
| 1636 | - } |
|
| 1637 | - // erreur survenue ? |
|
| 1638 | - if ($res === false) { |
|
| 1639 | - return Sqlite::traduire_requete($query, $serveur); |
|
| 1640 | - } |
|
| 1641 | - |
|
| 1642 | - return $res; |
|
| 1616 | + // version() n'est pas connu de sqlite |
|
| 1617 | + $select = str_replace('version()', 'sqlite_version()', $select); |
|
| 1618 | + |
|
| 1619 | + // recomposer from |
|
| 1620 | + $from = (!is_array($from) ? $from : _sqlite_calculer_select_as($from)); |
|
| 1621 | + |
|
| 1622 | + $query = |
|
| 1623 | + _sqlite_calculer_expression('SELECT', $select, ', ') |
|
| 1624 | + . _sqlite_calculer_expression('FROM', $from, ', ') |
|
| 1625 | + . _sqlite_calculer_expression('WHERE', $where) |
|
| 1626 | + . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
|
| 1627 | + . _sqlite_calculer_expression('HAVING', $having) |
|
| 1628 | + . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1629 | + . ($limit ? "\nLIMIT $limit" : ''); |
|
| 1630 | + |
|
| 1631 | + // dans un select, on doit renvoyer la requête en cas d'erreur |
|
| 1632 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1633 | + // texte de la requete demande ? |
|
| 1634 | + if (!$requeter) { |
|
| 1635 | + return $res; |
|
| 1636 | + } |
|
| 1637 | + // erreur survenue ? |
|
| 1638 | + if ($res === false) { |
|
| 1639 | + return Sqlite::traduire_requete($query, $serveur); |
|
| 1640 | + } |
|
| 1641 | + |
|
| 1642 | + return $res; |
|
| 1643 | 1643 | } |
| 1644 | 1644 | |
| 1645 | 1645 | |
@@ -1659,32 +1659,32 @@ discard block |
||
| 1659 | 1659 | **/ |
| 1660 | 1660 | function spip_sqlite_selectdb($db, $serveur = '', $requeter = true) |
| 1661 | 1661 | { |
| 1662 | - _sqlite_init(); |
|
| 1663 | - |
|
| 1664 | - // interdire la creation d'une nouvelle base, |
|
| 1665 | - // sauf si on est dans l'installation |
|
| 1666 | - if ( |
|
| 1667 | - !is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1668 | - && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
|
| 1669 | - ) { |
|
| 1670 | - spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1671 | - |
|
| 1672 | - return false; |
|
| 1673 | - } |
|
| 1674 | - |
|
| 1675 | - // se connecter a la base indiquee |
|
| 1676 | - // avec les identifiants connus |
|
| 1677 | - $index = $serveur ? $serveur : 0; |
|
| 1678 | - |
|
| 1679 | - if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1680 | - if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 1681 | - return $db; |
|
| 1682 | - } |
|
| 1683 | - } else { |
|
| 1684 | - spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1685 | - } |
|
| 1686 | - |
|
| 1687 | - return false; |
|
| 1662 | + _sqlite_init(); |
|
| 1663 | + |
|
| 1664 | + // interdire la creation d'une nouvelle base, |
|
| 1665 | + // sauf si on est dans l'installation |
|
| 1666 | + if ( |
|
| 1667 | + !is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1668 | + && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
|
| 1669 | + ) { |
|
| 1670 | + spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1671 | + |
|
| 1672 | + return false; |
|
| 1673 | + } |
|
| 1674 | + |
|
| 1675 | + // se connecter a la base indiquee |
|
| 1676 | + // avec les identifiants connus |
|
| 1677 | + $index = $serveur ? $serveur : 0; |
|
| 1678 | + |
|
| 1679 | + if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1680 | + if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 1681 | + return $db; |
|
| 1682 | + } |
|
| 1683 | + } else { |
|
| 1684 | + spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1685 | + } |
|
| 1686 | + |
|
| 1687 | + return false; |
|
| 1688 | 1688 | } |
| 1689 | 1689 | |
| 1690 | 1690 | |
@@ -1700,8 +1700,8 @@ discard block |
||
| 1700 | 1700 | */ |
| 1701 | 1701 | function spip_sqlite_set_charset($charset, $serveur = '', $requeter = true) |
| 1702 | 1702 | { |
| 1703 | - # spip_log("Gestion charset sql a ecrire : "."SET NAMES "._q($charset), 'sqlite.'._LOG_ERREUR); |
|
| 1704 | - # return spip_sqlite_query("SET NAMES ". spip_sqlite_quote($charset), $serveur); //<-- Passe pas ! |
|
| 1703 | + # spip_log("Gestion charset sql a ecrire : "."SET NAMES "._q($charset), 'sqlite.'._LOG_ERREUR); |
|
| 1704 | + # return spip_sqlite_query("SET NAMES ". spip_sqlite_quote($charset), $serveur); //<-- Passe pas ! |
|
| 1705 | 1705 | } |
| 1706 | 1706 | |
| 1707 | 1707 | |
@@ -1720,24 +1720,24 @@ discard block |
||
| 1720 | 1720 | **/ |
| 1721 | 1721 | function spip_sqlite_showbase($match, $serveur = '', $requeter = true) |
| 1722 | 1722 | { |
| 1723 | - // type est le type d'entrée : table / index / view |
|
| 1724 | - // on ne retourne que les tables (?) et non les vues... |
|
| 1725 | - # ESCAPE non supporte par les versions sqlite <3 |
|
| 1726 | - # return spip_sqlite_query("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name LIKE "._q($match)." ESCAPE '\'", $serveur, $requeter); |
|
| 1727 | - $match = preg_quote($match); |
|
| 1728 | - $match = str_replace('\\\_', '[[TIRETBAS]]', $match); |
|
| 1729 | - $match = str_replace('\\\%', '[[POURCENT]]', $match); |
|
| 1730 | - $match = str_replace('_', '.', $match); |
|
| 1731 | - $match = str_replace('%', '.*', $match); |
|
| 1732 | - $match = str_replace('[[TIRETBAS]]', '_', $match); |
|
| 1733 | - $match = str_replace('[[POURCENT]]', '%', $match); |
|
| 1734 | - $match = "^$match$"; |
|
| 1735 | - |
|
| 1736 | - return spip_sqlite_query( |
|
| 1737 | - "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1738 | - $serveur, |
|
| 1739 | - $requeter |
|
| 1740 | - ); |
|
| 1723 | + // type est le type d'entrée : table / index / view |
|
| 1724 | + // on ne retourne que les tables (?) et non les vues... |
|
| 1725 | + # ESCAPE non supporte par les versions sqlite <3 |
|
| 1726 | + # return spip_sqlite_query("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name LIKE "._q($match)." ESCAPE '\'", $serveur, $requeter); |
|
| 1727 | + $match = preg_quote($match); |
|
| 1728 | + $match = str_replace('\\\_', '[[TIRETBAS]]', $match); |
|
| 1729 | + $match = str_replace('\\\%', '[[POURCENT]]', $match); |
|
| 1730 | + $match = str_replace('_', '.', $match); |
|
| 1731 | + $match = str_replace('%', '.*', $match); |
|
| 1732 | + $match = str_replace('[[TIRETBAS]]', '_', $match); |
|
| 1733 | + $match = str_replace('[[POURCENT]]', '%', $match); |
|
| 1734 | + $match = "^$match$"; |
|
| 1735 | + |
|
| 1736 | + return spip_sqlite_query( |
|
| 1737 | + "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1738 | + $serveur, |
|
| 1739 | + $requeter |
|
| 1740 | + ); |
|
| 1741 | 1741 | } |
| 1742 | 1742 | |
| 1743 | 1743 | /** |
@@ -1756,19 +1756,19 @@ discard block |
||
| 1756 | 1756 | **/ |
| 1757 | 1757 | function spip_sqlite_table_exists(string $table, $serveur = '', $requeter = true) |
| 1758 | 1758 | { |
| 1759 | - $r = spip_sqlite_query( |
|
| 1760 | - 'SELECT name FROM sqlite_master WHERE' |
|
| 1761 | - . ' type=\'table\'' |
|
| 1762 | - . ' AND name=' . spip_sqlite_quote($table, 'string') |
|
| 1763 | - . ' AND name NOT LIKE \'sqlite_%\'', |
|
| 1764 | - $serveur, |
|
| 1765 | - $requeter |
|
| 1766 | - ); |
|
| 1767 | - if (!$requeter) { |
|
| 1768 | - return $r; |
|
| 1769 | - } |
|
| 1770 | - $res = spip_sqlite_fetch($r); |
|
| 1771 | - return (bool) $res; |
|
| 1759 | + $r = spip_sqlite_query( |
|
| 1760 | + 'SELECT name FROM sqlite_master WHERE' |
|
| 1761 | + . ' type=\'table\'' |
|
| 1762 | + . ' AND name=' . spip_sqlite_quote($table, 'string') |
|
| 1763 | + . ' AND name NOT LIKE \'sqlite_%\'', |
|
| 1764 | + $serveur, |
|
| 1765 | + $requeter |
|
| 1766 | + ); |
|
| 1767 | + if (!$requeter) { |
|
| 1768 | + return $r; |
|
| 1769 | + } |
|
| 1770 | + $res = spip_sqlite_fetch($r); |
|
| 1771 | + return (bool) $res; |
|
| 1772 | 1772 | } |
| 1773 | 1773 | |
| 1774 | 1774 | define('_SQLITE_RE_SHOW_TABLE', '/^[^(),]*\(((?:[^()]*\((?:[^()]*\([^()]*\))?[^()]*\)[^()]*)*[^()]*)\)[^()]*$/'); |
@@ -1792,129 +1792,129 @@ discard block |
||
| 1792 | 1792 | */ |
| 1793 | 1793 | function spip_sqlite_showtable($nom_table, $serveur = '', $requeter = true) |
| 1794 | 1794 | { |
| 1795 | - $query = |
|
| 1796 | - 'SELECT sql, type FROM' |
|
| 1797 | - . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1798 | - . ' SELECT * FROM sqlite_temp_master)' |
|
| 1799 | - . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1800 | - . " AND type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%'" |
|
| 1801 | - . ' ORDER BY substr(type,2,1), name'; |
|
| 1802 | - |
|
| 1803 | - $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1804 | - if (!$a) { |
|
| 1805 | - return ''; |
|
| 1806 | - } |
|
| 1807 | - if (!$requeter) { |
|
| 1808 | - return $a; |
|
| 1809 | - } |
|
| 1810 | - if (!($a = spip_sqlite_fetch($a, null, $serveur))) { |
|
| 1811 | - return ''; |
|
| 1812 | - } |
|
| 1813 | - $vue = ($a['type'] == 'view'); // table | vue |
|
| 1814 | - |
|
| 1815 | - // c'est une table |
|
| 1816 | - // il faut parser le create |
|
| 1817 | - if (!$vue) { |
|
| 1818 | - if (!preg_match(_SQLITE_RE_SHOW_TABLE, array_shift($a), $r)) { |
|
| 1819 | - return ''; |
|
| 1820 | - } else { |
|
| 1821 | - $desc = $r[1]; |
|
| 1822 | - // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 1823 | - // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 1824 | - if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 1825 | - $namedkeys = $r[2]; |
|
| 1826 | - $desc = $r[1]; |
|
| 1827 | - } else { |
|
| 1828 | - $namedkeys = ''; |
|
| 1829 | - } |
|
| 1830 | - |
|
| 1831 | - $fields = []; |
|
| 1832 | - $keys = []; |
|
| 1833 | - |
|
| 1834 | - // enlever les contenus des valeurs DEFAULT 'xxx' qui pourraient perturber |
|
| 1835 | - // par exemple s'il contiennent une virgule. |
|
| 1836 | - // /!\ cela peut aussi echapper le nom des champs si la table a eu des operations avec SQLite Manager ! |
|
| 1837 | - list($desc, $echaps) = query_echappe_textes($desc); |
|
| 1838 | - |
|
| 1839 | - // separer toutes les descriptions de champs, separes par des virgules |
|
| 1840 | - # /!\ explode peut exploser aussi DECIMAL(10,2) ! |
|
| 1841 | - $k_precedent = null; |
|
| 1842 | - foreach (explode(',', $desc) as $v) { |
|
| 1843 | - preg_match('/^\s*([^\s]+)\s+(.*)/', $v, $r); |
|
| 1844 | - // Les cles de champs peuvent etre entourees |
|
| 1845 | - // de guillements doubles " , simples ', graves ` ou de crochets [ ], ou rien. |
|
| 1846 | - // http://www.sqlite.org/lang_keywords.html |
|
| 1847 | - $k = strtolower(query_reinjecte_textes($r[1], $echaps)); // champ, "champ", [champ]... |
|
| 1848 | - if ($char = strpbrk($k[0], '\'"[`')) { |
|
| 1849 | - $k = trim($k, $char); |
|
| 1850 | - if ($char == '[') { |
|
| 1851 | - $k = rtrim($k, ']'); |
|
| 1852 | - } |
|
| 1853 | - } |
|
| 1854 | - $def = query_reinjecte_textes($r[2], $echaps); // valeur du champ |
|
| 1855 | - |
|
| 1856 | - // rustine pour DECIMAL(10,2) |
|
| 1857 | - // s'il y a une parenthèse fermante dans la clé |
|
| 1858 | - // ou dans la définition sans qu'il n'y ait une ouverture avant |
|
| 1859 | - if (str_contains($k, ')') or preg_match('/^[^\(]*\)/', $def)) { |
|
| 1860 | - $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1861 | - continue; |
|
| 1862 | - } |
|
| 1863 | - |
|
| 1864 | - // la primary key peut etre dans une des descriptions de champs |
|
| 1865 | - // et non en fin de table, cas encore decouvert avec Sqlite Manager |
|
| 1866 | - if (stripos($r[2], 'PRIMARY KEY') !== false) { |
|
| 1867 | - $keys['PRIMARY KEY'] = $k; |
|
| 1868 | - } |
|
| 1869 | - |
|
| 1870 | - $fields[$k] = $def; |
|
| 1871 | - $k_precedent = $k; |
|
| 1872 | - } |
|
| 1873 | - // key inclues dans la requete |
|
| 1874 | - foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 1875 | - if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', $v, $r)) { |
|
| 1876 | - $k = str_replace('`', '', trim($r[1])); |
|
| 1877 | - $t = trim(strtolower(str_replace('`', '', $r[2])), '"'); |
|
| 1878 | - if ($k && !isset($keys[$k])) { |
|
| 1879 | - $keys[$k] = $t; |
|
| 1880 | - } else { |
|
| 1881 | - $keys[] = $t; |
|
| 1882 | - } |
|
| 1883 | - } |
|
| 1884 | - } |
|
| 1885 | - // sinon ajouter les key index |
|
| 1886 | - $query = |
|
| 1887 | - 'SELECT name,sql FROM' |
|
| 1888 | - . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1889 | - . ' SELECT * FROM sqlite_temp_master)' |
|
| 1890 | - . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1891 | - . " AND type='index' AND name NOT LIKE 'sqlite_%'" |
|
| 1892 | - . 'ORDER BY substr(type,2,1), name'; |
|
| 1893 | - $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1894 | - while ($r = spip_sqlite_fetch($a, null, $serveur)) { |
|
| 1895 | - $key = str_replace($nom_table . '_', '', $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1896 | - $keytype = 'KEY'; |
|
| 1897 | - if (strpos($r['sql'], 'UNIQUE INDEX') !== false) { |
|
| 1898 | - $keytype = 'UNIQUE KEY'; |
|
| 1899 | - } |
|
| 1900 | - $colonnes = preg_replace(',.*\((.*)\).*,', '$1', $r['sql']); |
|
| 1901 | - $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1902 | - } |
|
| 1903 | - } |
|
| 1904 | - } // c'est une vue, on liste les champs disponibles simplement |
|
| 1905 | - else { |
|
| 1906 | - if ($res = sql_fetsel('*', $nom_table, '', '', '', '1', '', $serveur)) { // limit 1 |
|
| 1907 | - $fields = []; |
|
| 1908 | - foreach ($res as $c => $v) { |
|
| 1909 | - $fields[$c] = ''; |
|
| 1910 | - } |
|
| 1911 | - $keys = []; |
|
| 1912 | - } else { |
|
| 1913 | - return ''; |
|
| 1914 | - } |
|
| 1915 | - } |
|
| 1916 | - |
|
| 1917 | - return ['field' => $fields, 'key' => $keys]; |
|
| 1795 | + $query = |
|
| 1796 | + 'SELECT sql, type FROM' |
|
| 1797 | + . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1798 | + . ' SELECT * FROM sqlite_temp_master)' |
|
| 1799 | + . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1800 | + . " AND type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%'" |
|
| 1801 | + . ' ORDER BY substr(type,2,1), name'; |
|
| 1802 | + |
|
| 1803 | + $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1804 | + if (!$a) { |
|
| 1805 | + return ''; |
|
| 1806 | + } |
|
| 1807 | + if (!$requeter) { |
|
| 1808 | + return $a; |
|
| 1809 | + } |
|
| 1810 | + if (!($a = spip_sqlite_fetch($a, null, $serveur))) { |
|
| 1811 | + return ''; |
|
| 1812 | + } |
|
| 1813 | + $vue = ($a['type'] == 'view'); // table | vue |
|
| 1814 | + |
|
| 1815 | + // c'est une table |
|
| 1816 | + // il faut parser le create |
|
| 1817 | + if (!$vue) { |
|
| 1818 | + if (!preg_match(_SQLITE_RE_SHOW_TABLE, array_shift($a), $r)) { |
|
| 1819 | + return ''; |
|
| 1820 | + } else { |
|
| 1821 | + $desc = $r[1]; |
|
| 1822 | + // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 1823 | + // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 1824 | + if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 1825 | + $namedkeys = $r[2]; |
|
| 1826 | + $desc = $r[1]; |
|
| 1827 | + } else { |
|
| 1828 | + $namedkeys = ''; |
|
| 1829 | + } |
|
| 1830 | + |
|
| 1831 | + $fields = []; |
|
| 1832 | + $keys = []; |
|
| 1833 | + |
|
| 1834 | + // enlever les contenus des valeurs DEFAULT 'xxx' qui pourraient perturber |
|
| 1835 | + // par exemple s'il contiennent une virgule. |
|
| 1836 | + // /!\ cela peut aussi echapper le nom des champs si la table a eu des operations avec SQLite Manager ! |
|
| 1837 | + list($desc, $echaps) = query_echappe_textes($desc); |
|
| 1838 | + |
|
| 1839 | + // separer toutes les descriptions de champs, separes par des virgules |
|
| 1840 | + # /!\ explode peut exploser aussi DECIMAL(10,2) ! |
|
| 1841 | + $k_precedent = null; |
|
| 1842 | + foreach (explode(',', $desc) as $v) { |
|
| 1843 | + preg_match('/^\s*([^\s]+)\s+(.*)/', $v, $r); |
|
| 1844 | + // Les cles de champs peuvent etre entourees |
|
| 1845 | + // de guillements doubles " , simples ', graves ` ou de crochets [ ], ou rien. |
|
| 1846 | + // http://www.sqlite.org/lang_keywords.html |
|
| 1847 | + $k = strtolower(query_reinjecte_textes($r[1], $echaps)); // champ, "champ", [champ]... |
|
| 1848 | + if ($char = strpbrk($k[0], '\'"[`')) { |
|
| 1849 | + $k = trim($k, $char); |
|
| 1850 | + if ($char == '[') { |
|
| 1851 | + $k = rtrim($k, ']'); |
|
| 1852 | + } |
|
| 1853 | + } |
|
| 1854 | + $def = query_reinjecte_textes($r[2], $echaps); // valeur du champ |
|
| 1855 | + |
|
| 1856 | + // rustine pour DECIMAL(10,2) |
|
| 1857 | + // s'il y a une parenthèse fermante dans la clé |
|
| 1858 | + // ou dans la définition sans qu'il n'y ait une ouverture avant |
|
| 1859 | + if (str_contains($k, ')') or preg_match('/^[^\(]*\)/', $def)) { |
|
| 1860 | + $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1861 | + continue; |
|
| 1862 | + } |
|
| 1863 | + |
|
| 1864 | + // la primary key peut etre dans une des descriptions de champs |
|
| 1865 | + // et non en fin de table, cas encore decouvert avec Sqlite Manager |
|
| 1866 | + if (stripos($r[2], 'PRIMARY KEY') !== false) { |
|
| 1867 | + $keys['PRIMARY KEY'] = $k; |
|
| 1868 | + } |
|
| 1869 | + |
|
| 1870 | + $fields[$k] = $def; |
|
| 1871 | + $k_precedent = $k; |
|
| 1872 | + } |
|
| 1873 | + // key inclues dans la requete |
|
| 1874 | + foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 1875 | + if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', $v, $r)) { |
|
| 1876 | + $k = str_replace('`', '', trim($r[1])); |
|
| 1877 | + $t = trim(strtolower(str_replace('`', '', $r[2])), '"'); |
|
| 1878 | + if ($k && !isset($keys[$k])) { |
|
| 1879 | + $keys[$k] = $t; |
|
| 1880 | + } else { |
|
| 1881 | + $keys[] = $t; |
|
| 1882 | + } |
|
| 1883 | + } |
|
| 1884 | + } |
|
| 1885 | + // sinon ajouter les key index |
|
| 1886 | + $query = |
|
| 1887 | + 'SELECT name,sql FROM' |
|
| 1888 | + . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1889 | + . ' SELECT * FROM sqlite_temp_master)' |
|
| 1890 | + . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1891 | + . " AND type='index' AND name NOT LIKE 'sqlite_%'" |
|
| 1892 | + . 'ORDER BY substr(type,2,1), name'; |
|
| 1893 | + $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1894 | + while ($r = spip_sqlite_fetch($a, null, $serveur)) { |
|
| 1895 | + $key = str_replace($nom_table . '_', '', $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1896 | + $keytype = 'KEY'; |
|
| 1897 | + if (strpos($r['sql'], 'UNIQUE INDEX') !== false) { |
|
| 1898 | + $keytype = 'UNIQUE KEY'; |
|
| 1899 | + } |
|
| 1900 | + $colonnes = preg_replace(',.*\((.*)\).*,', '$1', $r['sql']); |
|
| 1901 | + $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1902 | + } |
|
| 1903 | + } |
|
| 1904 | + } // c'est une vue, on liste les champs disponibles simplement |
|
| 1905 | + else { |
|
| 1906 | + if ($res = sql_fetsel('*', $nom_table, '', '', '', '1', '', $serveur)) { // limit 1 |
|
| 1907 | + $fields = []; |
|
| 1908 | + foreach ($res as $c => $v) { |
|
| 1909 | + $fields[$c] = ''; |
|
| 1910 | + } |
|
| 1911 | + $keys = []; |
|
| 1912 | + } else { |
|
| 1913 | + return ''; |
|
| 1914 | + } |
|
| 1915 | + } |
|
| 1916 | + |
|
| 1917 | + return ['field' => $fields, 'key' => $keys]; |
|
| 1918 | 1918 | } |
| 1919 | 1919 | |
| 1920 | 1920 | |
@@ -1941,24 +1941,24 @@ discard block |
||
| 1941 | 1941 | */ |
| 1942 | 1942 | function spip_sqlite_update($table, $champs, $where = '', $desc = '', $serveur = '', $requeter = true) |
| 1943 | 1943 | { |
| 1944 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1945 | - $champs = _sqlite_ajouter_champs_timestamp($table, $champs, $desc, $serveur); |
|
| 1946 | - |
|
| 1947 | - $set = []; |
|
| 1948 | - foreach ($champs as $champ => $val) { |
|
| 1949 | - $set[] = $champ . "=$val"; |
|
| 1950 | - } |
|
| 1951 | - if (!empty($set)) { |
|
| 1952 | - return spip_sqlite_query( |
|
| 1953 | - _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1954 | - . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1955 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 1956 | - $serveur, |
|
| 1957 | - $requeter |
|
| 1958 | - ); |
|
| 1959 | - } |
|
| 1960 | - |
|
| 1961 | - return false; |
|
| 1944 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1945 | + $champs = _sqlite_ajouter_champs_timestamp($table, $champs, $desc, $serveur); |
|
| 1946 | + |
|
| 1947 | + $set = []; |
|
| 1948 | + foreach ($champs as $champ => $val) { |
|
| 1949 | + $set[] = $champ . "=$val"; |
|
| 1950 | + } |
|
| 1951 | + if (!empty($set)) { |
|
| 1952 | + return spip_sqlite_query( |
|
| 1953 | + _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1954 | + . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1955 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 1956 | + $serveur, |
|
| 1957 | + $requeter |
|
| 1958 | + ); |
|
| 1959 | + } |
|
| 1960 | + |
|
| 1961 | + return false; |
|
| 1962 | 1962 | } |
| 1963 | 1963 | |
| 1964 | 1964 | |
@@ -1989,38 +1989,38 @@ discard block |
||
| 1989 | 1989 | function spip_sqlite_updateq($table, $champs, $where = '', $desc = [], $serveur = '', $requeter = true) |
| 1990 | 1990 | { |
| 1991 | 1991 | |
| 1992 | - if (!$champs) { |
|
| 1993 | - return; |
|
| 1994 | - } |
|
| 1995 | - if (!$desc) { |
|
| 1996 | - $desc = description_table($table, $serveur); |
|
| 1997 | - } |
|
| 1998 | - if (!$desc) { |
|
| 1999 | - die("$table insertion sans description"); |
|
| 2000 | - } |
|
| 2001 | - $fields = $desc['field']; |
|
| 2002 | - |
|
| 2003 | - $set = []; |
|
| 2004 | - foreach ($champs as $champ => $val) { |
|
| 2005 | - $set[$champ] = $champ . '=' . _sqlite_calculer_cite($val, isset($fields[$champ]) ? $fields[$champ] : ''); |
|
| 2006 | - } |
|
| 2007 | - |
|
| 2008 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 2009 | - // attention ils sont deja quotes |
|
| 2010 | - $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 2011 | - foreach ($maj as $champ => $val) { |
|
| 2012 | - if (!isset($set[$champ])) { |
|
| 2013 | - $set[$champ] = $champ . '=' . $val; |
|
| 2014 | - } |
|
| 2015 | - } |
|
| 2016 | - |
|
| 2017 | - return spip_sqlite_query( |
|
| 2018 | - _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 2019 | - . _sqlite_calculer_expression('SET', $set, ',') |
|
| 2020 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 2021 | - $serveur, |
|
| 2022 | - $requeter |
|
| 2023 | - ); |
|
| 1992 | + if (!$champs) { |
|
| 1993 | + return; |
|
| 1994 | + } |
|
| 1995 | + if (!$desc) { |
|
| 1996 | + $desc = description_table($table, $serveur); |
|
| 1997 | + } |
|
| 1998 | + if (!$desc) { |
|
| 1999 | + die("$table insertion sans description"); |
|
| 2000 | + } |
|
| 2001 | + $fields = $desc['field']; |
|
| 2002 | + |
|
| 2003 | + $set = []; |
|
| 2004 | + foreach ($champs as $champ => $val) { |
|
| 2005 | + $set[$champ] = $champ . '=' . _sqlite_calculer_cite($val, isset($fields[$champ]) ? $fields[$champ] : ''); |
|
| 2006 | + } |
|
| 2007 | + |
|
| 2008 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 2009 | + // attention ils sont deja quotes |
|
| 2010 | + $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 2011 | + foreach ($maj as $champ => $val) { |
|
| 2012 | + if (!isset($set[$champ])) { |
|
| 2013 | + $set[$champ] = $champ . '=' . $val; |
|
| 2014 | + } |
|
| 2015 | + } |
|
| 2016 | + |
|
| 2017 | + return spip_sqlite_query( |
|
| 2018 | + _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 2019 | + . _sqlite_calculer_expression('SET', $set, ',') |
|
| 2020 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 2021 | + $serveur, |
|
| 2022 | + $requeter |
|
| 2023 | + ); |
|
| 2024 | 2024 | } |
| 2025 | 2025 | |
| 2026 | 2026 | |
@@ -2039,17 +2039,17 @@ discard block |
||
| 2039 | 2039 | */ |
| 2040 | 2040 | function _sqlite_init() |
| 2041 | 2041 | { |
| 2042 | - if (!defined('_DIR_DB')) { |
|
| 2043 | - define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 2044 | - } |
|
| 2045 | - if (!defined('_SQLITE_CHMOD')) { |
|
| 2046 | - define('_SQLITE_CHMOD', _SPIP_CHMOD); |
|
| 2047 | - } |
|
| 2048 | - |
|
| 2049 | - if (!is_dir($d = _DIR_DB)) { |
|
| 2050 | - include_spip('inc/flock'); |
|
| 2051 | - sous_repertoire($d); |
|
| 2052 | - } |
|
| 2042 | + if (!defined('_DIR_DB')) { |
|
| 2043 | + define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 2044 | + } |
|
| 2045 | + if (!defined('_SQLITE_CHMOD')) { |
|
| 2046 | + define('_SQLITE_CHMOD', _SPIP_CHMOD); |
|
| 2047 | + } |
|
| 2048 | + |
|
| 2049 | + if (!is_dir($d = _DIR_DB)) { |
|
| 2050 | + include_spip('inc/flock'); |
|
| 2051 | + sous_repertoire($d); |
|
| 2052 | + } |
|
| 2053 | 2053 | } |
| 2054 | 2054 | |
| 2055 | 2055 | |
@@ -2064,20 +2064,20 @@ discard block |
||
| 2064 | 2064 | */ |
| 2065 | 2065 | function _sqlite_is_version($version = '', $link = '', $serveur = '', $requeter = true) |
| 2066 | 2066 | { |
| 2067 | - if ($link === '') { |
|
| 2068 | - $link = _sqlite_link($serveur); |
|
| 2069 | - } |
|
| 2070 | - if (!$link) { |
|
| 2071 | - return false; |
|
| 2072 | - } |
|
| 2067 | + if ($link === '') { |
|
| 2068 | + $link = _sqlite_link($serveur); |
|
| 2069 | + } |
|
| 2070 | + if (!$link) { |
|
| 2071 | + return false; |
|
| 2072 | + } |
|
| 2073 | 2073 | |
| 2074 | - $v = 3; |
|
| 2074 | + $v = 3; |
|
| 2075 | 2075 | |
| 2076 | - if (!$version) { |
|
| 2077 | - return $v; |
|
| 2078 | - } |
|
| 2076 | + if (!$version) { |
|
| 2077 | + return $v; |
|
| 2078 | + } |
|
| 2079 | 2079 | |
| 2080 | - return ($version == $v); |
|
| 2080 | + return ($version == $v); |
|
| 2081 | 2081 | } |
| 2082 | 2082 | |
| 2083 | 2083 | |
@@ -2089,9 +2089,9 @@ discard block |
||
| 2089 | 2089 | */ |
| 2090 | 2090 | function _sqlite_link($serveur = '') |
| 2091 | 2091 | { |
| 2092 | - $link = &$GLOBALS['connexions'][$serveur ? $serveur : 0]['link']; |
|
| 2092 | + $link = &$GLOBALS['connexions'][$serveur ? $serveur : 0]['link']; |
|
| 2093 | 2093 | |
| 2094 | - return $link; |
|
| 2094 | + return $link; |
|
| 2095 | 2095 | } |
| 2096 | 2096 | |
| 2097 | 2097 | |
@@ -2107,54 +2107,54 @@ discard block |
||
| 2107 | 2107 | */ |
| 2108 | 2108 | function _sqlite_calculer_cite($v, $type) |
| 2109 | 2109 | { |
| 2110 | - if ($type) { |
|
| 2111 | - if ( |
|
| 2112 | - is_null($v) |
|
| 2113 | - and stripos($type, 'NOT NULL') === false |
|
| 2114 | - ) { |
|
| 2115 | - // null php se traduit en NULL SQL |
|
| 2116 | - return 'NULL'; |
|
| 2117 | - } |
|
| 2118 | - |
|
| 2119 | - if (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 2120 | - return $v; |
|
| 2121 | - } |
|
| 2122 | - if (sql_test_int($type)) { |
|
| 2123 | - if (is_numeric($v)) { |
|
| 2124 | - return $v; |
|
| 2125 | - } elseif ($v === null) { |
|
| 2126 | - return 0; |
|
| 2127 | - } elseif (ctype_xdigit(substr($v, 2)) and strncmp($v, '0x', 2) === 0) { |
|
| 2128 | - return hexdec(substr($v, 2)); |
|
| 2129 | - } else { |
|
| 2130 | - return intval($v); |
|
| 2131 | - } |
|
| 2132 | - } |
|
| 2133 | - } else { |
|
| 2134 | - // si on ne connait pas le type on le deduit de $v autant que possible |
|
| 2135 | - if (is_bool($v)) { |
|
| 2136 | - return strval(intval($v)); |
|
| 2137 | - } elseif (is_numeric($v)) { |
|
| 2138 | - return strval($v); |
|
| 2139 | - } |
|
| 2140 | - } |
|
| 2141 | - |
|
| 2142 | - // trouver un link sqlite pour faire l'echappement |
|
| 2143 | - foreach ($GLOBALS['connexions'] as $s) { |
|
| 2144 | - if ( |
|
| 2145 | - $l = $s['link'] |
|
| 2146 | - and is_object($l) |
|
| 2147 | - and $l instanceof \PDO |
|
| 2148 | - and $l->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'sqlite' |
|
| 2149 | - ) { |
|
| 2150 | - return $l->quote($v ?? ''); |
|
| 2151 | - } |
|
| 2152 | - } |
|
| 2153 | - |
|
| 2154 | - // echapper les ' en '' |
|
| 2155 | - spip_log('Pas de methode ->quote pour echapper', 'sqlite.' . _LOG_INFO_IMPORTANTE); |
|
| 2156 | - |
|
| 2157 | - return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 2110 | + if ($type) { |
|
| 2111 | + if ( |
|
| 2112 | + is_null($v) |
|
| 2113 | + and stripos($type, 'NOT NULL') === false |
|
| 2114 | + ) { |
|
| 2115 | + // null php se traduit en NULL SQL |
|
| 2116 | + return 'NULL'; |
|
| 2117 | + } |
|
| 2118 | + |
|
| 2119 | + if (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 2120 | + return $v; |
|
| 2121 | + } |
|
| 2122 | + if (sql_test_int($type)) { |
|
| 2123 | + if (is_numeric($v)) { |
|
| 2124 | + return $v; |
|
| 2125 | + } elseif ($v === null) { |
|
| 2126 | + return 0; |
|
| 2127 | + } elseif (ctype_xdigit(substr($v, 2)) and strncmp($v, '0x', 2) === 0) { |
|
| 2128 | + return hexdec(substr($v, 2)); |
|
| 2129 | + } else { |
|
| 2130 | + return intval($v); |
|
| 2131 | + } |
|
| 2132 | + } |
|
| 2133 | + } else { |
|
| 2134 | + // si on ne connait pas le type on le deduit de $v autant que possible |
|
| 2135 | + if (is_bool($v)) { |
|
| 2136 | + return strval(intval($v)); |
|
| 2137 | + } elseif (is_numeric($v)) { |
|
| 2138 | + return strval($v); |
|
| 2139 | + } |
|
| 2140 | + } |
|
| 2141 | + |
|
| 2142 | + // trouver un link sqlite pour faire l'echappement |
|
| 2143 | + foreach ($GLOBALS['connexions'] as $s) { |
|
| 2144 | + if ( |
|
| 2145 | + $l = $s['link'] |
|
| 2146 | + and is_object($l) |
|
| 2147 | + and $l instanceof \PDO |
|
| 2148 | + and $l->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'sqlite' |
|
| 2149 | + ) { |
|
| 2150 | + return $l->quote($v ?? ''); |
|
| 2151 | + } |
|
| 2152 | + } |
|
| 2153 | + |
|
| 2154 | + // echapper les ' en '' |
|
| 2155 | + spip_log('Pas de methode ->quote pour echapper', 'sqlite.' . _LOG_INFO_IMPORTANTE); |
|
| 2156 | + |
|
| 2157 | + return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 2158 | 2158 | } |
| 2159 | 2159 | |
| 2160 | 2160 | |
@@ -2171,21 +2171,21 @@ discard block |
||
| 2171 | 2171 | */ |
| 2172 | 2172 | function _sqlite_calculer_expression($expression, $v, $join = 'AND') |
| 2173 | 2173 | { |
| 2174 | - if (empty($v)) { |
|
| 2175 | - return ''; |
|
| 2176 | - } |
|
| 2177 | - |
|
| 2178 | - $exp = "\n$expression "; |
|
| 2179 | - |
|
| 2180 | - if (!is_array($v)) { |
|
| 2181 | - return $exp . $v; |
|
| 2182 | - } else { |
|
| 2183 | - if (strtoupper($join) === 'AND') { |
|
| 2184 | - return $exp . join("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2185 | - } else { |
|
| 2186 | - return $exp . join($join, $v); |
|
| 2187 | - } |
|
| 2188 | - } |
|
| 2174 | + if (empty($v)) { |
|
| 2175 | + return ''; |
|
| 2176 | + } |
|
| 2177 | + |
|
| 2178 | + $exp = "\n$expression "; |
|
| 2179 | + |
|
| 2180 | + if (!is_array($v)) { |
|
| 2181 | + return $exp . $v; |
|
| 2182 | + } else { |
|
| 2183 | + if (strtoupper($join) === 'AND') { |
|
| 2184 | + return $exp . join("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2185 | + } else { |
|
| 2186 | + return $exp . join($join, $v); |
|
| 2187 | + } |
|
| 2188 | + } |
|
| 2189 | 2189 | } |
| 2190 | 2190 | |
| 2191 | 2191 | |
@@ -2202,7 +2202,7 @@ discard block |
||
| 2202 | 2202 | */ |
| 2203 | 2203 | function _sqlite_calculer_order($orderby) |
| 2204 | 2204 | { |
| 2205 | - return (is_array($orderby)) ? join(', ', $orderby) : $orderby; |
|
| 2205 | + return (is_array($orderby)) ? join(', ', $orderby) : $orderby; |
|
| 2206 | 2206 | } |
| 2207 | 2207 | |
| 2208 | 2208 | |
@@ -2214,26 +2214,26 @@ discard block |
||
| 2214 | 2214 | */ |
| 2215 | 2215 | function _sqlite_calculer_select_as($args) |
| 2216 | 2216 | { |
| 2217 | - $res = ''; |
|
| 2218 | - foreach ($args as $k => $v) { |
|
| 2219 | - if (substr($k, -1) == '@') { |
|
| 2220 | - // c'est une jointure qui se refere au from precedent |
|
| 2221 | - // pas de virgule |
|
| 2222 | - $res .= ' ' . $v; |
|
| 2223 | - } else { |
|
| 2224 | - if (!is_numeric($k)) { |
|
| 2225 | - $p = strpos($v, ' '); |
|
| 2226 | - if ($p) { |
|
| 2227 | - $v = substr($v, 0, $p) . " AS '$k'" . substr($v, $p); |
|
| 2228 | - } else { |
|
| 2229 | - $v .= " AS '$k'"; |
|
| 2230 | - } |
|
| 2231 | - } |
|
| 2232 | - $res .= ', ' . $v; |
|
| 2233 | - } |
|
| 2234 | - } |
|
| 2235 | - |
|
| 2236 | - return substr($res, 2); |
|
| 2217 | + $res = ''; |
|
| 2218 | + foreach ($args as $k => $v) { |
|
| 2219 | + if (substr($k, -1) == '@') { |
|
| 2220 | + // c'est une jointure qui se refere au from precedent |
|
| 2221 | + // pas de virgule |
|
| 2222 | + $res .= ' ' . $v; |
|
| 2223 | + } else { |
|
| 2224 | + if (!is_numeric($k)) { |
|
| 2225 | + $p = strpos($v, ' '); |
|
| 2226 | + if ($p) { |
|
| 2227 | + $v = substr($v, 0, $p) . " AS '$k'" . substr($v, $p); |
|
| 2228 | + } else { |
|
| 2229 | + $v .= " AS '$k'"; |
|
| 2230 | + } |
|
| 2231 | + } |
|
| 2232 | + $res .= ', ' . $v; |
|
| 2233 | + } |
|
| 2234 | + } |
|
| 2235 | + |
|
| 2236 | + return substr($res, 2); |
|
| 2237 | 2237 | } |
| 2238 | 2238 | |
| 2239 | 2239 | |
@@ -2257,26 +2257,26 @@ discard block |
||
| 2257 | 2257 | */ |
| 2258 | 2258 | function _sqlite_calculer_where($v) |
| 2259 | 2259 | { |
| 2260 | - if (!is_array($v)) { |
|
| 2261 | - return $v; |
|
| 2262 | - } |
|
| 2263 | - |
|
| 2264 | - $op = array_shift($v); |
|
| 2265 | - if (!($n = count($v))) { |
|
| 2266 | - return $op; |
|
| 2267 | - } else { |
|
| 2268 | - $arg = _sqlite_calculer_where(array_shift($v)); |
|
| 2269 | - if ($n == 1) { |
|
| 2270 | - return "$op($arg)"; |
|
| 2271 | - } else { |
|
| 2272 | - $arg2 = _sqlite_calculer_where(array_shift($v)); |
|
| 2273 | - if ($n == 2) { |
|
| 2274 | - return "($arg $op $arg2)"; |
|
| 2275 | - } else { |
|
| 2276 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 2277 | - } |
|
| 2278 | - } |
|
| 2279 | - } |
|
| 2260 | + if (!is_array($v)) { |
|
| 2261 | + return $v; |
|
| 2262 | + } |
|
| 2263 | + |
|
| 2264 | + $op = array_shift($v); |
|
| 2265 | + if (!($n = count($v))) { |
|
| 2266 | + return $op; |
|
| 2267 | + } else { |
|
| 2268 | + $arg = _sqlite_calculer_where(array_shift($v)); |
|
| 2269 | + if ($n == 1) { |
|
| 2270 | + return "$op($arg)"; |
|
| 2271 | + } else { |
|
| 2272 | + $arg2 = _sqlite_calculer_where(array_shift($v)); |
|
| 2273 | + if ($n == 2) { |
|
| 2274 | + return "($arg $op $arg2)"; |
|
| 2275 | + } else { |
|
| 2276 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 2277 | + } |
|
| 2278 | + } |
|
| 2279 | + } |
|
| 2280 | 2280 | } |
| 2281 | 2281 | |
| 2282 | 2282 | |
@@ -2292,19 +2292,19 @@ discard block |
||
| 2292 | 2292 | */ |
| 2293 | 2293 | function _sqlite_charger_version($version = '') |
| 2294 | 2294 | { |
| 2295 | - $versions = []; |
|
| 2296 | - |
|
| 2297 | - // version 3 |
|
| 2298 | - if (!$version || $version == 3) { |
|
| 2299 | - if (extension_loaded('pdo') && extension_loaded('pdo_sqlite')) { |
|
| 2300 | - $versions[] = 3; |
|
| 2301 | - } |
|
| 2302 | - } |
|
| 2303 | - if ($version) { |
|
| 2304 | - return in_array($version, $versions); |
|
| 2305 | - } |
|
| 2306 | - |
|
| 2307 | - return $versions; |
|
| 2295 | + $versions = []; |
|
| 2296 | + |
|
| 2297 | + // version 3 |
|
| 2298 | + if (!$version || $version == 3) { |
|
| 2299 | + if (extension_loaded('pdo') && extension_loaded('pdo_sqlite')) { |
|
| 2300 | + $versions[] = 3; |
|
| 2301 | + } |
|
| 2302 | + } |
|
| 2303 | + if ($version) { |
|
| 2304 | + return in_array($version, $versions); |
|
| 2305 | + } |
|
| 2306 | + |
|
| 2307 | + return $versions; |
|
| 2308 | 2308 | } |
| 2309 | 2309 | |
| 2310 | 2310 | |
@@ -2343,147 +2343,147 @@ discard block |
||
| 2343 | 2343 | function _sqlite_modifier_table($table, $colonne, $opt = [], $serveur = '') |
| 2344 | 2344 | { |
| 2345 | 2345 | |
| 2346 | - if (is_array($table)) { |
|
| 2347 | - $table_destination = reset($table); |
|
| 2348 | - $table_origine = key($table); |
|
| 2349 | - } else { |
|
| 2350 | - $table_origine = $table_destination = $table; |
|
| 2351 | - } |
|
| 2352 | - // ne prend actuellement qu'un changement |
|
| 2353 | - // mais pourra etre adapte pour changer plus qu'une colonne a la fois |
|
| 2354 | - if (is_array($colonne)) { |
|
| 2355 | - $colonne_destination = reset($colonne); |
|
| 2356 | - $colonne_origine = key($colonne); |
|
| 2357 | - } else { |
|
| 2358 | - $colonne_origine = $colonne_destination = $colonne; |
|
| 2359 | - } |
|
| 2360 | - if (!isset($opt['field'])) { |
|
| 2361 | - $opt['field'] = []; |
|
| 2362 | - } |
|
| 2363 | - if (!isset($opt['key'])) { |
|
| 2364 | - $opt['key'] = []; |
|
| 2365 | - } |
|
| 2366 | - |
|
| 2367 | - // si les noms de tables sont differents, pas besoin de table temporaire |
|
| 2368 | - // on prendra directement le nom de la future table |
|
| 2369 | - $meme_table = ($table_origine == $table_destination); |
|
| 2370 | - |
|
| 2371 | - $def_origine = sql_showtable($table_origine, false, $serveur); |
|
| 2372 | - if (!$def_origine or !isset($def_origine['field'])) { |
|
| 2373 | - spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2374 | - |
|
| 2375 | - return false; |
|
| 2376 | - } |
|
| 2377 | - |
|
| 2378 | - |
|
| 2379 | - $table_tmp = $table_origine . '_tmp'; |
|
| 2380 | - |
|
| 2381 | - // 1) creer une table temporaire avec les modifications |
|
| 2382 | - // - DROP : suppression de la colonne |
|
| 2383 | - // - CHANGE : modification de la colonne |
|
| 2384 | - // (foreach pour conserver l'ordre des champs) |
|
| 2385 | - |
|
| 2386 | - // field |
|
| 2387 | - $fields = []; |
|
| 2388 | - // pour le INSERT INTO plus loin |
|
| 2389 | - // stocker la correspondance nouvelles->anciennes colonnes |
|
| 2390 | - $fields_correspondances = []; |
|
| 2391 | - foreach ($def_origine['field'] as $c => $d) { |
|
| 2392 | - if ($colonne_origine && ($c == $colonne_origine)) { |
|
| 2393 | - // si pas DROP |
|
| 2394 | - if ($colonne_destination) { |
|
| 2395 | - $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2396 | - $fields_correspondances[$colonne_destination] = $c; |
|
| 2397 | - } |
|
| 2398 | - } else { |
|
| 2399 | - $fields[$c] = $d; |
|
| 2400 | - $fields_correspondances[$c] = $c; |
|
| 2401 | - } |
|
| 2402 | - } |
|
| 2403 | - // cas de ADD sqlite2 (ajout du champ en fin de table): |
|
| 2404 | - if (!$colonne_origine && $colonne_destination) { |
|
| 2405 | - $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2406 | - } |
|
| 2407 | - |
|
| 2408 | - // key... |
|
| 2409 | - $keys = []; |
|
| 2410 | - foreach ($def_origine['key'] as $c => $d) { |
|
| 2411 | - $c = str_replace($colonne_origine, $colonne_destination, $c); |
|
| 2412 | - $d = str_replace($colonne_origine, $colonne_destination, $d); |
|
| 2413 | - // seulement si on ne supprime pas la colonne ! |
|
| 2414 | - if ($d) { |
|
| 2415 | - $keys[$c] = $d; |
|
| 2416 | - } |
|
| 2417 | - } |
|
| 2418 | - |
|
| 2419 | - // autres keys, on merge |
|
| 2420 | - $keys = array_merge($keys, $opt['key']); |
|
| 2421 | - $queries = []; |
|
| 2422 | - |
|
| 2423 | - // copier dans destination (si differente de origine), sinon tmp |
|
| 2424 | - $table_copie = ($meme_table) ? $table_tmp : $table_destination; |
|
| 2425 | - $autoinc = (isset($keys['PRIMARY KEY']) |
|
| 2426 | - and $keys['PRIMARY KEY'] |
|
| 2427 | - and stripos($keys['PRIMARY KEY'], ',') === false |
|
| 2428 | - and stripos($fields[$keys['PRIMARY KEY']], 'default') === false); |
|
| 2429 | - |
|
| 2430 | - if ( |
|
| 2431 | - $q = _sqlite_requete_create( |
|
| 2432 | - $table_copie, |
|
| 2433 | - $fields, |
|
| 2434 | - $keys, |
|
| 2435 | - $autoinc, |
|
| 2436 | - $temporary = false, |
|
| 2437 | - $ifnotexists = true, |
|
| 2438 | - $serveur |
|
| 2439 | - ) |
|
| 2440 | - ) { |
|
| 2441 | - $queries[] = $q; |
|
| 2442 | - } |
|
| 2443 | - |
|
| 2444 | - |
|
| 2445 | - // 2) y copier les champs qui vont bien |
|
| 2446 | - $champs_dest = join(', ', array_keys($fields_correspondances)); |
|
| 2447 | - $champs_ori = join(', ', $fields_correspondances); |
|
| 2448 | - $queries[] = "INSERT INTO $table_copie ($champs_dest) SELECT $champs_ori FROM $table_origine"; |
|
| 2449 | - |
|
| 2450 | - // 3) supprimer la table d'origine |
|
| 2451 | - $queries[] = "DROP TABLE $table_origine"; |
|
| 2452 | - |
|
| 2453 | - // 4) renommer la table temporaire |
|
| 2454 | - // avec le nom de la table destination |
|
| 2455 | - // si necessaire |
|
| 2456 | - if ($meme_table) { |
|
| 2457 | - $queries[] = "ALTER TABLE $table_copie RENAME TO $table_destination"; |
|
| 2458 | - } |
|
| 2459 | - |
|
| 2460 | - // 5) remettre les index ! |
|
| 2461 | - foreach ($keys as $k => $v) { |
|
| 2462 | - if ($k == 'PRIMARY KEY') { |
|
| 2463 | - } else { |
|
| 2464 | - // enlever KEY |
|
| 2465 | - $k = substr($k, 4); |
|
| 2466 | - $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2467 | - } |
|
| 2468 | - } |
|
| 2469 | - |
|
| 2470 | - |
|
| 2471 | - if (count($queries)) { |
|
| 2472 | - Sqlite::demarrer_transaction($serveur); |
|
| 2473 | - // il faut les faire une par une car $query = join('; ', $queries).";"; ne fonctionne pas |
|
| 2474 | - foreach ($queries as $q) { |
|
| 2475 | - if (!Sqlite::executer_requete($q, $serveur)) { |
|
| 2476 | - spip_log('SQLite : ALTER TABLE table :' |
|
| 2477 | - . " Erreur a l'execution de la requete : $q", 'sqlite.' . _LOG_ERREUR); |
|
| 2478 | - Sqlite::annuler_transaction($serveur); |
|
| 2479 | - |
|
| 2480 | - return false; |
|
| 2481 | - } |
|
| 2482 | - } |
|
| 2483 | - Sqlite::finir_transaction($serveur); |
|
| 2484 | - } |
|
| 2485 | - |
|
| 2486 | - return true; |
|
| 2346 | + if (is_array($table)) { |
|
| 2347 | + $table_destination = reset($table); |
|
| 2348 | + $table_origine = key($table); |
|
| 2349 | + } else { |
|
| 2350 | + $table_origine = $table_destination = $table; |
|
| 2351 | + } |
|
| 2352 | + // ne prend actuellement qu'un changement |
|
| 2353 | + // mais pourra etre adapte pour changer plus qu'une colonne a la fois |
|
| 2354 | + if (is_array($colonne)) { |
|
| 2355 | + $colonne_destination = reset($colonne); |
|
| 2356 | + $colonne_origine = key($colonne); |
|
| 2357 | + } else { |
|
| 2358 | + $colonne_origine = $colonne_destination = $colonne; |
|
| 2359 | + } |
|
| 2360 | + if (!isset($opt['field'])) { |
|
| 2361 | + $opt['field'] = []; |
|
| 2362 | + } |
|
| 2363 | + if (!isset($opt['key'])) { |
|
| 2364 | + $opt['key'] = []; |
|
| 2365 | + } |
|
| 2366 | + |
|
| 2367 | + // si les noms de tables sont differents, pas besoin de table temporaire |
|
| 2368 | + // on prendra directement le nom de la future table |
|
| 2369 | + $meme_table = ($table_origine == $table_destination); |
|
| 2370 | + |
|
| 2371 | + $def_origine = sql_showtable($table_origine, false, $serveur); |
|
| 2372 | + if (!$def_origine or !isset($def_origine['field'])) { |
|
| 2373 | + spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2374 | + |
|
| 2375 | + return false; |
|
| 2376 | + } |
|
| 2377 | + |
|
| 2378 | + |
|
| 2379 | + $table_tmp = $table_origine . '_tmp'; |
|
| 2380 | + |
|
| 2381 | + // 1) creer une table temporaire avec les modifications |
|
| 2382 | + // - DROP : suppression de la colonne |
|
| 2383 | + // - CHANGE : modification de la colonne |
|
| 2384 | + // (foreach pour conserver l'ordre des champs) |
|
| 2385 | + |
|
| 2386 | + // field |
|
| 2387 | + $fields = []; |
|
| 2388 | + // pour le INSERT INTO plus loin |
|
| 2389 | + // stocker la correspondance nouvelles->anciennes colonnes |
|
| 2390 | + $fields_correspondances = []; |
|
| 2391 | + foreach ($def_origine['field'] as $c => $d) { |
|
| 2392 | + if ($colonne_origine && ($c == $colonne_origine)) { |
|
| 2393 | + // si pas DROP |
|
| 2394 | + if ($colonne_destination) { |
|
| 2395 | + $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2396 | + $fields_correspondances[$colonne_destination] = $c; |
|
| 2397 | + } |
|
| 2398 | + } else { |
|
| 2399 | + $fields[$c] = $d; |
|
| 2400 | + $fields_correspondances[$c] = $c; |
|
| 2401 | + } |
|
| 2402 | + } |
|
| 2403 | + // cas de ADD sqlite2 (ajout du champ en fin de table): |
|
| 2404 | + if (!$colonne_origine && $colonne_destination) { |
|
| 2405 | + $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2406 | + } |
|
| 2407 | + |
|
| 2408 | + // key... |
|
| 2409 | + $keys = []; |
|
| 2410 | + foreach ($def_origine['key'] as $c => $d) { |
|
| 2411 | + $c = str_replace($colonne_origine, $colonne_destination, $c); |
|
| 2412 | + $d = str_replace($colonne_origine, $colonne_destination, $d); |
|
| 2413 | + // seulement si on ne supprime pas la colonne ! |
|
| 2414 | + if ($d) { |
|
| 2415 | + $keys[$c] = $d; |
|
| 2416 | + } |
|
| 2417 | + } |
|
| 2418 | + |
|
| 2419 | + // autres keys, on merge |
|
| 2420 | + $keys = array_merge($keys, $opt['key']); |
|
| 2421 | + $queries = []; |
|
| 2422 | + |
|
| 2423 | + // copier dans destination (si differente de origine), sinon tmp |
|
| 2424 | + $table_copie = ($meme_table) ? $table_tmp : $table_destination; |
|
| 2425 | + $autoinc = (isset($keys['PRIMARY KEY']) |
|
| 2426 | + and $keys['PRIMARY KEY'] |
|
| 2427 | + and stripos($keys['PRIMARY KEY'], ',') === false |
|
| 2428 | + and stripos($fields[$keys['PRIMARY KEY']], 'default') === false); |
|
| 2429 | + |
|
| 2430 | + if ( |
|
| 2431 | + $q = _sqlite_requete_create( |
|
| 2432 | + $table_copie, |
|
| 2433 | + $fields, |
|
| 2434 | + $keys, |
|
| 2435 | + $autoinc, |
|
| 2436 | + $temporary = false, |
|
| 2437 | + $ifnotexists = true, |
|
| 2438 | + $serveur |
|
| 2439 | + ) |
|
| 2440 | + ) { |
|
| 2441 | + $queries[] = $q; |
|
| 2442 | + } |
|
| 2443 | + |
|
| 2444 | + |
|
| 2445 | + // 2) y copier les champs qui vont bien |
|
| 2446 | + $champs_dest = join(', ', array_keys($fields_correspondances)); |
|
| 2447 | + $champs_ori = join(', ', $fields_correspondances); |
|
| 2448 | + $queries[] = "INSERT INTO $table_copie ($champs_dest) SELECT $champs_ori FROM $table_origine"; |
|
| 2449 | + |
|
| 2450 | + // 3) supprimer la table d'origine |
|
| 2451 | + $queries[] = "DROP TABLE $table_origine"; |
|
| 2452 | + |
|
| 2453 | + // 4) renommer la table temporaire |
|
| 2454 | + // avec le nom de la table destination |
|
| 2455 | + // si necessaire |
|
| 2456 | + if ($meme_table) { |
|
| 2457 | + $queries[] = "ALTER TABLE $table_copie RENAME TO $table_destination"; |
|
| 2458 | + } |
|
| 2459 | + |
|
| 2460 | + // 5) remettre les index ! |
|
| 2461 | + foreach ($keys as $k => $v) { |
|
| 2462 | + if ($k == 'PRIMARY KEY') { |
|
| 2463 | + } else { |
|
| 2464 | + // enlever KEY |
|
| 2465 | + $k = substr($k, 4); |
|
| 2466 | + $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2467 | + } |
|
| 2468 | + } |
|
| 2469 | + |
|
| 2470 | + |
|
| 2471 | + if (count($queries)) { |
|
| 2472 | + Sqlite::demarrer_transaction($serveur); |
|
| 2473 | + // il faut les faire une par une car $query = join('; ', $queries).";"; ne fonctionne pas |
|
| 2474 | + foreach ($queries as $q) { |
|
| 2475 | + if (!Sqlite::executer_requete($q, $serveur)) { |
|
| 2476 | + spip_log('SQLite : ALTER TABLE table :' |
|
| 2477 | + . " Erreur a l'execution de la requete : $q", 'sqlite.' . _LOG_ERREUR); |
|
| 2478 | + Sqlite::annuler_transaction($serveur); |
|
| 2479 | + |
|
| 2480 | + return false; |
|
| 2481 | + } |
|
| 2482 | + } |
|
| 2483 | + Sqlite::finir_transaction($serveur); |
|
| 2484 | + } |
|
| 2485 | + |
|
| 2486 | + return true; |
|
| 2487 | 2487 | } |
| 2488 | 2488 | |
| 2489 | 2489 | |
@@ -2494,61 +2494,61 @@ discard block |
||
| 2494 | 2494 | */ |
| 2495 | 2495 | function _sqlite_ref_fonctions() |
| 2496 | 2496 | { |
| 2497 | - $fonctions = [ |
|
| 2498 | - 'alter' => 'spip_sqlite_alter', |
|
| 2499 | - 'count' => 'spip_sqlite_count', |
|
| 2500 | - 'countsel' => 'spip_sqlite_countsel', |
|
| 2501 | - 'create' => 'spip_sqlite_create', |
|
| 2502 | - 'create_base' => 'spip_sqlite_create_base', |
|
| 2503 | - 'create_view' => 'spip_sqlite_create_view', |
|
| 2504 | - 'date_proche' => 'spip_sqlite_date_proche', |
|
| 2505 | - 'delete' => 'spip_sqlite_delete', |
|
| 2506 | - 'drop_table' => 'spip_sqlite_drop_table', |
|
| 2507 | - 'drop_view' => 'spip_sqlite_drop_view', |
|
| 2508 | - 'errno' => 'spip_sqlite_errno', |
|
| 2509 | - 'error' => 'spip_sqlite_error', |
|
| 2510 | - 'explain' => 'spip_sqlite_explain', |
|
| 2511 | - 'fetch' => 'spip_sqlite_fetch', |
|
| 2512 | - 'seek' => 'spip_sqlite_seek', |
|
| 2513 | - 'free' => 'spip_sqlite_free', |
|
| 2514 | - 'hex' => 'spip_sqlite_hex', |
|
| 2515 | - 'in' => 'spip_sqlite_in', |
|
| 2516 | - 'insert' => 'spip_sqlite_insert', |
|
| 2517 | - 'insertq' => 'spip_sqlite_insertq', |
|
| 2518 | - 'insertq_multi' => 'spip_sqlite_insertq_multi', |
|
| 2519 | - 'listdbs' => 'spip_sqlite_listdbs', |
|
| 2520 | - 'multi' => 'spip_sqlite_multi', |
|
| 2521 | - 'optimize' => 'spip_sqlite_optimize', |
|
| 2522 | - 'query' => 'spip_sqlite_query', |
|
| 2523 | - 'quote' => 'spip_sqlite_quote', |
|
| 2524 | - 'repair' => 'spip_sqlite_repair', |
|
| 2525 | - 'replace' => 'spip_sqlite_replace', |
|
| 2526 | - 'replace_multi' => 'spip_sqlite_replace_multi', |
|
| 2527 | - 'select' => 'spip_sqlite_select', |
|
| 2528 | - 'selectdb' => 'spip_sqlite_selectdb', |
|
| 2529 | - 'set_charset' => 'spip_sqlite_set_charset', |
|
| 2530 | - 'get_charset' => 'spip_sqlite_get_charset', |
|
| 2531 | - 'showbase' => 'spip_sqlite_showbase', |
|
| 2532 | - 'showtable' => 'spip_sqlite_showtable', |
|
| 2533 | - 'table_exists' => 'spip_sqlite_table_exists', |
|
| 2534 | - 'update' => 'spip_sqlite_update', |
|
| 2535 | - 'updateq' => 'spip_sqlite_updateq', |
|
| 2536 | - 'preferer_transaction' => 'spip_sqlite_preferer_transaction', |
|
| 2537 | - 'demarrer_transaction' => 'spip_sqlite_demarrer_transaction', |
|
| 2538 | - 'terminer_transaction' => 'spip_sqlite_terminer_transaction', |
|
| 2539 | - ]; |
|
| 2540 | - |
|
| 2541 | - // association de chaque nom http d'un charset aux couples sqlite |
|
| 2542 | - // SQLite supporte utf-8 et utf-16 uniquement. |
|
| 2543 | - $charsets = [ |
|
| 2544 | - 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'], |
|
| 2545 | - //'utf-16be'=>array('charset'=>'utf16be','collation'=>'UTF-16BE'),// aucune idee de quoi il faut remplir dans es champs la |
|
| 2546 | - //'utf-16le'=>array('charset'=>'utf16le','collation'=>'UTF-16LE') |
|
| 2547 | - ]; |
|
| 2548 | - |
|
| 2549 | - $fonctions['charsets'] = $charsets; |
|
| 2550 | - |
|
| 2551 | - return $fonctions; |
|
| 2497 | + $fonctions = [ |
|
| 2498 | + 'alter' => 'spip_sqlite_alter', |
|
| 2499 | + 'count' => 'spip_sqlite_count', |
|
| 2500 | + 'countsel' => 'spip_sqlite_countsel', |
|
| 2501 | + 'create' => 'spip_sqlite_create', |
|
| 2502 | + 'create_base' => 'spip_sqlite_create_base', |
|
| 2503 | + 'create_view' => 'spip_sqlite_create_view', |
|
| 2504 | + 'date_proche' => 'spip_sqlite_date_proche', |
|
| 2505 | + 'delete' => 'spip_sqlite_delete', |
|
| 2506 | + 'drop_table' => 'spip_sqlite_drop_table', |
|
| 2507 | + 'drop_view' => 'spip_sqlite_drop_view', |
|
| 2508 | + 'errno' => 'spip_sqlite_errno', |
|
| 2509 | + 'error' => 'spip_sqlite_error', |
|
| 2510 | + 'explain' => 'spip_sqlite_explain', |
|
| 2511 | + 'fetch' => 'spip_sqlite_fetch', |
|
| 2512 | + 'seek' => 'spip_sqlite_seek', |
|
| 2513 | + 'free' => 'spip_sqlite_free', |
|
| 2514 | + 'hex' => 'spip_sqlite_hex', |
|
| 2515 | + 'in' => 'spip_sqlite_in', |
|
| 2516 | + 'insert' => 'spip_sqlite_insert', |
|
| 2517 | + 'insertq' => 'spip_sqlite_insertq', |
|
| 2518 | + 'insertq_multi' => 'spip_sqlite_insertq_multi', |
|
| 2519 | + 'listdbs' => 'spip_sqlite_listdbs', |
|
| 2520 | + 'multi' => 'spip_sqlite_multi', |
|
| 2521 | + 'optimize' => 'spip_sqlite_optimize', |
|
| 2522 | + 'query' => 'spip_sqlite_query', |
|
| 2523 | + 'quote' => 'spip_sqlite_quote', |
|
| 2524 | + 'repair' => 'spip_sqlite_repair', |
|
| 2525 | + 'replace' => 'spip_sqlite_replace', |
|
| 2526 | + 'replace_multi' => 'spip_sqlite_replace_multi', |
|
| 2527 | + 'select' => 'spip_sqlite_select', |
|
| 2528 | + 'selectdb' => 'spip_sqlite_selectdb', |
|
| 2529 | + 'set_charset' => 'spip_sqlite_set_charset', |
|
| 2530 | + 'get_charset' => 'spip_sqlite_get_charset', |
|
| 2531 | + 'showbase' => 'spip_sqlite_showbase', |
|
| 2532 | + 'showtable' => 'spip_sqlite_showtable', |
|
| 2533 | + 'table_exists' => 'spip_sqlite_table_exists', |
|
| 2534 | + 'update' => 'spip_sqlite_update', |
|
| 2535 | + 'updateq' => 'spip_sqlite_updateq', |
|
| 2536 | + 'preferer_transaction' => 'spip_sqlite_preferer_transaction', |
|
| 2537 | + 'demarrer_transaction' => 'spip_sqlite_demarrer_transaction', |
|
| 2538 | + 'terminer_transaction' => 'spip_sqlite_terminer_transaction', |
|
| 2539 | + ]; |
|
| 2540 | + |
|
| 2541 | + // association de chaque nom http d'un charset aux couples sqlite |
|
| 2542 | + // SQLite supporte utf-8 et utf-16 uniquement. |
|
| 2543 | + $charsets = [ |
|
| 2544 | + 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'], |
|
| 2545 | + //'utf-16be'=>array('charset'=>'utf16be','collation'=>'UTF-16BE'),// aucune idee de quoi il faut remplir dans es champs la |
|
| 2546 | + //'utf-16le'=>array('charset'=>'utf16le','collation'=>'UTF-16LE') |
|
| 2547 | + ]; |
|
| 2548 | + |
|
| 2549 | + $fonctions['charsets'] = $charsets; |
|
| 2550 | + |
|
| 2551 | + return $fonctions; |
|
| 2552 | 2552 | } |
| 2553 | 2553 | |
| 2554 | 2554 | |
@@ -2561,56 +2561,56 @@ discard block |
||
| 2561 | 2561 | */ |
| 2562 | 2562 | function _sqlite_remplacements_definitions_table($query, $autoinc = false) |
| 2563 | 2563 | { |
| 2564 | - // quelques remplacements |
|
| 2565 | - $num = '(\s*\([0-9]*\))?'; |
|
| 2566 | - $enum = '(\s*\([^\)]*\))?'; |
|
| 2567 | - |
|
| 2568 | - $remplace = [ |
|
| 2569 | - '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2570 | - '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
|
| 2571 | - '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
|
| 2572 | - '/auto_increment/is' => '', |
|
| 2573 | - '/current_timestamp\(\)/is' => 'CURRENT_TIMESTAMP', // Fix export depuis mariaDB #4374 |
|
| 2574 | - '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 2575 | - '/character set \w+/is' => '', |
|
| 2576 | - '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2577 | - '/(text\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2578 | - '/((char|varchar)' . $num . '\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2579 | - '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
|
| 2580 | - '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
|
| 2581 | - ]; |
|
| 2582 | - |
|
| 2583 | - // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
|
| 2584 | - $remplace_autocinc = [ |
|
| 2585 | - '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2586 | - ]; |
|
| 2587 | - // pour les int non autoincrement, il faut un DEFAULT |
|
| 2588 | - $remplace_nonautocinc = [ |
|
| 2589 | - '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2590 | - ]; |
|
| 2591 | - |
|
| 2592 | - if (is_string($query)) { |
|
| 2593 | - $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 2594 | - if ($autoinc or preg_match(',AUTO_INCREMENT,is', $query)) { |
|
| 2595 | - $query = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query); |
|
| 2596 | - } else { |
|
| 2597 | - $query = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query); |
|
| 2598 | - $query = _sqlite_collate_ci($query); |
|
| 2599 | - } |
|
| 2600 | - } elseif (is_array($query)) { |
|
| 2601 | - foreach ($query as $k => $q) { |
|
| 2602 | - $ai = ($autoinc ? $k == $autoinc : preg_match(',AUTO_INCREMENT,is', $q)); |
|
| 2603 | - $query[$k] = preg_replace(array_keys($remplace), $remplace, $query[$k]); |
|
| 2604 | - if ($ai) { |
|
| 2605 | - $query[$k] = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query[$k]); |
|
| 2606 | - } else { |
|
| 2607 | - $query[$k] = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query[$k]); |
|
| 2608 | - $query[$k] = _sqlite_collate_ci($query[$k]); |
|
| 2609 | - } |
|
| 2610 | - } |
|
| 2611 | - } |
|
| 2612 | - |
|
| 2613 | - return $query; |
|
| 2564 | + // quelques remplacements |
|
| 2565 | + $num = '(\s*\([0-9]*\))?'; |
|
| 2566 | + $enum = '(\s*\([^\)]*\))?'; |
|
| 2567 | + |
|
| 2568 | + $remplace = [ |
|
| 2569 | + '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2570 | + '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
|
| 2571 | + '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
|
| 2572 | + '/auto_increment/is' => '', |
|
| 2573 | + '/current_timestamp\(\)/is' => 'CURRENT_TIMESTAMP', // Fix export depuis mariaDB #4374 |
|
| 2574 | + '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 2575 | + '/character set \w+/is' => '', |
|
| 2576 | + '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2577 | + '/(text\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2578 | + '/((char|varchar)' . $num . '\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2579 | + '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
|
| 2580 | + '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
|
| 2581 | + ]; |
|
| 2582 | + |
|
| 2583 | + // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
|
| 2584 | + $remplace_autocinc = [ |
|
| 2585 | + '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2586 | + ]; |
|
| 2587 | + // pour les int non autoincrement, il faut un DEFAULT |
|
| 2588 | + $remplace_nonautocinc = [ |
|
| 2589 | + '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2590 | + ]; |
|
| 2591 | + |
|
| 2592 | + if (is_string($query)) { |
|
| 2593 | + $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 2594 | + if ($autoinc or preg_match(',AUTO_INCREMENT,is', $query)) { |
|
| 2595 | + $query = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query); |
|
| 2596 | + } else { |
|
| 2597 | + $query = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query); |
|
| 2598 | + $query = _sqlite_collate_ci($query); |
|
| 2599 | + } |
|
| 2600 | + } elseif (is_array($query)) { |
|
| 2601 | + foreach ($query as $k => $q) { |
|
| 2602 | + $ai = ($autoinc ? $k == $autoinc : preg_match(',AUTO_INCREMENT,is', $q)); |
|
| 2603 | + $query[$k] = preg_replace(array_keys($remplace), $remplace, $query[$k]); |
|
| 2604 | + if ($ai) { |
|
| 2605 | + $query[$k] = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query[$k]); |
|
| 2606 | + } else { |
|
| 2607 | + $query[$k] = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query[$k]); |
|
| 2608 | + $query[$k] = _sqlite_collate_ci($query[$k]); |
|
| 2609 | + } |
|
| 2610 | + } |
|
| 2611 | + } |
|
| 2612 | + |
|
| 2613 | + return $query; |
|
| 2614 | 2614 | } |
| 2615 | 2615 | |
| 2616 | 2616 | /** |
@@ -2622,17 +2622,17 @@ discard block |
||
| 2622 | 2622 | */ |
| 2623 | 2623 | function _sqlite_collate_ci($champ) |
| 2624 | 2624 | { |
| 2625 | - if (stripos($champ, 'COLLATE') !== false) { |
|
| 2626 | - return $champ; |
|
| 2627 | - } |
|
| 2628 | - if (stripos($champ, 'BINARY') !== false) { |
|
| 2629 | - return str_ireplace('BINARY', 'COLLATE BINARY', $champ); |
|
| 2630 | - } |
|
| 2631 | - if (preg_match(',^(char|varchar|(long|small|medium|tiny)?text),i', $champ)) { |
|
| 2632 | - return $champ . ' COLLATE NOCASE'; |
|
| 2633 | - } |
|
| 2634 | - |
|
| 2635 | - return $champ; |
|
| 2625 | + if (stripos($champ, 'COLLATE') !== false) { |
|
| 2626 | + return $champ; |
|
| 2627 | + } |
|
| 2628 | + if (stripos($champ, 'BINARY') !== false) { |
|
| 2629 | + return str_ireplace('BINARY', 'COLLATE BINARY', $champ); |
|
| 2630 | + } |
|
| 2631 | + if (preg_match(',^(char|varchar|(long|small|medium|tiny)?text),i', $champ)) { |
|
| 2632 | + return $champ . ' COLLATE NOCASE'; |
|
| 2633 | + } |
|
| 2634 | + |
|
| 2635 | + return $champ; |
|
| 2636 | 2636 | } |
| 2637 | 2637 | |
| 2638 | 2638 | |
@@ -2651,84 +2651,84 @@ discard block |
||
| 2651 | 2651 | * @return bool|string |
| 2652 | 2652 | */ |
| 2653 | 2653 | function _sqlite_requete_create( |
| 2654 | - $nom, |
|
| 2655 | - $champs, |
|
| 2656 | - $cles, |
|
| 2657 | - $autoinc = false, |
|
| 2658 | - $temporary = false, |
|
| 2659 | - $_ifnotexists = true, |
|
| 2660 | - $serveur = '', |
|
| 2661 | - $requeter = true |
|
| 2654 | + $nom, |
|
| 2655 | + $champs, |
|
| 2656 | + $cles, |
|
| 2657 | + $autoinc = false, |
|
| 2658 | + $temporary = false, |
|
| 2659 | + $_ifnotexists = true, |
|
| 2660 | + $serveur = '', |
|
| 2661 | + $requeter = true |
|
| 2662 | 2662 | ) { |
| 2663 | - $query = $keys = $s = $p = ''; |
|
| 2664 | - |
|
| 2665 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 2666 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 2667 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 2668 | - return; |
|
| 2669 | - } |
|
| 2670 | - |
|
| 2671 | - // sqlite ne gere pas KEY tout court dans une requete CREATE TABLE |
|
| 2672 | - // il faut passer par des create index |
|
| 2673 | - // Il gere par contre primary key ! |
|
| 2674 | - // Soit la PK est definie dans les cles, soit dans un champs |
|
| 2675 | - // soit faussement dans les 2 (et dans ce cas, il faut l’enlever à un des 2 endroits !) |
|
| 2676 | - $pk = 'PRIMARY KEY'; |
|
| 2677 | - // le champ de cle primaire |
|
| 2678 | - $champ_pk = !empty($cles[$pk]) ? $cles[$pk] : ''; |
|
| 2679 | - |
|
| 2680 | - foreach ($champs as $k => $v) { |
|
| 2681 | - if (false !== stripos($v, $pk)) { |
|
| 2682 | - $champ_pk = $k; |
|
| 2683 | - // on n'en a plus besoin dans field, vu que defini dans key |
|
| 2684 | - $champs[$k] = preg_replace("/$pk/is", '', $champs[$k]); |
|
| 2685 | - break; |
|
| 2686 | - } |
|
| 2687 | - } |
|
| 2688 | - |
|
| 2689 | - if ($champ_pk) { |
|
| 2690 | - $keys = "\n\t\t$pk ($champ_pk)"; |
|
| 2691 | - } |
|
| 2692 | - // Pas de DEFAULT 0 sur les cles primaires en auto-increment |
|
| 2693 | - if ( |
|
| 2694 | - isset($champs[$champ_pk]) |
|
| 2695 | - and stripos($champs[$champ_pk], 'default 0') !== false |
|
| 2696 | - ) { |
|
| 2697 | - $champs[$champ_pk] = trim(str_ireplace('default 0', '', $champs[$champ_pk])); |
|
| 2698 | - } |
|
| 2699 | - |
|
| 2700 | - $champs = _sqlite_remplacements_definitions_table($champs, $autoinc ? $champ_pk : false); |
|
| 2701 | - foreach ($champs as $k => $v) { |
|
| 2702 | - $query .= "$s\n\t\t$k $v"; |
|
| 2703 | - $s = ','; |
|
| 2704 | - } |
|
| 2705 | - |
|
| 2706 | - $ifnotexists = ''; |
|
| 2707 | - if ($_ifnotexists) { |
|
| 2708 | - $version = spip_sqlite_fetch( |
|
| 2709 | - spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), |
|
| 2710 | - '', |
|
| 2711 | - $serveur |
|
| 2712 | - ); |
|
| 2713 | - if (!function_exists('spip_version_compare')) { |
|
| 2714 | - include_spip('plugins/installer'); |
|
| 2715 | - } |
|
| 2716 | - |
|
| 2717 | - if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 2718 | - $ifnotexists = ' IF NOT EXISTS'; |
|
| 2719 | - } else { |
|
| 2720 | - /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 2721 | - $a = spip_sqlite_showtable($nom, $serveur); |
|
| 2722 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 2723 | - return true; |
|
| 2724 | - } |
|
| 2725 | - } |
|
| 2726 | - } |
|
| 2727 | - |
|
| 2728 | - $temporary = $temporary ? ' TEMPORARY' : ''; |
|
| 2729 | - $q = "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2730 | - |
|
| 2731 | - return $q; |
|
| 2663 | + $query = $keys = $s = $p = ''; |
|
| 2664 | + |
|
| 2665 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 2666 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 2667 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 2668 | + return; |
|
| 2669 | + } |
|
| 2670 | + |
|
| 2671 | + // sqlite ne gere pas KEY tout court dans une requete CREATE TABLE |
|
| 2672 | + // il faut passer par des create index |
|
| 2673 | + // Il gere par contre primary key ! |
|
| 2674 | + // Soit la PK est definie dans les cles, soit dans un champs |
|
| 2675 | + // soit faussement dans les 2 (et dans ce cas, il faut l’enlever à un des 2 endroits !) |
|
| 2676 | + $pk = 'PRIMARY KEY'; |
|
| 2677 | + // le champ de cle primaire |
|
| 2678 | + $champ_pk = !empty($cles[$pk]) ? $cles[$pk] : ''; |
|
| 2679 | + |
|
| 2680 | + foreach ($champs as $k => $v) { |
|
| 2681 | + if (false !== stripos($v, $pk)) { |
|
| 2682 | + $champ_pk = $k; |
|
| 2683 | + // on n'en a plus besoin dans field, vu que defini dans key |
|
| 2684 | + $champs[$k] = preg_replace("/$pk/is", '', $champs[$k]); |
|
| 2685 | + break; |
|
| 2686 | + } |
|
| 2687 | + } |
|
| 2688 | + |
|
| 2689 | + if ($champ_pk) { |
|
| 2690 | + $keys = "\n\t\t$pk ($champ_pk)"; |
|
| 2691 | + } |
|
| 2692 | + // Pas de DEFAULT 0 sur les cles primaires en auto-increment |
|
| 2693 | + if ( |
|
| 2694 | + isset($champs[$champ_pk]) |
|
| 2695 | + and stripos($champs[$champ_pk], 'default 0') !== false |
|
| 2696 | + ) { |
|
| 2697 | + $champs[$champ_pk] = trim(str_ireplace('default 0', '', $champs[$champ_pk])); |
|
| 2698 | + } |
|
| 2699 | + |
|
| 2700 | + $champs = _sqlite_remplacements_definitions_table($champs, $autoinc ? $champ_pk : false); |
|
| 2701 | + foreach ($champs as $k => $v) { |
|
| 2702 | + $query .= "$s\n\t\t$k $v"; |
|
| 2703 | + $s = ','; |
|
| 2704 | + } |
|
| 2705 | + |
|
| 2706 | + $ifnotexists = ''; |
|
| 2707 | + if ($_ifnotexists) { |
|
| 2708 | + $version = spip_sqlite_fetch( |
|
| 2709 | + spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), |
|
| 2710 | + '', |
|
| 2711 | + $serveur |
|
| 2712 | + ); |
|
| 2713 | + if (!function_exists('spip_version_compare')) { |
|
| 2714 | + include_spip('plugins/installer'); |
|
| 2715 | + } |
|
| 2716 | + |
|
| 2717 | + if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 2718 | + $ifnotexists = ' IF NOT EXISTS'; |
|
| 2719 | + } else { |
|
| 2720 | + /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 2721 | + $a = spip_sqlite_showtable($nom, $serveur); |
|
| 2722 | + if (isset($a['key']['KEY ' . $nom])) { |
|
| 2723 | + return true; |
|
| 2724 | + } |
|
| 2725 | + } |
|
| 2726 | + } |
|
| 2727 | + |
|
| 2728 | + $temporary = $temporary ? ' TEMPORARY' : ''; |
|
| 2729 | + $q = "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2730 | + |
|
| 2731 | + return $q; |
|
| 2732 | 2732 | } |
| 2733 | 2733 | |
| 2734 | 2734 | |
@@ -2748,40 +2748,40 @@ discard block |
||
| 2748 | 2748 | */ |
| 2749 | 2749 | function _sqlite_ajouter_champs_timestamp($table, $couples, $desc = '', $serveur = '') |
| 2750 | 2750 | { |
| 2751 | - static $tables = []; |
|
| 2752 | - |
|
| 2753 | - if (!isset($tables[$table])) { |
|
| 2754 | - if (!$desc) { |
|
| 2755 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2756 | - $desc = $trouver_table($table, $serveur); |
|
| 2757 | - // si pas de description, on ne fait rien, ou on die() ? |
|
| 2758 | - if (!$desc) { |
|
| 2759 | - return $couples; |
|
| 2760 | - } |
|
| 2761 | - } |
|
| 2762 | - |
|
| 2763 | - // recherche des champs avec simplement 'TIMESTAMP' |
|
| 2764 | - // cependant, il faudra peut etre etendre |
|
| 2765 | - // avec la gestion de DEFAULT et ON UPDATE |
|
| 2766 | - // mais ceux-ci ne sont pas utilises dans le core |
|
| 2767 | - $tables[$table] = ['valeur' => [], 'cite' => [], 'desc' => []]; |
|
| 2768 | - |
|
| 2769 | - $now = _sqlite_func_now(true); |
|
| 2770 | - foreach ($desc['field'] as $k => $v) { |
|
| 2771 | - if (strpos(strtolower(ltrim($v)), 'timestamp') === 0) { |
|
| 2772 | - $tables[$table]['desc'][$k] = $v; |
|
| 2773 | - $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2774 | - } |
|
| 2775 | - } |
|
| 2776 | - } else { |
|
| 2777 | - $now = _sqlite_func_now(true); |
|
| 2778 | - foreach (array_keys($tables[$table]['desc']) as $k) { |
|
| 2779 | - $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2780 | - } |
|
| 2781 | - } |
|
| 2782 | - |
|
| 2783 | - // ajout des champs type 'timestamp' absents |
|
| 2784 | - return array_merge($tables[$table]['valeur'], $couples); |
|
| 2751 | + static $tables = []; |
|
| 2752 | + |
|
| 2753 | + if (!isset($tables[$table])) { |
|
| 2754 | + if (!$desc) { |
|
| 2755 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2756 | + $desc = $trouver_table($table, $serveur); |
|
| 2757 | + // si pas de description, on ne fait rien, ou on die() ? |
|
| 2758 | + if (!$desc) { |
|
| 2759 | + return $couples; |
|
| 2760 | + } |
|
| 2761 | + } |
|
| 2762 | + |
|
| 2763 | + // recherche des champs avec simplement 'TIMESTAMP' |
|
| 2764 | + // cependant, il faudra peut etre etendre |
|
| 2765 | + // avec la gestion de DEFAULT et ON UPDATE |
|
| 2766 | + // mais ceux-ci ne sont pas utilises dans le core |
|
| 2767 | + $tables[$table] = ['valeur' => [], 'cite' => [], 'desc' => []]; |
|
| 2768 | + |
|
| 2769 | + $now = _sqlite_func_now(true); |
|
| 2770 | + foreach ($desc['field'] as $k => $v) { |
|
| 2771 | + if (strpos(strtolower(ltrim($v)), 'timestamp') === 0) { |
|
| 2772 | + $tables[$table]['desc'][$k] = $v; |
|
| 2773 | + $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2774 | + } |
|
| 2775 | + } |
|
| 2776 | + } else { |
|
| 2777 | + $now = _sqlite_func_now(true); |
|
| 2778 | + foreach (array_keys($tables[$table]['desc']) as $k) { |
|
| 2779 | + $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2780 | + } |
|
| 2781 | + } |
|
| 2782 | + |
|
| 2783 | + // ajout des champs type 'timestamp' absents |
|
| 2784 | + return array_merge($tables[$table]['valeur'], $couples); |
|
| 2785 | 2785 | } |
| 2786 | 2786 | |
| 2787 | 2787 | |
@@ -2793,5 +2793,5 @@ discard block |
||
| 2793 | 2793 | */ |
| 2794 | 2794 | function spip_versions_sqlite() |
| 2795 | 2795 | { |
| 2796 | - return _sqlite_charger_version(); |
|
| 2796 | + return _sqlite_charger_version(); |
|
| 2797 | 2797 | } |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | */ |
| 23 | 23 | |
| 24 | 24 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 25 | - return; |
|
| 25 | + return; |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | /** |
@@ -43,90 +43,90 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | function _sqlite_init_functions(&$sqlite) { |
| 45 | 45 | |
| 46 | - if (!$sqlite) { |
|
| 47 | - return false; |
|
| 48 | - } |
|
| 46 | + if (!$sqlite) { |
|
| 47 | + return false; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | 50 | |
| 51 | - $fonctions = [ |
|
| 52 | - // A |
|
| 53 | - 'ACOS' => ['acos', 1], |
|
| 54 | - 'ASIN' => ['asin', 1], |
|
| 55 | - 'ATAN' => ['atan', 1], // mysql accepte 2 params comme atan2… hum ? |
|
| 56 | - 'ATAN2' => ['atan2', 2], |
|
| 51 | + $fonctions = [ |
|
| 52 | + // A |
|
| 53 | + 'ACOS' => ['acos', 1], |
|
| 54 | + 'ASIN' => ['asin', 1], |
|
| 55 | + 'ATAN' => ['atan', 1], // mysql accepte 2 params comme atan2… hum ? |
|
| 56 | + 'ATAN2' => ['atan2', 2], |
|
| 57 | 57 | |
| 58 | - // C |
|
| 59 | - 'CEIL' => ['_sqlite_func_ceil', 1], |
|
| 60 | - 'CONCAT' => ['_sqlite_func_concat', -1], |
|
| 61 | - 'COS' => ['cos', 1], |
|
| 58 | + // C |
|
| 59 | + 'CEIL' => ['_sqlite_func_ceil', 1], |
|
| 60 | + 'CONCAT' => ['_sqlite_func_concat', -1], |
|
| 61 | + 'COS' => ['cos', 1], |
|
| 62 | 62 | |
| 63 | - // D |
|
| 64 | - 'DATE_FORMAT' => ['_sqlite_func_date_format', 2], // équivalent a strftime avec args inversés |
|
| 65 | - 'DAYOFMONTH' => ['_sqlite_func_dayofmonth', 1], |
|
| 66 | - 'DEGREES' => ['rad2deg', 1], |
|
| 63 | + // D |
|
| 64 | + 'DATE_FORMAT' => ['_sqlite_func_date_format', 2], // équivalent a strftime avec args inversés |
|
| 65 | + 'DAYOFMONTH' => ['_sqlite_func_dayofmonth', 1], |
|
| 66 | + 'DEGREES' => ['rad2deg', 1], |
|
| 67 | 67 | |
| 68 | - // E |
|
| 69 | - 'EXTRAIRE_MULTI' => ['_sqlite_func_extraire_multi', 2], // specifique a SPIP/sql_multi() |
|
| 70 | - 'EXP' => ['exp', 1], |
|
| 68 | + // E |
|
| 69 | + 'EXTRAIRE_MULTI' => ['_sqlite_func_extraire_multi', 2], // specifique a SPIP/sql_multi() |
|
| 70 | + 'EXP' => ['exp', 1], |
|
| 71 | 71 | |
| 72 | - // F |
|
| 73 | - 'FIND_IN_SET' => ['_sqlite_func_find_in_set', 2], |
|
| 74 | - 'FLOOR' => ['_sqlite_func_floor', 1], |
|
| 72 | + // F |
|
| 73 | + 'FIND_IN_SET' => ['_sqlite_func_find_in_set', 2], |
|
| 74 | + 'FLOOR' => ['_sqlite_func_floor', 1], |
|
| 75 | 75 | |
| 76 | - // G |
|
| 77 | - 'GREATEST' => ['_sqlite_func_greatest', -1], |
|
| 76 | + // G |
|
| 77 | + 'GREATEST' => ['_sqlite_func_greatest', -1], |
|
| 78 | 78 | |
| 79 | - // I |
|
| 80 | - 'IF' => ['_sqlite_func_if', 3], |
|
| 81 | - 'INSERT' => ['_sqlite_func_insert', 4], |
|
| 82 | - 'INSTR' => ['_sqlite_func_instr', 2], |
|
| 79 | + // I |
|
| 80 | + 'IF' => ['_sqlite_func_if', 3], |
|
| 81 | + 'INSERT' => ['_sqlite_func_insert', 4], |
|
| 82 | + 'INSTR' => ['_sqlite_func_instr', 2], |
|
| 83 | 83 | |
| 84 | - // L |
|
| 85 | - 'LEAST' => ['_sqlite_func_least', -1], |
|
| 86 | - '_LEFT' => ['_sqlite_func_left', 2], |
|
| 84 | + // L |
|
| 85 | + 'LEAST' => ['_sqlite_func_least', -1], |
|
| 86 | + '_LEFT' => ['_sqlite_func_left', 2], |
|
| 87 | 87 | |
| 88 | - // N |
|
| 89 | - 'NOW' => ['_sqlite_func_now', 0], |
|
| 88 | + // N |
|
| 89 | + 'NOW' => ['_sqlite_func_now', 0], |
|
| 90 | 90 | |
| 91 | - // M |
|
| 92 | - 'MD5' => ['md5', 1], |
|
| 93 | - 'MONTH' => ['_sqlite_func_month', 1], |
|
| 91 | + // M |
|
| 92 | + 'MD5' => ['md5', 1], |
|
| 93 | + 'MONTH' => ['_sqlite_func_month', 1], |
|
| 94 | 94 | |
| 95 | - // P |
|
| 96 | - 'PREG_REPLACE' => ['_sqlite_func_preg_replace', 3], |
|
| 95 | + // P |
|
| 96 | + 'PREG_REPLACE' => ['_sqlite_func_preg_replace', 3], |
|
| 97 | 97 | |
| 98 | - // R |
|
| 99 | - 'RADIANS' => ['deg2rad', 1], |
|
| 100 | - 'RAND' => ['_sqlite_func_rand', 0], // sinon random() v2.4 |
|
| 101 | - 'REGEXP' => ['_sqlite_func_regexp_match', 2], // critere REGEXP supporte a partir de v3.3.2 |
|
| 102 | - 'RIGHT' => ['_sqlite_func_right', 2], |
|
| 98 | + // R |
|
| 99 | + 'RADIANS' => ['deg2rad', 1], |
|
| 100 | + 'RAND' => ['_sqlite_func_rand', 0], // sinon random() v2.4 |
|
| 101 | + 'REGEXP' => ['_sqlite_func_regexp_match', 2], // critere REGEXP supporte a partir de v3.3.2 |
|
| 102 | + 'RIGHT' => ['_sqlite_func_right', 2], |
|
| 103 | 103 | |
| 104 | - // S |
|
| 105 | - 'SETTYPE' => ['settype', 2], // CAST present en v3.2.3 |
|
| 106 | - 'SIN' => ['sin', 1], |
|
| 107 | - 'SQRT' => ['sqrt', 1], |
|
| 108 | - 'SUBSTRING' => ['_sqlite_func_substring' /*, 3*/], // peut etre appelee avec 2 ou 3 arguments, index base 1 et non 0 |
|
| 104 | + // S |
|
| 105 | + 'SETTYPE' => ['settype', 2], // CAST present en v3.2.3 |
|
| 106 | + 'SIN' => ['sin', 1], |
|
| 107 | + 'SQRT' => ['sqrt', 1], |
|
| 108 | + 'SUBSTRING' => ['_sqlite_func_substring' /*, 3*/], // peut etre appelee avec 2 ou 3 arguments, index base 1 et non 0 |
|
| 109 | 109 | |
| 110 | - // T |
|
| 111 | - 'TAN' => ['tan', 1], |
|
| 112 | - 'TIMESTAMPDIFF' => ['_sqlite_timestampdiff' /*, 3*/], |
|
| 113 | - 'TO_DAYS' => ['_sqlite_func_to_days', 1], |
|
| 110 | + // T |
|
| 111 | + 'TAN' => ['tan', 1], |
|
| 112 | + 'TIMESTAMPDIFF' => ['_sqlite_timestampdiff' /*, 3*/], |
|
| 113 | + 'TO_DAYS' => ['_sqlite_func_to_days', 1], |
|
| 114 | 114 | |
| 115 | - // U |
|
| 116 | - 'UNIX_TIMESTAMP' => ['_sqlite_func_unix_timestamp', 1], |
|
| 115 | + // U |
|
| 116 | + 'UNIX_TIMESTAMP' => ['_sqlite_func_unix_timestamp', 1], |
|
| 117 | 117 | |
| 118 | - // V |
|
| 119 | - 'VIDE' => ['_sqlite_func_vide', 0], // du vide pour SELECT 0 as x ... ORDER BY x -> ORDER BY vide() |
|
| 118 | + // V |
|
| 119 | + 'VIDE' => ['_sqlite_func_vide', 0], // du vide pour SELECT 0 as x ... ORDER BY x -> ORDER BY vide() |
|
| 120 | 120 | |
| 121 | - // Y |
|
| 122 | - 'YEAR' => ['_sqlite_func_year', 1] |
|
| 123 | - ]; |
|
| 121 | + // Y |
|
| 122 | + 'YEAR' => ['_sqlite_func_year', 1] |
|
| 123 | + ]; |
|
| 124 | 124 | |
| 125 | - foreach ($fonctions as $f => $r) { |
|
| 126 | - _sqlite_add_function($sqlite, $f, $r); |
|
| 127 | - } |
|
| 125 | + foreach ($fonctions as $f => $r) { |
|
| 126 | + _sqlite_add_function($sqlite, $f, $r); |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - #spip_log('functions sqlite chargees ','sqlite.'._LOG_DEBUG); |
|
| 129 | + #spip_log('functions sqlite chargees ','sqlite.'._LOG_DEBUG); |
|
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | |
@@ -147,9 +147,9 @@ discard block |
||
| 147 | 147 | * |
| 148 | 148 | **/ |
| 149 | 149 | function _sqlite_add_function(&$sqlite, &$f, &$r) { |
| 150 | - isset($r[1]) |
|
| 151 | - ? $sqlite->sqliteCreateFunction($f, $r[0], $r[1]) |
|
| 152 | - : $sqlite->sqliteCreateFunction($f, $r[0]); |
|
| 150 | + isset($r[1]) |
|
| 151 | + ? $sqlite->sqliteCreateFunction($f, $r[0], $r[1]) |
|
| 152 | + : $sqlite->sqliteCreateFunction($f, $r[0]); |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | /** |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | * @return int |
| 160 | 160 | */ |
| 161 | 161 | function _sqlite_func_ceil($a) { |
| 162 | - return ceil($a); |
|
| 162 | + return ceil($a); |
|
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | /** |
@@ -169,7 +169,7 @@ discard block |
||
| 169 | 169 | * @return string |
| 170 | 170 | */ |
| 171 | 171 | function _sqlite_func_concat(...$args) { |
| 172 | - return join('', $args); |
|
| 172 | + return join('', $args); |
|
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | * @return string |
| 183 | 183 | */ |
| 184 | 184 | function _sqlite_func_dayofmonth($d) { |
| 185 | - return _sqlite_func_date('d', $d); |
|
| 185 | + return _sqlite_func_date('d', $d); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | |
@@ -194,15 +194,15 @@ discard block |
||
| 194 | 194 | * @return int |
| 195 | 195 | */ |
| 196 | 196 | function _sqlite_func_find_in_set($num, $set) { |
| 197 | - $rank = 0; |
|
| 198 | - foreach (explode(',', $set) as $v) { |
|
| 199 | - if ($v == $num) { |
|
| 200 | - return (++$rank); |
|
| 201 | - } |
|
| 202 | - $rank++; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - return 0; |
|
| 197 | + $rank = 0; |
|
| 198 | + foreach (explode(',', $set) as $v) { |
|
| 199 | + if ($v == $num) { |
|
| 200 | + return (++$rank); |
|
| 201 | + } |
|
| 202 | + $rank++; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + return 0; |
|
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | /** |
@@ -212,7 +212,7 @@ discard block |
||
| 212 | 212 | * @return int |
| 213 | 213 | */ |
| 214 | 214 | function _sqlite_func_floor($a) { |
| 215 | - return floor($a); |
|
| 215 | + return floor($a); |
|
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | |
@@ -225,7 +225,7 @@ discard block |
||
| 225 | 225 | * @return mixed |
| 226 | 226 | */ |
| 227 | 227 | function _sqlite_func_if($bool, $oui, $non) { |
| 228 | - return ($bool) ? $oui : $non; |
|
| 228 | + return ($bool) ? $oui : $non; |
|
| 229 | 229 | } |
| 230 | 230 | |
| 231 | 231 | |
@@ -242,10 +242,10 @@ discard block |
||
| 242 | 242 | * @return string |
| 243 | 243 | */ |
| 244 | 244 | function _sqlite_func_insert($s, $index, $longueur, $chaine) { |
| 245 | - return |
|
| 246 | - substr($s, 0, $index) |
|
| 247 | - . $chaine |
|
| 248 | - . substr(substr($s, $index), $longueur); |
|
| 245 | + return |
|
| 246 | + substr($s, 0, $index) |
|
| 247 | + . $chaine |
|
| 248 | + . substr(substr($s, $index), $longueur); |
|
| 249 | 249 | } |
| 250 | 250 | |
| 251 | 251 | |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | * @return int |
| 258 | 258 | */ |
| 259 | 259 | function _sqlite_func_instr($s, $search) { |
| 260 | - return strpos($s, $search); |
|
| 260 | + return strpos($s, $search); |
|
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | |
@@ -268,7 +268,7 @@ discard block |
||
| 268 | 268 | * @return int |
| 269 | 269 | */ |
| 270 | 270 | function _sqlite_func_least(...$args) { |
| 271 | - return min($args); |
|
| 271 | + return min($args); |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | |
@@ -279,7 +279,7 @@ discard block |
||
| 279 | 279 | * @return int |
| 280 | 280 | */ |
| 281 | 281 | function _sqlite_func_greatest(...$args) { |
| 282 | - return max($args); |
|
| 282 | + return max($args); |
|
| 283 | 283 | } |
| 284 | 284 | |
| 285 | 285 | |
@@ -291,7 +291,7 @@ discard block |
||
| 291 | 291 | * @return string |
| 292 | 292 | */ |
| 293 | 293 | function _sqlite_func_left($s, $lenght) { |
| 294 | - return substr($s, $lenght); |
|
| 294 | + return substr($s, $lenght); |
|
| 295 | 295 | } |
| 296 | 296 | |
| 297 | 297 | /** |
@@ -301,13 +301,13 @@ discard block |
||
| 301 | 301 | * @return string |
| 302 | 302 | */ |
| 303 | 303 | function _sqlite_func_now($force_refresh = false) { |
| 304 | - static $now = null; |
|
| 305 | - if (is_null($now) or $force_refresh) { |
|
| 306 | - $now = date('Y-m-d H:i:s'); |
|
| 307 | - } |
|
| 304 | + static $now = null; |
|
| 305 | + if (is_null($now) or $force_refresh) { |
|
| 306 | + $now = date('Y-m-d H:i:s'); |
|
| 307 | + } |
|
| 308 | 308 | |
| 309 | - #spip_log("Passage avec NOW : $now | ".time(),'sqlite.'._LOG_DEBUG); |
|
| 310 | - return $now; |
|
| 309 | + #spip_log("Passage avec NOW : $now | ".time(),'sqlite.'._LOG_DEBUG); |
|
| 310 | + return $now; |
|
| 311 | 311 | } |
| 312 | 312 | |
| 313 | 313 | |
@@ -320,7 +320,7 @@ discard block |
||
| 320 | 320 | * @return string |
| 321 | 321 | */ |
| 322 | 322 | function _sqlite_func_month($d) { |
| 323 | - return _sqlite_func_date('m', $d); |
|
| 323 | + return _sqlite_func_date('m', $d); |
|
| 324 | 324 | } |
| 325 | 325 | |
| 326 | 326 | |
@@ -333,10 +333,10 @@ discard block |
||
| 333 | 333 | * @return string |
| 334 | 334 | */ |
| 335 | 335 | function _sqlite_func_preg_replace($quoi, $cherche, $remplace) { |
| 336 | - $return = preg_replace('%' . $cherche . '%', $remplace, $quoi); |
|
| 336 | + $return = preg_replace('%' . $cherche . '%', $remplace, $quoi); |
|
| 337 | 337 | |
| 338 | - #spip_log("preg_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG); |
|
| 339 | - return $return; |
|
| 338 | + #spip_log("preg_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG); |
|
| 339 | + return $return; |
|
| 340 | 340 | } |
| 341 | 341 | |
| 342 | 342 | /** |
@@ -349,13 +349,13 @@ discard block |
||
| 349 | 349 | * @return string, l'extrait trouve. |
| 350 | 350 | **/ |
| 351 | 351 | function _sqlite_func_extraire_multi($quoi, $lang) { |
| 352 | - if (strpos($quoi, '<') !== false) { |
|
| 353 | - include_spip("src/Texte/Collecteur/AbstractCollecteur"); |
|
| 354 | - include_spip("src/Texte/Collecteur/Multis"); |
|
| 355 | - $collecteurMultis = new Spip\Texte\Collecteur\Multis(); |
|
| 356 | - $quoi = $collecteurMultis->traiter($quoi, ['lang' => $lang, 'appliquer_typo' => false]); |
|
| 357 | - } |
|
| 358 | - return $quoi; |
|
| 352 | + if (strpos($quoi, '<') !== false) { |
|
| 353 | + include_spip("src/Texte/Collecteur/AbstractCollecteur"); |
|
| 354 | + include_spip("src/Texte/Collecteur/Multis"); |
|
| 355 | + $collecteurMultis = new Spip\Texte\Collecteur\Multis(); |
|
| 356 | + $quoi = $collecteurMultis->traiter($quoi, ['lang' => $lang, 'appliquer_typo' => false]); |
|
| 357 | + } |
|
| 358 | + return $quoi; |
|
| 359 | 359 | } |
| 360 | 360 | |
| 361 | 361 | |
@@ -365,7 +365,7 @@ discard block |
||
| 365 | 365 | * @return float |
| 366 | 366 | */ |
| 367 | 367 | function _sqlite_func_rand() { |
| 368 | - return random_int(0, mt_getrandmax()); |
|
| 368 | + return random_int(0, mt_getrandmax()); |
|
| 369 | 369 | } |
| 370 | 370 | |
| 371 | 371 | |
@@ -377,7 +377,7 @@ discard block |
||
| 377 | 377 | * @return string |
| 378 | 378 | */ |
| 379 | 379 | function _sqlite_func_right($s, $length) { |
| 380 | - return substr($s, 0 - $length); |
|
| 380 | + return substr($s, 0 - $length); |
|
| 381 | 381 | } |
| 382 | 382 | |
| 383 | 383 | |
@@ -389,17 +389,17 @@ discard block |
||
| 389 | 389 | * @return bool |
| 390 | 390 | */ |
| 391 | 391 | function _sqlite_func_regexp_match($cherche, $quoi) { |
| 392 | - // optimiser un cas tres courant avec les requetes en base |
|
| 393 | - if (!$quoi and !strlen($quoi)) { |
|
| 394 | - return false; |
|
| 395 | - } |
|
| 396 | - // il faut enlever un niveau d'echappement pour être homogène à mysql |
|
| 397 | - $cherche = str_replace('\\\\', '\\', $cherche); |
|
| 398 | - $u = $GLOBALS['meta']['pcre_u'] ?? 'u'; |
|
| 399 | - $return = preg_match('%' . $cherche . '%imsS' . $u, $quoi); |
|
| 400 | - |
|
| 401 | - #spip_log("regexp_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG); |
|
| 402 | - return $return; |
|
| 392 | + // optimiser un cas tres courant avec les requetes en base |
|
| 393 | + if (!$quoi and !strlen($quoi)) { |
|
| 394 | + return false; |
|
| 395 | + } |
|
| 396 | + // il faut enlever un niveau d'echappement pour être homogène à mysql |
|
| 397 | + $cherche = str_replace('\\\\', '\\', $cherche); |
|
| 398 | + $u = $GLOBALS['meta']['pcre_u'] ?? 'u'; |
|
| 399 | + $return = preg_match('%' . $cherche . '%imsS' . $u, $quoi); |
|
| 400 | + |
|
| 401 | + #spip_log("regexp_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG); |
|
| 402 | + return $return; |
|
| 403 | 403 | } |
| 404 | 404 | |
| 405 | 405 | |
@@ -414,8 +414,8 @@ discard block |
||
| 414 | 414 | * @return string |
| 415 | 415 | */ |
| 416 | 416 | function _sqlite_func_date_format($date, $conv) { |
| 417 | - $conv = _sqlite_func_strftime_format_converter($conv); |
|
| 418 | - return strftime($conv, is_int($date) ? $date : strtotime($date)); |
|
| 417 | + $conv = _sqlite_func_strftime_format_converter($conv); |
|
| 418 | + return strftime($conv, is_int($date) ? $date : strtotime($date)); |
|
| 419 | 419 | } |
| 420 | 420 | |
| 421 | 421 | /** |
@@ -431,28 +431,28 @@ discard block |
||
| 431 | 431 | * @return void |
| 432 | 432 | */ |
| 433 | 433 | function _sqlite_func_strftime_format_converter(string $conv): string { |
| 434 | - // ok : %a %b %d %e %H %I %l %j %k %m %p %r %S %T %w %y %Y |
|
| 435 | - // on ne sait pas en gérer certains... |
|
| 436 | - static $mysql_to_strftime_not_ok = ['%c', '%D', '%f', '%U', '%V', '%W', '%X']; |
|
| 437 | - static $mysql_to_strftime = [ |
|
| 438 | - '%h' => '%I', |
|
| 439 | - '%i' => '%M', |
|
| 440 | - '%M' => '%B', |
|
| 441 | - '%s' => '%S', |
|
| 442 | - '%u' => '%U', |
|
| 443 | - '%v' => '%V', |
|
| 444 | - '%x' => '%G', |
|
| 445 | - ]; |
|
| 446 | - static $to_strftime = []; |
|
| 447 | - if (!isset($to_strftime[$conv])) { |
|
| 448 | - $count = 0; |
|
| 449 | - str_replace($mysql_to_strftime_not_ok, '', $conv, $count); |
|
| 450 | - if ($count > 0) { |
|
| 451 | - spip_log("DATE_FORMAT : At least one parameter can't be parsed by strftime with format '$conv'", 'sqlite.' . _LOG_ERREUR); |
|
| 452 | - } |
|
| 453 | - $to_strftime[$conv] = str_replace(array_keys($mysql_to_strftime), $mysql_to_strftime, $conv); |
|
| 454 | - } |
|
| 455 | - return $to_strftime[$conv]; |
|
| 434 | + // ok : %a %b %d %e %H %I %l %j %k %m %p %r %S %T %w %y %Y |
|
| 435 | + // on ne sait pas en gérer certains... |
|
| 436 | + static $mysql_to_strftime_not_ok = ['%c', '%D', '%f', '%U', '%V', '%W', '%X']; |
|
| 437 | + static $mysql_to_strftime = [ |
|
| 438 | + '%h' => '%I', |
|
| 439 | + '%i' => '%M', |
|
| 440 | + '%M' => '%B', |
|
| 441 | + '%s' => '%S', |
|
| 442 | + '%u' => '%U', |
|
| 443 | + '%v' => '%V', |
|
| 444 | + '%x' => '%G', |
|
| 445 | + ]; |
|
| 446 | + static $to_strftime = []; |
|
| 447 | + if (!isset($to_strftime[$conv])) { |
|
| 448 | + $count = 0; |
|
| 449 | + str_replace($mysql_to_strftime_not_ok, '', $conv, $count); |
|
| 450 | + if ($count > 0) { |
|
| 451 | + spip_log("DATE_FORMAT : At least one parameter can't be parsed by strftime with format '$conv'", 'sqlite.' . _LOG_ERREUR); |
|
| 452 | + } |
|
| 453 | + $to_strftime[$conv] = str_replace(array_keys($mysql_to_strftime), $mysql_to_strftime, $conv); |
|
| 454 | + } |
|
| 455 | + return $to_strftime[$conv]; |
|
| 456 | 456 | } |
| 457 | 457 | |
| 458 | 458 | /** |
@@ -466,11 +466,11 @@ discard block |
||
| 466 | 466 | * @return int |
| 467 | 467 | */ |
| 468 | 468 | function _sqlite_func_to_days($d) { |
| 469 | - static $offset = 719528; // nb de jour entre 0000-00-00 et timestamp 0=1970-01-01 |
|
| 470 | - $result = $offset + (int)ceil(_sqlite_func_unix_timestamp($d) / (24 * 3600)); |
|
| 469 | + static $offset = 719528; // nb de jour entre 0000-00-00 et timestamp 0=1970-01-01 |
|
| 470 | + $result = $offset + (int)ceil(_sqlite_func_unix_timestamp($d) / (24 * 3600)); |
|
| 471 | 471 | |
| 472 | - #spip_log("Passage avec TO_DAYS : $d, $result",'sqlite.'._LOG_DEBUG); |
|
| 473 | - return $result; |
|
| 472 | + #spip_log("Passage avec TO_DAYS : $d, $result",'sqlite.'._LOG_DEBUG); |
|
| 473 | + return $result; |
|
| 474 | 474 | } |
| 475 | 475 | |
| 476 | 476 | /** |
@@ -482,13 +482,13 @@ discard block |
||
| 482 | 482 | * @return string |
| 483 | 483 | */ |
| 484 | 484 | function _sqlite_func_substring($string, $start, $len = null) { |
| 485 | - // SQL compte a partir de 1, php a partir de 0 |
|
| 486 | - $start = ($start > 0) ? $start - 1 : $start; |
|
| 487 | - if (is_null($len)) { |
|
| 488 | - return substr($string, $start); |
|
| 489 | - } else { |
|
| 490 | - return substr($string, $start, $len); |
|
| 491 | - } |
|
| 485 | + // SQL compte a partir de 1, php a partir de 0 |
|
| 486 | + $start = ($start > 0) ? $start - 1 : $start; |
|
| 487 | + if (is_null($len)) { |
|
| 488 | + return substr($string, $start); |
|
| 489 | + } else { |
|
| 490 | + return substr($string, $start, $len); |
|
| 491 | + } |
|
| 492 | 492 | } |
| 493 | 493 | |
| 494 | 494 | /** |
@@ -504,33 +504,33 @@ discard block |
||
| 504 | 504 | * @return int |
| 505 | 505 | */ |
| 506 | 506 | function _sqlite_timestampdiff($unit, $date1, $date2) { |
| 507 | - $d1 = date_create($date1); |
|
| 508 | - $d2 = date_create($date2); |
|
| 509 | - $diff = date_diff($d1, $d2); |
|
| 510 | - $inv = $diff->invert ? -1 : 1; |
|
| 511 | - switch ($unit) { |
|
| 512 | - case 'YEAR': |
|
| 513 | - return $inv * $diff->y; |
|
| 514 | - case 'QUARTER': |
|
| 515 | - return $inv * (4 * $diff->y + intval(floor($diff->m / 3))); |
|
| 516 | - case 'MONTH': |
|
| 517 | - return $inv * (12 * $diff->y + $diff->m); |
|
| 518 | - case 'WEEK': |
|
| 519 | - return $inv * intval(floor($diff->days / 7)); |
|
| 520 | - case 'DAY': |
|
| 521 | - #var_dump($inv*$diff->days); |
|
| 522 | - return $inv * $diff->days; |
|
| 523 | - case 'HOUR': |
|
| 524 | - return $inv * (24 * $diff->days + $diff->h); |
|
| 525 | - case 'MINUTE': |
|
| 526 | - return $inv * ((24 * $diff->days + $diff->h) * 60 + $diff->i); |
|
| 527 | - case 'SECOND': |
|
| 528 | - return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s); |
|
| 529 | - case 'MICROSECOND': |
|
| 530 | - return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s) * 1_000_000; |
|
| 531 | - } |
|
| 532 | - |
|
| 533 | - return 0; |
|
| 507 | + $d1 = date_create($date1); |
|
| 508 | + $d2 = date_create($date2); |
|
| 509 | + $diff = date_diff($d1, $d2); |
|
| 510 | + $inv = $diff->invert ? -1 : 1; |
|
| 511 | + switch ($unit) { |
|
| 512 | + case 'YEAR': |
|
| 513 | + return $inv * $diff->y; |
|
| 514 | + case 'QUARTER': |
|
| 515 | + return $inv * (4 * $diff->y + intval(floor($diff->m / 3))); |
|
| 516 | + case 'MONTH': |
|
| 517 | + return $inv * (12 * $diff->y + $diff->m); |
|
| 518 | + case 'WEEK': |
|
| 519 | + return $inv * intval(floor($diff->days / 7)); |
|
| 520 | + case 'DAY': |
|
| 521 | + #var_dump($inv*$diff->days); |
|
| 522 | + return $inv * $diff->days; |
|
| 523 | + case 'HOUR': |
|
| 524 | + return $inv * (24 * $diff->days + $diff->h); |
|
| 525 | + case 'MINUTE': |
|
| 526 | + return $inv * ((24 * $diff->days + $diff->h) * 60 + $diff->i); |
|
| 527 | + case 'SECOND': |
|
| 528 | + return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s); |
|
| 529 | + case 'MICROSECOND': |
|
| 530 | + return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s) * 1_000_000; |
|
| 531 | + } |
|
| 532 | + |
|
| 533 | + return 0; |
|
| 534 | 534 | } |
| 535 | 535 | |
| 536 | 536 | /** |
@@ -540,24 +540,24 @@ discard block |
||
| 540 | 540 | * @return int |
| 541 | 541 | */ |
| 542 | 542 | function _sqlite_func_unix_timestamp($d) { |
| 543 | - static $mem = []; |
|
| 544 | - static $n = 0; |
|
| 545 | - if (isset($mem[$d])) { |
|
| 546 | - return $mem[$d]; |
|
| 547 | - } |
|
| 548 | - if ($n++ > 100) { |
|
| 549 | - $mem = []; |
|
| 550 | - $n = 0; |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - //2005-12-02 20:53:53 |
|
| 554 | - #spip_log("Passage avec UNIX_TIMESTAMP : $d",'sqlite.'._LOG_DEBUG); |
|
| 555 | - if (!$d) { |
|
| 556 | - return $mem[$d] = time(); |
|
| 557 | - } |
|
| 558 | - |
|
| 559 | - // une pile plus grosse n'accelere pas le calcul |
|
| 560 | - return $mem[$d] = strtotime($d); |
|
| 543 | + static $mem = []; |
|
| 544 | + static $n = 0; |
|
| 545 | + if (isset($mem[$d])) { |
|
| 546 | + return $mem[$d]; |
|
| 547 | + } |
|
| 548 | + if ($n++ > 100) { |
|
| 549 | + $mem = []; |
|
| 550 | + $n = 0; |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + //2005-12-02 20:53:53 |
|
| 554 | + #spip_log("Passage avec UNIX_TIMESTAMP : $d",'sqlite.'._LOG_DEBUG); |
|
| 555 | + if (!$d) { |
|
| 556 | + return $mem[$d] = time(); |
|
| 557 | + } |
|
| 558 | + |
|
| 559 | + // une pile plus grosse n'accelere pas le calcul |
|
| 560 | + return $mem[$d] = strtotime($d); |
|
| 561 | 561 | } |
| 562 | 562 | |
| 563 | 563 | |
@@ -570,7 +570,7 @@ discard block |
||
| 570 | 570 | * @return string |
| 571 | 571 | */ |
| 572 | 572 | function _sqlite_func_year($d) { |
| 573 | - return _sqlite_func_date('Y', $d); |
|
| 573 | + return _sqlite_func_date('Y', $d); |
|
| 574 | 574 | } |
| 575 | 575 | |
| 576 | 576 | /** |
@@ -583,20 +583,20 @@ discard block |
||
| 583 | 583 | * @return string |
| 584 | 584 | */ |
| 585 | 585 | function _sqlite_func_date($quoi, $d) { |
| 586 | - static $mem = []; |
|
| 587 | - static $n = 0; |
|
| 588 | - if (isset($mem[$d])) { |
|
| 589 | - return $mem[$d][$quoi]; |
|
| 590 | - } |
|
| 591 | - if ($n++ > 100) { |
|
| 592 | - $mem = []; |
|
| 593 | - $n = 0; |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - $dec = date('Y-m-d', _sqlite_func_unix_timestamp($d)); |
|
| 597 | - $mem[$d] = ['Y' => substr($dec, 0, 4), 'm' => substr($dec, 5, 2), 'd' => substr($dec, 8, 2)]; |
|
| 598 | - |
|
| 599 | - return $mem[$d][$quoi]; |
|
| 586 | + static $mem = []; |
|
| 587 | + static $n = 0; |
|
| 588 | + if (isset($mem[$d])) { |
|
| 589 | + return $mem[$d][$quoi]; |
|
| 590 | + } |
|
| 591 | + if ($n++ > 100) { |
|
| 592 | + $mem = []; |
|
| 593 | + $n = 0; |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + $dec = date('Y-m-d', _sqlite_func_unix_timestamp($d)); |
|
| 597 | + $mem[$d] = ['Y' => substr($dec, 0, 4), 'm' => substr($dec, 5, 2), 'd' => substr($dec, 8, 2)]; |
|
| 598 | + |
|
| 599 | + return $mem[$d][$quoi]; |
|
| 600 | 600 | } |
| 601 | 601 | |
| 602 | 602 | /** |
@@ -605,5 +605,5 @@ discard block |
||
| 605 | 605 | * @return void |
| 606 | 606 | */ |
| 607 | 607 | function _sqlite_func_vide() { |
| 608 | - return; |
|
| 608 | + return; |
|
| 609 | 609 | } |