1e4f68eff97cd1776721911a9329fd89f529dcc5
[mtweb] / mw / app / data / impl / mw_pdo_sqlite.php
1 <?php
2
3   class mw_pdo_sqlite{
4
5     var $link;
6
7     var $host;
8     var $base;
9     var $user;
10     var $password;
11
12     var $EXTENTION_OK;
13
14     function get_link(){
15       return $this->link;
16     }
17
18     function extention_ok(&$env) { return $this->EXTENTION_OK; }
19
20     function authentication_required() { return false; }
21
22     function sgbd_name() { return "PDO SQLite"; }
23
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"));
30     }
31
32     function connect($host, $base, $user, $password){
33       if($host) $host .= substr($host, -1) != "/" ? "/" : "";
34       try{
35         $this->link = null;
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'");
39       }
40       catch(PDOException $e){
41         throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur"));
42       }
43       return true;
44     }
45
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
49     }
50
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"));
54       }
55       $desc = array(
56         "name" => $table_name,
57         "attributs" => array()
58       );
59       $sql = "PRAGMA table_info(".$table_name.")";
60       try{
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
66           );
67         }
68         $this->free_result($rst);
69       }
70       catch(Exception $e){
71         throw new Exception($this->exception_out("impossible de lire les champs de la table ".$table_name));
72       }
73       return $desc;
74     }
75
76     function table_exists($table_name){
77       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
78       $EXISTS = false;
79       try{
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){
83             $EXISTS = true;
84             break;
85           }\r
86         }\r
87         $this->free_result($rst);
88       }
89       catch(Exception $e){
90         throw new Exception($this->exception_out("Impossible de savoir si la table existe"));
91       }
92       return $EXISTS;\r
93     }
94
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"));
99       }
100       $EXISTS = false;
101       foreach($desc["attributs"] as $attribut_name => $attribut){
102         if($field_name == $attribut_name){
103           $EXISTS = true;
104           break;
105         }
106       }
107       return $EXISTS;
108     }
109
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"));
114       }
115       return $result;\r
116     }
117
118     function fetch_assoc($rst){
119       if(!$this->link){
120         throw new Exception($this->exception_out("fetch_assoc sans connexion"));
121       }
122       if(!$rst){
123         throw new Exception($this->exception_out("fetch_assoc sans result handler"));
124       }
125       try{
126         $tuple = $rst->fetch(PDO::FETCH_ASSOC);
127       }
128       catch(Exception $e){
129         throw new Exception($this->exception_out("erreur fetch_assoc"));
130       }
131       return $tuple;
132     }
133
134     function insert_id(){
135       if(!$this->link){
136         throw new Exception($this->exception_out("insert_id sans connexion"));
137       }
138       try{
139         $id = $this->link->lastInsertId();
140       }
141       catch(Exception $e){
142         throw new Exception($this->exception_out("erreur insert_id"));
143       }
144       return $id;
145     }
146
147     function free_result($rst){
148       if(!$this->link){
149         throw new Exception($this->exception_out("free_result sans connexion"));
150       }
151       if(!$rst){
152         throw new Exception($this->exception_out("free_result sans result handler"));
153       }
154       try{
155         $rst->closeCursor();
156         $rst = null;
157       }
158       catch(Exception $e){
159         throw new Exception($this->exception_out("erreur free_result"));
160       }
161       return true;
162     }
163
164     function close(){
165       $this->link = null;
166       return true;
167     }
168
169     function exception_out($message){\r
170       return "[erreur] sqlite : ".$message;\r
171     }
172
173   }
174
175 ?>