From 280617b547f0480454690737e67bd95901e10656 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Wed, 18 Jun 2014 09:10:18 +0000 Subject: [PATCH] Add support to list of tables in dblayer for the 3 PDO engines Add support to connect database git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1469 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- dblayer.php | 66 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 7 deletions(-) diff --git a/dblayer.php b/dblayer.php index 88f39b8..f82270e 100644 --- a/dblayer.php +++ b/dblayer.php @@ -52,14 +52,65 @@ class dblayer extends PDO public function verifyAll ($datas) {} /** Debug of the SQL */ public $debug = FALSE; - /** Return all the tables available in the database */ - function listTables () + /** The connecting DSN */ + private $dsn = null; + + /** Return the connected database name from DSN used to connect */ + public function databasename () { - $driver = $this->getAttribute (PDO::ATTR_DRIVER_NAME); - $rc = @include_once ("dbLayer".ucfirst ($driver).".php"); - if ($rc === FALSE) - throw new Exception (sprintf (_("dbLayer driver %s not available"), - $driver), 500); + if ($this->db === null) + throw new Exception ("Database not connected"); + $vals = explode (";", substr (strstr ($this->dsn, ":"), 1)); + $dsnExplode = array (); + foreach ($vals as $val) + { + @list ($k, $v) = explode ("=", $val); + $dsnExplode[$k] = $v; + } + if (isset ($dsnExplode["dbname"])) + return $dsnExplode["dbname"]; + return NULL; + } + + /** Return all the tables available in the database */ + public function listTables () + { + if ($this->db === null) + throw new Exception ("Database not connected"); + switch ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME)) + { + case "sqlite": + $req = "SELECT name FROM sqlite_master WHERE type='table'"; + $st = $this->db->prepare ($req); + $st->execute (); + $res = array (); + while ($d = $st->fetch (PDO::FETCH_ASSOC)) + $res[] = $d["name"]; + break; + case "mysql": + $req = "SELECT TABLE_NAME + FROM information_schema.tables + WHERE TABLE_SCHEMA='".$this->databasename()."'"; + $st = $this->db->prepare ($req); + $st->execute (); + $res = array (); + while ($d = $st->fetch (PDO::FETCH_ASSOC)) + $res[] = $d["TABLE_NAME"]; + break; + case "pgsql": + $req = "SELECT * + FROM pg_tables + WHERE schemaname = 'public'"; + $st = $this->db->prepare ($req); + $st->execute (); + $res = array (); + while ($d = $st->fetch (PDO::FETCH_ASSOC)) + $res[] = $d["tablename"]; + break; + default: + throw new Exception (_("Unknown database driver in listTables")); + } + return $res; } // TODO !! @@ -114,6 +165,7 @@ class dblayer extends PDO $this->db->exec("PRAGMA foreign_keys = ON"); break; } + $this->dsn = $dsn; } /** Create a new entry in the table. Datas must be an indexed array