From 6c4237ec9bf9b59d733a226f76acdbfcb84cfa0d Mon Sep 17 00:00:00 2001 From: Conner Harkness Date: Sat, 28 Jun 2025 06:41:34 -0600 Subject: [PATCH] Refactored and consolidated DatabaseConnection (simplified PDO wrapper) --- .gitignore | 3 ++ app.sh | 17 +++++++++++ lib/DatabaseConnection.php | 61 ++++++++++++++++++++++++++++++++++---- php-webapp-framework.sh | 8 ----- 4 files changed, 75 insertions(+), 14 deletions(-) create mode 100644 app.sh delete mode 100644 php-webapp-framework.sh diff --git a/.gitignore b/.gitignore index 0aeca79..7253e7c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ /files/ /plugins/ +/*.css /*.db +/*.js +/*.txt diff --git a/app.sh b/app.sh new file mode 100644 index 0000000..f698d21 --- /dev/null +++ b/app.sh @@ -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 diff --git a/lib/DatabaseConnection.php b/lib/DatabaseConnection.php index b166df3..09d9517 100644 --- a/lib/DatabaseConnection.php +++ b/lib/DatabaseConnection.php @@ -4,6 +4,8 @@ private $strConnection; private $pdo; + public $strEngine; + public function __construct($strConnection, $strUsername = null, $strPassword = null) { $this->strConnection = $strConnection; @@ -17,6 +19,14 @@ $this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + // Fetch the engine being used: + preg_match( + "/^([a-z]{1,})\:/", + $strConnection, + $varMatches); + + $this->strEngine = $varMatches[1]; } private static function flatten(...$args) @@ -46,17 +56,56 @@ 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) throw new Exception("query takes at least one argument, the query!"); $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}"; $varStatement = $this->pdo->prepare($strQuery); @@ -69,7 +118,7 @@ $varOutput = array(); // Engines that do not support multiple rowsets: - if (preg_match("/^(sqlite)/", $this->strConnection)) + if (preg_match("/^(sqlite)/", $this->strEngine)) { while ($varRow = $varStatement->fetch()) { diff --git a/php-webapp-framework.sh b/php-webapp-framework.sh deleted file mode 100644 index f511f09..0000000 --- a/php-webapp-framework.sh +++ /dev/null @@ -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