implementation sgbd herite de mw_sgbd
[mtweb] / mw / app / data / impl / mw_mysql.php
1 <?php
2
3   class mw_mysql extends mw_sgbd{
4
5     public function name(){
6       return "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 function_exists("mysql_connect");
20     }
21
22     public function authentication_required(){
23       return true;
24     }
25
26     public function connect($host, $base, $user, $password){
27       $this->link = @mysql_connect($host, $user, $password);
28       if(!$this->link) throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur"));
29       @mysql_query("SET NAMES 'utf8'");
30       if($base){
31         $connected = @mysql_select_db($base, $this->link);
32         if(!$connected) throw new Exception($this->exception_out("Impossible de selectioner la base ".$base));
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);\r
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 = mysql_fetch_row($rst)){\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;
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       $result = @mysql_query($query_string, $this->link);\r
107       if(!$result) throw new Exception($this->exception_out("Syntaxe invalide dans une requete"));\r
108       return $result;\r
109     }
110
111     public function fetch_assoc($rst){
112       if($this->link){
113         if($rst){
114           return mysql_fetch_assoc($rst);
115         }
116         else throw new Exception($this->exception_out("fetch_assoc sans result handler"));
117       }
118       else throw new Exception($this->exception_out("fetch_assoc sans connexion"));
119     }
120
121     public function insert_id(){
122       if($this->link){
123         return mysql_insert_id($this->link);
124       }
125       else throw new Exception($this->exception_out("insert_id sans connexion"));
126     }
127
128     public function free_result($rst){
129       if($this->link){
130         if($rst){
131           return mysql_free_result($rst);
132         }
133         else throw new Exception($this->exception_out("free_result sans result handler"));
134       }
135       else throw new Exception($this->exception_out("free_result sans connexion"));
136     }
137
138     public function close(){
139       if($this->link) return mysql_close($this->link);
140       return true;
141     }
142
143     public function exception_out($message){\r
144       return "[erreur] mysql : ".$message;\r
145     }
146
147   }