diff options
Diffstat (limited to 'src/interfaces/libpq++/pglobject.cc')
-rw-r--r-- | src/interfaces/libpq++/pglobject.cc | 201 |
1 files changed, 88 insertions, 113 deletions
diff --git a/src/interfaces/libpq++/pglobject.cc b/src/interfaces/libpq++/pglobject.cc index 4de4a1f0d9e..917a368f50c 100644 --- a/src/interfaces/libpq++/pglobject.cc +++ b/src/interfaces/libpq++/pglobject.cc @@ -1,154 +1,129 @@ - /*------------------------------------------------------------------------- * * FILE * pglobject.cc * * DESCRIPTION - * implementation of the PGlobj class. - * PGlobj encapsulates a frontend to backend connection + * implementation of the PgLargeObject class. + * PgLargeObject encapsulates a frontend to backend connection * * Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pglobject.cc,v 1.2 1996/11/12 11:42:31 bryanh Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq++/Attic/pglobject.cc,v 1.3 1997/02/13 10:00:34 scrappy Exp $ * *------------------------------------------------------------------------- */ -#include "libpq++.H" - + extern "C" { -#include <unistd.h> -#include <fcntl.h> #include "libpq/libpq-fs.h" } +#include "pglobject.h" + + + +// **************************************************************** +// +// PgLargeObject Implementation +// +// **************************************************************** // default constructor // creates a large object in the default database -PGlobj::PGlobj() : PGconnection::PGconnection() { - object = lo_creat(conn, INV_READ|INV_WRITE); - if (object == 0) { - sprintf(errorMessage, "PGlobj: can't create large object"); - } - fd = lo_open(conn, object, INV_READ|INV_WRITE); - if (fd < 0) { - sprintf(errorMessage, "PGlobj: can't open large object %d", object); - } else - sprintf(errorMessage, "PGlobj: created and opened large object %d", - object); - +PgLargeObject::PgLargeObject(const char* dbName) + : PgConnection(dbName) +{ + Init(); + Create(); + Open(); } // constructor // open an existing large object in the default database -PGlobj::PGlobj(Oid lobjId) : PGconnection::PGconnection() { - object = lobjId; - fd = lo_open(conn, object, INV_READ|INV_WRITE); - if (fd < 0) { - sprintf(errorMessage, "PGlobj: can't open large object %d", object); - } else - sprintf(errorMessage, "PGlobj: opened large object %d", - object); +PgLargeObject::PgLargeObject(Oid lobjId, const char* dbName) + : PgConnection(dbName) +{ + Init(lobjId); + if ( !pgObject ) + Create(); + Open(); } // constructor // create a large object in the given database -PGlobj::PGlobj(PGenv* env, char* dbName) : PGconnection::PGconnection(env,dbName) { - object = lo_creat(conn, INV_READ|INV_WRITE); - if (object == 0) { - sprintf(errorMessage, "PGlobj: can't create large object"); - } - fd = lo_open(conn, object, INV_READ|INV_WRITE); - if (fd < 0) { - sprintf(errorMessage, "PGlobj: can't open large object %d", object); - } else - sprintf(errorMessage, "PGlobj: created and opened large object %d", - object); +PgLargeObject::PgLargeObject(const PgEnv& env, const char* dbName) + : PgConnection(env, dbName) +{ + Init(); + Create(); + Open(); } // constructor // open an existing large object in the given database -PGlobj::PGlobj(PGenv* env, char* dbName, Oid lobjId) : PGconnection::PGconnection(env,dbName) { - object = lobjId; - fd = lo_open(conn, object, INV_READ|INV_WRITE); - if (fd < 0) { - sprintf(errorMessage, "PGlobj: can't open large object %d", object); - } else - sprintf(errorMessage, "PGlobj: created and opened large object %d", - object); +PgLargeObject::PgLargeObject(const PgEnv& env, const char* dbName, Oid lobjId) + : PgConnection(env, dbName) +{ + Init(lobjId); + if ( !pgObject ) + Create(); + Open(); } -// PGlobj::unlink -// destruct large object and delete from it from the database -int -PGlobj::unlink() { - int temp = lo_unlink(conn, object); - if (temp) { - return temp; - } else { - delete this; - return temp; - } +// destructor -- closes large object +PgLargeObject::~PgLargeObject() +{ + Close(); } -// PGlobj::import -- import a given file into the large object -int -PGlobj::import(char* filename) { - char buf[BUFSIZE]; - int nbytes, tmp; - int in_fd; - - // open the file to be read in - in_fd = open(filename, O_RDONLY, 0666); - if (in_fd < 0) { /* error */ - sprintf(errorMessage, "PGlobj::import: can't open unix file\"%s\"", filename); - return -1; - } - - // read in from the Unix file and write to the inversion file - while ((nbytes = ::read(in_fd, buf, BUFSIZE)) > 0) { - tmp = lo_write(conn, fd, buf, nbytes); - if (tmp < nbytes) { - sprintf(errorMessage, "PGlobj::import: error while reading \"%s\"", - filename); - return -1; - } - } - - (void) close(in_fd); - return 0; +// PgLargeObject::Init +// Initialize the variables +void PgLargeObject::Init(Oid lobjId) +{ + pgFd = -1; + pgObject = lobjId; } -// PGlobj::export -- export large object to given file -int -PGlobj::export(char* filename) { - int out_fd; - char buf[BUFSIZE]; - int nbytes, tmp; - - // open the file to be written to - out_fd = open(filename, O_CREAT|O_WRONLY, 0666); - if (out_fd < 0) { /* error */ - sprintf(errorMessage, "PGlobj::export: can't open unix file\"%s\"", - filename); - return -1; - } +// PgLargeObject::create +// create large object and check for errors +void PgLargeObject::Create() +{ + // Create the object + pgObject = lo_creat(pgConn, INV_READ|INV_WRITE); + + // Check for possible errors + if (!pgObject) + SetErrorMessage( "PgLargeObject: can't create large object" ); +} - // read in from the Unix file and write to the inversion file - while ((nbytes = lo_read(conn, fd, buf, BUFSIZE)) > 0) { - tmp = ::write(out_fd, buf, nbytes); - if (tmp < nbytes) { - sprintf(errorMessage,"PGlobj::export: error while writing \"%s\"", - filename); - return -1; - } - } - (void) close(out_fd); - return 0; +// PgLargeObject::open +// open large object and check for errors +void PgLargeObject::Open() +{ + // Open the object + pgFd = lo_open(pgConn, pgObject, INV_READ|INV_WRITE); + + // Check for possible errors + string objStr( IntToString(pgObject) ); + if (pgFd < 0) + SetErrorMessage( "PgLargeObject: can't open large object " + objStr ); + else + SetErrorMessage( "PgLargeObject: created and opened large object " + objStr ); } -// default destructor -- closes large object -PGlobj::~PGlobj() { - if (fd >= 0) - lo_close(conn, fd); +// PgLargeObject::unlink +// destruct large object and delete from it from the database +int PgLargeObject::Unlink() +{ + // Unlink the object + int temp = lo_unlink(pgConn, pgObject); + + // Initialize the large object upon success + if (!temp) { + Close(); + Init(); + } + + // Return the status + return temp; } |