2010-03-07 18:42 UTC+0100 Viktor Szakats (harbour.01 syenar.hu)

* contrib/hbpgsql/tests/simple.prg
    ! Fixed to use ANSI date format in INSERT command.

  * contrib/hbpgsql/tests/async.prg
  * contrib/hbpgsql/tests/test.prg
  * contrib/hbpgsql/tests/cache.prg
  * contrib/hbpgsql/tests/stress.prg
  * contrib/hbpgsql/tests/dbf2pg.prg
    ! Fixed indentation.
This commit is contained in:
Viktor Szakats
2010-03-07 17:43:18 +00:00
parent 95f7efb18e
commit 5687bf4f6d
7 changed files with 557 additions and 584 deletions

View File

@@ -17,6 +17,17 @@
past entries belonging to author(s): Viktor Szakats.
*/
2010-03-07 18:42 UTC+0100 Viktor Szakats (harbour.01 syenar.hu)
* contrib/hbpgsql/tests/simple.prg
! Fixed to use ANSI date format in INSERT command.
* contrib/hbpgsql/tests/async.prg
* contrib/hbpgsql/tests/test.prg
* contrib/hbpgsql/tests/cache.prg
* contrib/hbpgsql/tests/stress.prg
* contrib/hbpgsql/tests/dbf2pg.prg
! Fixed indentation.
2010-03-07 17:31 UTC+0100 Viktor Szakats (harbour.01 syenar.hu)
* utils/hbmk2/hbmk2.prg
! Cosmetic fix to generated temp stub file. Normally nobody

View File

@@ -1,69 +1,70 @@
/*
* $Id$
*
*/
/*
* This sample show howto use asynchronous/nonblocking queries
*
*/
Function Main( cServer, cDatabase, cUser, cPass )
Local conn
Local conn
CLEAR SCREEN
CLEAR SCREEN
? "Connect", conn := PQConnect( cDatabase, cServer, cUser, cPass, 5432)
? "Connect", conn := PQConnect( cDatabase, cServer, cUser, cPass, 5432)
? "Conection status", PQerrorMessage(conn), PQstatus(conn)
? "Conection status", PQerrorMessage(conn), PQstatus(conn)
Query( conn, 'SELECT codigo, descri FROM client limit 100', .f. )
Query( conn, 'SELECT codigo, descri FROM fornec limit 100', .f. )
Query( conn, 'SELECT pedido, vlrped FROM pedido', .t. )
Query( conn, 'SELECT codigo, descri FROM client limit 100', .f. )
Query( conn, 'SELECT codigo, descri FROM fornec limit 100', .f. )
Query( conn, 'SELECT pedido, vlrped FROM pedido', .t. )
conn := NIL
conn := NIL
return nil
return nil
Procedure Query( conn, cQuery, lCancel )
Local pCancel, cErrMsg := space(30)
Local res, x, y, xTime
Local pCancel, cErrMsg := space(30)
Local res, x, y, xTime
? "PQSendQuery", PQsendQuery(conn, cQuery)
? "PQSendQuery", PQsendQuery(conn, cQuery)
xTime := time()
CLEAR TYPEAHEAD
xTime := time()
CLEAR TYPEAHEAD
do while inkey() != 27
DevPos(Row(), 20)
DevOut("Processing: " + Elaptime(xtime, time()))
do while inkey() != 27
DevPos(Row(), 20)
DevOut("Processing: " + Elaptime(xtime, time()))
inkey(1)
inkey(1)
if lCancel
if .t.
pCancel := PQgetCancel(conn)
? "Canceled: ", PQcancel( pCancel, @cErrMsg ), cErrMsg
pCancel := NIL
else
? PQrequestCancel(conn) // Deprecated
endif
endif
if lCancel
if .t.
pCancel := PQgetCancel(conn)
? "Canceled: ", PQcancel( pCancel, @cErrMsg ), cErrMsg
pCancel := NIL
else
? PQrequestCancel(conn) // Deprecated
endif
endif
if PQconsumeInput(conn)
if ! PQisBusy(conn)
exit
endif
endif
enddo
if PQconsumeInput(conn)
if ! PQisBusy(conn)
exit
endif
endif
enddo
if inkey() != 27
? "PQgetResult", hb_valtoexp(res := PQgetResult(conn))
if inkey() != 27
? "PQgetResult", hb_valtoexp(res := PQgetResult(conn))
for x := 1 to PQlastrec(res)
?
for y := 1 to PQfcount(res)
?? PQgetvalue(res, x, y), " "
next
next
else
? "Canceling Query", PQrequestCancel(conn)
endif
Return
for x := 1 to PQlastrec(res)
?
for y := 1 to PQfcount(res)
?? PQgetvalue(res, x, y), " "
next
next
else
? "Canceling Query", PQrequestCancel(conn)
endif
Return

View File

