Refactored and consolidated DatabaseConnection (simplified PDO wrapper)
This commit is contained in:
parent
45a63e81db
commit
6c4237ec9b
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,6 @@
|
|||||||
/files/
|
/files/
|
||||||
/plugins/
|
/plugins/
|
||||||
|
/*.css
|
||||||
/*.db
|
/*.db
|
||||||
|
/*.js
|
||||||
|
/*.txt
|
||||||
|
17
app.sh
Normal file
17
app.sh
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# https://stackoverflow.com/a/1482133
|
||||||
|
SCRIPT_DIR="$(dirname -- "$(readlink -f -- "$0";)";)"
|
||||||
|
cd "${SCRIPT_DIR}"
|
||||||
|
|
||||||
|
# Allow port setting via first argument:
|
||||||
|
PORT=8000
|
||||||
|
if [[ ! "$1" == "" ]]
|
||||||
|
then
|
||||||
|
PORT="$1"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Handle multiple simultaneous requests in Linux only:
|
||||||
|
export PHP_CLI_SERVER_WORKERS=4
|
||||||
|
|
||||||
|
php -S 0.0.0.0:$PORT index.php --enable-mbstring
|
@ -4,6 +4,8 @@
|
|||||||
private $strConnection;
|
private $strConnection;
|
||||||
private $pdo;
|
private $pdo;
|
||||||
|
|
||||||
|
public $strEngine;
|
||||||
|
|
||||||
public function __construct($strConnection, $strUsername = null, $strPassword = null)
|
public function __construct($strConnection, $strUsername = null, $strPassword = null)
|
||||||
{
|
{
|
||||||
$this->strConnection = $strConnection;
|
$this->strConnection = $strConnection;
|
||||||
@ -17,6 +19,14 @@
|
|||||||
$this->pdo->setAttribute(
|
$this->pdo->setAttribute(
|
||||||
PDO::ATTR_ERRMODE,
|
PDO::ATTR_ERRMODE,
|
||||||
PDO::ERRMODE_EXCEPTION);
|
PDO::ERRMODE_EXCEPTION);
|
||||||
|
|
||||||
|
// Fetch the engine being used:
|
||||||
|
preg_match(
|
||||||
|
"/^([a-z]{1,})\:/",
|
||||||
|
$strConnection,
|
||||||
|
$varMatches);
|
||||||
|
|
||||||
|
$this->strEngine = $varMatches[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function flatten(...$args)
|
private static function flatten(...$args)
|
||||||
@ -46,17 +56,56 @@
|
|||||||
|
|
||||||
public function query($input)
|
public function query($input)
|
||||||
{
|
{
|
||||||
$varArgs = self::flatten(func_get_args());
|
$varArgs = func_get_args();
|
||||||
|
|
||||||
|
// Support for single argument with multiple queries:
|
||||||
|
if (count($varArgs) == 1 && is_array($varArgs[0]))
|
||||||
|
{
|
||||||
|
$varQueries = $varArgs[0];
|
||||||
|
$varOutputs = [];
|
||||||
|
|
||||||
|
while (count($varQueries) > 0)
|
||||||
|
array_push($varOutputs, $this->query(array_shift($varQueries)));
|
||||||
|
|
||||||
|
return $varOutputs;
|
||||||
|
}
|
||||||
|
|
||||||
|
$varArgs = self::flatten($varArgs);
|
||||||
|
|
||||||
if (count($varArgs) < 1)
|
if (count($varArgs) < 1)
|
||||||
throw new Exception("query takes at least one argument, the query!");
|
throw new Exception("query takes at least one argument, the query!");
|
||||||
|
|
||||||
$strQuery = array_shift($varArgs);
|
$strQuery = array_shift($varArgs);
|
||||||
$strQuery = file_exists("db/{$strQuery}")?
|
|
||||||
file_get_contents("db/{$strQuery}") :
|
|
||||||
$strQuery;
|
|
||||||
|
|
||||||
if (preg_match("/^sqlsrv/", $this->strConnection))
|
// Support for plugins' queries in db directory:
|
||||||
|
$varQueryPaths = [];
|
||||||
|
|
||||||
|
if (preg_match("/\.sql$/", $strQuery))
|
||||||
|
{
|
||||||
|
if (is_dir("plugins"))
|
||||||
|
foreach (scandir("plugins") as $strPluginName)
|
||||||
|
{
|
||||||
|
if ($strPluginName == ".") continue;
|
||||||
|
if ($strPluginName == "..") continue;
|
||||||
|
|
||||||
|
array_push($varQueryPaths, "plugins/{$strPluginName}/db/{$strQuery}");
|
||||||
|
array_push($varQueryPaths, "plugins/{$strPluginName}/db/{$this->strEngine}/{$strQuery}");
|
||||||
|
}
|
||||||
|
|
||||||
|
array_push($varQueryPaths, "db/{$strQuery}");
|
||||||
|
}
|
||||||
|
|
||||||
|
while (count($varQueryPaths) > 0)
|
||||||
|
{
|
||||||
|
$strTryPath = array_shift($varQueryPaths);
|
||||||
|
if (is_file($strTryPath))
|
||||||
|
{
|
||||||
|
$strQuery = file_get_contents($strTryPath);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (preg_match("/^sqlsrv/", $this->strEngine))
|
||||||
$strQuery = "set nocount on; {$strQuery}";
|
$strQuery = "set nocount on; {$strQuery}";
|
||||||
|
|
||||||
$varStatement = $this->pdo->prepare($strQuery);
|
$varStatement = $this->pdo->prepare($strQuery);
|
||||||
@ -69,7 +118,7 @@
|
|||||||
$varOutput = array();
|
$varOutput = array();
|
||||||
|
|
||||||
// Engines that do not support multiple rowsets:
|
// Engines that do not support multiple rowsets:
|
||||||
if (preg_match("/^(sqlite)/", $this->strConnection))
|
if (preg_match("/^(sqlite)/", $this->strEngine))
|
||||||
{
|
{
|
||||||
while ($varRow = $varStatement->fetch())
|
while ($varRow = $varStatement->fetch())
|
||||||
{
|
{
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# https://stackoverflow.com/a/1482133
|
|
||||||
SCRIPT_DIR="$(dirname -- "$(readlink -f -- "$0";)";)"
|
|
||||||
cd "${SCRIPT_DIR}"
|
|
||||||
|
|
||||||
export PHP_CLI_SERVER_WORKERS=4
|
|
||||||
php -S 0.0.0.0:$1 index.php --enable-mbstring
|
|
Loading…
Reference in New Issue
Block a user