nouveau module "models" dans l'environnement
[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 mw_mysql($host, $base, $user, $password){
17       $this->host = $host;
18       $this->base = $base;
19       $this->user = $user;
20       $this->password = $password;
21       $this->EXTENTION_OK = function_exists("mysql_connect");
22     }
23
24     function connect($host, $base, $user, $password){
25       $this->link = @mysql_connect($host, $user, $password);
26       if(!$this->link) throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur"));
27       @mysql_query("SET NAMES 'utf8'");
28       if($base){
29         $connected = @mysql_select_db($base, $this->link);
30         if(!$connected) throw new Exception($this->exception_out("Impossible de selectioner la base ".$base));
31       }
32       return true;
33     }
34
35     function select_db($db_name){\r
36       $this->base = $db_name;\r
37       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
38       return $this->query("USE ".$db_name);\r
39     }
40
41     function desc_table($table_name){
42       $sql = "SELECT * from information_schema.columns where table_name='".$table_name."'";
43       $rst = $this->query($sql);
44       $desc = array(
45         "name" => $table_name,
46         "attributs" => array()
47       );
48       try{
49         while($v_rst = $this->fetch_assoc($rst)){\r
50           $desc["attributs"][$v_rst["COLUMN_NAME"]] = array(
51             "name" => $v_rst["COLUMN_NAME"],
52             "prymary_key" => $v_rst["COLUMN_KEY"] == "PRI" ? true : false,
53             "auto_increment" => $v_rst["EXTRA"] == "auto_increment" ? true : false
54           );
55         }\r
56         $this->free_result($rst);
57       }
58       catch(Exception $e){
59         throw new Exception($this->exception_out("Impossible de lire la description de la table"));
60       }
61       return $desc;
62     }
63
64     function table_exists($table){\r
65       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
66       $rst = $this->query("SHOW TABLES");\r
67       while($v_rst = mysql_fetch_array($rst)){\r
68         if(strcmp($v_rst[0], $table) == 0) return true;\r
69       }\r
70       mysql_free_result($rst);\r
71       return false;\r
72     }
73
74     function field_exists($table_name, $field_name){
75       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
76       $sql = "SHOW COLUMNS FROM `".$table_name."` LIKE ".$field_name;
77       $rst = $this->query($sql);
78       $exists = false;
79       $v_rst = $this->fetch_assoc($rst);
80       if($v_rst) $exists = true;
81       $this->free_result($rst);
82       return $exists;
83     }
84
85     function query($query_string){\r
86       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
87       $result = @mysql_query($query_string, $this->link);\r
88       if(!$result) throw new Exception($this->exception_out("Syntaxe invalide dans une requete"));\r
89       return $result;\r
90     }
91
92     function fetch_assoc($rst){
93       if($this->link){
94         if($rst){
95           return mysql_fetch_assoc($rst);
96         }
97         else throw new Exception($this->exception_out("fetch_assoc sans result handler"));
98       }
99       else throw new Exception($this->exception_out("fetch_assoc sans connexion"));
100     }
101
102     function insert_id(){
103       if($this->link){
104         return mysql_insert_id($this->link);
105       }
106       else throw new Exception($this->exception_out("insert_id sans connexion"));
107     }
108
109     function free_result($rst){
110       if($this->link){
111         if($rst){
112           return mysql_free_result($rst);
113         }
114         else throw new Exception($this->exception_out("free_result sans result handler"));
115       }
116       else throw new Exception($this->exception_out("free_result sans connexion"));
117     }
118
119     function close(){
120       if($this->link) return mysql_close($this->link);
121       return true;
122     }
123
124     function exception_out($message){\r
125       return "[erreur] mysql : ".$message;\r
126     }
127
128   }
129
130 ?>