70f9876b6b83185597a8c13ef142bf2d1026a937
[mtweb] / mw / app / data / impl / mw_pdo_mysql.php
1 <?php
2
3   class mw_pdo_mysql{
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 true; }
21
22     function sgbd_name() { return "PDO MySql"; }
23
24     function mw_pdo_mysql($params = array()){
25       $this->host = isset($params["host"]) ? $params["host"] : "localhost";
26       $this->base = isset($params["base"]) ? $params["base"] : "mtweb";
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_mysql"));
30     }
31
32     function connect($host, $base, $user, $password){
33       try{
34         $this->link = new PDO("mysql:host=".$host.";dbname=".$base, $user, $password);
35         $this->link->query("SET NAMES 'utf8'");
36       }
37       catch(PDOException $e){
38         throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur"));
39       }
40       return true;
41     }
42
43     function select_db($db_name){\r
44       $this->base = $db_name;\r
45       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
46       return $this->query("USE ".$db_name);\r
47     }
48
49     function desc_table($table_name){
50       $sql = "SELECT * from information_schema.columns where table_name='".$table_name."'";
51       $rst = $this->query($sql);
52       $desc = array(
53         "name" => $table_name,
54         "attributs" => array()
55       );
56       try{
57         while($v_rst = $this->fetch_assoc($rst)){\r
58           $desc["attributs"][$v_rst["COLUMN_NAME"]] = array(
59             "name" => $v_rst["COLUMN_NAME"],
60             "prymary_key" => $v_rst["COLUMN_KEY"] == "PRI" ? true : false,
61             "auto_increment" => $v_rst["EXTRA"] == "auto_increment" ? true : false
62           );
63         }\r
64         $this->free_result($rst);
65       }
66       catch(Exception $e){
67         throw new Exception($this->exception_out("Impossible de lire la description de la table"));
68       }
69       return $desc;
70     }
71
72     function table_exists($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("SHOW TABLES");
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     function field_exists($table_name, $field_name){
92       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
93       if(!($desc = $this->desc_table($table_name))){
94         throw new Exception($this->exception_out("Impossible de lire la description de la table"));
95       }
96       $EXISTS = false;
97       foreach($desc["attributs"] as $attribut_name => $attribut){
98         if($field_name == $attribut_name){
99           $EXISTS = true;
100           break;
101         }
102       }
103       return $EXISTS;
104     }
105
106     function query($query_string){\r
107       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
108       if(!($result = $this->link->query($query_string))){
109         throw new Exception($this->exception_out("Syntaxe invalide dans une requete"));
110       }
111       return $result;\r
112     }
113
114     function fetch_assoc($rst){
115       if(!$this->link){
116         throw new Exception($this->exception_out("fetch_assoc sans connexion"));
117       }
118       if(!$rst){
119         throw new Exception($this->exception_out("fetch_assoc sans result handler"));
120       }
121       try{
122         $tuple = $rst->fetch(PDO::FETCH_ASSOC);
123       }
124       catch(Exception $e){
125         throw new Exception($this->exception_out("erreur fetch_assoc"));
126       }
127       return $tuple;
128     }
129
130     function insert_id(){
131       if(!$this->link){
132         throw new Exception($this->exception_out("insert_id sans connexion"));
133       }
134       try{
135         $id = $this->link->lastInsertId();
136       }
137       catch(Exception $e){
138         throw new Exception($this->exception_out("erreur insert_id"));
139       }
140       return $id;
141     }
142
143     function free_result($rst){
144       if(!$this->link){
145         throw new Exception($this->exception_out("free_result sans connexion"));
146       }
147       if(!$rst){
148         throw new Exception($this->exception_out("free_result sans result handler"));
149       }
150       try{
151         $rst->closeCursor();
152         $rst = null;
153       }
154       catch(Exception $e){
155         throw new Exception($this->exception_out("erreur free_result"));
156       }
157       return true;
158     }
159
160     function close(){
161       $this->link = null;
162       return true;
163     }
164
165     function exception_out($message){\r
166       return "[erreur] mysql : ".$message;\r
167     }
168
169   }
170
171 ?>