MCPcopy
hub / github.com/rqlite/rqlite / RegisterUpdateHook

Method RegisterUpdateHook

db/db.go:461–512  ·  view source on GitHub ↗

RegisterUpdateHook registers a callback that is called when a row is modified in the database. If a callback is already registered, it is replaced. If hook is nil, the callback is removed.

(hook UpdateHookCallback)

Source from the content-addressed store, hash-verified

459// in the database. If a callback is already registered, it is replaced. If hook is nil,
460// the callback is removed.
461func (db *DB) RegisterUpdateHook(hook UpdateHookCallback) error {
462 // Convert from SQLite hook data to rqlite hook data.
463 convertFn := func(op int, _, table string, rowID int64) (*command.UpdateHookEvent, error) {
464 he := &command.UpdateHookEvent{
465 Table: table,
466 RowId: rowID,
467 }
468
469 switch op {
470 case sqlite3.SQLITE_INSERT:
471 he.Op = command.UpdateHookEvent_INSERT
472 case sqlite3.SQLITE_UPDATE:
473 he.Op = command.UpdateHookEvent_UPDATE
474 case sqlite3.SQLITE_DELETE:
475 he.Op = command.UpdateHookEvent_DELETE
476 default:
477 return nil, fmt.Errorf("unknown update hook operation %d", op)
478 }
479 return he, nil
480 }
481
482 // Register the callback with the SQLite connection.
483 var cb func(int, string, string, int64)
484 if hook != nil {
485 cb = func(op int, dbName, tblName string, rowID int64) {
486 stats.Add(numUpdateHooks, 1)
487 ev, err := convertFn(op, dbName, tblName, rowID)
488 if err != nil {
489 stats.Add(numUpdateHooksErrors, 1)
490 ev.Error = err.Error()
491 }
492 if err := hook(ev); err != nil {
493 stats.Add(numUpdateHooksCBErrors, 1)
494 }
495 }
496 }
497 f := func(driverConn any) error {
498 conn := driverConn.(*sqlite3.SQLiteConn)
499 conn.RegisterUpdateHook(cb)
500 return nil
501 }
502
503 conn, err := db.rwDB.Conn(context.Background())
504 if err != nil {
505 return err
506 }
507 defer conn.Close()
508 if err := conn.Raw(f); err != nil {
509 return err
510 }
511 return nil
512}
513
514// CommitHookCallback is a callback function that is called whenever a transaction
515// is committed to the database. If the callback returns true the transaction

Callers 2

Test_UpdateHook_BasicFunction · 0.80
Test_UpdateHook_TxFunction · 0.80

Calls 3

ErrorMethod · 0.65
CloseMethod · 0.65
AddMethod · 0.45

Tested by 2

Test_UpdateHook_BasicFunction · 0.64
Test_UpdateHook_TxFunction · 0.64