Skip to content

Commit

Permalink
Investigate connecting to PostgreSQL (early test)
Browse files Browse the repository at this point in the history
I think it might be cleaner to split DB-specific code into separate packages.
  • Loading branch information
theodorejb committed Oct 8, 2024
1 parent 3e09fad commit 72df0e3
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
21 changes: 21 additions & 0 deletions test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use PeachySQL\Test\src\DbConnector;
use Ramsey\Uuid\Uuid;

require 'test/bootstrap.php';

$conn = DbConnector::getPgsqlConn();

$sql = "INSERT INTO Users (name, dob, weight, isDisabled)
VALUES ($1, $2, $3, $4)";

$result = pg_query_params($conn, $sql, ['George McFly', '1938-04-01', 133.8, true]);

if (!$result) {
echo 'Failed to insert row: ' . pg_last_error($conn) . "\n";
} else {
echo 'Successfully inserted row!';
}

DbConnector::deleteTestTables();
20 changes: 20 additions & 0 deletions test/src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ public function getMysqlDatabase(): string
return 'PeachySQL';
}

public function getPgsqlHost(): string
{
return 'localhost';
}

public function getPgsqlDatabase(): string
{
return 'PeachySQL';
}

public function getPgsqlUser(): string
{
return 'postgres';
}

public function getPgsqlPassword(): string
{
return '';
}

public function getSqlsrvServer(): string
{
return '(local)\SQLEXPRESS';
Expand Down
43 changes: 43 additions & 0 deletions test/src/DbConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

use Exception;
use mysqli;
use PgSql\Connection;

class DbConnector
{
private static Config $config;
private static ?mysqli $mysqlConn = null;
private static ?Connection $pgsqlConn = null;

/**
* @var resource|null
Expand Down Expand Up @@ -43,6 +45,25 @@ public static function getMysqlConn(): mysqli
return self::$mysqlConn;
}

public static function getPgsqlConn(): Connection

Check failure on line 48 in test/src/DbConnector.php

View workflow job for this annotation

GitHub Actions / Run tests on 8.3

PossiblyUnusedMethod

test/src/DbConnector.php:48:28: PossiblyUnusedMethod: Cannot find any calls to method PeachySQL\Test\src\DbConnector::getPgsqlConn (see https://psalm.dev/087)
{
if (!self::$pgsqlConn) {
$c = self::getConfig();
$connStr = "host={$c->getPgsqlHost()} dbname={$c->getPgsqlDatabase()} "
. "user={$c->getPgsqlUser()} password={$c->getPgsqlPassword()}";
$conn = pg_connect($connStr);

if (!$conn) {
throw new Exception('Failed to connect to PostgreSQL');
}

self::$pgsqlConn = $conn;
self::createPgsqlTestTable(self::$pgsqlConn);
}

return self::$pgsqlConn;
}

/**
* @return resource
*/
Expand Down Expand Up @@ -97,6 +118,22 @@ private static function createMysqlTestTable(mysqli $conn): void
}
}

private static function createPgsqlTestTable(Connection $conn): void
{
$sql = 'CREATE TABLE Users (
user_id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
dob DATE NOT NULL,
weight REAL NOT NULL,
isDisabled BOOLEAN NOT NULL,
uuid bytea NULL
);';

if (!pg_query($conn, $sql)) {
throw new Exception('Failed to create PostgreSQL test table: ' . pg_last_error($conn));
}
}

public static function deleteTestTables(): void
{
$sql = 'DROP TABLE Users';
Expand All @@ -107,6 +144,12 @@ public static function deleteTestTables(): void
}
}

if (self::$pgsqlConn) {
if (!pg_query(self::$pgsqlConn, $sql)) {
throw new Exception('Failed to drop PostgreSQL test table: ' . pg_last_error(self::$pgsqlConn));
}
}

if (self::$sqlsrvConn) {
if (!sqlsrv_query(self::$sqlsrvConn, $sql)) {
throw new Exception('Failed to drop SQL Server test table: ' . print_r(sqlsrv_errors(), true));
Expand Down

0 comments on commit 72df0e3

Please sign in to comment.