Embedding a 1 in n mapping for C ++ ORM

I am writing a project where I need to implement a stripped-down version of an ORM solution in C ++. I am amazed at implementing a 1-n relationship for the same.

For example, if the following classes:

class A
{
    ...
}

class B
{
    ...
    std::list<A> _a_list;
    ...
}

I have provided load / save methods for loading / saving in db. Now, if I take case B and the following workflow:

  • 1 entry from _a_list deleted
  • Changed 1 entry from _a_list
  • 1 entry is added to _a_list

Now I need to update db using something like "b.save ()". So what would be the best way to save the changes, i.e. Define additions, deletions, and updates in _a_list.

+5
source share
5 answers

, db (Command pattern). , , Save() . , . :

:

#include <vector>

using namespace std;

class B;
class Cmd;

class B
{
    private:
        vector<Cmd*> m_commands;
    public:
        void AddCmd( Cmd* p_command );
        void Save();
};

class Cmd
{
    protected:
        B* m_database;

    public:
        Cmd( B* p_database );
        virtual void Execute() = 0;
        virtual void Undo() = 0;
};

class InsertCmd : public Cmd
{
    private:
        int m_newEntry;
    public:
        InsertCmd( B* p_database, int p_newEntry );
        void Execute() { cout << "insert " << m_newEntry << endl; }
        void Undo()    { /* undo of insert */ }
};

:

#include "DbClass.h"

void B::AddCmd( Cmd* p_command )
{
    m_commands.push_back(p_command);
}

void B::Save()
{
    for( unsigned int i=0; i<m_commands.size(); i++ )
        m_commands[i]->Execute();
}

Cmd::Cmd( B* p_database ) : m_database(p_database)
{
    m_database->AddCmd(this);
}

InsertCmd::InsertCmd( B* p_database, int p_newEntry ) 
: Cmd(p_database), m_newEntry(p_newEntry)
{
}

:

#include "DbClass.h"

int main()
{
    B database;
    InsertCmd  insert( &database, 10 );
    database.Save();

    return 0;
}
+3

"" . ..

enum RecordState {
    RECORD_UNMODIFIED,
    RECORD_NEW,
    RECORD_CHANGED,
    RECORD_DELETED
};

RecordState ( - RECORD _NEW/RECORD _UNMODIFIED, ), Save(), reset RECORD _UNMODIFIED. .

+1

.

:

(a) , ORM- (.. INSERT, UPDATE DELETE)

(b) ORM , , (, , ). , ORM , INSERT, UPDATE DELETE .

, / ORM , , ( ) , . , / ORM, , , , , .

, , , SQL DELETE, UNMODIFED, NEW, CHANGED, DELETED , NEW_DELETED, .

+1

, , , Unit of Work. Registry, UoW.

+1

, " 1 n". "1" , "n" (). , . , , // , . , , // . , , , :

  • . , , .
  • - "-", . .

:

  • ? IdentityMap? ?
  • , , ? , , ?
  • ?

, ORM- . , IdentityMap , , - . "". , . , " " . ORM ++: YB.ORM. , DataObject.h, DataObject.cpp TestDataObject.cpp( lib/orm/). YB.ORM "" , . DataObject , . . . . , ( : New, Ghost, Dirty, Sync, ToBeDeleted, Deleted) . , "n" , RelationObject (slave_relations_ member). , "1" , RelationObject (member_relations_ member).

A class RelationObjectis an instance of a relationship. Such objects are always allocated on the heap and are not copied. They store and list pointers to related instances DataObject: one pointer to master and a set of shared pointers to slaves. Thus, they "own" subordinate instances DataObject, and instances DataObject"own" (indirectly) all subordinate objects. Note that it RelationObjectitself supports something like state to support lazy loading.

+1
source

All Articles