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"));
20 public function get_link(){
24 public function extention_ok(&$env){
25 return $this->EXTENTION_OK;
28 public function authentication_required(){
32 public function sgbd_name(){
36 public function connect($host, $base, $user, $password){
37 if($host) $host .= substr($host, -1) != "/" ? "/" : "";
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'");
44 catch(PDOException $e){
45 throw new Exception($this->exception_out("Impossible d'etablir une connection au serveur"));
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
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"));
60 "name" => $table_name,
61 "attributs" => array()
63 $sql = "PRAGMA table_info(".$table_name.")";
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
72 $this->free_result($rst);
75 throw new Exception($this->exception_out("impossible de lire les champs de la table ".$table_name));
80 public function table_exists($table_name){
81 if(!$this->link) $this->connect($this->host, $this->base, $this->user, $this->password);
\r
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){
91 $this->free_result($rst);
94 throw new Exception($this->exception_out("Impossible de savoir si la table existe"));
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"));
105 foreach($desc["attributs"] as $attribut_name => $attribut){
106 if($field_name == $attribut_name){
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"));
122 public function fetch_assoc($rst){
124 throw new Exception($this->exception_out("fetch_assoc sans connexion"));
127 throw new Exception($this->exception_out("fetch_assoc sans result handler"));
130 $tuple = $rst->fetch(PDO::FETCH_ASSOC);
133 throw new Exception($this->exception_out("erreur fetch_assoc"));
138 public function insert_id(){
140 throw new Exception($this->exception_out("insert_id sans connexion"));
143 $id = $this->link->lastInsertId();
146 throw new Exception($this->exception_out("erreur insert_id"));
151 public function free_result($rst){
153 throw new Exception($this->exception_out("free_result sans connexion"));
156 throw new Exception($this->exception_out("free_result sans result handler"));
163 throw new Exception($this->exception_out("erreur free_result"));
168 public function close(){
173 public function exception_out($message){
\r
174 return "[erreur] sqlite : ".$message;
\r