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