forked from go-goracle/goracle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscr.go
265 lines (238 loc) · 7.95 KB
/
subscr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright 2017 Tamás Gulácsi
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package goracle
/*
#include <stdlib.h>
#include <stdio.h>
#include "dpiImpl.h"
void CallbackSubscrDebug(void *context, dpiSubscrMessage *message);
*/
import "C"
import (
"log"
"strings"
"unsafe"
"github.com/pkg/errors"
)
// CallbackSubscr is the callback for C code on subscription event.
//export CallbackSubscr
func CallbackSubscr(ctx unsafe.Pointer, message *C.dpiSubscrMessage) {
log.Printf("CB %p %+v", ctx, message)
if ctx == nil {
return
}
subscr := (*Subscription)(ctx)
getRows := func(rws *C.dpiSubscrMessageRow, rwsNum C.uint32_t) []RowEvent {
if rwsNum == 0 {
return nil
}
cRws := (*((*[maxArraySize]C.dpiSubscrMessageRow)(unsafe.Pointer(rws))))[:int(rwsNum)]
rows := make([]RowEvent, len(cRws))
for i, row := range cRws {
rows[i] = RowEvent{
Operation: Operation(row.operation),
Rowid: C.GoStringN(row.rowid, C.int(row.rowidLength)),
}
}
return rows
}
getTables := func(tbls *C.dpiSubscrMessageTable, tblsNum C.uint32_t) []TableEvent {
if tblsNum == 0 {
return nil
}
cTbls := (*((*[maxArraySize]C.dpiSubscrMessageTable)(unsafe.Pointer(tbls))))[:int(tblsNum)]
tables := make([]TableEvent, len(cTbls))
for i, tbl := range cTbls {
tables[i] = TableEvent{
Operation: Operation(tbl.operation),
Name: C.GoStringN(tbl.name, C.int(tbl.nameLength)),
Rows: getRows(tbl.rows, tbl.numRows),
}
}
return tables
}
getQueries := func(qrys *C.dpiSubscrMessageQuery, qrysNum C.uint32_t) []QueryEvent {
if qrysNum == 0 {
return nil
}
cQrys := (*((*[maxArraySize]C.dpiSubscrMessageQuery)(unsafe.Pointer(qrys))))[:int(qrysNum)]
queries := make([]QueryEvent, len(cQrys))
for i, qry := range cQrys {
queries[i] = QueryEvent{
ID: uint64(qry.id),
Operation: Operation(qry.operation),
Tables: getTables(qry.tables, qry.numTables),
}
}
return queries
}
var err error
if message.errorInfo != nil {
err = fromErrorInfo(*message.errorInfo)
}
subscr.callback(Event{
Err: err,
Type: EventType(message.eventType),
DB: C.GoStringN(message.dbName, C.int(message.dbNameLength)),
Tables: getTables(message.tables, message.numTables),
Queries: getQueries(message.queries, message.numQueries),
})
}
// Event for a subscription.
type Event struct {
Tables []TableEvent
Queries []QueryEvent
DB string
Err error
Type EventType
}
// QueryEvent is an event of a Query.
type QueryEvent struct {
Tables []TableEvent
ID uint64
Operation
}
// TableEvent is for a Table-related event.
type TableEvent struct {
Rows []RowEvent
Name string
Operation
}
// RowEvent is for row-related event.
type RowEvent struct {
Rowid string
Operation
}
// Subscription for events in the DB.
type Subscription struct {
conn *conn
dpiSubscr *C.dpiSubscr
callback func(Event)
}
func (s *Subscription) getError() error { return s.conn.getError() }
// NewSubscription creates a new Subscription in the DB.
//
// Make sure your user has CHANGE NOTIFICATION privilege!
//
// This code is EXPERIMENTAL yet!
func (c *conn) NewSubscription(name string, cb func(Event)) (*Subscription, error) {
if !c.connParams.EnableEvents {
return nil, errors.New("subscription must be allowed by specifying \"enableEvents=1\" in the connection parameters")
}
subscr := Subscription{conn: c, callback: cb}
params := (*C.dpiSubscrCreateParams)(C.malloc(C.sizeof_dpiSubscrCreateParams))
//defer func() { C.free(unsafe.Pointer(params)) }()
C.dpiContext_initSubscrCreateParams(c.dpiContext, params)
params.subscrNamespace = C.DPI_SUBSCR_NAMESPACE_DBCHANGE
params.protocol = C.DPI_SUBSCR_PROTO_CALLBACK
params.qos = C.DPI_SUBSCR_QOS_BEST_EFFORT | C.DPI_SUBSCR_QOS_QUERY | C.DPI_SUBSCR_QOS_ROWIDS
params.operations = C.DPI_OPCODE_ALL_OPS
if name != "" {
params.name = C.CString(name)
params.nameLength = C.uint32_t(len(name))
}
// typedef void (*dpiSubscrCallback)(void* context, dpiSubscrMessage *message);
params.callback = C.dpiSubscrCallback(C.CallbackSubscrDebug)
params.callbackContext = unsafe.Pointer(&subscr)
dpiSubscr := (*C.dpiSubscr)(C.malloc(C.sizeof_void))
if C.dpiConn_subscribe(c.dpiConn,
params,
(**C.dpiSubscr)(unsafe.Pointer(&dpiSubscr)),
) == C.DPI_FAILURE {
C.free(unsafe.Pointer(params))
C.free(unsafe.Pointer(dpiSubscr))
err := errors.Wrap(c.getError(), "newSubscription")
if strings.Contains(errors.Cause(err).Error(), "DPI-1065:") {
err = errors.WithMessage(err, "specify \"enableEvents=1\" connection parameter on connection to be able to use subscriptions")
}
return nil, err
}
subscr.dpiSubscr = dpiSubscr
return &subscr, nil
}
// Register a query for Change Notification.
//
// This code is EXPERIMENTAL yet!
func (s *Subscription) Register(qry string, params ...interface{}) error {
cQry := C.CString(qry)
defer func() { C.free(unsafe.Pointer(cQry)) }()
var dpiStmt *C.dpiStmt
if C.dpiSubscr_prepareStmt(s.dpiSubscr, cQry, C.uint32_t(len(qry)), &dpiStmt) == C.DPI_FAILURE {
return errors.Wrapf(s.getError(), "prepareStmt[%p]", s.dpiSubscr)
}
defer func() { C.dpiStmt_release(dpiStmt) }()
mode := C.dpiExecMode(C.DPI_MODE_EXEC_DEFAULT)
var qCols C.uint32_t
if C.dpiStmt_execute(dpiStmt, mode, &qCols) == C.DPI_FAILURE {
return errors.Wrap(s.getError(), "executeStmt")
}
var queryID C.uint64_t
if C.dpiStmt_getSubscrQueryId(dpiStmt, &queryID) == C.DPI_FAILURE {
return errors.Wrap(s.getError(), "getSubscrQueryId")
}
if Log != nil {
Log("msg", "subscribed", "query", qry, "id", queryID)
}
return nil
}
// Close the subscription.
//
// This code is EXPERIMENTAL yet!
func (s *Subscription) Close() error {
dpiSubscr := s.dpiSubscr
conn := s.conn
s.conn = nil
s.dpiSubscr = nil
s.callback = nil
if dpiSubscr == nil || conn == nil || conn.dpiConn == nil {
return nil
}
if C.dpiConn_unsubscribe(conn.dpiConn, dpiSubscr) == C.DPI_FAILURE {
return errors.Wrap(s.getError(), "close")
}
return nil
}
// EventType is the type of an event.
type EventType C.dpiEventType
// Events that can be watched.
const (
EvtStartup = EventType(C.DPI_EVENT_STARTUP)
EvtShutdown = EventType(C.DPI_EVENT_SHUTDOWN)
EvtShutdownAny = EventType(C.DPI_EVENT_SHUTDOWN_ANY)
EvtDropDB = EventType(C.DPI_EVENT_DROP_DB)
EvtDereg = EventType(C.DPI_EVENT_DEREG)
EvtObjChange = EventType(C.DPI_EVENT_OBJCHANGE)
EvtQueryChange = EventType(C.DPI_EVENT_QUERYCHANGE)
)
// Operation in the DB.
type Operation C.dpiOpCode
const (
// OpAll Indicates that notifications should be sent for all operations on the table or query.
OpAll = Operation(C.DPI_OPCODE_ALL_OPS)
// OpAllRows Indicates that all rows have been changed in the table or query (or too many rows were changed or row information was not requested).
OpAllRows = Operation(C.DPI_OPCODE_ALL_ROWS)
// OpInsert Indicates that an insert operation has taken place in the table or query.
OpInsert = Operation(C.DPI_OPCODE_INSERT)
// OpUpdate Indicates that an update operation has taken place in the table or query.
OpUpdate = Operation(C.DPI_OPCODE_UPDATE)
// OpDelete Indicates that a delete operation has taken place in the table or query.
OpDelete = Operation(C.DPI_OPCODE_DELETE)
// OpAlter Indicates that the registered table or query has been altered.
OpAlter = Operation(C.DPI_OPCODE_ALTER)
// OpDrop Indicates that the registered table or query has been dropped.
OpDrop = Operation(C.DPI_OPCODE_DROP)
// OpUnknown An unknown operation has taken place.
OpUnknown = Operation(C.DPI_OPCODE_UNKNOWN)
)