implementation sgbd herite de mw_sgbd
[mtweb] / mw / app / data / impl / mw_pdo_mysql.php
1 <?php
2
3   class mw_pdo_mysql extends mw_sgbd{
4
5     public function name(){
6       return "PDO MySql";
7     }
8
9     public function default_params(){
10       return array(
11         "host" => "localhost",
12         "base" => "mtweb",
13         "user" => "",
14         "password" => ""
15       );
16     }
17
18     public function validate_extention(){
19       return extension_loaded("pdo") && extension_loaded("pdo_mysql");
20     }
21
22     public function authentication_required(){
23       return true;
24     }
25
26     public function connect($host, $base, $user, $password){
27       try{
28         $this->link = new PDO("mysql:host=".$host.";dbname=".$base, $user, $password);
29         $this->link->query("SET NAMES 'utf8'");
30       }
31       catch(PDOException $e){
32         throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur"));
33       }
34       return true;
35     }
36
37     public function select_db($db_name){\r
38       $this->base = $db_name;\r
39       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
40       return $this->query("USE ".$db_name);\r
41     }
42
43     public function desc_table($table_name){
44       $table_name = $this->replace_prefixes($table_name);
45       $sql = "SELECT * from information_schema.columns where table_name='".$table_name."'";
46       $rst = $this->query($sql);
47       $desc = array(
48         "name" => $table_name,
49         "attributs" => array()
50       );
51       try{
52         while($v_rst = $this->fetch_assoc($rst)){\r
53           $desc["attributs"][$v_rst["COLUMN_NAME"]] = array(
54             "name" => $v_rst["COLUMN_NAME"],
55             "prymary_key" => $v_rst["COLUMN_KEY"] == "PRI" ? true : false,
56             "auto_increment" => $v_rst["EXTRA"] == "auto_increment" ? true : false
57           );
58         }\r
59         $this->free_result($rst);
60       }
61       catch(Exception $e){
62         throw new Exception($this->exception_out("Impossible de lire la description de la table"));
63       }
64       return $desc;
65     }
66
67     public function table_exists($table_name){
68       $table_name = $this->replace_prefixes($table_name);
69       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
70       $EXISTS = false;
71       try{
72         $rst = $this->query("SHOW TABLES");
73         while($v_rst = $rst->fetch()){\r
74           if($v_rst[0] == $table_name){
75             $EXISTS = true;
76             break;
77           }\r
78         }\r
79         $this->free_result($rst);
80       }
81       catch(Exception $e){
82         throw new Exception($this->exception_out("Impossible de savoir si la table existe"));
83       }
84       return $EXISTS;\r
85     }
86
87     public function field_exists($table_name, $field_name){
88       $table_name = $this->replace_prefixes($table_name);
89       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
90       if(!($desc = $this->desc_table($table_name))){
91         throw new Exception($this->exception_out("Impossible de lire la description de la table"));
92       }
93       $EXISTS = false;
94       foreach($desc["attributs"] as $attribut_name => $attribut){
95         if($field_name == $attribut_name){
96           $EXISTS = true;
97           break;
98         }
99       }
100       return $EXISTS;
101     }
102
103     public function query($query_string){
104       $query_string = $this->replace_prefixes($query_string);\r
105       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
106       if(!($result = $this->link->query($query_string))){
107         throw new Exception($this->exception_out("Syntaxe invalide dans une requete"));
108       }
109       return $result;\r
110     }
111
112     public function fetch_assoc($rst){
113       if(!$this->link){
114         throw new Exception($this->exception_out("fetch_assoc sans connexion"));
115       }
116       if(!$rst){
117         throw new Exception($this->exception_out("fetch_assoc sans result handler"));
118       }
119       try{
120         $tuple = $rst->fetch(PDO::FETCH_ASSOC);
121       }
122       catch(Exception $e){
123         throw new Exception($this->exception_out("erreur fetch_assoc"));
124       }
125       return $tuple;
126     }
127
128     public function insert_id(){
129       if(!$this->link){
130         throw new Exception($this->exception_out("insert_id sans connexion"));
131       }
132       try{
133         $id = $this->link->lastInsertId();
134       }
135       catch(Exception $e){
136         throw new Exception($this->exception_out("erreur insert_id"));
137       }
138       return $id;
139     }
140
141     public function free_result($rst){
142       if(!$this->link){
143         throw new Exception($this->exception_out("free_result sans connexion"));
144       }
145       if(!$rst){
146         throw new Exception($this->exception_out("free_result sans result handler"));
147       }
148       try{
149         $rst->closeCursor();
150         $rst = null;
151       }
152       catch(Exception $e){
153         throw new Exception($this->exception_out("erreur free_result"));
154       }
155       return true;
156     }
157
158     public function close(){
159       $this->link = null;
160       return true;
161     }
162
163     public function exception_out($message){\r
164       return "[erreur] mysql : ".$message;\r
165     }
166
167   }