Code: Select all
; else you get a polink error
UsePostgreSQLDatabase()
#INV_WRITE = $00020000
#INV_READ = $00040000
; else you get an error symbols not found
ImportC "oldnames.lib" : EndImport
ImportC "postgresql.lib"
PQconnectdb(Conn$) As "_PQconnectdb"
PQstatus(*PGconn) As "_PQstatus"
PQsendQuery(*PGconn, command$) As "_PQsendQuery"
PQgetResult(*PGconn) As "_PQgetResult"
PQclear(*PGresult) As "_PQclear"
PQfinish(*PGconn) As "_PQfinish"
pg_lo_create(*PGconn, OID.l=0) As "_lo_create"
pg_lo_open(*PGconn, oid.l, mode.l) As "_lo_open"
pg_lo_read(*PGconn, fd.l, *buffer, size.l) As "_lo_read"
pg_lo_write(*PGconn, fd.l, *buffer, size.l) As "_lo_write"
pg_lo_close(*PGconn, fd.l) As "_lo_close"
pg_lo_import(*PGconn, Filename$) As "_lo_import"
pg_lo_export(*PGconn, OID.l, Filename$) As "_lo_export"
pg_lo_unlink(*PGconn, OID.l) As "_lo_unlink"
EndImport
Procedure.i PGSaveOidDataFromFile(Connect$, Filename$)
Protected Result.i, SQL$, OID.l, *PGConn, *PGresult
*PGConn = PQconnectdb(Connect$)
Debug *PGConn
Debug PQstatus(*PGConn)
SQL$ = "BEGIN;"
PQsendQuery(*PGConn, SQL$)
Repeat
*PGresult = PQgetResult(*PGConn)
If *PGresult
PQclear(*PGresult)
EndIf
Until *PGresult = #Null
OID = pg_lo_import(*PGconn, Filename$)
Debug OID
Result = OID
If Result > 0
SQL$ = "INSERT INTO " + #DQUOTE$ + "Aktuell" + #DQUOTE$ + ".dokumente (datei_name, datei_oid, datei_groesse) VALUES (E'" + ReplaceString(Filename$,"\", "\\") + "', '" + Str(OID) + "'," + Str(FileSize(Filename$)) + ");"
Debug SQL$
PQsendQuery(*PGConn, SQL$)
Repeat
*PGresult = PQgetResult(*PGConn)
If *PGresult
PQclear(*PGresult)
EndIf
Until *PGresult = #Null
EndIf
SQL$ = "COMMIT;"
PQsendQuery(*PGConn, SQL$)
Repeat
*PGresult = PQgetResult(*PGConn)
If *PGresult
PQclear(*PGresult)
EndIf
Until *PGresult = #Null
PQfinish(*PGConn)
ProcedureReturn Result
EndProcedure
Procedure.i PGLoadOIDDataToFile(Connect$, Filename$, OID.l)
Protected Result.i, *PGconn, SQL$, *PGresult
*PGConn = PQconnectdb(Connect$)
Debug *PGConn
Debug PQstatus(*PGConn)
SQL$ = "BEGIN;"
PQsendQuery(*PGConn, SQL$)
Repeat
*PGresult = PQgetResult(*PGConn)
If *PGresult
PQclear(*PGresult)
EndIf
Until *PGresult = #Null
If pg_lo_export(*PGConn, OID, Filename$) > 0
Result = #True
EndIf
SQL$ = "COMMIT;"
PQsendQuery(*PGConn, SQL$)
Repeat
*PGresult = PQgetResult(*PGConn)
If *PGresult
PQclear(*PGresult)
EndIf
Until *PGresult = #Null
PQfinish(*PGConn)
ProcedureReturn Result
EndProcedure
Procedure.i PGSaveOidDataFromMemory(Connect$, *Buffer, Length.i)
Protected Result.i, SQL$, OID.l, OIDHandle.i, *PGConn, *PGresult
*PGConn = PQconnectdb(Connect$)
Debug *PGConn
Debug PQstatus(*PGConn)
SQL$ = "BEGIN;"
PQsendQuery(*PGConn, SQL$)
Repeat
*PGresult = PQgetResult(*PGConn)
If *PGresult
PQclear(*PGresult)
EndIf
Until *PGresult = #Null
OID = pg_lo_create(*PGconn)
Debug OID
SQL$ = "INSERT INTO " + #DQUOTE$ + "Aktuell" + #DQUOTE$ + ".dokumente (datei_name, datei_oid, datei_groesse) VALUES ('" + Filename$ + "', '" + Str(OID) + "'," + Str(Size) + ");"
Debug SQL$
PQsendQuery(*PGConn, SQL$)
Repeat
*PGresult = PQgetResult(*PGConn)
If *PGresult
PQclear(*PGresult)
EndIf
Until *PGresult = #Null
OIDHandle = pg_lo_open(*PGConn, OID, #INV_WRITE)
If OIDHandle > -1
If pg_lo_write(*PGConn, OIDHandle, *Buffer, Length) = Length
Result = #True
EndIf
pg_lo_close(*PGConn, OIDHandle)
EndIf
SQL$ = "COMMIT;"
PQsendQuery(*PGConn, SQL$)
Repeat
*PGresult = PQgetResult(*PGConn)
If *PGresult
PQclear(*PGresult)
EndIf
Until *PGresult = #Null
PQfinish(*PGConn)
ProcedureReturn Result
EndProcedure
Procedure.i PGLoadOIDDataToMemory(Connect$, *Buffer, Length.i, OID.l)
Protected Result.i, *PGconn, SQL$, OIDHandle.l, *PGresult
*PGConn = PQconnectdb(Connect$)
Debug *PGConn
Debug PQstatus(*PGConn)
SQL$ = "BEGIN;"
PQsendQuery(*PGConn, SQL$)
Repeat
*PGresult = PQgetResult(*PGConn)
If *PGresult
PQclear(*PGresult)
EndIf
Until *PGresult = #Null
OIDHandle = pg_lo_open(*PGConn, OID, #INV_READ)
If OIDHandle > -1
Debug "-----"
If pg_lo_read(*PGConn, OIDHandle, *Buffer, Length) > 0
Result = #True
EndIf
pg_lo_close(*PGConn, OIDHandle)
EndIf
SQL$ = "COMMIT;"
PQsendQuery(*PGConn, SQL$)
Repeat
*PGresult = PQgetResult(*PGConn)
If *PGresult
PQclear(*PGresult)
EndIf
Until *PGresult = #Null
PQfinish(*PGConn)
ProcedureReturn Result
EndProcedure
Procedure PGDeleteOIDData(Connect$, OID.l)
Protected *PGConn, SQL$, *PGresult
*PGConn = PQconnectdb(Connect$)
Debug *PGConn
Debug PQstatus(*PGConn)
SQL$ = "BEGIN;"
PQsendQuery(*PGConn, SQL$)
Repeat
*PGresult = PQgetResult(*PGConn)
If *PGresult
PQclear(*PGresult)
EndIf
Until *PGresult = #Null
If pg_lo_unlink(*PGConn, OID) > 0
Result = #True
EndIf
SQL$ = "COMMIT;"
PQsendQuery(*PGConn, SQL$)
Repeat
*PGresult = PQgetResult(*PGConn)
If *PGresult
PQclear(*PGresult)
EndIf
Until *PGresult = #Null
PQfinish(*PGConn)
ProcedureReturn Result
EndProcedure
Define Connect$, Filename$, OID.i
Connect$ = "host=xxx.xxx.xxx.xxx dbname=xxx user=xxx password=xxx"
Filename$ = OpenFileRequester("Choose a file", "", "*.*", 0)
If Filename$
Debug Filename$
OID = PGSaveOidDataFromFile(Connect$, Filename$)
PGLoadOIDDataToFile(Connect$, Filename$ + "." + GetExtensionPart(Filename$), OID)
PGDeleteOIDData(Connect$, OID)
EndIf
You need to adjust the Connect$ and the INSERT commands.