You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sqlite3_opt_preupdate_hook.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (C) 2019 G.J.R. Timmer <gjr.timmer@gmail.com>.
  2. // Copyright (C) 2018 segment.com <friends@segment.com>
  3. //
  4. // Use of this source code is governed by an MIT-style
  5. // license that can be found in the LICENSE file.
  6. // +build sqlite_preupdate_hook
  7. package sqlite3
  8. /*
  9. #cgo CFLAGS: -DSQLITE_ENABLE_PREUPDATE_HOOK
  10. #cgo LDFLAGS: -lm
  11. #ifndef USE_LIBSQLITE3
  12. #include <sqlite3-binding.h>
  13. #else
  14. #include <sqlite3.h>
  15. #endif
  16. #include <stdlib.h>
  17. #include <string.h>
  18. void preUpdateHookTrampoline(void*, sqlite3 *, int, char *, char *, sqlite3_int64, sqlite3_int64);
  19. */
  20. import "C"
  21. import (
  22. "errors"
  23. "unsafe"
  24. )
  25. // RegisterPreUpdateHook sets the pre-update hook for a connection.
  26. //
  27. // The callback is passed a SQLitePreUpdateData struct with the data for
  28. // the update, as well as methods for fetching copies of impacted data.
  29. //
  30. // If there is an existing update hook for this connection, it will be
  31. // removed. If callback is nil the existing hook (if any) will be removed
  32. // without creating a new one.
  33. func (c *SQLiteConn) RegisterPreUpdateHook(callback func(SQLitePreUpdateData)) {
  34. if callback == nil {
  35. C.sqlite3_preupdate_hook(c.db, nil, nil)
  36. } else {
  37. C.sqlite3_preupdate_hook(c.db, (*[0]byte)(unsafe.Pointer(C.preUpdateHookTrampoline)), unsafe.Pointer(newHandle(c, callback)))
  38. }
  39. }
  40. // Depth returns the source path of the write, see sqlite3_preupdate_depth()
  41. func (d *SQLitePreUpdateData) Depth() int {
  42. return int(C.sqlite3_preupdate_depth(d.Conn.db))
  43. }
  44. // Count returns the number of columns in the row
  45. func (d *SQLitePreUpdateData) Count() int {
  46. return int(C.sqlite3_preupdate_count(d.Conn.db))
  47. }
  48. func (d *SQLitePreUpdateData) row(dest []interface{}, new bool) error {
  49. for i := 0; i < d.Count() && i < len(dest); i++ {
  50. var val *C.sqlite3_value
  51. var src interface{}
  52. // Initially I tried making this just a function pointer argument, but
  53. // it's absurdly complicated to pass C function pointers.
  54. if new {
  55. C.sqlite3_preupdate_new(d.Conn.db, C.int(i), &val)
  56. } else {
  57. C.sqlite3_preupdate_old(d.Conn.db, C.int(i), &val)
  58. }
  59. switch C.sqlite3_value_type(val) {
  60. case C.SQLITE_INTEGER:
  61. src = int64(C.sqlite3_value_int64(val))
  62. case C.SQLITE_FLOAT:
  63. src = float64(C.sqlite3_value_double(val))
  64. case C.SQLITE_BLOB:
  65. len := C.sqlite3_value_bytes(val)
  66. blobptr := C.sqlite3_value_blob(val)
  67. src = C.GoBytes(blobptr, len)
  68. case C.SQLITE_TEXT:
  69. len := C.sqlite3_value_bytes(val)
  70. cstrptr := unsafe.Pointer(C.sqlite3_value_text(val))
  71. src = C.GoBytes(cstrptr, len)
  72. case C.SQLITE_NULL:
  73. src = nil
  74. }
  75. err := convertAssign(&dest[i], src)
  76. if err != nil {
  77. return err
  78. }
  79. }
  80. return nil
  81. }
  82. // Old populates dest with the row data to be replaced. This works similar to
  83. // database/sql's Rows.Scan()
  84. func (d *SQLitePreUpdateData) Old(dest ...interface{}) error {
  85. if d.Op == SQLITE_INSERT {
  86. return errors.New("There is no old row for INSERT operations")
  87. }
  88. return d.row(dest, false)
  89. }
  90. // New populates dest with the replacement row data. This works similar to
  91. // database/sql's Rows.Scan()
  92. func (d *SQLitePreUpdateData) New(dest ...interface{}) error {
  93. if d.Op == SQLITE_DELETE {
  94. return errors.New("There is no new row for DELETE operations")
  95. }
  96. return d.row(dest, true)
  97. }