crawshaw / sqlite

Go SQLite3 driver
ISC License
569 stars 68 forks source link

Modify hooks? #1

Open nkev opened 6 years ago

nkev commented 6 years ago

This is very cool, thank you for sharing. One thing I would love to see are event hooks where I can register my custom function to be called after all INSERT, UPDATE and DELETE operations. Is that possible?

crawshaw commented 6 years ago

This would be a wrapper for sqlite_preupdate_hook? Yes I think that's a good feature.

bvinc commented 6 years ago

If you look at the mxk driver, he allows setting hooks on commit, rollback, update, but also on busy.

https://github.com/mxk/go-sqlite/

You can see how he does it. He exports his go functions that accept a pointer to his Conn struct.

https://github.com/mxk/go-sqlite/blob/master/sqlite3/util.go#L357-L375

When I forked his driver, I ended up removing them, because they break a fundamental rule in go. You cannot pass a go pointer to a go pointer to C.

The way I've decided that you have to do it is by passing an integer index as your function data, and having a global go function handler that can take that index and find the proper callback in a map, like this:

https://github.com/golang/go/wiki/cgo#function-variables

AdamSLevy commented 4 years ago

I may take a pass at implementing this. I have implemented a similar kind of registry for avoiding passing illegal go pointers to C.