public attr / function, constructeurs __construct
[mtweb] / mw / app / data / impl / mw_pdo_mysql.php
1 <?php
2
3   class mw_pdo_mysql{
4
5     public $link;
6     public $host;
7     public $base;
8     public $user;
9     public $password;
10     public $EXTENTION_OK;
11
12     public function __construct($params = array()){
13       $this->host = isset($params["host"]) ? $params["host"] : "localhost";
14       $this->base = isset($params["base"]) ? $params["base"] : "mtweb";
15       $this->user = isset($params["user"]) ? $params["user"] : "";
16       $this->password = isset($params["password"]) ? $params["password"] : "";
17       $this->EXTENTION_OK = (extension_loaded("pdo") && extension_loaded("pdo_mysql"));
18     }
19
20     public function get_link(){
21       return $this->link;
22     }
23
24     public function extention_ok(&$env){
25       return $this->EXTENTION_OK;
26     }
27
28     public function authentication_required(){
29       return true;
30     }
31
32     public function sgbd_name(){
33       return "PDO MySql";
34     }
35
36     public function connect($host, $base, $user, $password){
37       try{
38         $this->link = new PDO("mysql:host=".$host.";dbname=".$base, $user, $password);
39         $this->link->query("SET NAMES 'utf8'");
40       }
41       catch(PDOException $e){
42         throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur"));
43       }
44       return true;
45     }
46
47     public function select_db($db_name){\r
48       $this->base = $db_name;\r
49       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
50       return $this->query("USE ".$db_name);\r
51     }
52
53     public function desc_table($table_name){
54       $sql = "SELECT * from information_schema.columns where table_name='".$table_name."'";
55       $rst = $this->query($sql);
56       $desc = array(
57         "name" => $table_name,
58         "attributs" => array()
59       );
60       try{
61         while($v_rst = $this->fetch_assoc($rst)){\r
62           $desc["attributs"][$v_rst["COLUMN_NAME"]] = array(
63             "name" => $v_rst["COLUMN_NAME"],
64             "prymary_key" => $v_rst["COLUMN_KEY"] == "PRI" ? true : false,
65             "auto_increment" => $v_rst["EXTRA"] == "auto_increment" ? true : false
66           );
67         }\r
68         $this->free_result($rst);
69       }
70       catch(Exception $e){
71         throw new Exception($this->exception_out("Impossible de lire la description de la table"));
72       }
73       return $desc;
74     }
75
76     public 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("SHOW TABLES");
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     public 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     public 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     public 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     public 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     public 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     public function close(){
165       $this->link = null;
166       return true;
167     }
168
169     public function exception_out($message){\r
170       return "[erreur] mysql : ".$message;\r
171     }
172
173   }