X-Git-Url: http://git.dj3c1t.com/?a=blobdiff_plain;f=mw%2Fapp%2Fdata%2Fimpl%2Fmw_pdo_mysql.php;fp=mw%2Fapp%2Fdata%2Fimpl%2Fmw_pdo_mysql.php;h=283e50f334e24524a379176105fb28b086fa701d;hb=422d883e3ed8ee55ee41e3b7826f32b79cea646d;hp=0000000000000000000000000000000000000000;hpb=e1b64e4088232b9d7b4acb2dc24279bb38fcafba;p=mtweb diff --git a/mw/app/data/impl/mw_pdo_mysql.php b/mw/app/data/impl/mw_pdo_mysql.php new file mode 100644 index 0000000..283e50f --- /dev/null +++ b/mw/app/data/impl/mw_pdo_mysql.php @@ -0,0 +1,167 @@ +link; + } + + function extention_ok(&$env) { return $this->EXTENTION_OK; } + + function mw_pdo_mysql($host, $base, $user, $password){ + $this->host = $host; + $this->base = $base; + $this->user = $user; + $this->password = $password; + $this->EXTENTION_OK = (extension_loaded("pdo") && extension_loaded("pdo_mysql")); + } + + function connect($host, $base, $user, $password){ + try{ + $this->link = new PDO("mysql:host=".$host.";dbname=".$base, $user, $password); + $this->link->query("SET NAMES 'utf8'"); + } + catch(PDOException $e){ + throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur")); + } + return true; + } + + function select_db($db_name){ + $this->base = $db_name; + if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password); + return $this->query("USE ".$db_name); + } + + function desc_table($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)){ + $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 + ); + } + $this->free_result($rst); + } + catch(Exception $e){ + throw new Exception($this->exception_out("Impossible de lire la description de la table")); + } + return $desc; + } + + function table_exists($table_name){ + if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password); + $EXISTS = false; + try{ + $rst = $this->query("SHOW TABLES"); + while($v_rst = $rst->fetch()){ + if($v_rst[0] == $table_name){ + $EXISTS = true; + break; + } + } + $this->free_result($rst); + } + catch(Exception $e){ + throw new Exception($this->exception_out("Impossible de savoir si la table existe")); + } + return $EXISTS; + } + + function field_exists($table_name, $field_name){ + if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password); + 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; + } + + function query($query_string){ + if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password); + if(!($result = $this->link->query($query_string))){ + throw new Exception($this->exception_out("Syntaxe invalide dans une requete")); + } + return $result; + } + + function fetch_assoc($rst){ + if(!$this->link){ + throw new Exception($this->exception_out("fetch_assoc sans connexion")); + } + if(!$rst){ + throw new Exception($this->exception_out("fetch_assoc sans result handler")); + } + try{ + $tuple = $rst->fetch(PDO::FETCH_ASSOC); + } + catch(Exception $e){ + throw new Exception($this->exception_out("erreur fetch_assoc")); + } + return $tuple; + } + + function insert_id(){ + if(!$this->link){ + throw new Exception($this->exception_out("insert_id sans connexion")); + } + try{ + $id = $this->link->lastInsertId(); + } + catch(Exception $e){ + throw new Exception($this->exception_out("erreur insert_id")); + } + return $id; + } + + function free_result($rst){ + if(!$this->link){ + throw new Exception($this->exception_out("free_result sans connexion")); + } + if(!$rst){ + throw new Exception($this->exception_out("free_result sans result handler")); + } + try{ + $rst->closeCursor(); + $rst = null; + } + catch(Exception $e){ + throw new Exception($this->exception_out("erreur free_result")); + } + return true; + } + + function close(){ + $this->link = null; + return true; + } + + function exception_out($message){ + return "[erreur] mysql : ".$message; + } + + } + +?> \ No newline at end of file