dbLayer : don't connect multiple times to the same database/username
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1949 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
27
dblayer.php
27
dblayer.php
@@ -85,6 +85,9 @@ class dblayer extends PDO
|
|||||||
Attention : SQLite don't supports adding Foreign keys without deleting all
|
Attention : SQLite don't supports adding Foreign keys without deleting all
|
||||||
the table, and re-import the datas (http://www.sqlite.org/omitted.html) */
|
the table, and re-import the datas (http://www.sqlite.org/omitted.html) */
|
||||||
|
|
||||||
|
/** Limit to one instance of the connection to the same database */
|
||||||
|
private static $instance = null;
|
||||||
|
|
||||||
/** Connection to the database engine
|
/** Connection to the database engine
|
||||||
See http://fr2.php.net/manual/en/pdo.construct.php for the $dsn format
|
See http://fr2.php.net/manual/en/pdo.construct.php for the $dsn format
|
||||||
@param string $dsn PDO Data Source Name
|
@param string $dsn PDO Data Source Name
|
||||||
@@ -98,6 +101,16 @@ class dblayer extends PDO
|
|||||||
if (! isset ($driver[0]))
|
if (! isset ($driver[0]))
|
||||||
throw new Exception (_("No valid DSN provided"), 500);
|
throw new Exception (_("No valid DSN provided"), 500);
|
||||||
// Force specifics initialisations
|
// Force specifics initialisations
|
||||||
|
$oldInst = self::getInstance($dsn, $username);
|
||||||
|
|
||||||
|
if ($oldInst !== null)
|
||||||
|
{
|
||||||
|
$this->db = $oldInst["db"];
|
||||||
|
$this->sep = $oldInst["sep"];
|
||||||
|
$this->dsn = $oldInst["dsn"];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
switch ($driver[0])
|
switch ($driver[0])
|
||||||
{
|
{
|
||||||
case "sqlite":
|
case "sqlite":
|
||||||
@@ -161,6 +174,20 @@ class dblayer extends PDO
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
$this->dsn = $dsn;
|
$this->dsn = $dsn;
|
||||||
|
self::$instance["$dsn-$username"]["db"] = $this->db;
|
||||||
|
self::$instance["$dsn-$username"]["dsn"] = $this->dsn;
|
||||||
|
self::$instance["$dsn-$username"]["sep"] = $this->sep;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Singleton function to open one connection to each database/user wanted */
|
||||||
|
private function getInstance ($dsn, $username)
|
||||||
|
{
|
||||||
|
if (self::$instance === null ||
|
||||||
|
array_key_exists ("$dsn-$username", self::$instance) === false)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return self::$instance["$dsn-$username"];
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Return the connected database name from DSN used to connect */
|
/** Return the connected database name from DSN used to connect */
|
||||||
|
|||||||
Reference in New Issue
Block a user