18 function extention_ok(&$env) { return $this->EXTENTION_OK; }
20 function authentication_required() { return false; }
22 function sgbd_name() { return "PDO SQLite"; }
24 function mw_pdo_sqlite($params = array()){
25 $this->host = isset($params["host"]) ? $params["host"] : "content/data/sqlite";
26 $this->base = isset($params["base"]) ? $params["base"] : "mtweb.db";
27 $this->user = isset($params["user"]) ? $params["user"] : "";
28 $this->password = isset($params["password"]) ? $params["password"] : "";
29 $this->EXTENTION_OK = (extension_loaded("pdo") && extension_loaded("pdo_sqlite"));
32 function connect($host, $base, $user, $password){
33 if($host) $host .= substr($host, -1) != "/" ? "/" : "";
36 $this->link = new PDO("sqlite:".$host.$base);
37 $this->link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
38 $this->link->query("PRAGMA encoding = 'UTF-8'");
40 catch(PDOException $e){
41 throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur"));
46 function select_db($db_name){
\r
47 $this->base = $db_name;
\r
48 return $this->connect($this->host, $this->base, $this->user, $this->password);
\r
51 function desc_table($table_name){
52 if(strpos($table_name, "'") !== false){
53 throw new Exception($this->exception_out("nom de table avec un simple quote"));
56 "name" => $table_name,
57 "attributs" => array()
59 $sql = "PRAGMA table_info(".$table_name.")";
61 $rst = $this->query($sql);
62 while($v_rst = $this->fetch_assoc($rst)){
63 $desc["attributs"][$v_rst["name"]] = array(
64 "name" => $v_rst["name"],
65 "prymary_key" => isset($v_rst["pk"]) && $v_rst["pk"] ? true : false
68 $this->free_result($rst);
71 throw new Exception($this->exception_out("impossible de lire les champs de la table ".$table_name));
76 function table_exists($table_name){
77 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
80 $rst = $this->query("SELECT name FROM sqlite_master WHERE type='table'");
81 while($v_rst = $rst->fetch()){
\r
82 if($v_rst[0] == $table_name){
87 $this->free_result($rst);
90 throw new Exception($this->exception_out("Impossible de savoir si la table existe"));
95 function field_exists($table_name, $field_name){
96 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
97 if(!($desc = $this->desc_table($table_name))){
98 throw new Exception($this->exception_out("Impossible de lire la description de la table"));
101 foreach($desc["attributs"] as $attribut_name => $attribut){
102 if($field_name == $attribut_name){
110 function query($query_string){
\r
111 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
112 if(!($result = $this->link->query($query_string))){
113 throw new Exception($this->exception_out("Syntaxe invalide dans une requete"));
118 function fetch_assoc($rst){
120 throw new Exception($this->exception_out("fetch_assoc sans connexion"));
123 throw new Exception($this->exception_out("fetch_assoc sans result handler"));
126 $tuple = $rst->fetch(PDO::FETCH_ASSOC);
129 throw new Exception($this->exception_out("erreur fetch_assoc"));
134 function insert_id(){
136 throw new Exception($this->exception_out("insert_id sans connexion"));
139 $id = $this->link->lastInsertId();
142 throw new Exception($this->exception_out("erreur insert_id"));
147 function free_result($rst){
149 throw new Exception($this->exception_out("free_result sans connexion"));
152 throw new Exception($this->exception_out("free_result sans result handler"));
159 throw new Exception($this->exception_out("erreur free_result"));
169 function exception_out($message){
\r
170 return "[erreur] sqlite : ".$message;
\r