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 $sql = "SELECT * from information_schema.columns where table_name='".$table_name."'";
53 $rst = $this->query($sql);
55 "name" => $table_name,
56 "attributs" => array()
59 while($v_rst = $this->fetch_assoc($rst)){
\r
60 $desc["attributs"][$v_rst["COLUMN_NAME"]] = array(
61 "name" => $v_rst["COLUMN_NAME"],
62 "prymary_key" => $v_rst["COLUMN_KEY"] == "PRI" ? true : false,
63 "auto_increment" => $v_rst["EXTRA"] == "auto_increment" ? true : false
66 $this->free_result($rst);
69 throw new Exception($this->exception_out("Impossible de lire la description de la table"));
74 function table_exists($table_name){
75 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
78 $rst = $this->query("SELECT name FROM sqlite_master WHERE type='table'");
79 while($v_rst = $rst->fetch()){
\r
80 if($v_rst[0] == $table_name){
85 $this->free_result($rst);
88 throw new Exception($this->exception_out("Impossible de savoir si la table existe"));
93 function field_exists($table_name, $field_name){
94 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
95 if(!($desc = $this->desc_table($table_name))){
96 throw new Exception($this->exception_out("Impossible de lire la description de la table"));
99 foreach($desc["attributs"] as $attribut_name => $attribut){
100 if($field_name == $attribut_name){
108 function query($query_string){
\r
109 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
110 if(!($result = $this->link->query($query_string))){
111 throw new Exception($this->exception_out("Syntaxe invalide dans une requete"));
116 function fetch_assoc($rst){
118 throw new Exception($this->exception_out("fetch_assoc sans connexion"));
121 throw new Exception($this->exception_out("fetch_assoc sans result handler"));
124 $tuple = $rst->fetch(PDO::FETCH_ASSOC);
127 throw new Exception($this->exception_out("erreur fetch_assoc"));
132 function insert_id(){
134 throw new Exception($this->exception_out("insert_id sans connexion"));
137 $id = $this->link->lastInsertId();
140 throw new Exception($this->exception_out("erreur insert_id"));
145 function free_result($rst){
147 throw new Exception($this->exception_out("free_result sans connexion"));
150 throw new Exception($this->exception_out("free_result sans result handler"));
157 throw new Exception($this->exception_out("erreur free_result"));
167 function exception_out($message){
\r
168 return "[erreur] sqlite : ".$message;
\r