Skip to content

Commit

Permalink
Investigate connecting to PostgreSQL (early test)
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorejb committed Oct 13, 2024
1 parent 47fee0e commit 3ae243c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
56 changes: 56 additions & 0 deletions test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

use PeachySQL\Test\src\App;
use PgSql\Connection;

require 'test/bootstrap.php';

$conn = 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!\n";
}

if (!pg_query($conn, "DROP TABLE Users")) {
throw new Exception('Failed to drop PostgreSQL test table: ' . pg_last_error($conn));
} else {
echo "Successfully dropped test table\n";
}

function getPgsqlConn(): Connection
{
$c = App::$config;
$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');
}

createPgsqlTestTable($conn);
return $conn;
}

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));
}
}
3 changes: 3 additions & 0 deletions test/DbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function testTransactions(PeachySql $peachySql): void
];

$id = $peachySql->insertRow($this->table, $colVals)->id;
// this won't work with pgsql
$sql = "SELECT user_id, isDisabled FROM {$this->table} WHERE user_id = ?";
$result = $peachySql->query($sql, [$id]);

Expand Down Expand Up @@ -134,6 +135,7 @@ public function testIteratorQuery(PeachySql $peachySql): void
$this->assertSame($colVals, $colValsCompare);

// use a prepared statement to update both of the rows
// this won't work with pgsql
$sql = "UPDATE {$this->table} SET name = ? WHERE user_id = ?";
$_id = $_name = null;
$stmt = $peachySql->prepare($sql, [&$_name, &$_id]);
Expand Down Expand Up @@ -240,6 +242,7 @@ public function testSelectFromBinding(PeachySql $peachySql): void
$row = ['name' => 'Test User', 'dob' => '2000-01-01', 'weight' => 123, 'isDisabled' => true];
$id = $peachySql->insertRow($this->table, $row)->id;

// this won't work with pgsql
$result = $peachySql->select(new SqlParams("SELECT name, ? AS bound FROM {$this->table}", ['value']))
->where(['user_id' => $id])->query()->getFirst();

Expand Down
20 changes: 20 additions & 0 deletions test/src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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

0 comments on commit 3ae243c

Please sign in to comment.