RWDBBlob
Class
RWDBBlob provides memory management services and access to a binary large object (blob) with arbitrary data and length. Class
RWDBBlob is derived from
RWCollectable, so all features of a collectable are supported, including persistance. Class
RWDBBlob does not provide any semantic interpretation of its data; it is designed as a base class that applications can specialize to provide application-specific semantics.
In the following example, we illustrate how a hypothetical structure, struct SOUND, might be transferred between a database and program variables. To use this code, the <ver> placeholder in any library names would need to be replaced with the actual digits representing the library version number.
#include <fstream.h>
#include <rw/db/db.h>
struct SOUND {
char *data;
size_t length;
};
void playSound(const SOUND& s) {
// Hypothetical function which plays the sound s.
}
istream& operator>>(istream& iStream, SOUND& sound) {
// Hypothetical function that reads the next sound from
// the stream into sound.
}
main() {
SOUND sound;
RWDBBlob aBlob;
RWDBDatabase myDatabase = RWDBManager::database("odb<ver>12d.dll",
"simba_driver", "myUserName", "myPassword", "");
// Play all the sounds stored in the table.
RWDBTable soundTable = myDatabase.table("sounds");
RWDBReader aReader = soundTable.reader();
while (aReader()) { //1
aReader >> aBlob; //2
sound.length = aBlob.length(); //3
sound.data = aBlob.data(); //4
playSound(sound);
}
// Add all the sounds in input file soundfile.dat to
// the database table sounds.
ifstream soundStream("soundfile.dat");
while (soundStream) { //5
soundStream >> sound;
if (soundStream.eof() || soundStream.fail() ||
soundStream.bad()) { //6
break;
}
RWDBBlob newBlob(sound.data, sound.length); //7
RWDBInserter anInserter = soundTable.inserter(); //8
anInserter << newBlob; //9
anInserter.execute(); //10
}
The example transfers data back and forth between
SOUND,
sound, and the
RWDBBlobs,
aBlob and
newBlob.
The loop at
//1 uses a reader to iterate through a table holding
SOUNDs. The data is transferred to
aBlob from the reader on
//2. The
RWDBBlob member functions
length() and
data() are used on
//3 and
//4 to set the related components of sound.
The loop at
//5 reads from the input file stream and on
//6, we ensure that we read data successfully and break out of the loop if we don't. On
//7, we create an
RWDBBlob instance
newBlob using the data and length in the
sound instance. The
newBlob does not copy the data from
sound, but simply holds a pointer to the data, saving memory. On
//8 an
RWDBInserter is produced from the
soundTable. On
//9 the
newBlob is shifted into the inserter. On
//10 the
execute() method is invoked and the new sound is added to the database table.
Class
RWDBBlob can be used effectively with the bulk interface. Please see
Bulk Classes and RWDBBlob for an example.