18 function extention_ok(&$env) { return $this->EXTENTION_OK; }
20 function authentication_required() { return true; }
22 function sgbd_name() { return "PDO MySql"; }
24 function mw_pdo_mysql($params = array()){
25 $this->host = isset($params["host"]) ? $params["host"] : "localhost";
26 $this->base = isset($params["base"]) ? $params["base"] : "mtweb";
27 $this->user = isset($params["user"]) ? $params["user"] : "";
28 $this->password = isset($params["password"]) ? $params["password"] : "";
29 $this->EXTENTION_OK = (extension_loaded("pdo") && extension_loaded("pdo_mysql"));
32 function connect($host, $base, $user, $password){
34 $this->link = new PDO("mysql:host=".$host.";dbname=".$base, $user, $password);
35 $this->link->query("SET NAMES 'utf8'");
37 catch(PDOException $e){
38 throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur"));
43 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
49 function desc_table($table_name){
50 $sql = "SELECT * from information_schema.columns where table_name='".$table_name."'";
51 $rst = $this->query($sql);
53 "name" => $table_name,
54 "attributs" => array()
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
64 $this->free_result($rst);
67 throw new Exception($this->exception_out("Impossible de lire la description de la table"));
72 function table_exists($table_name){
73 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
76 $rst = $this->query("SHOW TABLES");
77 while($v_rst = $rst->fetch()){
\r
78 if($v_rst[0] == $table_name){
83 $this->free_result($rst);
86 throw new Exception($this->exception_out("Impossible de savoir si la table existe"));
91 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"));
97 foreach($desc["attributs"] as $attribut_name => $attribut){
98 if($field_name == $attribut_name){
106 function query($query_string){
\r
107 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
108 if(!($result = $this->link->query($query_string))){
109 throw new Exception($this->exception_out("Syntaxe invalide dans une requete"));
114 function fetch_assoc($rst){
116 throw new Exception($this->exception_out("fetch_assoc sans connexion"));
119 throw new Exception($this->exception_out("fetch_assoc sans result handler"));
122 $tuple = $rst->fetch(PDO::FETCH_ASSOC);
125 throw new Exception($this->exception_out("erreur fetch_assoc"));
130 function insert_id(){
132 throw new Exception($this->exception_out("insert_id sans connexion"));
135 $id = $this->link->lastInsertId();
138 throw new Exception($this->exception_out("erreur insert_id"));
143 function free_result($rst){
145 throw new Exception($this->exception_out("free_result sans connexion"));
148 throw new Exception($this->exception_out("free_result sans result handler"));
155 throw new Exception($this->exception_out("erreur free_result"));
165 function exception_out($message){
\r
166 return "[erreur] mysql : ".$message;
\r