ajout d'un installeur en ligne
[mtweb] / mw / app / data / impl / mw_mysql.php
1 <?php
2
3   class mw_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 extention_ok(&$env) { return $this->EXTENTION_OK; }
15
16     function authentication_required() { return true; }
17
18     function sgbd_name() { return "MySql"; }
19
20     function mw_mysql($params = array()){
21       $this->host = isset($params["host"]) ? $params["host"] : "localhost";
22       $this->base = isset($params["base"]) ? $params["base"] : "mtweb";
23       $this->user = isset($params["user"]) ? $params["user"] : "";
24       $this->password = isset($params["password"]) ? $params["password"] : "";
25       $this->EXTENTION_OK = function_exists("mysql_connect");
26     }
27
28     function connect($host, $base, $user, $password){
29       $this->link = @mysql_connect($host, $user, $password);
30       if(!$this->link) throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur"));
31       @mysql_query("SET NAMES 'utf8'");
32       if($base){
33         $connected = @mysql_select_db($base, $this->link);
34         if(!$connected) throw new Exception($this->exception_out("Impossible de selectioner la base ".$base));
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){\r
69       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
70       $rst = $this->query("SHOW TABLES");\r
71       while($v_rst = mysql_fetch_array($rst)){\r
72         if(strcmp($v_rst[0], $table) == 0) return true;\r
73       }\r
74       mysql_free_result($rst);\r
75       return false;\r
76     }
77
78     function field_exists($table_name, $field_name){
79       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
80       $sql = "SHOW COLUMNS FROM `".$table_name."` LIKE ".$field_name;
81       $rst = $this->query($sql);
82       $exists = false;
83       $v_rst = $this->fetch_assoc($rst);
84       if($v_rst) $exists = true;
85       $this->free_result($rst);
86       return $exists;
87     }
88
89     function query($query_string){\r
90       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
91       $result = @mysql_query($query_string, $this->link);\r
92       if(!$result) throw new Exception($this->exception_out("Syntaxe invalide dans une requete"));\r
93       return $result;\r
94     }
95
96     function fetch_assoc($rst){
97       if($this->link){
98         if($rst){
99           return mysql_fetch_assoc($rst);
100         }
101         else throw new Exception($this->exception_out("fetch_assoc sans result handler"));
102       }
103       else throw new Exception($this->exception_out("fetch_assoc sans connexion"));
104     }
105
106     function insert_id(){
107       if($this->link){
108         return mysql_insert_id($this->link);
109       }
110       else throw new Exception($this->exception_out("insert_id sans connexion"));
111     }
112
113     function free_result($rst){
114       if($this->link){
115         if($rst){
116           return mysql_free_result($rst);
117         }
118         else throw new Exception($this->exception_out("free_result sans result handler"));
119       }
120       else throw new Exception($this->exception_out("free_result sans connexion"));
121     }
122
123     function close(){
124       if($this->link) return mysql_close($this->link);
125       return true;
126     }
127
128     function exception_out($message){\r
129       return "[erreur] mysql : ".$message;\r
130     }
131
132   }
133
134 ?>