Btrieve 2
Loading...
Searching...
No Matches
bcollection.cpp

This is an example of collection use written in C++.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stdint.h>
#include <string>
#include "btrieveCpp.h"
static char *btrieveCollectionName = (char *)"squaresAndSquareRoots.btr";
#define MIN_X 0
#define MAX_X 255
createCollection(BtrieveClient *btrieveClient)
{
// If CollectionCreate() fails.
if ((status = btrieveClient->CollectionCreate(btrieveCollectionName)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveClient::CollectionCreate():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
leave:
return status;
}
openCollection(BtrieveClient *btrieveClient, BtrieveCollection *btrieveCollection)
{
// If CollectionOpen() fails.
if ((status = btrieveClient->CollectionOpen(btrieveCollection, btrieveCollectionName)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveClient::CollectionOpen():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
leave:
return status;
}
loadCollection(BtrieveCollection *btrieveCollection)
{
int i;
char json[1024];
int iSquared;
double iSquareRoot;
for (i = MIN_X; i <= MAX_X; i++)
{
iSquared = i * i;
iSquareRoot = sqrt((double)i);
snprintf(json, sizeof(json),
"{ \"x\": %d, \"xSquared\": %d, \"xSquareRoot\": %f, \"text\": \"The square of %d is %d and its square root is %f.\" }",
i, iSquared, iSquareRoot, i, iSquared, iSquareRoot);
json[sizeof(json) - 1] = '\0';
// If DocumentCreate() fails.
if (btrieveCollection->DocumentCreate(json) < 0)
{
status = btrieveCollection->GetLastStatusCode();
printf("Error: BtrieveCollection::DocumentCreate():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
}
leave:
return status;
}
closeCollection(BtrieveClient *btrieveClient, BtrieveCollection *btrieveCollection)
{
// If CollectionClose() fails.
if ((status = btrieveClient->CollectionClose(btrieveCollection)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveClient::CollectionClose():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
leave:
return status;
}
deleteCollection(BtrieveClient *btrieveClient)
{
// If CollectionDelete() fails.
if ((status = btrieveClient->CollectionDelete(btrieveCollectionName)) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveClient::CollectionDelete():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
leave:
return status;
}
retrieveDocument(BtrieveCollection *btrieveCollection, const char *key)
{
std::string query;
BtrieveDocumentSet btrieveDocumentSet;
int size;
int id;
const char *json;
query = "{ \"x\": ";
query = query + key;
query = query + "}";
// If Query() fails.
if ((status = btrieveCollection->Query(&btrieveDocumentSet, query.c_str())) != Btrieve::STATUS_CODE_NO_ERROR)
{
printf("Error: BtrieveCollection::Query():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
if ((size = btrieveDocumentSet.Size()) < 0)
{
status = btrieveDocumentSet.GetLastStatusCode();
printf("Error: BtrieveDocumentSet::Size():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
if (size != 1)
{
printf("Error: BtrieveDocumentSet::Size():%d:%s.\n", size, "Unexpected size. Size should be one.");
goto leave;
}
if ((id = btrieveDocumentSet.PopFirst()) < 0)
{
status = btrieveDocumentSet.GetLastStatusCode();
printf("Error: BtrieveDocumentSet::PopFirst():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
// If DocumentRetrieveJson() fails.
if ((json = btrieveCollection->DocumentRetrieveJson(id)) == NULL)
{
status = btrieveCollection->GetLastStatusCode();
printf("Error: BtrieveCollection::DocumentRetrieveJson():%d:%s.\n", status, Btrieve::StatusCodeToString(status));
goto leave;
}
printf("json: %s\n", json);
leave:
return status;
}
int
main(int argc, char *argv[])
{
static const char *usage = "Usage: %s uint8_value\n";
BtrieveClient btrieveClient;
BtrieveCollection btrieveCollection;
const char *key;
uint64_t integerValue;
// If the incorrect number of arguments were given.
if (argc != 2)
{
printf(usage, argv[0]);
goto leave;
}
integerValue = atoi(argv[1]);
// If integerValue is out of range.
if ((integerValue < MIN_X) || (integerValue > MAX_X))
{
printf(usage, argv[0]);
goto leave;
}
key = argv[1];
// If createCollection() fails.
if ((status = createCollection(&btrieveClient)) != Btrieve::STATUS_CODE_NO_ERROR)
{
goto leave;
}
// If openCollection() fails.
if ((status = openCollection(&btrieveClient, &btrieveCollection)) != Btrieve::STATUS_CODE_NO_ERROR)
{
goto leave;
}
// If loadCollection() fails.
if ((status = loadCollection(&btrieveCollection)) != Btrieve::STATUS_CODE_NO_ERROR)
{
goto leave;
}
// If retrieveDocument() fails.
if ((status = retrieveDocument(&btrieveCollection, key)) != Btrieve::STATUS_CODE_NO_ERROR)
{
goto leave;
}
// If closeCollection() fails.
if ((status = closeCollection(&btrieveClient, &btrieveCollection)) != Btrieve::STATUS_CODE_NO_ERROR)
{
goto leave;
}
// If deleteCollection() fails.
if ((status = deleteCollection(&btrieveClient)) != Btrieve::STATUS_CODE_NO_ERROR)
{
goto leave;
}
leave:
// If there wasn't a failure.
return 0;
return 1;
}
The Btrieve client class.
Definition btrieveCpp.h:1388
Btrieve::StatusCode CollectionDelete(const char *collectionName)
Delete a collection.
Btrieve::StatusCode CollectionClose(BtrieveCollection *btrieveCollection)
Close a collection.
Btrieve::StatusCode CollectionOpen(BtrieveCollection *btrieveCollection, const char *collectionName, const char *password=NULL)
Open a collection.
Btrieve::StatusCode CollectionCreate(const char *collectionName)
Create a collection.
The Btrieve collection class.
Definition btrieveCpp.h:4154
const char * DocumentRetrieveJson(int id)
Retrieve a document json.
Btrieve::StatusCode Query(BtrieveDocumentSet *btrieveDocumentSet, const char *query=NULL)
Query the collection.
Btrieve::StatusCode GetLastStatusCode()
Get the last status code.
int DocumentCreate(const char *json)
Create a document.
The Btrieve document set class.
Definition btrieveCpp.h:4393
int PopFirst()
Remove and return the first document identifier from the set.
int Size()
Return the number of document identifiers in the set.
Btrieve::StatusCode GetLastStatusCode()
Get the last status code.
static const char * StatusCodeToString(StatusCode statusCode)
Return a status code as a string.
StatusCode
The Btrieve status codes.
Definition btrieveCpp.h:713
@ STATUS_CODE_UNKNOWN
-7 - Unknown.
Definition btrieveCpp.h:1194
@ STATUS_CODE_NO_ERROR
0 - No error.
Definition btrieveCpp.h:715