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
This commit is contained in:
2014-06-18 09:10:18 +00:00
parent 9e2da45514
commit 280617b547

View File

@@ -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