WIP on data (JSON Path queries)

This commit is contained in:
2023-08-25 12:20:48 -04:00
parent fa281124bb
commit dc31b65be8
5 changed files with 90 additions and 10 deletions

View File

@@ -3,6 +3,8 @@ declare(strict_types=1);
namespace BitBadger\PgSQL\Documents;
use \PgSql\Connection;
/**
* Document table configuration
*/
@@ -14,6 +16,27 @@ class Configuration
/** @var ?\PDO $conn The active connection */
private static ?\PDO $conn = null;
/** @var ?Connection $rawConn An active non-PDO PostgreSQL connection */
private static ?Connection $rawConn = null;
/** @var ?string $startUp The name of a function to run on first connection to the database */
public static ?string $startUp = null;
/**
* Ensure that the connection string is set, either explicity, by environment variables, or with defaults
*/
private static function ensureConnectionString()
{
if (self::$connectionString == "") {
$host = $_ENV['PGDOC_HOST'] ?? 'localhost';
$port = $_ENV['PGDOC_PORT'] ?? 5432;
$db = $_ENV['PGDOC_DB'] ?? 'postgres';
$user = $_ENV['PGDOC_USER'] ?? 'postgres';
$pass = $_ENV['PGDOC_PASS'] ?? 'postgres';
self::$connectionString = "pgsql:host=$host;port=$port;dbname=$db;user=$user;pass=$pass";
}
}
/**
* Get the database connection, connecting on first request
*
@@ -22,10 +45,31 @@ class Configuration
public static function getConn(): \PDO
{
if (is_null(self::$conn)) {
self::ensureConnectionString();
self::$conn = new \PDO(self::$connectionString);
if (!is_null(self::$startUp)) {
call_user_func(self::$startUp);
}
}
return self::$conn;
}
/**
*
*/
public static function getRawConn(): Connection
{
if (is_null(self::$rawConn)) {
self::ensureConnectionString();
self::$rawConn = pg_connect(str_replace(';', ' ', self::$connectionString));
if (!is_null(self::$startUp)) {
call_user_func(self::$startUp);
}
}
return self::$rawConn;
}
}
require('functions.php');