implementation sgbd herite de mw_sgbd
[mtweb] / mw / app / data / impl / mw_pdo_sqlite.php
1 <?php
2
3   class mw_pdo_sqlite extends mw_sgbd{
4
5     public function name(){
6       return "PDO SQLite";
7     }
8
9     public function default_params(){
10       return array(
11         "host" => "content/data/sqlite",
12         "base" => "mtweb.db",
13         "user" => "",
14         "password" => ""
15       );
16     }
17
18     public function validate_extention(){
19       return extension_loaded("pdo") && extension_loaded("pdo_sqlite");
20     }
21
22     public function authentication_required(){
23       return false;
24     }
25
26     public function connect($host, $base, $user, $password){
27       if($host) $host .= substr($host, -1) != "/" ? "/" : "";
28       try{
29         $this->link = null;
30         $this->link = new PDO("sqlite:".$host.$base);
31         $this->link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
32         $this->link->query("PRAGMA encoding = 'UTF-8'");
33       }
34       catch(PDOException $e){
35         throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur"));
36       }
37       return true;
38     }
39
40     public function select_db($db_name){\r
41       $this->base = $db_name;\r
42       return $this->connect($this->host, $this->base, $this->user, $this->password);\r
43     }
44
45     public function desc_table($table_name){
46       $table_name = $this->replace_prefixes($table_name);
47       if(strpos($table_name, "'") !== false){
48         throw new Exception($this->exception_out("nom de table avec un simple quote"));
49       }
50       $desc = array(
51         "name" => $table_name,
52         "attributs" => array()
53       );
54       $sql = "PRAGMA table_info(".$table_name.")";
55       try{
56         $rst = $this->query($sql);
57         while($v_rst = $this->fetch_assoc($rst)){
58           $desc["attributs"][$v_rst["name"]] = array(
59             "name" => $v_rst["name"],
60             "prymary_key" => isset($v_rst["pk"]) && $v_rst["pk"] ? true : false
61           );
62         }
63         $this->free_result($rst);
64       }
65       catch(Exception $e){
66         throw new Exception($this->exception_out("impossible de lire les champs de la table ".$table_name));
67       }
68       return $desc;
69     }
70
71     public function table_exists($table_name){
72       $table_name = $this->replace_prefixes($table_name);
73       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
74       $EXISTS = false;
75       try{
76         $rst = $this->query("SELECT name FROM sqlite_master WHERE type='table'");
77         while($v_rst = $rst->fetch()){\r
78           if($v_rst[0] == $table_name){
79             $EXISTS = true;
80             break;
81           }\r
82         }\r
83         $this->free_result($rst);
84       }
85       catch(Exception $e){
86         throw new Exception($this->exception_out("Impossible de savoir si la table existe"));
87       }
88       return $EXISTS;\r
89     }
90
91     public function field_exists($table_name, $field_name){
92       $table_name = $this->replace_prefixes($table_name);
93       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
94       if(!($desc = $this->desc_table($table_name))){
95         throw new Exception($this->exception_out("Impossible de lire la description de la table"));
96       }
97       $EXISTS = false;
98       foreach($desc["attributs"] as $attribut_name => $attribut){
99         if($field_name == $attribut_name){
100           $EXISTS = true;
101           break;
102         }
103       }
104       return $EXISTS;
105     }
106
107     public function query($query_string){
108       $query_string = $this->replace_prefixes($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     public 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     public 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     public 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     public function close(){
163       $this->link = null;
164       return true;
165     }
166
167     public function exception_out($message){\r
168       return "[erreur] sqlite : ".$message;\r
169     }
170
171   }