dbLayer : new format with static call to limit the number of connection to the database

authzgroups : update to support the new dbLayer format


git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1950 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2015-01-27 17:38:25 +00:00
parent 4f23451432
commit e49a5a97af
2 changed files with 44 additions and 35 deletions

View File

@@ -54,7 +54,7 @@ class authzgroups
if ($this->debug) echo "$req\n";
try
{
$st = $this->dbObject->db->prepare ($req);
$st = $this->dbObject->prepare ($req);
}
catch (Exception $e)
{

View File

@@ -101,23 +101,27 @@ class dblayer extends PDO
if (! isset ($driver[0]))
throw new Exception (_("No valid DSN provided"), 500);
// Force specifics initialisations
$oldInst = self::getInstance($dsn, $username);
/* $oldInst = self::getInstance($dsn, $username);
if ($oldInst !== null)
{
$this->db = $oldInst["db"];
$this = $oldInst["db"];
$this->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->exec("PRAGMA foreign_keys = ON");
$this->sep = $oldInst["sep"];
$this->dsn = $oldInst["dsn"];
echo "OLD\n";
return;
}
echo "NEW\n";*/
switch ($driver[0])
{
case "sqlite":
try
{
$this->db = new PDO ($dsn, $username, $password, $driver_options);
$this->db->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
parent::__construct ($dsn, $username, $password, $driver_options);
parent::setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (Exception $e)
{
@@ -138,15 +142,15 @@ class dblayer extends PDO
fileowner ($file) === posix_getuid ())
chmod ($file, 0666);
// Force ForeignKeys support (disabled by default)
$this->db->exec("PRAGMA foreign_keys = ON");
$this->exec("PRAGMA foreign_keys = ON");
$this->sep = "`";
break;
case "mysql":
try
{
$driver_options[PDO::MYSQL_ATTR_FOUND_ROWS] = 1;
$this->db = new PDO ($dsn, $username, $password, $driver_options);
$this->db->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
parent::__construct ($dsn, $username, $password, $driver_options);
parent::setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (Exception $e)
{
@@ -154,14 +158,14 @@ class dblayer extends PDO
}
// Set the coding to UTF8
$this->db->exec("SET CHARACTER SET utf8");
$this->exec("SET CHARACTER SET utf8");
$this->sep = "`";
break;
case "pgsql":
try
{
$this->db = new PDO ($dsn, $username, $password, $driver_options);
$this->db->setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
parent::__construct ($dsn, $username, $password, $driver_options);
parent::setAttribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (Exception $e)
{
@@ -169,12 +173,12 @@ class dblayer extends PDO
}
// Set the coding to UTF8
$this->db->exec("SET NAMES 'utf8'");
$this->exec("SET NAMES 'utf8'");
$this->sep = "\"";
break;
}
$this->dsn = $dsn;
self::$instance["$dsn-$username"]["db"] = $this->db;
self::$instance["$dsn-$username"]["db"] = $this;
self::$instance["$dsn-$username"]["dsn"] = $this->dsn;
self::$instance["$dsn-$username"]["sep"] = $this->sep;
}
@@ -193,7 +197,7 @@ class dblayer extends PDO
/** Return the connected database name from DSN used to connect */
public function databasename ()
{
if ($this->db === null)
if ($this === null)
throw new Exception (dgettext("domframework", "Database not connected"),
500);
$vals = explode (";", substr (strstr ($this->dsn, ":"), 1));
@@ -211,14 +215,14 @@ class dblayer extends PDO
/** Return all the tables available in the database */
public function listTables ()
{
if ($this->db === null)
if ($this === null)
throw new Exception (dgettext("domframework", "Database not connected"),
500);
switch ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME))
switch ($this->getAttribute(PDO::ATTR_DRIVER_NAME))
{
case "sqlite":
$req = "SELECT name FROM sqlite_master WHERE type='table'";
$st = $this->db->prepare ($req);
$st = $this->prepare ($req);
$st->execute ();
$res = array ();
while ($d = $st->fetch (PDO::FETCH_ASSOC))
@@ -228,7 +232,7 @@ class dblayer extends PDO
$req = "SELECT TABLE_NAME
FROM information_schema.tables
WHERE TABLE_SCHEMA='".$this->databasename()."'";
$st = $this->db->prepare ($req);
$st = $this->prepare ($req);
$st->execute ();
$res = array ();
while ($d = $st->fetch (PDO::FETCH_ASSOC))
@@ -238,7 +242,7 @@ class dblayer extends PDO
$req = "SELECT *
FROM pg_tables
WHERE schemaname = 'public'";
$st = $this->db->prepare ($req);
$st = $this->prepare ($req);
$st->execute ();
$res = array ();
while ($d = $st->fetch (PDO::FETCH_ASSOC))
@@ -492,7 +496,7 @@ class dblayer extends PDO
"FROM $this->sep$this->tableprefix$table$this->sep ".
"WHERE $this->sep$column$this->sep=:".md5 ($column);
if ($this->debug) echo "DEBUG : $req\n";
$st = $this->db->prepare ($req);
$st = $this->prepare ($req);
$val = $datas[$foreign];
$key = $column;
if ($this->debug) echo "DEBUG BIND : ".$this->fields[$foreign][0]."\n";
@@ -541,7 +545,7 @@ class dblayer extends PDO
public function insert ($datas)
{
if ($this->debug) echo "== Entering insert\n";
if ($this->db === null)
if ($this === null)
throw new Exception (dgettext("domframework", "Database not connected"),
500);
if ($this->unique === null)
@@ -584,7 +588,7 @@ class dblayer extends PDO
$req .= " VALUES ";
$req .= "(:".implode (",:", $binds).")";
if ($this->debug) echo "DEBUG : $req\n";
$st = $this->db->prepare ($req);
$st = $this->prepare ($req);
foreach ($datasOK as $key=>$val)
{
if ($this->debug) echo "DEBUG BIND : $key(".md5 ($key).")->".
@@ -612,7 +616,7 @@ class dblayer extends PDO
echo "dblayer execute exception : ".$e->getMessage()."\n";
exit;
}
return $this->db->lastInsertId();
return $this->lastInsertId();
}
/** Read the table content based on a select filter, ordered by order
@@ -631,7 +635,7 @@ class dblayer extends PDO
$whereOr=false)
{
if ($this->debug) echo "== Entering read\n";
if ($this->db === null)
if ($this === null)
throw new Exception (dgettext("domframework", "Database not connected"),
500);
if ($select !== null && !is_array ($select))
@@ -708,7 +712,7 @@ class dblayer extends PDO
if ($this->debug) echo "DEBUG : $req\n";
try
{
$st = $this->db->prepare ($req);
$st = $this->prepare ($req);
}
catch (Exception $e)
{
@@ -747,7 +751,7 @@ class dblayer extends PDO
public function update ($updatekey, $datas)
{
if ($this->debug) echo "== Entering update\n";
if ($this->db === null)
if ($this === null)
throw new Exception (dgettext("domframework", "Database not connected"),
500);
if (count ($this->fields) === 0)
@@ -780,7 +784,7 @@ class dblayer extends PDO
$req .= " WHERE $this->sep$this->primary$this->sep=:".
md5 ("PRIMARY".$this->primary);
if ($this->debug) echo "DEBUG : $req\n";
$st = $this->db->prepare ($req);
$st = $this->prepare ($req);
// Add the primary key to field list temporaly. It will permit to update the
// primary key
$fields = $this->fields;
@@ -832,11 +836,11 @@ class dblayer extends PDO
public function delete ($deletekey)
{
if ($this->debug) echo "== Entering delete\n";
if ($this->db === null)
if ($this === null)
throw new Exception (dgettext("domframework", "Database not connected"));
$req = "DELETE FROM $this->sep$this->tableprefix$this->table$this->sep ";
$req .= "WHERE $this->primary = :primary";
$st = $this->db->prepare ($req);
$st = $this->prepare ($req);
if ($this->debug) echo "DEBUG : $req\n";
if ($this->debug) echo "DEBUG BIND : primary->".
var_export ($deletekey, TRUE)."\n";
@@ -869,12 +873,12 @@ class dblayer extends PDO
public function dropTable ()
{
if ($this->debug) echo "== Entering dropTables\n";
if ($this->db === null)
if ($this === null)
throw new Exception (dgettext("domframework", "Database not connected"));
$sql = "DROP TABLE $this->sep$this->tableprefix$this->table$this->sep";
if ($this->debug)
echo "$sql\n";
return $this->db->exec($sql);
return $this->exec($sql);
}
/** Create the table defined by the differents fields.
@@ -896,12 +900,12 @@ class dblayer extends PDO
public function createTable ()
{
if ($this->debug) echo "== Entering createTable\n";
if ($this->db === null)
if ($this === null)
throw new Exception (dgettext("domframework", "Database not connected"),
500);
if (count ($this->fields) === 0)
throw new Exception (dgettext("domframework", "No Field defined"), 500);
switch ($this->db->getAttribute(PDO::ATTR_DRIVER_NAME))
switch ($this->getAttribute(PDO::ATTR_DRIVER_NAME))
{
case "sqlite":
$sql = "CREATE TABLE $this->sep$this->tableprefix$this->table$this->sep ".
@@ -1191,9 +1195,14 @@ class dblayer extends PDO
"PDO Engine not supported in dbLayer"), 500);
}
$this->debug = true;
if ($this->debug)
echo "$sql\n";
return $this->db->exec($sql);
$rc=$this->exec($sql);
var_dump ($rc);
//print_r($this->errorInfo());
return $rc;
return $this->exec($sql);
}
/** This function permit to send a SQL request to the database to do a SELECT
@@ -1201,7 +1210,7 @@ class dblayer extends PDO
public function directRead ($sql)
{
if ($this->debug) echo "== Entering directRead\n";
$st = $this->db->prepare ($sql);
$st = $this->prepare ($sql);
$st->execute ();
$res = array ();
while ($d = $st->fetch (PDO::FETCH_ASSOC))