3 class mw_pdo_sqlite extends mw_sgbd{
5 public function name(){
9 public function default_params(){
11 "host" => "content/data/sqlite",
18 public function validate_extention(){
19 return extension_loaded("pdo") && extension_loaded("pdo_sqlite");
22 public function authentication_required(){
26 public function connect($host, $base, $user, $password){
27 if($host) $host .= substr($host, -1) != "/" ? "/" : "";
30 $this->link = new PDO("sqlite:".$host.$base);
31 $this->link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
32 $this->link->query("PRAGMA encoding = 'UTF-8'");
34 catch(PDOException $e){
35 throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur"));
40 public function select_db($db_name){
\r
41 $this->base = $db_name;
\r
42 return $this->connect($this->host, $this->base, $this->user, $this->password);
\r
45 public function desc_table($table_name){
46 $table_name = $this->replace_prefixes($table_name);
47 if(strpos($table_name, "'") !== false){
48 throw new Exception($this->exception_out("nom de table avec un simple quote"));
51 "name" => $table_name,
52 "attributs" => array()
54 $sql = "PRAGMA table_info(".$table_name.")";
56 $rst = $this->query($sql);
57 while($v_rst = $this->fetch_assoc($rst)){
58 $desc["attributs"][$v_rst["name"]] = array(
59 "name" => $v_rst["name"],
60 "prymary_key" => isset($v_rst["pk"]) && $v_rst["pk"] ? true : false
63 $this->free_result($rst);
66 throw new Exception($this->exception_out("impossible de lire les champs de la table ".$table_name));
71 public function table_exists($table_name){
72 $table_name = $this->replace_prefixes($table_name);
73 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
76 $rst = $this->query("SELECT name FROM sqlite_master WHERE type='table'");
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 public function field_exists($table_name, $field_name){
92 $table_name = $this->replace_prefixes($table_name);
93 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
94 if(!($desc = $this->desc_table($table_name))){
95 throw new Exception($this->exception_out("Impossible de lire la description de la table"));
98 foreach($desc["attributs"] as $attribut_name => $attribut){
99 if($field_name == $attribut_name){
107 public function query($query_string){
108 $query_string = $this->replace_prefixes($query_string);
\r
109 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
110 if(!($result = $this->link->query($query_string))){
111 throw new Exception($this->exception_out("Syntaxe invalide dans une requete"));
116 public function fetch_assoc($rst){
118 throw new Exception($this->exception_out("fetch_assoc sans connexion"));
121 throw new Exception($this->exception_out("fetch_assoc sans result handler"));
124 $tuple = $rst->fetch(PDO::FETCH_ASSOC);
127 throw new Exception($this->exception_out("erreur fetch_assoc"));
132 public function insert_id(){
134 throw new Exception($this->exception_out("insert_id sans connexion"));
137 $id = $this->link->lastInsertId();
140 throw new Exception($this->exception_out("erreur insert_id"));
145 public function free_result($rst){
147 throw new Exception($this->exception_out("free_result sans connexion"));
150 throw new Exception($this->exception_out("free_result sans result handler"));
157 throw new Exception($this->exception_out("erreur free_result"));
162 public function close(){
167 public function exception_out($message){
\r
168 return "[erreur] sqlite : ".$message;
\r