public attr / function, constructeurs __construct
[mtweb] / mw / app / data / impl / mw_pdo_sqlite.php
1 <?php
2
3   class mw_pdo_sqlite{
4
5     public $link;
6     public $host;
7     public $base;
8     public $user;
9     public $password;
10     public $EXTENTION_OK;
11
12     public function __construct($params = array()){
13       $this->host = isset($params["host"]) ? $params["host"] : "content/data/sqlite";
14       $this->base = isset($params["base"]) ? $params["base"] : "mtweb.db";
15       $this->user = isset($params["user"]) ? $params["user"] : "";
16       $this->password = isset($params["password"]) ? $params["password"] : "";
17       $this->EXTENTION_OK = (extension_loaded("pdo") && extension_loaded("pdo_sqlite"));
18     }
19
20     public function get_link(){
21       return $this->link;
22     }
23
24     public function extention_ok(&$env){
25       return $this->EXTENTION_OK;
26     }
27
28     public function authentication_required(){
29       return false;
30     }
31
32     public function sgbd_name(){
33       return "PDO SQLite";
34     }
35
36     public function connect($host, $base, $user, $password){
37       if($host) $host .= substr($host, -1) != "/" ? "/" : "";
38       try{
39         $this->link = null;
40         $this->link = new PDO("sqlite:".$host.$base);
41         $this->link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
42         $this->link->query("PRAGMA encoding = 'UTF-8'");
43       }
44       catch(PDOException $e){
45         throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur"));
46       }
47       return true;
48     }
49
50     public function select_db($db_name){\r
51       $this->base = $db_name;\r
52       return $this->connect($this->host, $this->base, $this->user, $this->password);\r
53     }
54
55     public function desc_table($table_name){
56       if(strpos($table_name, "'") !== false){
57         throw new Exception($this->exception_out("nom de table avec un simple quote"));
58       }
59       $desc = array(
60         "name" => $table_name,
61         "attributs" => array()
62       );
63       $sql = "PRAGMA table_info(".$table_name.")";
64       try{
65         $rst = $this->query($sql);
66         while($v_rst = $this->fetch_assoc($rst)){
67           $desc["attributs"][$v_rst["name"]] = array(
68             "name" => $v_rst["name"],
69             "prymary_key" => isset($v_rst["pk"]) && $v_rst["pk"] ? true : false
70           );
71         }
72         $this->free_result($rst);
73       }
74       catch(Exception $e){
75         throw new Exception($this->exception_out("impossible de lire les champs de la table ".$table_name));
76       }
77       return $desc;
78     }
79
80     public function table_exists($table_name){
81       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
82       $EXISTS = false;
83       try{
84         $rst = $this->query("SELECT name FROM sqlite_master WHERE type='table'");
85         while($v_rst = $rst->fetch()){\r
86           if($v_rst[0] == $table_name){
87             $EXISTS = true;
88             break;
89           }\r
90         }\r
91         $this->free_result($rst);
92       }
93       catch(Exception $e){
94         throw new Exception($this->exception_out("Impossible de savoir si la table existe"));
95       }
96       return $EXISTS;\r
97     }
98
99     public function field_exists($table_name, $field_name){
100       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
101       if(!($desc = $this->desc_table($table_name))){
102         throw new Exception($this->exception_out("Impossible de lire la description de la table"));
103       }
104       $EXISTS = false;
105       foreach($desc["attributs"] as $attribut_name => $attribut){
106         if($field_name == $attribut_name){
107           $EXISTS = true;
108           break;
109         }
110       }
111       return $EXISTS;
112     }
113
114     public function query($query_string){\r
115       if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);\r
116       if(!($result = $this->link->query($query_string))){
117         throw new Exception($this->exception_out("Syntaxe invalide dans une requete"));
118       }
119       return $result;\r
120     }
121
122     public function fetch_assoc($rst){
123       if(!$this->link){
124         throw new Exception($this->exception_out("fetch_assoc sans connexion"));
125       }
126       if(!$rst){
127         throw new Exception($this->exception_out("fetch_assoc sans result handler"));
128       }
129       try{
130         $tuple = $rst->fetch(PDO::FETCH_ASSOC);
131       }
132       catch(Exception $e){
133         throw new Exception($this->exception_out("erreur fetch_assoc"));
134       }
135       return $tuple;
136     }
137
138     public function insert_id(){
139       if(!$this->link){
140         throw new Exception($this->exception_out("insert_id sans connexion"));
141       }
142       try{
143         $id = $this->link->lastInsertId();
144       }
145       catch(Exception $e){
146         throw new Exception($this->exception_out("erreur insert_id"));
147       }
148       return $id;
149     }
150
151     public function free_result($rst){
152       if(!$this->link){
153         throw new Exception($this->exception_out("free_result sans connexion"));
154       }
155       if(!$rst){
156         throw new Exception($this->exception_out("free_result sans result handler"));
157       }
158       try{
159         $rst->closeCursor();
160         $rst = null;
161       }
162       catch(Exception $e){
163         throw new Exception($this->exception_out("erreur free_result"));
164       }
165       return true;
166     }
167
168     public function close(){
169       $this->link = null;
170       return true;
171     }
172
173     public function exception_out($message){\r
174       return "[erreur] sqlite : ".$message;\r
175     }
176
177   }