/* CREATE DATABASE zaptest WITH OWNER = postgres TEMPLATE = template0 ENCODING = 'UTF8' TABLESPACE = pg_default LC_COLLATE = 'C' LC_CTYPE = 'C' CONNECTION LIMIT = -1; */ #pragma library("ADAC20B.LIB") #include "Common.ch" #include "pgdbe.ch" #include "collat.ch" PROCEDURE Main LOCAL oSession LOCAL cConnStr LOCAL I // Load the PostgreSQL DatabaseEngine DbeLoad("pgdbe") DbeSetDefault("pgdbe") // Connect to the Server cConnStr := "DBE=pgdbe;server=localhost;db=zaptest;uid=postgres;pwd=thesafepassword" oSession := DacSession():New(cConnStr) IF(!oSession:IsConnected()) MsgBox("connection failed, database missing") QUIT ENDIF DbCreate("TestTable",{ {"ID","N",9,0} } ) DbUseArea(.t., oSession,"TestTable","test",.t.) test->(DbAppend()) test->ID := 0 test->(DbCommit()) test->(DbRUnlock()) DbCloseArea() if ( ! oSession:beginTransaction() ) Alert( "Start transaction failure" , {"OK"} ) QUIT ENDIF DbUseArea(.t., oSession,"TestTable","test",.t.) msgbox("Ready to start: please, run select count(*) from testtable and you should get 1") // Here you will continue to see 1 record via select count(*) from testtable for I := 1 to 20 test->(DbAppend()) test->ID := I test->(DbCommit()) test->(DbRUnlock()) Sleep(50) next I if ( oSession:IsConnected() ) MsgBox("Session still connected") endif if ( oSession:InTransaction() ) MsgBox("Session still in transaction") endif test->(DbZap()) // Also if we are inside a transaction select count(*) from testtable see 0 if ( oSession:IsConnected() ) MsgBox("Session still connected") endif if ( oSession:InTransaction() ) MsgBox("Session still in transaction") endif msgbox("select count(*) from testtable should continue to return 1 because we are still inside a transaction") // ... and here I see the records going up with an external // select count(*) from testtable for I := 1 to 20 test->(DbAppend()) test->ID := I test->(DbCommit()) test->(DbRUnlock()) Sleep(50) next I DbCloseArea() msgbox("select count(*) from testtable should continue to return 1 because we are still inside a transaction, but I'm going to commit after this message") if ( ! oSession:CommitTransaction() ) Alert( "Transaction not committed" , {"OK"} ) QUIT ENDIF msgbox("select count(*) from testtable should return 20") RETURN