[ Index ] |
|
Code source de Dotclear 2.0-beta6 |
1 <?php 2 # ***** BEGIN LICENSE BLOCK ***** 3 # This file is part of Clearbricks. 4 # Copyright (c) 2006 Olivier Meunier and contributors. All rights 5 # reserved. 6 # 7 # Clearbricks is free software; you can redistribute it and/or modify 8 # it under the terms of the GNU General Public License as published by 9 # the Free Software Foundation; either version 2 of the License, or 10 # (at your option) any later version. 11 # 12 # Clearbricks is distributed in the hope that it will be useful, 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 # GNU General Public License for more details. 16 # 17 # You should have received a copy of the GNU General Public License 18 # along with Clearbricks; if not, write to the Free Software 19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 # 21 # ***** END LICENSE BLOCK ***** 22 23 /** 24 @ingroup CB_DBLAYER 25 @brief SQLite Database Driver. 26 27 See the dbLayer documentation for common methods. 28 */ 29 class sqliteConnection extends dbLayer implements i_dbLayer 30 { 31 protected $__driver = 'sqlite'; 32 33 public function db_connect($host,$user,$password,$database) 34 { 35 if (!function_exists('sqlite_open')) { 36 throw new Exception('PHP SQLite functions are not available'); 37 } 38 39 if (($link = @sqlite_open($database)) === false) { 40 throw new Exception('Unable to connect to database'); 41 } 42 43 $this->db_post_connect($link,$database); 44 45 return $link; 46 } 47 48 public function db_pconnect($host,$user,$password,$database) 49 { 50 if (!function_exists('sqlite_popen')) { 51 throw new Exception('PHP SQLite functions are not available'); 52 } 53 54 if (($link = @sqlite_popen($database)) === false) { 55 throw new Exception('Unable to connect to database'); 56 } 57 58 $this->db_post_connect($link,$database); 59 60 return $link; 61 } 62 63 private function db_post_connect($link,$database) 64 { 65 $this->db_exec($link,'PRAGMA short_column_names = 1'); 66 sqlite_create_function($link,'now',array($this,'now'),0); 67 } 68 69 public function db_close($handle) 70 { 71 if (is_resource($handle)) { 72 sqlite_close($handle); 73 } 74 } 75 76 public function db_version($handle) 77 { 78 return sqlite_libversion(); 79 } 80 81 public function db_query($handle,$query) 82 { 83 if (is_resource($handle)) 84 { 85 $res = @sqlite_query($query,$handle); 86 if ($res === false) { 87 $e = new Exception($this->db_last_error($handle)); 88 $e->sql = $query; 89 throw $e; 90 } 91 return $res; 92 } 93 } 94 95 public function db_exec($handle,$query) 96 { 97 if (is_resource($handle)) 98 { 99 $res = @sqlite_exec($query,$handle); 100 if ($res === false) { 101 $e = new Exception($this->db_last_error($handle)); 102 $e->sql = $query; 103 throw $e; 104 } 105 return $res; 106 } 107 } 108 109 public function db_num_fields($res) 110 { 111 if (is_resource($res)) { 112 return sqlite_num_fields($res); 113 } 114 return 0; 115 } 116 117 public function db_num_rows($res) 118 { 119 if (is_resource($res)) { 120 return sqlite_num_rows($res); 121 } 122 return 0; 123 } 124 125 public function db_field_name($res,$position) 126 { 127 if (is_resource($res)) { 128 return sqlite_field_name($res,$position); 129 } 130 } 131 132 public function db_field_type($res,$position) 133 { 134 return 'varchar'; 135 } 136 137 public function db_fetch_assoc($res) 138 { 139 if (is_resource($res)) { 140 return sqlite_fetch_array($res,SQLITE_ASSOC); 141 } 142 } 143 144 public function db_result_seek($res,$row) 145 { 146 if (is_resource($res)) { 147 return sqlite_seek($res,$row); 148 } 149 } 150 151 public function db_changes($handle,$res) 152 { 153 if (is_resource($handle)) { 154 return sqlite_changes($handle); 155 } 156 } 157 158 public function db_get_tables($handle) 159 { 160 $sql = 'SHOW TABLES'; 161 162 $res = array(); 163 $sql = " 164 Select * from 165 ( select 'main' as TABLE_CATALOG , 'sqlite' as TABLE_SCHEMA , 166 tbl_name as TABLE_NAME , case when type = 'table' 167 then 'BASE TABLE' when type = 'view' then 'VIEW' 168 end as TABLE_TYPE, sql as TABLE_SOURCE from sqlite_master 169 where type in('table','view') 170 and tbl_name not like 'INFORMATION_SCHEMA_%' 171 union select 'main' as TABLE_CATALOG , 'sqlite' as TABLE_SCHEMA, 172 tbl_name as TABLE_NAME , case when type = 'table' 173 then 'TEMPORARY TABLE' when type = 'view' 174 then 'TEMPORARY VIEW' end as TABLE_TYPE, sql as 175 TABLE_SOURCE from sqlite_temp_master where type in('table','view') 176 and tbl_name not like 'INFORMATION_SCHEMA_%' ) 177 BT order by TABLE_TYPE , TABLE_NAME 178 "; 179 180 $c = $this->db_query($handle,$sql); 181 182 $res = array(); 183 while (($f = $this->db_fetch_assoc($c)) !== false) { 184 //$res[] = current($f); 185 $res[] = $f['TABLE_NAME']; 186 } 187 return $res; 188 189 } 190 191 public function db_get_columns($handle,$table) 192 { 193 if (is_resource($handle)) 194 { 195 $cols = @sqlite_fetch_column_types($table,$handle); 196 return $cols !== false ? $cols : array(); 197 } 198 return array(); 199 } 200 201 public function db_last_error($handle) 202 { 203 if (is_resource($handle)) 204 { 205 $e = sqlite_last_error($handle); 206 if ($e) { 207 return sqlite_error_string($e).' ('.$e.')'; 208 } 209 } 210 return false; 211 } 212 213 public function db_escape_string($str,$handle=null) 214 { 215 return sqlite_escape_string($str); 216 } 217 218 public function vacuum($table) 219 { 220 $this->execute('VACUUM '.$this->escapeSystem($table)); 221 } 222 223 public function dateFormat($field,$pattern) 224 { 225 return "strftime('".$this->escape($pattern)."',".$field.') '; 226 } 227 228 public function escapeSystem($str) 229 { 230 return "'".sqlite_escape_string($str)."'"; 231 } 232 233 /// Internal SQLite function that adds NOW() SQL function. 234 public function now() 235 { 236 return date('Y-m-d H:i:s'); 237 } 238 } 239 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Feb 23 22:16:06 2007 | par Balluche grâce à PHPXref 0.7 |