Wednesday, July 5, 2017

How to create signals on insert, delete or update commands in PostgresSQL and handle them in C++?

Leave a Comment

I'm trying to find the best way to get notified by the database when there are insert, delete or update commands in PostgresSQL.

My goal is to handle changes in the database as soon as they happen.

How can I do that?

3 Answers

Answers 1

I've found a good way of doing that in Qt.

First, you need to create rules to notify you that updates had happened:

CREATE RULE table_notification_insert AS ON INSERT TO public.table DO NOTIFY table_inserted; CREATE RULE table_notification_update AS ON UPDATE TO public.table DO NOTIFY table_updated; CREATE RULE table_notification_delete AS ON DELETE TO public.table DO NOTIFY table_deleted; 

Then, you can use Qt to receive each notification ("table_inserted", "table_updated", "table_deleted") as follows:

QSqlDatabase::database().driver()->subscribeToNotification("table_inserted"); QObject::connect(QSqlDatabase::database().driver(), SIGNAL(notification(const QString&)), /*handlerObjectPointer*/, SLOT(handleNotificationFunction(const QString&))); 

Here is where I found part of the answer: forum.qt.io

Answers 2

The following book might help you especially page 392 : https://books.google.ca/books?id=gkQVL9pyFVYC&pg=PA379&lpg=PA379&dq=signal+postgresql+c%2B%2B&source=bl&ots=E7AhQWKBrW&sig=N-lj9prsYMe7eTEou9A84ITKSbI&hl=en&sa=X&ved=0ahUKEwi_sJO1tPLUAhWEWD4KHY1JCI4Q6AEINDAD#v=onepage&q=signal%20postgresql%20c%2B%2B&f=false

Answers 3

CREATE RULE table_notification_insert AS ON INSERT TO public.table DO NOTIFY table_inserted; CREATE RULE table_notification_update AS ON UPDATE TO public.table DO NOTIFY table_updated; CREATE RULE table_notification_delete AS ON DELETE TO public.table DO NOTIFY table_deleted; 

https://javacodepoint.com

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment