public attr / function, constructeurs __construct
[mtweb] / mw / app / data / impl / mw_mysql.php
1 <?php
2
3   class mw_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 = function_exists("mysql_connect");
18     }
19
20     public function extention_ok(&$env){
21       return $this->EXTENTION_OK;
22     }
23
24     public function authentication_required(){
25       return true;
26     }
27
28     public function sgbd_name(){
29       return "MySql";
30     }
31
32     public function connect($host, $base, $user, $password){
33       $this->link = @mysql_connect($host, $user, $password);
34       if(!$this->link) throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur"));
35       @mysql_query("SET NAMES 'utf8'");
36       if($base){
37         $connected = @mysql_select_db($base, $this->link);
38         if(!$connected) throw new Exception($this->exception_out("Impossible de selectioner la base ".$base));
39       }
40       return true;
41     }
42
43     public function select_db($db_name){\r
44       $this->base = $db_name;\r
45       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
46       return $this->query("USE ".$db_name);\r
47     }
48
49     public function desc_table($table_name){
50       $sql = "SELECT * from information_schema.columns where table_name='".$table_name."'";
51       $rst = $this->query($sql);
52       $desc = array(
53         "name" => $table_name,
54         "attributs" => array()
55       );
56       try{
57         while($v_rst = $this->fetch_assoc($rst)){\r
58           $desc["attributs"][$v_rst["COLUMN_NAME"]] = array(
59             "name" => $v_rst["COLUMN_NAME"],
60             "prymary_key" => $v_rst["COLUMN_KEY"] == "PRI" ? true : false,
61             "auto_increment" => $v_rst["EXTRA"] == "auto_increment" ? true : false
62           );
63         }\r
64         $this->free_result($rst);
65       }
66       catch(Exception $e){
67         throw new Exception($this->exception_out("Impossible de lire la description de la table"));
68       }
69       return $desc;
70     }
71
72     public function table_exists($table_name){\r
73       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
74       $EXISTS = false;
75       try{
76         $rst = $this->query("SHOW TABLES");
77         while($v_rst = mysql_fetch_row($rst)){\r
78           if($v_rst[0] == $table_name){
79             $EXISTS = true;
80             break;
81           }\r
82         }\r
83         $this->free_result($rst);
84       }
85       catch(Exception $e){
86         throw new Exception($this->exception_out("Impossible de savoir si la table existe"));
87       }
88       return $EXISTS;
89     }
90
91     public function field_exists($table_name, $field_name){
92       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
93       if(!($desc = $this->desc_table($table_name))){
94         throw new Exception($this->exception_out("Impossible de lire la description de la table"));
95       }
96       $EXISTS = false;
97       foreach($desc["attributs"] as $attribut_name => $attribut){
98         if($field_name == $attribut_name){
99           $EXISTS = true;
100           break;
101         }
102       }
103       return $EXISTS;
104     }
105
106     public function query($query_string){\r
107       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
108       $result = @mysql_query($query_string, $this->link);\r
109       if(!$result) throw new Exception($this->exception_out("Syntaxe invalide dans une requete"));\r
110       return $result;\r
111     }
112
113     public function fetch_assoc($rst){
114       if($this->link){
115         if($rst){
116           return mysql_fetch_assoc($rst);
117         }
118         else throw new Exception($this->exception_out("fetch_assoc sans result handler"));
119       }
120       else throw new Exception($this->exception_out("fetch_assoc sans connexion"));
121     }
122
123     public function insert_id(){
124       if($this->link){
125         return mysql_insert_id($this->link);
126       }
127       else throw new Exception($this->exception_out("insert_id sans connexion"));
128     }
129
130     public function free_result($rst){
131       if($this->link){
132         if($rst){
133           return mysql_free_result($rst);
134         }
135         else throw new Exception($this->exception_out("free_result sans result handler"));
136       }
137       else throw new Exception($this->exception_out("free_result sans connexion"));
138     }
139
140     public function close(){
141       if($this->link) return mysql_close($this->link);
142       return true;
143     }
144
145     public function exception_out($message){\r
146       return "[erreur] mysql : ".$message;\r
147     }
148
149   }