Consolidated DatabaseConnection class to be a PDO wrapper
This commit is contained in:
parent
5f3dd988b6
commit
45a63e81db
@ -1,55 +1,49 @@
|
|||||||
<?php
|
<?php
|
||||||
class DatabaseConnection
|
class DatabaseConnection
|
||||||
{
|
{
|
||||||
private $strEngine;
|
private $strConnection;
|
||||||
private $strHost;
|
|
||||||
private $strDatabaseName;
|
|
||||||
private $strUsername;
|
|
||||||
private $strPassword;
|
|
||||||
private $pdo;
|
private $pdo;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct($strConnection, $strUsername = null, $strPassword = null)
|
||||||
$strEngine = null,
|
|
||||||
$strHost = null,
|
|
||||||
$strDatabaseName = null,
|
|
||||||
$strUsername = null,
|
|
||||||
$strPassword = null)
|
|
||||||
{
|
{
|
||||||
$this->strEngine = $strEngine;
|
$this->strConnection = $strConnection;
|
||||||
|
|
||||||
switch ($strEngine)
|
// SQL Server: "sqlsrv:Server={$strHost};Database={$strDatabaseName}"
|
||||||
{
|
// MySQL: "mysql:host={$strHost};dbname={$strDatabaseName}"
|
||||||
case "sqlsrv":
|
// SQLite: "sqlite:{$strFileName}"
|
||||||
$this->pdo = new PDO(
|
// SQLite: "sqlite::memory:"
|
||||||
"sqlsrv:Server={$strHost};Database={$strDatabaseName}",
|
|
||||||
$strUsername,
|
|
||||||
$strPassword);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "mysql":
|
|
||||||
$this->pdo = new PDO(
|
|
||||||
"mysql:host={$strHost};dbname={$strDatabaseName}",
|
|
||||||
$strUsername,
|
|
||||||
$strPassword);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "sqlite":
|
|
||||||
$strFileName = $strHost;
|
|
||||||
if ($strFileName == null || strlen($strFileName) < 1)
|
|
||||||
$strFileName = ":memory:";
|
|
||||||
|
|
||||||
$this->pdo = new PDO("sqlite:{$strFileName}");
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
throw new Exception("Unknown database engine {$strEngine}.");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
$this->pdo = new PDO($strConnection, $strUsername, $strPassword);
|
||||||
$this->pdo->setAttribute(
|
$this->pdo->setAttribute(
|
||||||
PDO::ATTR_ERRMODE,
|
PDO::ATTR_ERRMODE,
|
||||||
PDO::ERRMODE_EXCEPTION);
|
PDO::ERRMODE_EXCEPTION);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static function flatten(...$args)
|
||||||
|
{
|
||||||
|
if (is_null($args))
|
||||||
|
return array();
|
||||||
|
|
||||||
|
if (count($args) > 0)
|
||||||
|
{
|
||||||
|
$varOutput = array();
|
||||||
|
$varFirst = array_shift($args);
|
||||||
|
|
||||||
|
if (is_array($varFirst))
|
||||||
|
foreach ($varFirst as $varItem)
|
||||||
|
$varOutput = array_merge($varOutput, self::flatten($varItem));
|
||||||
|
else
|
||||||
|
array_push($varOutput, $varFirst);
|
||||||
|
|
||||||
|
if (count($args) > 0)
|
||||||
|
$varOutput = array_merge($varOutput, self::flatten($args));
|
||||||
|
|
||||||
|
return $varOutput;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
|
||||||
public function query($input)
|
public function query($input)
|
||||||
{
|
{
|
||||||
$varArgs = self::flatten(func_get_args());
|
$varArgs = self::flatten(func_get_args());
|
||||||
@ -62,7 +56,7 @@
|
|||||||
file_get_contents("db/{$strQuery}") :
|
file_get_contents("db/{$strQuery}") :
|
||||||
$strQuery;
|
$strQuery;
|
||||||
|
|
||||||
if ($this->strEngine == "sqlsrv")
|
if (preg_match("/^sqlsrv/", $this->strConnection))
|
||||||
$strQuery = "set nocount on; {$strQuery}";
|
$strQuery = "set nocount on; {$strQuery}";
|
||||||
|
|
||||||
$varStatement = $this->pdo->prepare($strQuery);
|
$varStatement = $this->pdo->prepare($strQuery);
|
||||||
@ -75,7 +69,7 @@
|
|||||||
$varOutput = array();
|
$varOutput = array();
|
||||||
|
|
||||||
// Engines that do not support multiple rowsets:
|
// Engines that do not support multiple rowsets:
|
||||||
if (in_array($this->strEngine, array("sqlite")))
|
if (preg_match("/^(sqlite)/", $this->strConnection))
|
||||||
{
|
{
|
||||||
while ($varRow = $varStatement->fetch())
|
while ($varRow = $varStatement->fetch())
|
||||||
{
|
{
|
||||||
@ -112,39 +106,5 @@
|
|||||||
|
|
||||||
return $varOutput;
|
return $varOutput;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//
|
|
||||||
|
|
||||||
// Used to combine a mixture of values and arrays of values as one flat array of values in the order they arrive in. For example: self::flatten("a", ["b", "c", ["d", "e", "f"], "g"], "h") should return an array of ["a", "b", "c", "d", "e", "f", "g", "h"].
|
|
||||||
private static function flatten(...$args)
|
|
||||||
{
|
|
||||||
if (is_null($args))
|
|
||||||
return array();
|
|
||||||
|
|
||||||
if (count($args) > 0)
|
|
||||||
{
|
|
||||||
$varOutput = array();
|
|
||||||
$varFirst = array_shift($args);
|
|
||||||
|
|
||||||
if (is_array($varFirst))
|
|
||||||
foreach ($varFirst as $varItem)
|
|
||||||
$varOutput = array_merge($varOutput, self::flatten($varItem));
|
|
||||||
else
|
|
||||||
array_push($varOutput, $varFirst);
|
|
||||||
|
|
||||||
if (count($args) > 0)
|
|
||||||
$varOutput = array_merge($varOutput, self::flatten($args));
|
|
||||||
|
|
||||||
return $varOutput;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return array();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
Loading…
Reference in New Issue
Block a user