Nested Objects - Program Example


C++ program below shows how to insert records into a table with nested objects (objects within objects). Table 'customers11' is a table with nested objects. There is one object derived from the table, and two objects (nested) derived from this object. Insert is based on 'main' object. Insert columns must be explicitly specified by object and name.

// ins_nested_objects.cc
#include <orac_lib.h>

  int main()
  {
          Connect c;
          try {
                Database d = c.OpenDatabase("demo", "demo");
                Table t = d.GetTable("customers11");

                Object o = t.GetObject("cust_data"); // main object
                Object o1 = o.GetObject("cust_name"); // nested object
                Object o2 = o.GetObject("cust_address"); // nested object

                Insert i(o); // insert based on main object

                // specify all columns to be inserted (add columns from all derived objects)
                i << o["id"] << o1["f_name"] << o1["l_name"] << o2["street"] << o2["city"];
                i >> "Frank" >> "Tyxen" >> "Widen Street" >> "Akron";

                cout << i.Row() << " rows inserted" << endl;


                d.Close();
          }
          catch ( OException& e)  {
              cerr << e.Show() << endl;
          }
          catch ( ...)  {
              cerr << " Error ..." << endl;
          }
          return 0;
  }

C++ program below shows how to fetch records from a table with nested objects (objects within objects). Table 'customers11' is a table with nested objects. There is one object derived from the table, and two objects (nested) derived from this object. There are three selects presented, each one is based on one object (there is one 'main' object and two 'nested' objects). Columns displayed by deafult are those only which belong to a given object.

// sel_nested_objects.cc
#include <orac_lib.h>

  int main()
  {
          Connect c;
          try {
                Database d = c.OpenDatabase("demo", "demo");
                Table t = d.GetTable("customers11");

                Object o = t.GetObject("cust_data");
                Object o1 = o.GetObject("cust_name");
                Object o2 = o.GetObject("cust_address");

                Select s1(o1);
                while(s1.Row()) cout << s1; // columns from 'o1' only

                Selects2(o2);
                while(s2.Row()) cout << s2; // columns from 'o2' only

                Select s(o);
                while(s.Row()) cout << s; // columns from 'o' only

                d.Close();
          }
          catch ( OException& e)  {
              cerr << e.Show() << endl;
          }
          catch ( ...)  {
              cerr << " Error ..." << endl;
          }
          return 0;
  }

C++ program below is a variation of a previous one. Difference is that select based on 'main' object 'o' has explicitly added columns from derived objects ('o1' and 'o2') to make them visible.

// sel_nested_objects_1.cc
#include <orac_lib.h>

  int main()
  {
          Connect c;
          try {
                Database d = c.OpenDatabase("demo", "demo");
                Table t = d.GetTable("customers11");

                Object o = t.GetObject("cust_data");
                Object o1 = o.GetObject("cust_name");
                Object o2 = o.GetObject("cust_address");

                Select s(o);
                s << o["id"] << o1["f_name"] << o2["city"]; // specify selected columns
                while(s.Row()) cout << s; // selected columns are displayed

                d.Close();
          }
          catch ( OException& e)  {
              cerr << e.Show() << endl;
          }
          catch ( ...)  {
              cerr << " Error ..." << endl;
          }
          return 0;
  }