Eu tenho duas function que uso muito aqui, que são as duas abaixo, onde coloquei elas no seguinte arquivo: tpostgre.prg
Em anexo segue um exemplo de como usar algumas funções dessa LIB e mais essas duas abaixo( para compilar tem que linkar com Hwgui )
************************************************************
FUNCTION EXECUTA_PGSQL2( oServer, cQuery, aResult, aColunas )
************************************************************
local pQuery, temp, aTemp, x, y, __nTOTCOL:=0, __nTOTLIN:=0
pQuery := PQexec( oServer:pDB, cQuery )
if PQresultstatus( pQuery ) == PGRES_TUPLES_OK
__nTOTLIN:=PQLastrec( pQuery )
if __nTOTLIN != 0
__nTOTCOL:=PQFcount( pQuery )
if __nTOTCOL == 1 .and. __nTOTLIN == 1
temp := PQGetValue( pQuery, 1, 1 )
aResult := iif( temp == NIL, "", temp )
if aColunas#nil
temp := PQfname(pQuery,1)
if !empty(temp)
AADD(aColunas, temp )
endif
endif
else
aResult := {}
for x := 1 to __nTOTLIN
if aColunas#nil
temp := PQfname(pQuery,x)
if !empty(temp)
AADD(aColunas, temp )
endif
endif
aTemp := {}
for y := 1 to __nTOTCOL
temp := PQGetValue( pQuery, x, y )
aadd( aTemp, iif( temp == NIL, "", temp ) )
next
aadd( aResult, aTemp)
next
endif
endif
endif
pQuery:=nil
return NIL
**********************************************************
FUNCTION EXECUTA_PGSQL(oServer, cQuery, aResult, aColunas)
**********************************************************
Local x, y, __nTOTCOL:=0, __nTOTLIN:=0
Local aTemp, __oQuery, __lret:=.t.
__oQuery := oServer:Query( cQuery ) // exeuta uma query
if __oQuery:neterr()
__lret:=.f.
oServer:cError:='Erro:'+ __oQuery:ErrorMsg()
else
if aColunas#Nil
aColunas:=__oQuery:Struct(.t.) // somente nome das colunas
endif
if aResult#Nil
__nTOTCOL:=__oQuery:nFields
__nTOTLIN:=__oQuery:nLastrec
aResult := {}
for x := 1 to __nTOTLIN
aTemp := {}
for y := 1 to __nTOTCOL
temp := __oQuery:FieldGet( y , x )
aadd( aTemp, iif( temp == NIL, "", temp ) )
next
aadd( aResult, aTemp)
next
endif
endif
__oQuery:Destroy()
RETURN(__lret)