开发者

MySQL callback - is there such a thing?

开发者 https://www.devze.com 2023-03-31 20:08 出处:网络
I was wondering if there\'s such thing as the 开发者_如何学编程equivalent as a callback function using mysql after an INSERT or UPDATE which could return me the row # and maybe values of such rows.You

I was wondering if there's such thing as the 开发者_如何学编程equivalent as a callback function using mysql after an INSERT or UPDATE which could return me the row # and maybe values of such rows.


You can create triggers that are called on insert and update. They do not return value, but they can set variables you can read outside them.


I am not aware of any callback as you say but surely from your calling application you can retrieve the last inserted ID in case you did not specify it and the db has generated an auto increment value. Other values you should already know because you have inserted them.

if you need to know those values within the database server, you can have a SQL trigger which is executed at every insert so you can do more processing on the newly inserted record, for example write something in another table etc...


This isn't available in MySQL as it stands. I think there are two methods to achieve this:

  1. You would have to simulate it with polling - a rather ugly method, easy on the programming but tough on the server.

  2. See Does MySQL permit callbacks in C such that when a change happens, I can be notified? - write a user defined function that could notify a registered listener using some proprietary method of your devising. Tougher on the programmer, easy on the server. In this case make sure your UDF is robust, doesn't freeze while passing a notification to a listener who may have died, etc.
This would be a handy generic UDF for MySQL and bring it something that it has consistently lacked in comparison to Firebird / Interbase, which they call events, not to be confused with MySQL timed events. The timed event feature of MySQL on the other hand is something that Firebird & Interbase lacked the last time I looked at them.

See this for a Firebird event description in case you (or some later reader) decides to do it via UDF - it's a good design spec to aim at: http://www.janus-software.com/fbmanual/manual.php?book=php&topic=49

0

精彩评论

暂无评论...
验证码 换一张
取 消