Predicate Query - C++

Click here to view in Java

This is an example on how to use predicate scans. In this example, the database has 4 "Car" objects.

This class has a field ooUtf8String VIN;

The value of value in the four "Car" objects are:

  • 158L3X
  • 462A2H
  • 158P7D
  • 472G1Q

After ingesting some data, the application does a predicate scan on the container that has these four objects for 158*. That is: "VIN=~ \"158.*.\""

Program Output



Found Cars with VIN starting with 158
158L3X
158P7D


objects.ddl


class Car : ooObj
{
private:
    ooUtf8String VIN;
public:
    void setVin(ooUtf8String vinNum)
    {
        ooUpdate();
        VIN = vinNum;
    }
    ooUtf8String returnVin()
    {
        return VIN;
    }
};

createObj.cpp


#include <iostream>
using namespace std;
#include <ooObjy.h>
#include "objects.h"

int main(int argc, char* argv[])
{
  int returnval = 0;
  //ooObjy::setLoggingOptions(oocLogAll, oocTrue, oocFalse);
  ooObjy::startup();
  char *bootfile = "test.boot";
  char *dbName = "testDB";
  char *contName = "testCont";

  ooConnection *connection = ooObjy::getConnection(bootfile);

  ooSession *session = connection->createSession("main_session");

  try
  {

    session->begin(oocUpdate);

    ooHandle(ooFDObj) fd;
    fd.exist(bootfile, oocUpdate);

    ooHandle(ooDBObj) db;
    if (db.exist(fd, dbName));
    else
    {
      db = new ooDBObj(dbName);
    }

    ooHandle(ooContObj) cont;
    if (cont.exist(db, contName, oocUpdate));
    else
    {
      cont = new(db, 4, 0, contName) ooContObj;
    }

    ooHandle(Car) carObj;
    carObj = new(cont) Car();
    carObj->setVin("158L3X");
    carObj = new(cont) Car();
    carObj->setVin("462A2H");
    carObj = new(cont) Car();
    carObj->setVin("158P7D");
    carObj = new(cont) Car();
    carObj->setVin("472G1Q");

    session->commit();

    session->begin(oocUpdate);
    ooItr(Car) carItr;
    carItr.scan(cont, "VIN=~ \"158.*.\"");
    cout << "Found Cars with VIN starting with 158\n";

    while(carItr.next())
    {
        cout << ((ooHandle(Car))carItr)->returnVin() << endl;
    }
    session->commit();
  }

  catch(ooException &error)
  {
    cerr << error.what() << endl;
    session->abort();
    returnval = 1;
  }

  catch(...)
  {
    cerr << "unknown error" << endl;
    session->abort();
    returnval = 1;
  }

  ooObjy::shutdown();
  return returnval;
}