diff --git a/test.php b/test.php index ca17c26..d487b7f 100644 --- a/test.php +++ b/test.php @@ -1,56 +1,40 @@ prepare($sql); +$stmt->execute(['George McFly', '1938-04-01', 133.8, true]); +echo "Successfully inserted row!\n"; -if (!$result) { - echo 'Failed to insert row: ' . pg_last_error($conn) . "\n"; -} else { - echo "Successfully inserted row!\n"; -} +$conn->query("DROP TABLE Users"); +echo "Successfully dropped test table\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 +function getPgsqlConn(): PDO { $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'); - } + $dsn = "pgsql:host={$c->getPgsqlHost()};dbname={$c->getPgsqlDatabase()}"; - createPgsqlTestTable($conn); - return $conn; + $pdo = new PDO($dsn, $c->getPgsqlUser(), $c->getPgsqlPassword()); + createPgsqlTestTable($pdo); + return $pdo; } -function createPgsqlTestTable(Connection $conn): void +function createPgsqlTestTable(PDO $pdo): 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)); - } + 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 + )"; + + $pdo->query($sql); }