--- /dev/null
+<?php
+
+ class mw_mysql extends mw_sgbd{
+
+ public function name(){
+ return "MySql";
+ }
+
+ public function default_params(){
+ return array(
+ "host" => "localhost",
+ "base" => "mtweb",
+ "user" => "",
+ "password" => ""
+ );
+ }
+
+ public function validate_extention(){
+ return function_exists("mysql_connect");
+ }
+
+ public function authentication_required(){
+ return true;
+ }
+
+ public function connect($host, $base, $user, $password){
+ $this->link = @mysql_connect($host, $user, $password);
+ if(!$this->link) throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur"));
+ @mysql_query("SET NAMES 'utf8'");
+ if($base){
+ $connected = @mysql_select_db($base, $this->link);
+ if(!$connected) throw new Exception($this->exception_out("Impossible de selectioner la base ".$base));
+ }
+ return true;
+ }
+
+ public function select_db($db_name){\r
+ $this->base = $db_name;\r
+ if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
+ return $this->query("USE ".$db_name);\r
+ }
+
+ public function desc_table($table_name){
+ $table_name = $this->replace_prefixes($table_name);
+ $sql = "SELECT * from information_schema.columns where table_name='".$table_name."'";
+ $rst = $this->query($sql);
+ $desc = array(
+ "name" => $table_name,
+ "attributs" => array()
+ );
+ try{
+ while($v_rst = $this->fetch_assoc($rst)){\r
+ $desc["attributs"][$v_rst["COLUMN_NAME"]] = array(
+ "name" => $v_rst["COLUMN_NAME"],
+ "prymary_key" => $v_rst["COLUMN_KEY"] == "PRI" ? true : false,
+ "auto_increment" => $v_rst["EXTRA"] == "auto_increment" ? true : false
+ );
+ }\r
+ $this->free_result($rst);
+ }
+ catch(Exception $e){
+ throw new Exception($this->exception_out("Impossible de lire la description de la table"));
+ }
+ return $desc;
+ }
+
+ public function table_exists($table_name){
+ $table_name = $this->replace_prefixes($table_name);\r
+ if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
+ $EXISTS = false;
+ try{
+ $rst = $this->query("SHOW TABLES");
+ while($v_rst = mysql_fetch_row($rst)){\r
+ if($v_rst[0] == $table_name){
+ $EXISTS = true;
+ break;
+ }\r
+ }\r
+ $this->free_result($rst);
+ }
+ catch(Exception $e){
+ throw new Exception($this->exception_out("Impossible de savoir si la table existe"));
+ }
+ return $EXISTS;
+ }
+
+ public function field_exists($table_name, $field_name){
+ $table_name = $this->replace_prefixes($table_name);
+ if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
+ if(!($desc = $this->desc_table($table_name))){
+ throw new Exception($this->exception_out("Impossible de lire la description de la table"));
+ }
+ $EXISTS = false;
+ foreach($desc["attributs"] as $attribut_name => $attribut){
+ if($field_name == $attribut_name){
+ $EXISTS = true;
+ break;
+ }
+ }
+ return $EXISTS;
+ }
+
+ public function query($query_string){
+ $query_string = $this->replace_prefixes($query_string);\r
+ if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
+ $result = @mysql_query($query_string, $this->link);\r
+ if(!$result) throw new Exception($this->exception_out("Syntaxe invalide dans une requete"));\r
+ return $result;\r
+ }
+
+ public function fetch_assoc($rst){
+ if($this->link){
+ if($rst){
+ return mysql_fetch_assoc($rst);
+ }
+ else throw new Exception($this->exception_out("fetch_assoc sans result handler"));
+ }
+ else throw new Exception($this->exception_out("fetch_assoc sans connexion"));
+ }
+
+ public function insert_id(){
+ if($this->link){
+ return mysql_insert_id($this->link);
+ }
+ else throw new Exception($this->exception_out("insert_id sans connexion"));
+ }
+
+ public function free_result($rst){
+ if($this->link){
+ if($rst){
+ return mysql_free_result($rst);
+ }
+ else throw new Exception($this->exception_out("free_result sans result handler"));
+ }
+ else throw new Exception($this->exception_out("free_result sans connexion"));
+ }
+
+ public function close(){
+ if($this->link) return mysql_close($this->link);
+ return true;
+ }
+
+ public function exception_out($message){\r
+ return "[erreur] mysql : ".$message;\r
+ }
+
+ }