Creating a User Defined Object - C++

You define your object like any other class in C++. The only requirement is that all persistent objects (objects you would like to store in the database) will have to inherit from ooObj. You define your class in a DDL file and process it using Objectivity's ooddlx.exe tool. Here's an example:

objects.ddl



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

class Part : ooObj
{
public:
    int partNum;
    void setPartNum(int num)
    {
        partNum = num;
    }
    int returnPartNum()
    {
        return partNum;
    }
};



Once you've finished defining all of your classes, you can add the schema (class definitions) to your Federated database:

ooddlx output



C:\test>ooddlx objects.ddl test.boot

Objectivity/DB (TM) C++ DDL Translator, Version: 9.4
Copyright (c) Objectivity, Inc 1989, 2008. All rights reserved.



ooddlx will create 3 files for default specification;
"file".h, "file"_ddl.cpp, and "file"_ref.h. So in this case you'll see:
objects.h
objects_ddl.cpp
objects_ref.h

Be sure to include objects.h in your application and all of the other files in your project.

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);	//enable session logging for debug
  ooObjy::startup();
  char *bootfile = "test.boot";     //insert bootfile name here
  char *dbName = "testDB";          //insert database name here
  char *contName = "testCont";      //insert container name here

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

  ooSession *session = connection->createSession("main_session");     //insert session name for log or '0' for default

  try
  {

    session->begin(oocUpdate);

    ooHandle(ooFDObj) fd;
    fd.exist(bootfile, oocUpdate);        //open in update mode

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

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

    ooHandle(Car) carObj;
    carObj = new(cont) Car();
    carObj.nameObj(cont, "Car"); //name object "Car" in scope of cont

    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;
}