@@ -45,502 +45,461 @@ STATIC aTableTemp := {}
STATIC aTempDBF := {}
Function Main( cServer, cDatabase, cUser, cPass )
Local i
Local cQuery
Local i
Local cQuery
SetMode( 25, 80 )
SetMode( 25, 80 )
if SQLConnect( cServer, cDatabase, cUser, cPass )
QuickQuery('DROP TABLE test')
if SQLConnect( cServer, cDatabase, cUser, cPass )
QuickQuery('DROP TABLE test')
cQuery := 'CREATE TABLE test ( '
cQuery += ' codigo integer primary key, '
cQuery += ' descri char(50), '
cQuery += ' email varchar(50) ) '
SQLQuery(cQuery)
cQuery := 'CREATE TABLE test ( '
cQuery += ' codigo integer primary key, '
cQuery += ' descri char(50), '
cQuery += ' email varchar(50) ) '
SQLQuery(cQuery)
SQLOpen( 'nomes', 'SELECT * FROM test')
SQLOpen( 'nomes', 'SELECT * FROM test')
for i := 1 to 50
append blank
replace codigo with i
replace descri with 'test ' + str(i)
next
for i := 1 to 50
append blank
replace codigo with i
replace descri with 'test ' + str(i)
next
SQLApplyUpdates()
SQLApplyUpdates()
cQuery := 'SELECT * FROM test WHERE codigo >= :1 ORDER BY codigo'
cQuery := SQLPrepare( cQuery, 1 )
SQLOpen( 'nomes', cQuery)
cQuery := 'SELECT * FROM test WHERE codigo >= :1 ORDER BY codigo'
cQuery := SQLPrepare( cQuery, 1 )
SQLOpen( 'nomes', cQuery)
Do while ! Eof()
? recno(), nomes->Codigo, nomes->descri, nomes->email
Do while ! Eof()
? recno(), nomes->Codigo, nomes->descri, nomes->email
if recno() == 10
delete
endif
if recno() == 10
delete
endif
if recno() == 20
REPLACE email WITH 'teste'
endif
if recno() == 20
REPLACE email WITH 'teste'
endif
SQLFetch()
enddo
SQLApplyUpdates()
endif
Return SQLGarbageCollector()
SQLFetch()
enddo
SQLApplyUpdates()
endif
Return SQLGarbageCollector()
/* Put theses functions in a library */
Function SQLApplyUpdates()
Local cAlias := Upper(Alias())
Local i, x
Local oQuery
Local oRow
Local lUpdate
Local lError := .F.
Local cError
Local cAlias := Upper(Alias())
Local i, x
Local oQuery
Local oRow
Local lUpdate
Local lError := .F.
Local cError
i := ASCAN(aTableTemp, {|aVal| aVal[DB_ALIAS] == cAlias})
i := ASCAN(aTableTemp, {|aVal| aVal[DB_ALIAS] == cAlias})
IF i != 0
IF i != 0
oQuery := aTableTemp[i, 3]
oQuery := aTableTemp[i, 3]
FOR i := 1 TO Lastrec()
DBGoto(i)
FOR i := 1 TO Lastrec()
IF i > oQuery:Lastrec()
/* Verifica se eh um registro novo */
if ! Deleted()
oRow := oQuery:GetBlankRow()
DBGoto(i)
FOR x := 1 TO FCount()
if oRow:Fieldpos( Fieldname(x) ) != 0
oRow:FieldPut(Fieldname(x), Fieldget(x))
endif
NEXT
oQuery:Append(oRow)
cError := oQuery:ErrorMsg()
IF i > oQuery:Lastrec()
/* Verifica se eh um registro novo */
if ! Deleted()
oRow := oQuery:GetBlankRow()
FOR x := 1 TO FCount()
if oRow:Fieldpos( Fieldname(x) ) != 0
oRow:FieldPut(Fieldname(x), Fieldget(x))
endif
NEXT
oQuery:Append(oRow)
cError := oQuery:ErrorMsg()
lError := oQuery:NetErr()
endif
ELSE
oRow := oQuery:GetRow(i)
lUpdate := .F.
IF Deleted()
oQuery:Delete(oRow)
cError := oQuery:ErrorMsg()
lError := oQuery:NetErr()
lError := oQuery:NetErr()
endif
ELSE
/* Faz update, mas compara quais campos sao diferentes */
oRow := oQuery:GetRow(i)
FOR x := 1 TO Fcount()
lUpdate := .F.
if oRow:Fieldpos( Fieldname(x) ) != 0
IF Deleted()
if .not. (Fieldget(x) == oRow:Fieldget(Fieldname(x)))
oQuery:Delete(oRow)
cError := oQuery:ErrorMsg()
lError := oQuery:NetErr()
ELSE
oRow:Fieldput(Fieldname(x), Fieldget(x))
/* Faz update, mas compara quais campos sao diferentes */
lUpdate := .t.
FOR x := 1 TO Fcount()
if oRow:Fieldpos( Fieldname(x) ) != 0
if .not. (Fieldget(x) == oRow:Fieldget(Fieldname(x)))
oRow:Fieldput(Fieldname(x), Fieldget(x))
lUpdate := .t.
endif
endif
NEXT
endif
IF lUpdate
NEXT
oQuery:Update(oRow)
cError := oQuery:ErrorMsg()
lError := oQuery:NetErr()
IF lUpdate
oQuery:Update(oRow)
cError := oQuery:ErrorMsg()
lError := oQuery:NetErr()
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
if lError
exit
endif
NEXT
ENDIF
if lError
exit
endif
IF lError
Alert(cError)
ENDIF
NEXT
ENDIF
IF lError
Alert(cError)
ENDIF
Return ! lError
Return ! lError
Procedure SQLCloseTemp( cAlias )
Local x
Local x
IF ! Empty(Select(cAlias))
CLOSE &calias
ENDIF
IF ! Empty(Select(cAlias))
CLOSE &calias
ENDIF
x := ASCAN(aTableTemp, {|aVal| aVal[DB_ALIAS] == cAlias})
x := ASCAN(aTableTemp, {|aVal| aVal[DB_ALIAS] == cAlias})
IF ! Empty(x)
IF ! Empty(x)
ADel( aTableTemp, x )
//ASize( aTableTemp, Len(aTableTemp) - 1 )
ENDIF
Return
ENDIF
Return
Procedure SQLGarbageCollector()
Local i
Local oQuery
Local i
Local oQuery
DBCloseAll()
DBCloseAll()
FOR i := 1 TO Len(aTableTemp)
/* Apaga arquivos dbfs criados */
FErase(aTableTemp[i, DB_FILE])
oQuery := aTableTemp[i, DB_QUERY]
FOR i := 1 TO Len(aTableTemp)
/* Apaga arquivos dbfs criados */
FErase(aTableTemp[i, DB_FILE])
oQuery := aTableTemp[i, DB_QUERY]
IF oQuery != NIL
oQuery:Destroy()
ENDIF
IF oQuery != NIL
oQuery:Destroy()
ENDIF
NEXT
NEXT
FOR i := 1 TO Len(aTempDBF)
IF File(aTempDBF[i])
FErase(aTempDBF[i])
ENDIF
FOR i := 1 TO Len(aTempDBF)
IF File(aTempDBF[i])
FErase(aTempDBF[i])
ENDIF
IF File(strtran(aTempDBF[i], '.tmp', '.dbf'))
FErase(strtran(aTempDBF[i], '.tmp', '.dbf'))
ENDIF
IF File(strtran(aTempDBF[i], '.tmp', '.dbf'))
FErase(strtran(aTempDBF[i], '.tmp', '.dbf'))
ENDIF
IF File(strtran(aTempDBF[i], '.tmp', '.dbt'))
FErase(strtran(aTempDBF[i], '.tmp', '.dbt'))
ENDIF
NEXT
IF File(strtran(aTempDBF[i], '.tmp', '.dbt'))
FErase(strtran(aTempDBF[i], '.tmp', '.dbt'))
ENDIF
NEXT
aTableTemp := {}
aTempDBF := {}
Return
aTableTemp := {}
aTempDBF := {}
Return
Function SQLFetch( fetchall )
Local oQuery
Local oRow
Local cAlias := Upper(Alias())
Local i, x, y
Local nPos
Local lEof := .F.
Local oQuery
Local oRow
Local cAlias := Upper(Alias())
Local i, x, y
Local nPos
Local lEof := .F.
Default Fetchall TO .f.
Default Fetchall TO .f.
/* Procura pela tabela no array */
i := ASCAN(aTableTemp, {|aVal| aVal[DB_ALIAS] == cAlias})
/* Procura pela tabela no array */
i := ASCAN(aTableTemp, {|aVal| aVal[DB_ALIAS] == cAlias})
IF i != 0
/* Traz registros da base de dados */
IF i != 0
/* Traz registros da base de dados */
oQuery := aTableTemp[i, DB_QUERY]
nPos := aTableTemp[i, DB_ROW] + 1
oQuery := aTableTemp[i, DB_QUERY]
nPos := aTableTemp[i, DB_ROW] + 1
if Fetchall
aTableTemp[i, DB_FETCH] := .t.
endif
if Fetchall
aTableTemp[i, DB_FETCH] := .t.
endif
IF oQuery:Lastrec() >= nPos
IF oQuery:Lastrec() >= nPos
y := nPos
y := nPos
do while nPos <= IIF( FetchAll, oQuery:Lastrec(), y )
oRow := oQuery:GetRow(nPos)
DBAppend()
do while nPos <= IIF( FetchAll, oQuery:Lastrec(), y )
oRow := oQuery:GetRow(nPos)
DBAppend()
FOR x := 1 TO oRow:FCount()
FieldPut( FieldPos( oRow:FieldName(x) ), oRow:FieldGet(x) )
NEXT
FOR x := 1 TO oRow:FCount()
FieldPut( FieldPos( oRow:FieldName(x) ), oRow:FieldGet(x) )
NEXT
aTableTemp[i, DB_ROW] := nPos
nPos++
enddo
aTableTemp[i, DB_ROW] := nPos
nPos++
enddo
ELSE
// Posiciona registro no eof
DBSkip()
ELSE
// Posiciona registro no eof
DBSkip()
ENDIF
ENDIF
lEof := nPos > oQuery:Lastrec()
ENDIF
return lEof
lEof := nPos > oQuery:Lastrec()
ENDIF
return lEof
Procedure SQLFetchAll()
SQLFetch(.t.); DBGotop()
Return
SQLFetch(.t.); DBGotop()
Return
Function SQLOpen( cAlias, cQuery, xFetch, cOrder )
Local cFile
Local Result := .t.
Local x
Local oServer
Local oQuery
Local aStrudbf
Local lFetch
Local cFile
Local Result := .t.
Local x
Local oServer
Local oQuery
Local aStrudbf
Local lFetch
oServer := SQLCurrentServer()
cAlias := Upper(cAlias)
oServer := SQLCurrentServer()
cAlias := Upper(cAlias)
/* Procura por query na area temporaria */
x := ASCAN(aTableTemp, {|aVal| aVal[DB_ALIAS] == cAlias})
/* Procura por query na area temporaria */
x := ASCAN(aTableTemp, {|aVal| aVal[DB_ALIAS] == cAlias})
IF ! Empty(x)
IF ! Empty(x)
oQuery := aTableTemp[x, 3]
oQuery:Destroy()
ENDIF
ENDIF
IF cQuery == NIL
IF cQuery == NIL
cQuery := 'SELECT * FROM ' + cAlias
IF ! Empty( cOrder )
cQuery += ' ORDER BY ' + cOrder
cQuery += ' ORDER BY ' + cOrder
ENDIF
ENDIF
ENDIF
cQuery := cQuery
oQuery := oServer:Query(cQuery)
cQuery := cQuery
oQuery := oServer:Query(cQuery)
IF oQuery:NetErr()
Alert(oQuery:ErrorMsg())
RETURN .F.
ENDIF
IF oQuery:NetErr()
Alert(oQuery:ErrorMsg())
RETURN .F.
ENDIF
IF Empty(Select(cAlias))
/* Pega estrutura da base de dados */
aStrudbf := oQuery:Struct()
IF Empty(Select(cAlias))
/* Pega estrutura da base de dados */
aStrudbf := oQuery:Struct()
/* Cria tabela */
cFile := TempFile()
DBCreate( cFile, aStrudbf )
/* Cria tabela */
cFile := TempFile()
DBCreate( cFile, aStrudbf )
/* Abre Tabela */
DBUseArea(.T., NIL, cFile, cAlias, .F.)
/* Abre Tabela */
DBUseArea(.T., NIL, cFile, cAlias, .F.)
ELSE
SELECT &cAlias
Zap
ELSE
SELECT &cAlias
Zap
ENDIF
ENDIF
IF xFetch != NIL
lFetch := xFetch
ELSE
lFetch := .F.
ENDIF
IF xFetch != NIL
lFetch := xFetch
ELSE
lFetch := .F.
ENDIF
/* Se nao houver query na area temporaria entao adiciona, caso contrario, apenas atualiza */
IF Empty(x)
AADD( aTableTemp, { cAlias,; // Table Name
cFile,; // Temporary File Name
oQuery,; // Object Query
0,; // Current Row
lFetch } ) // Fetch Status
ELSE
/* Se nao houver query na area temporaria entao adiciona, caso contrario, apenas atualiza */
IF Empty(x)
AADD( aTableTemp, { cAlias,; // Table Name
cFile,; // Temporary File Name
oQuery,; // Object Query
0,; // Current Row
lFetch } ) // Fetch Status
ELSE
aTableTemp[ x, DB_QUERY ] := oQuery
aTableTemp[ x, DB_ROW ] := 0
aTableTemp[ x, DB_FETCH ] := lFetch
aTableTemp[ x, DB_QUERY ] := oQuery
aTableTemp[ x, DB_ROW ] := 0
aTableTemp[ x, DB_FETCH ] := lFetch
ENDIF
ENDIF
/* Traz registros da base de dados */
SQLFetch(lFetch)
/* Traz registros da base de dados */
SQLFetch(lFetch)
IF lFetch
DBGotop()
ENDIF
IF lFetch
DBGotop()
ENDIF
Return result
Return result
Function SQLConnect( cServer, cDatabase, cUser, cPassword, cSchema )
Local lRetval := .t.
Local lRetval := .t.
oServer := TPQServer():New(cServer, cDatabase, cUser, cPassWord, 5432, cSchema)
if oServer:NetErr()
Alert(oServer:ErrorMsg())
lRetval := .f.
endif
oServer:lAllCols := .F.
Return lRetval
oServer := TPQServer():New(cServer, cDatabase, cUser, cPassWord, 5432, cSchema)
if oServer:NetErr()
Alert(oServer:ErrorMsg())
lRetval := .f.
endif
oServer:lAllCols := .F.
Return lRetval
Procedure SQLDestroy()
if oServer != NIL
oServer:Destroy()
endif
return
if oServer != NIL
oServer:Destroy()
endif
return
Function SQLCurrentServer
Return oServer
Return oServer
Function SQLQuery( cQuery )
Local oQuery
Local oQuery
oQuery := oServer:Query(cQuery)
IF oQuery:NetErr()
Alert(cQuery + ':' + oQuery:ErrorMsg())
ENDIF
Return oQuery
oQuery := oServer:Query(cQuery)
IF oQuery:NetErr()
Alert(cQuery + ':' + oQuery:ErrorMsg())
ENDIF
Return oQuery
Function SQLExecQuery( cQuery )
Local oQuery
Local result := .T.
Local oQuery
Local result := .T.
oQuery := oServer:Query(cQuery)
IF oQuery:NetErr()
Alert('Nao foi possível executar ' + cQuery + ':' + oQuery:ErrorMsg())
oQuery := oServer:Query(cQuery)
IF oQuery:NetErr()
Alert('Nao foi possível executar ' + cQuery + ':' + oQuery:ErrorMsg())
result := .F.
ELSE
oQuery:Destroy()
ENDIF
Return result
result := .F.
ELSE
oQuery:Destroy()
ENDIF
Return result
Function SQLPrepare( cQuery, ... )
Local i, x
Local i, x
if Pcount() >= 2
/* Limpa espacos desnecessarios */
do while at( Space(2), cQuery ) != 0
cQuery := strtran( cQuery, Space(2), Space(1) )
enddo
if Pcount() >= 2
/* Limpa espacos desnecessarios */
do while at( Space(2), cQuery ) != 0
cQuery := strtran( cQuery, Space(2), Space(1) )
enddo
/* Coloca {} nos parametros */
for i := 1 to Pcount() - 1
if ! empty(x := at( ':' + ltrim(str(i)), cQuery))
/* Coloca {} nos parametros */
for i := 1 to Pcount() - 1
if ! empty(x := at( ':' + ltrim(str(i)), cQuery))
cQuery := stuff( cQuery, x, 0, '{' )
cQuery := stuff( cQuery, x + len(ltrim(str(i))) + 2, 0, '}' )
endif
next
endif
next
/* Substitui parametros por valores passados */
for i := 2 to PCount()
x := hb_PValue(i)
/* Substitui parametros por valores passados */
for i := 2 to PCount()
x := hb_PValue(i)
if x != NIL .and. Empty(x)
x := 'null'
if x != NIL .and. Empty(x)
x := 'null'
elseif valtype(x) == 'N'
x := ltrim(str(x))
elseif valtype(x) == 'N'
x := ltrim(str(x))
elseif valtype(x) == 'D'
x := DtoQ(x)
elseif valtype(x) == 'D'
x := DtoQ(x)
elseif valtype(x) == 'L'
x := IIF( x, "'t'", "'f'" )
elseif valtype(x) == 'L'
x := IIF( x, "'t'", "'f'" )
elseif valtype(x) == "C" .or. valtype(x) == 'M'
x := StoQ(Trim(x))
elseif valtype(x) == "C" .or. valtype(x) == 'M'
x := StoQ(Trim(x))
else
x := 'null'
endif
else
x := 'null'
endif
cQuery := strtran(cQuery, '{:' + ltrim(str(i-1)) + '}', x)
next
endif
cQuery := strtran(cQuery, '{:' + ltrim(str(i-1)) + '}', x)
next
endif
cQuery := strtran(cQuery, '==', '=')
cQuery := strtran(cQuery, '!=', '<>')
cQuery := strtran(cQuery, '.and.', 'and')
cQuery := strtran(cQuery, '.or.', 'or')
cQuery := strtran(cQuery, '.not.', 'not')
Return cQuery
cQuery := strtran(cQuery, '==', '=')
cQuery := strtran(cQuery, '!=', '<>')
cQuery := strtran(cQuery, '.and.', 'and')
cQuery := strtran(cQuery, '.or.', 'or')
cQuery := strtran(cQuery, '.not.', 'not')
Return cQuery
/* Pega resultado de uma sequence */
Function SQLSequence( Sequence_name )
Local nValue
nValue := Val(QuickQuery("SELECT nextval(" + StoQ(sequence_name) + ")" ))
Return nValue
Local nValue
nValue := Val(QuickQuery("SELECT nextval(" + StoQ(sequence_name) + ")" ))
Return nValue
Function SQLStartTrans()
if PQtransactionstatus(oServer:pDB) != PQTRANS_INTRANS
oServer:StartTransaction()
endif
Return nil
if PQtransactionstatus(oServer:pDB) != PQTRANS_INTRANS
oServer:StartTransaction()
endif
Return nil
Function SQLInTrans()
Local result
result := (PQtransactionstatus(oServer:pDB) == PQTRANS_INTRANS)
Return result
Local result
result := (PQtransactionstatus(oServer:pDB) == PQTRANS_INTRANS)
Return result
Function SQLCommitTrans()
oServer:Commit()
Return nil
oServer:Commit()
Return nil
Function SQLRollbackTrans()
oServer:rollback()
Return nil
oServer:rollback()
Return nil
/* Faz querie que retorna apenas 1 valor de coluna */
@@ -553,80 +512,81 @@ Function QuickQuery( cQuery )
pQuery := PQexec( oServer:pDB, cQuery )
if PQresultstatus(pQuery) == PGRES_TUPLES_OK
if PQLastrec(pQuery) != 0
if PQFcount(pQuery) == 1 .and. PQLastrec(pQuery) == 1
temp := PQGetValue( pQuery, 1, 1 )
result := iif( temp == NIL, "", temp )
else
result := {}
for x := 1 to PQLastrec(pQuery)
aTemp := {}
for y := 1 to PQfcount(pQuery)
temp := PQGetValue( pQuery, x, y )
aadd( aTemp, iif( temp == NIL, "", temp ) )
next
aadd(result, aTemp)
next
endif
endif
if PQLastrec(pQuery) != 0
if PQFcount(pQuery) == 1 .and. PQLastrec(pQuery) == 1
temp := PQGetValue( pQuery, 1, 1 )
result := iif( temp == NIL, "", temp )
else
result := {}
for x := 1 to PQLastrec(pQuery)
aTemp := {}
for y := 1 to PQfcount(pQuery)
temp := PQGetValue( pQuery, x, y )
aadd( aTemp, iif( temp == NIL, "", temp ) )
next
aadd(result, aTemp)
next
endif
endif
endif
Return result
Return result
Procedure MakeDBF( cAlias, aStructure, aIndex )
Local cFile, i, cIndex, cKey
Local cFile, i, cIndex, cKey
Default aIndex TO {}
Default aIndex TO {}
cFile := TempFile()
DBCreate( cFile, aStructure )
cFile := TempFile()
DBCreate( cFile, aStructure )
/* Abre Tabela */
DBUseArea(.T., NIL, cFile, cAlias, .F.)
/* Abre Tabela */
DBUseArea(.T., NIL, cFile, cAlias, .F.)
For i := 1 to Len(aIndex)
cKey := aIndex[i]
cIndex := TempFile()
Index On &cKey To &cIndex
For i := 1 to Len(aIndex)
cKey := aIndex[i]
cIndex := TempFile()
Index On &cKey To &cIndex
aadd( aTempDBF, cIndex)
Next
aadd( aTempDBF, cIndex)
Next
AADD( aTempDBF, cFile )
return
AADD( aTempDBF, cFile )
return
Function DirTmp()
Local xDirectory
xDirectory := IIF(Empty(Getenv("TMP")), Getenv("TEMP"), Getenv("TMP"))
Local xDirectory
xDirectory := IIF(Empty(Getenv("TMP")), Getenv("TEMP"), Getenv("TMP"))
IF Empty(xDirectory)
xDirectory := ''
ENDIF
IF Empty(xDirectory)
xDirectory := ''
ENDIF
IF ';' $ xDirectory
xDirectory := LEFT( xDirectory, AT( ';', xDirectory ) - 1 )
ENDIF
IF ';' $ xDirectory
xDirectory := LEFT( xDirectory, AT( ';', xDirectory ) - 1 )
ENDIF
RETURN xDirectory + IIF( Right(xDirectory, 1) != '\' .and. ! Empty(xDirectory), '\', '' )
RETURN xDirectory + IIF( Right(xDirectory, 1) != '\' .and. ! Empty(xDirectory), '\', '' )
Function TempFile( cPath, cExt )
Local cString
Local cString
Default cPath to DirTmp(),;
cExt to 'tmp'
Default cPath to DirTmp(),;
cExt to 'tmp'
cString := cPath + strzero(int(hb_random(val(strtran(time(), ":", "")))), 8) + '.' + cExt
cString := cPath + strzero(int(hb_random(val(strtran(time(), ":", "")))), 8) + '.' + cExt
DO WHILE File( cString )
cString := cPath + strzero(int(hb_random(val(strtran(time(), ":", "")))), 8) + '.' + cExt
ENDDO
Return cString
DO WHILE File( cString )
cString := cPath + strzero(int(hb_random(val(strtran(time(), ":", "")))), 8) + '.' + cExt
ENDDO
Return cString
Function DtoQ(cData)
Return "'" + Str(Year(cData),4) + "-" + StrZero(Month(cData), 2) + "-" + StrZero(Day(cData), 2) + "'"
Return "'" + Str(Year(cData),4) + "-" + StrZero(Month(cData), 2) + "-" + StrZero(Day(cData), 2) + "'"
Function StoQ(cData)
Return "'" + cData + "'"
Return "'" + cData + "'"

View File

@@ -302,7 +302,8 @@ procedure main(...)
close all
oTable:Destroy()
oServer:Destroy()
return
return
procedure Help()
@@ -323,4 +324,4 @@ procedure Help()
? ""
return
return

View File

@@ -71,7 +71,7 @@ FUNCTION Main( cHost, cDatabase, cUser, cPass )
FOR i := 1 TO 10
cQuery := "INSERT INTO test(code, dept, name, sales, tax, salary, budget, Discount, Creation, Description) "
cQuery += "VALUES( " + Str( i ) + ", 2, "TEST", "y", 5, 3000, 1500.2, 7.5, "01-01-2003", "Short Description about what ? ")"
cQuery += "VALUES( " + Str( i ) + ", 2, "TEST", "y", 5, 3000, 1500.2, 7.5, "2003-12-17", "Short Description about what ? ")"
oQuery := oServer:Query( cQuery )

View File

@@ -7,121 +7,121 @@
*/
#include "common.ch"
#include "../postgres.ch"
#include "postgres.ch"
Function Main( cServer, cDatabase, cUser, cPass )
Local conn, res, i, x
Local conn, res, i, x
Local cQuery
Local cQuery
CLEAR SCREEN
CLEAR SCREEN
? 'Connecting....'
conn := PQconnect(cDatabase, cServer, cUser, cPass, 5432)
? 'Connecting....'
conn := PQconnect(cDatabase, cServer, cUser, cPass, 5432)
? PQstatus(conn), PQerrormessage(conn)
? PQstatus(conn), PQerrormessage(conn)
if PQstatus(conn) != CONNECTION_OK
quit
endif
if PQstatus(conn) != CONNECTION_OK
quit
endif
? 'Dropping table...'
? 'Dropping table...'
res := PQexec(conn, 'DROP TABLE test')
res := NIL
res := PQexec(conn, 'DROP TABLE test')
res := NIL
? 'Creating test table...'
cQuery := 'CREATE TABLE test('
cQuery += ' Code integer not null primary key, '
cQuery += ' dept Integer, '
cQuery += ' Name Varchar(40), '
cQuery += ' Sales boolean, '
cQuery += ' Tax Float4, '
cQuery += ' Salary Double Precision, '
cQuery += ' Budget Numeric(12,2), '
cQuery += ' Discount Numeric (5,2), '
cQuery += ' Creation Date, '
cQuery += ' Description text ) '
? 'Creating test table...'
cQuery := 'CREATE TABLE test('
cQuery += ' Code integer not null primary key, '
cQuery += ' dept Integer, '
cQuery += ' Name Varchar(40), '
cQuery += ' Sales boolean, '
cQuery += ' Tax Float4, '
cQuery += ' Salary Double Precision, '
cQuery += ' Budget Numeric(12,2), '
cQuery += ' Discount Numeric (5,2), '
cQuery += ' Creation Date, '
cQuery += ' Description text ) '
res := PQexec(conn, cQuery)
res := NIL
res := PQexec(conn, cQuery)
res := NIL
res := PQexec(conn, 'SELECT code, dept, name, sales, salary, creation FROM test')
res := NIL
res := PQexec(conn, 'SELECT code, dept, name, sales, salary, creation FROM test')
res := NIL
res := PQexec(conn, 'BEGIN')
res := NIL
res := PQexec(conn, 'BEGIN')
res := NIL
For i := 1 to 10000
@ 15,0 say 'Inserting values....' + str(i)
For i := 1 to 10000
@ 15,0 say 'Inserting values....' + str(i)
cQuery := 'INSERT INTO test(code, dept, name, sales, salary, creation) '
cQuery += 'VALUES( ' + str(i) + ',' + str(i+1) + ", 'DEPARTMENT NAME " + strzero(i) + "', 'y', " + str(300.49+i) + ", '2003-12-28' )"
cQuery := 'INSERT INTO test(code, dept, name, sales, salary, creation) '
cQuery += 'VALUES( ' + str(i) + ',' + str(i+1) + ", 'DEPARTMENT NAME " + strzero(i) + "', 'y', " + str(300.49+i) + ", '2003-12-28' )"
res := PQexec(conn, cQuery)
res := NIL
res := PQexec(conn, cQuery)
res := NIL
if mod(i,100) == 0
? res := PQexec(conn, 'COMMIT')
? res := NIL
if mod(i,100) == 0
? res := PQexec(conn, 'COMMIT')
? res := NIL
? res := PQexec(conn, 'BEGIN')
? res := NIL
endif
Next
? res := PQexec(conn, 'BEGIN')
? res := NIL
endif
Next
For i := 5000 to 7000
@ 16,0 say 'Deleting values....' + str(i)
For i := 5000 to 7000
@ 16,0 say 'Deleting values....' + str(i)
cQuery := 'DELETE FROM test WHERE code = ' + str(i)
res := PQexec(conn, cQuery)
res := NIL
cQuery := 'DELETE FROM test WHERE code = ' + str(i)
res := PQexec(conn, cQuery)
res := NIL
if mod(i,100) == 0
res := PQexec(conn, 'COMMIT')
res := NIL
if mod(i,100) == 0
res := PQexec(conn, 'COMMIT')
res := NIL
res := PQexec(conn, 'BEGIN')
res := NIL
endif
Next
res := PQexec(conn, 'BEGIN')
res := NIL
endif
Next
For i := 2000 to 3000
@ 17,0 say 'Updating values....' + str(i)
For i := 2000 to 3000
@ 17,0 say 'Updating values....' + str(i)
cQuery := 'UPDATE FROM test SET salary = 400 WHERE code = ' + str(i)
res := PQexec(conn, cQuery)
res := NIL
cQuery := 'UPDATE FROM test SET salary = 400 WHERE code = ' + str(i)
res := PQexec(conn, cQuery)
res := NIL
if mod(i,100) == 0
res := PQexec(conn, 'COMMIT')
res := NIL
if mod(i,100) == 0
res := PQexec(conn, 'COMMIT')
res := NIL
res := PQexec(conn, 'BEGIN')
res := NIL
endif
Next
res := PQexec(conn, 'BEGIN')
res := NIL
endif
Next
res := PQexec(conn, 'SELECT sum(salary) as sum_salary FROM test WHERE code between 1 and 4000')
res := PQexec(conn, 'SELECT sum(salary) as sum_salary FROM test WHERE code between 1 and 4000')
if PQresultStatus(res) == PGRES_TUPLES_OK
@ 18,0 say 'Sum values....' + PQgetvalue(res, 1, 1)
endif
if PQresultStatus(res) == PGRES_TUPLES_OK
@ 18,0 say 'Sum values....' + PQgetvalue(res, 1, 1)
endif
res := NIL
res := NIL
x := 0
For i := 1 to 4000
res := PQexec(conn, 'SELECT salary FROM test WHERE code = ' + str(i))
x := 0
For i := 1 to 4000
res := PQexec(conn, 'SELECT salary FROM test WHERE code = ' + str(i))
if PQresultStatus(res) == PGRES_TUPLES_OK
x += val(PQgetvalue(res, 1, 1))
if PQresultStatus(res) == PGRES_TUPLES_OK
x += val(PQgetvalue(res, 1, 1))
@ 19,0 say 'Sum values....' + str(x)
endif
Next
@ 19,0 say 'Sum values....' + str(x)
endif
Next
? "Closing..."
conn := NIL
? "Closing..."
conn := NIL
return nil
return nil

View File

@@ -2,98 +2,98 @@
* $Id$
*/
#include "../postgres.ch"
#include "postgres.ch"
Function main()
Local conn, res, aTemp, x, y, pFile
Local cDb := 'test'
Local cUser := 'user'
Local cPass := 'pass'
Local conn, res, aTemp, x, y, pFile
Local cDb := 'test'
Local cUser := 'user'
Local cPass := 'pass'
CLEAR SCREEN
CLEAR SCREEN
conn := PQsetdbLogin( 'localhost', "5432", NIL, NIL, cDb, cUser, cPass)
? PQdb(conn), PQuser(conn), PQpass(conn), PQhost(conn), PQport(conn), PQtty(conn), PQoptions(conn)
? conn := NIL
conn := PQsetdbLogin( 'localhost', "5432", NIL, NIL, cDb, cUser, cPass)
? PQdb(conn), PQuser(conn), PQpass(conn), PQhost(conn), PQport(conn), PQtty(conn), PQoptions(conn)
? conn := NIL
conn := PQConnect(cDb, 'localhost', cuser, cpass, 5432)
conn := PQConnect(cDb, 'localhost', cuser, cpass, 5432)
? PQstatus(conn), PQerrormessage(conn)
? PQstatus(conn), PQerrormessage(conn)
if PQstatus(conn) != CONNECTION_OK
quit
endif
if PQstatus(conn) != CONNECTION_OK
quit
endif
? "Blocking: ", PQisnonblocking(conn), PQsetnonblocking(conn, .t.), PQisnonblocking(conn)
? "Blocking: ", PQisnonblocking(conn), PQsetnonblocking(conn, .t.), PQisnonblocking(conn)
pFile := PQcreatetrace( 'trace.log' )
PQtrace( conn, pFile )
pFile := PQcreatetrace( 'trace.log' )
PQtrace( conn, pFile )
? "Verbose: ", PQsetErrorVerbosity(conn, 2)
? "Verbose: ", PQsetErrorVerbosity(conn, 2)
? "Protocol: ", PQprotocolVersion(conn), ;
" Server Version: ", PQserverVersion(conn), ;
" Client Encoding: ", PQsetClientEncoding(conn, "ASCII"), ;
"New encode: ", PQclientEncoding(conn)
? "Protocol: ", PQprotocolVersion(conn), ;
" Server Version: ", PQserverVersion(conn), ;
" Client Encoding: ", PQsetClientEncoding(conn, "ASCII"), ;
"New encode: ", PQclientEncoding(conn)
? PQdb(conn), PQuser(conn), PQpass(conn), PQhost(conn), PQport(conn), PQtty(conn), PQoptions(conn)
? PQdb(conn), PQuser(conn), PQpass(conn), PQhost(conn), PQport(conn), PQtty(conn), PQoptions(conn)
res := PQexec('drop table products')
? PQresultStatus(res), PQresultErrorMessage(res)
res := NIL
res := PQexec('drop table products')
? PQresultStatus(res), PQresultErrorMessage(res)
res := NIL
res := PQexec('create table products ( product_no numeric(10), name varchar(20), price numeric(10,2) )')
? PQresultStatus(res), PQresultErrorMessage(res)
res := NIL
res := PQexec('create table products ( product_no numeric(10), name varchar(20), price numeric(10,2) )')
? PQresultStatus(res), PQresultErrorMessage(res)
res := NIL
res := PQexecParams(conn, 'insert into products(product_no, name, price) values ($1, $2, $3)', {'2', 'bread', '10.95'})
? "Oid Row: ", PQoidValue(res), PQoidStatus(res)
res := PQexecParams(conn, 'insert into products(product_no, name, price) values ($1, $2, $3)', {'2', 'bread', '10.95'})
? "Oid Row: ", PQoidValue(res), PQoidStatus(res)
if PQresultStatus(res) != PGRES_COMMAND_OK
? PQresultStatus(res), PQresultErrorMessage(res)
endif
res := NIL
if PQresultStatus(res) != PGRES_COMMAND_OK
? PQresultStatus(res), PQresultErrorMessage(res)
endif
res := NIL
res := PQexec(conn, 'select price, name, product_no as "produto" from products')
res := PQexec(conn, 'select price, name, product_no as "produto" from products')
if PQresultStatus(res) != PGRES_TUPLES_OK
? PQresultStatus(res), PQresultErrorMessage(res)
endif
if PQresultStatus(res) != PGRES_TUPLES_OK
? PQresultStatus(res), PQresultErrorMessage(res)
endif
? "Binary: ", PQbinaryTuples(res)
? "Rows: ", PQntuples(res), "Cols: ", PQnfields(res)
? PQfname(res, 1), PQftable(res, 1), PQftype(res, 1), PQfnumber(res, "name"), PQfmod(res, 1), PQfsize(res, 1), PQgetisnull(res,1,1)
? "Binary: ", PQbinaryTuples(res)
? "Rows: ", PQntuples(res), "Cols: ", PQnfields(res)
? PQfname(res, 1), PQftable(res, 1), PQftype(res, 1), PQfnumber(res, "name"), PQfmod(res, 1), PQfsize(res, 1), PQgetisnull(res,1,1)
aTemp := PQmetadata(res)
aTemp := PQmetadata(res)
for x := 1 to len(aTemp)
? "Linha 1: "
for y := 1 to 6
?? aTemp[x,y], ", "
next
next
for x := 1 to len(aTemp)
? "Linha 1: "
for y := 1 to 6
?? aTemp[x,y], ", "
next
next
? PQFcount(res)
? PQFcount(res)
? PQlastrec(res)
? PQlastrec(res)
? PQGetvalue(res,1, 2)
? PQGetvalue(res,1, 2)
? res := NIL
? res := NIL
? "Large Objects, always should be in a transaction..."
? "Large Objects, always should be in a transaction..."
res := PQexec(conn, 'begin')
res := NIL
res := PQexec(conn, 'begin')
res := NIL
? (x := lo_Import( conn, 'test.prg' ))
? lo_Export( conn, x, 'test.new' )
? lo_Unlink( conn, x )
? (x := lo_Import( conn, 'test.prg' ))
? lo_Export( conn, x, 'test.new' )
? lo_Unlink( conn, x )
res := PQexec(conn, 'commit')
res := NIL
res := PQexec(conn, 'commit')
res := NIL
PQuntrace( conn )
pFile := NIL
conn := NIL
return NIL
PQuntrace( conn )
pFile := NIL
conn := NIL
return NIL