Krozark / cpp-ORM

A project to create a simple ORM.
BSD 2-Clause "Simplified" License
7 stars 1 forks source link

inheretence #7

Open Krozark opened 9 years ago

Krozark commented 9 years ago

There is 3 possibilities with 2 classes

A B : A
int a int b

1

A B : A
int pk int a_ptr (this is the PK)
int a int b

Each class as it's own table. B refer to a A table line through the a_ptr FK.

2

A B : A
void somthing() int pk
int a
int b

in this case, A is an abstract class that is not stored in the database. All the attributes of A are forwards to its children.

3

A B : A
int pk b()
int a

in this case, B is not store in database. In fact, B use the A table as it's own. The idea is to add into the A table all the attributes of B. Problem : what we do if there is a A and B instance referring to the same object in the database?

Krozark commented 9 years ago

At this time, the solution 1 appear to be the best one to deal with down casting. But I've no idea how to implement it right now.

Maybe using something like:

class A : public SqlObject<A>
{
   // ... code
}
class B : public SqlExtendObject<A>
{
   // other code
}
Krozark commented 9 years ago

done since d8ec30619c94eba2e6064e1467d9477f0744b8f9 .

Usage :

class A : public SqlObject<A>
{
   orm::intergerField a;

  MAKE_STATIC_COLUMN(i)
}
//Usual MACROS
class B : public SqlExtendObject<B,A>
{
  orm::intergerField b;
 MAKE_STATIC_COLUMN(b);
}
//Usual MACROS

internaly B contain a FK that point to its A base.

DB representation:

A B
int pk int pk
int a int b
int _base_obj_ptr (reference A pk)

Query:

All query are supported, BUT you HAVE TO use the FK to make query on the base object of B (A).

Examples usage:

auto list = B::all(); //no changes

auto list = B::query().filter(
     orm::Q<B>(18,orm::op::gt,B::$b)
); //query on B object

auto list = B::query().filter(
     orm::Q<B>(12,orm::op::gt,B::$base_obj_ptr,A::$a)
); //query on A base using the FK

B::type_ptr b = B::create(); 
 /// ...
b->save(); //will save all the bases of B (B and A)

 A::type_ptr a = b; //this will works. point to the same object
/// ...
a->save(); // will save all the object (B and A bases)