Debian Codegraph @

sqlite3_opt_preupdate_hook.go

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