3 class mw_pdo_mysql extends mw_sgbd{
5 public function name(){
9 public function default_params(){
11 "host" => "localhost",
18 public function validate_extention(){
19 return extension_loaded("pdo") && extension_loaded("pdo_mysql");
22 public function authentication_required(){
26 public function connect($host, $base, $user, $password){
28 $this->link = new PDO("mysql:host=".$host.";dbname=".$base, $user, $password);
29 $this->link->query("SET NAMES 'utf8'");
31 catch(PDOException $e){
32 throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur"));
37 public function select_db($db_name){
\r
38 $this->base = $db_name;
\r
39 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
40 return $this->query("USE ".$db_name);
\r
43 public function desc_table($table_name){
44 $table_name = $this->replace_prefixes($table_name);
45 $sql = "SELECT * from information_schema.columns where table_name='".$table_name."'";
46 $rst = $this->query($sql);
48 "name" => $table_name,
49 "attributs" => array()
52 while($v_rst = $this->fetch_assoc($rst)){
\r
53 $desc["attributs"][$v_rst["COLUMN_NAME"]] = array(
54 "name" => $v_rst["COLUMN_NAME"],
55 "prymary_key" => $v_rst["COLUMN_KEY"] == "PRI" ? true : false,
56 "auto_increment" => $v_rst["EXTRA"] == "auto_increment" ? true : false
59 $this->free_result($rst);
62 throw new Exception($this->exception_out("Impossible de lire la description de la table"));
67 public function table_exists($table_name){
68 $table_name = $this->replace_prefixes($table_name);
69 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
72 $rst = $this->query("SHOW TABLES");
73 while($v_rst = $rst->fetch()){
\r
74 if($v_rst[0] == $table_name){
79 $this->free_result($rst);
82 throw new Exception($this->exception_out("Impossible de savoir si la table existe"));
87 public function field_exists($table_name, $field_name){
88 $table_name = $this->replace_prefixes($table_name);
89 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
90 if(!($desc = $this->desc_table($table_name))){
91 throw new Exception($this->exception_out("Impossible de lire la description de la table"));
94 foreach($desc["attributs"] as $attribut_name => $attribut){
95 if($field_name == $attribut_name){
103 public function query($query_string){
104 $query_string = $this->replace_prefixes($query_string);
\r
105 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
106 if(!($result = $this->link->query($query_string))){
107 throw new Exception($this->exception_out("Syntaxe invalide dans une requete"));
112 public function fetch_assoc($rst){
114 throw new Exception($this->exception_out("fetch_assoc sans connexion"));
117 throw new Exception($this->exception_out("fetch_assoc sans result handler"));
120 $tuple = $rst->fetch(PDO::FETCH_ASSOC);
123 throw new Exception($this->exception_out("erreur fetch_assoc"));
128 public function insert_id(){
130 throw new Exception($this->exception_out("insert_id sans connexion"));
133 $id = $this->link->lastInsertId();
136 throw new Exception($this->exception_out("erreur insert_id"));
141 public function free_result($rst){
143 throw new Exception($this->exception_out("free_result sans connexion"));
146 throw new Exception($this->exception_out("free_result sans result handler"));
153 throw new Exception($this->exception_out("erreur free_result"));
158 public function close(){
163 public function exception_out($message){
\r
164 return "[erreur] mysql : ".$message;
\r