// app/pg_test.prg — pgrtl RTL smoke test. // // Tries to connect to a local Postgres; if none is up the test still // passes by checking the failure path. With LABDB_DSN set in the // environment it runs a real round-trip query against labdb. FUNCTION Main() LOCAL cDsn := hb_GetEnv( "LABDB_DSN", "postgres://nope:nope@127.0.0.1:1/nope" ) LOCAL nH, aRows, n, hRow, i ? "Trying:", cDsn nH := PG_OPEN( cDsn ) ? "PG_OPEN handle:", nH IF nH < 0 ? "(no live Postgres at that DSN — expected when LABDB_DSN unset)" RETURN NIL ENDIF ? "--- SELECT 1 round trip ---" aRows := PG_QUERY( nH, "SELECT 1 AS one, 'hello'::text AS greet, NULL AS empty" ) IF aRows == NIL ? "PG_QUERY failed:", PG_LAST_ERROR( nH ) PG_CLOSE( nH ) RETURN NIL ENDIF ? "rows:", Len( aRows ) FOR i := 1 TO Len( aRows ) hRow := aRows[ i ] ? " one :", hRow[ "one" ], ValType( hRow[ "one" ] ) ? " greet:", hRow[ "greet" ], ValType( hRow[ "greet" ] ) ? " empty:", hRow[ "empty" ], ValType( hRow[ "empty" ] ) NEXT ? "--- parameter binding ---" aRows := PG_QUERY( nH, "SELECT $1::int AS n, $2::text AS s", { 42, "answer" } ) IF aRows != NIL .AND. Len( aRows ) > 0 ? " n:", aRows[ 1 ][ "n" ] ? " s:", aRows[ 1 ][ "s" ] ELSE ? "param query failed:", PG_LAST_ERROR( nH ) ENDIF PG_CLOSE( nH ) ? "closed." RETURN NIL