4ef1696ed51ff755af7b0c6e61fa02c2241feb9e
[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       $sql = "SELECT * from information_schema.columns where table_name='".$table_name."'";
53       $rst = $this->query($sql);
54       $desc = array(
55         "name" => $table_name,
56         "attributs" => array()
57       );
58       try{
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
64           );
65         }\r
66         $this->free_result($rst);
67       }
68       catch(Exception $e){
69         throw new Exception($this->exception_out("Impossible de lire la description de la table"));
70       }
71       return $desc;
72     }
73
74     function table_exists($table_name){
75       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
76       $EXISTS = false;
77       try{
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){
81             $EXISTS = true;
82             break;
83           }\r
84         }\r
85         $this->free_result($rst);
86       }
87       catch(Exception $e){
88         throw new Exception($this->exception_out("Impossible de savoir si la table existe"));
89       }
90       return $EXISTS;\r
91     }
92
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"));
97       }
98       $EXISTS = false;
99       foreach($desc["attributs"] as $attribut_name => $attribut){
100         if($field_name == $attribut_name){
101           $EXISTS = true;
102           break;
103         }
104       }
105       return $EXISTS;
106     }
107
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"));
112       }
113       return $result;\r
114     }
115
116     function fetch_assoc($rst){
117       if(!$this->link){
118         throw new Exception($this->exception_out("fetch_assoc sans connexion"));
119       }
120       if(!$rst){
121         throw new Exception($this->exception_out("fetch_assoc sans result handler"));
122       }
123       try{
124         $tuple = $rst->fetch(PDO::FETCH_ASSOC);
125       }
126       catch(Exception $e){
127         throw new Exception($this->exception_out("erreur fetch_assoc"));
128       }
129       return $tuple;
130     }
131
132     function insert_id(){
133       if(!$this->link){
134         throw new Exception($this->exception_out("insert_id sans connexion"));
135       }
136       try{
137         $id = $this->link->lastInsertId();
138       }
139       catch(Exception $e){
140         throw new Exception($this->exception_out("erreur insert_id"));
141       }
142       return $id;
143     }
144
145     function free_result($rst){
146       if(!$this->link){
147         throw new Exception($this->exception_out("free_result sans connexion"));
148       }
149       if(!$rst){
150         throw new Exception($this->exception_out("free_result sans result handler"));
151       }
152       try{
153         $rst->closeCursor();
154         $rst = null;
155       }
156       catch(Exception $e){
157         throw new Exception($this->exception_out("erreur free_result"));
158       }
159       return true;
160     }
161
162     function close(){
163       $this->link = null;
164       return true;
165     }
166
167     function exception_out($message){\r
168       return "[erreur] sqlite : ".$message;\r
169     }
170
171   }
172
173 ?>