Project

General

Profile

How to capture database insert/update/delete triggers with Wt::Dbo?

Added by Ejaz Anwer almost 10 years ago

Hi,

In one of my up coming projects, I'm suppose to add listeners to database tables and upon any insert/update/delete, perform some logic, which will be coded in C (the back end would be in MySQL).

Therefore, in this context, I'm looking into Wt::Dbo, to see how best it can suit my needs. I've checked the tutorials, but it doesn't state anything like that (or may be I've missed it).

I would highly appreciate if someone can point me to the right direction.

Regards!


Replies (9)

RE: How to capture database insert/update/delete triggers with Wt::Dbo? - Added by Wim Dumon almost 10 years ago

Hi,

Within Dbo, you can watch in the persist method what Dbo is doing on the database.

There's currently no support in Dbo for being triggered on database insert/update/delete events. I believe the mysql api does support this, so you could use the mysql api directly for this functionality.

BR,

Wim.

RE: How to capture database insert/update/delete triggers with Wt::Dbo? - Added by Emeric Poupon almost 10 years ago

Hello,

Actually, I have a similar question.

Would it be possible to manage a global revision of the database or even of a table?

It would be increased each time a 'write' commit is performed on the database.

The idea is to have a kind of check that there is nothing updated to be uploaded to clients that are connecting over a slow network.

Do you have any idea for that?

Best Regards,

Emeric

RE: How to capture database insert/update/delete triggers with Wt::Dbo? - Added by Koen Deforche almost 10 years ago

Hey,

Yes, but you'll need to rely on the actual class that implements the saving action, which isn't documented (yet).

You could do something like this:

class A {

  void persist(...) {
     if (typeid(action) == typeid(Wt::Dbo::SaveDbAction<A>)) {
        ...
     }

     ...
  }

};

This does not guarantee that the value has actually been written to the database, it could still be that the transaction fails (but then again that would be okay if you use this to write something to the database in the same transaction).

Regards,

koen

RE: How to capture database insert/update/delete triggers with Wt::Dbo? - Added by Emeric Poupon almost 10 years ago

This sounds interesting!

But what is the persist mehod you refer to?

As far as I understand, it cannot be the 'persist' method that dbo handled classes may implement in order to define db fields and other relationships?

RE: How to capture database insert/update/delete triggers with Wt::Dbo? - Added by Koen Deforche almost 10 years ago

Hey,

Yes, that's the one. Why can it not be?

Regards,

koen

RE: How to capture database insert/update/delete triggers with Wt::Dbo? - Added by Emeric Poupon almost 10 years ago

Of course, you are right!

I made a quick and dirty test in a class called 'Path' :

class Path
{
        public:
...
                template<class Action>
                        void persist(Action& a)
                        {
                                std::cout << "IN PERSIST" << std::endl; 
                                if (typeid(a) == typeid(Wt::Dbo::SaveDbAction<Action>)) {
                                        std::cout << "Want to save" << std::endl;
                                }
                                else {
                                        std::cout << "typeid(a) = " << typeid(a).name() << std::endl;
                                        std::cout << "typeid(Wt::Dbo::SaveDbAction<Action>) = " << typeid(Wt::Dbo::SaveDbAction<Action>).name() << std::endl;
                                }

                        ...
                        }
};

Output is :

IN PERSIST
typeid(a) = N2Wt3Dbo10InitSchemaE
typeid(Wt::Dbo::SaveDbAction<Action>) = N2Wt3Dbo12SaveDbActionINS0_10InitSchemaEEE
...
IN PERSIST
typeid(a) = N2Wt3Dbo16SessionAddActionE
typeid(Wt::Dbo::SaveDbAction<Action>) = N2Wt3Dbo12SaveDbActionINS0_16SessionAddActionEEE
...
IN PERSIST!!
typeid(a) = N2Wt3Dbo12SaveDbActionI4PathEE
typeid(Wt::Dbo::SaveDbAction<Action>) = N2Wt3Dbo12SaveDbActionINS1_I4PathEEEE
...
IN PERSIST!!
typeid(a) = N2Wt3Dbo16SessionAddActionE
typeid(Wt::Dbo::SaveDbAction<Action>) = N2Wt3Dbo12SaveDbActionINS0_16SessionAddActionEEE
...
IN PERSIST!!
typeid(a) = N2Wt3Dbo12SaveDbActionI4PathEE
typeid(Wt::Dbo::SaveDbAction<Action>) = N2Wt3Dbo12SaveDbActionINS1_I4PathEEEE
...
IN PERSIST!!
typeid(a) = N2Wt3Dbo21TransactionDoneActionE
typeid(Wt::Dbo::SaveDbAction<Action>) = N2Wt3Dbo12SaveDbActionINS0_21TransactionDoneActionEEE
...

Unfortunately it never gets into the first condition.

Regards,

RE: How to capture database insert/update/delete triggers with Wt::Dbo? - Added by T Y over 9 years ago

Hi,

Did you find solution for the above?

Thank you.

Kind regards,

TY

RE: How to capture database insert/update/delete triggers with Wt::Dbo? - Added by T Y over 9 years ago

Hi,

I figured it out. There is a typo in latest example: SaveDbAction should take class name as argument -> Wt::Dbo::SaveDbAction<Path>

Here is correct version:

class Path
{
        public:
...
                template<class Action>
                        void persist(Action& a)
                        {
                                std::cout << "IN PERSIST" << std::endl; 
                                if (typeid(a) == typeid(Wt::Dbo::SaveDbAction<Path>)) {
                                        std::cout << "Want to save" << std::endl;
                                }
                                else {
                                        std::cout << "typeid(a) = " << typeid(a).name() << std::endl;
                                        std::cout << "typeid(Wt::Dbo::SaveDbAction<Action>) = " << typeid(Wt::Dbo::SaveDbAction<Action>).name() << std::endl;
                                }

                        ...
                        }
};

RE: How to capture database insert/update/delete triggers with Wt::Dbo? - Added by T Y over 9 years ago

Here is extended example.

Please note: It does not catch delete action (solution?)

class Path
{
public:
    ...
    template<class Action>
    void persist(Action& a)
    {
        // Application side trigger
        if (typeid(a) == typeid(Wt::Dbo::InitSchema))
        {
            // Initialize Schema
            std::cout << "typeid(a) = " << typeid(a).name() << std::endl;
        }
        if (typeid(a) == typeid(Wt::Dbo::SessionAddAction))
        {
            // Insert action            
        }
        else if (typeid(a) == typeid(Wt::Dbo::SaveDbAction<Path>))
        {
            // Save action
        }
        else if (typeid(a) == typeid(Wt::Dbo::LoadDbAction<Path>))
        {
            // Load action
        }
        else if (typeid(a) == typeid(Wt::Dbo::TransactionDoneAction))
        {
            // Transaction Done
        }
        else
        {
            // Other cases
            std::cout << "typeid(a) = " << typeid(a).name() << std::endl;
        }

        ...
    }
};
    (1-9/9)