-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubsub.go
134 lines (113 loc) · 4.56 KB
/
pubsub.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
//
// //// SQLite Cloud
// //////////// ///
// /// /// /// Product : SQLite Cloud GO SDK
// /// /// /// Version : 1.0.0
// // /// /// /// Date : 2021/10/12
// /// /// /// /// Author : Andreas Pfeil
// /// /// /// ///
// /// ////////// /// /// Description : GO Methods related to the
// //// /// /// SQCloud class for handling
// //// ////////// /// asynchronous communication.
// //// ////
// //// /////
// /// Copyright : 2021 by SQLite Cloud Inc.
//
// -----------------------------------------------------------------------TAB=2
/*
REM LISTEN CHANNEL:
REM LISTEN TabName = Listen on WRITES on Table "TabName" in this database (execute 1...n times)
REM LISTEN * = Listen on WRITES on All Tables in this database (execute 1...n times)
REM LISTEN ChanName = Listen on NOTIFYs on the Channel ChanName (execute 1...n times)
REM UNLISTEN ChanName|TabName = Unregisteres a previous registration
REM UNLISTEN * = Unregisteres ALL (=TabName,*,ChanName) registrations
REM NOTIFY ChanName = NOTIFY ChanName ""
REM NOTIFY ChanName <STRING-PAYLOAD>
REM LISTEN
10 SEND "LISTEN *"
20 RECEIVE "|79 PAUTH a365efef-cfb7-4672-8ed4-45a489ddb194 9230b8d8-93dc-4edc-bcaf-cc118fe32d4d"
30 IF NO 2.socket IS THERE: OPEN 2.socket
40 SEND "PAUTH a365efef-cfb7-4672-8ed4-45a489ddb194 9230b8d8-93dc-4edc-bcaf-cc118fe32d4d"
50 RECEIVE "OK"
60 START 2.thread
2.10 IF 2.socket LOST CONNECTION: CLOSE 2.socket and TERMINATE 2.thread
2.20 RECEIVE "#LEN json"
2.30 CALL callback_function WITH json
2.40 GOTO 2.10
ON_CLOSE EVENT:
10 IF 2.socket IS CONNECTED: CLOSE 2.socket
20 IF main.socket IS CONNECTED: CLOSE main.socket
*/
package sqlitecloud
import "strings"
// GetUUID returns the UUID as string
func (this *SQCloud) GetUUID() string {
return this.uuid // this.CGetCloudUUID()
}
// psubClose closes the PSUB connection to the SQLite Cloud Database server.
func (this *SQCloud) psubClose() error {
var err error = nil
if this.psub != nil {
err = (*this.psub).Close()
}
this.psub = nil
return err
}
// Creates the specified Channel.
func (this *SQCloud) CreateChannel(Channel string, NoError bool) error {
command := "CREATE CHANNEL ?"
if NoError {
command += " IF NOT EXISTS"
}
return this.ExecuteArray(command, []interface{}{Channel})
}
func (this *SQCloud) ListChannels() ([]string, error) {
return this.SelectStringList("LIST CHANNELS")
}
// Listen subscribes this connection to the specified Channel.
func (this *SQCloud) Listen(Channel string) error { // add a call back function...
return this.ExecuteArray("LISTEN ?", []interface{}{Channel})
}
// Listen subscribes this connection to the specified Table.
func (this *SQCloud) ListenTable(TableName string, DatabaseName string) error { // add a call back function...
sql := "LISTEN TABLE ?"
args := []interface{}{TableName}
if strings.TrimSpace(DatabaseName) != "" {
sql += " DATABASE ?"
args = append(args, DatabaseName)
}
return this.ExecuteArray(sql, args)
}
// Notify sends a wakeup call to the channel Channel
func (this *SQCloud) Notify(Channel string) error {
return this.ExecuteArray("NOTIFY ?", []interface{}{Channel})
}
// SendNotificationMessage sends the message Message to the channel Channel
func (this *SQCloud) SendNotificationMessage(Channel string, Message string) error {
return this.ExecuteArray("NOTIFY ? ?", []interface{}{Channel, Message})
}
// Unlisten unsubsribs this connection from the specified Channel.
func (this *SQCloud) Unlisten(Channel string) error {
return this.ExecuteArray("UNLISTEN ?", []interface{}{Channel})
}
// Unlisten unsubsribs this connection from the specified Table.
func (this *SQCloud) UnlistenTable(TableName string, DatabaseName string) error {
sql := "UNLISTEN TABLE ?"
args := []interface{}{TableName}
if strings.TrimSpace(DatabaseName) != "" {
sql += " DATABASE ?"
args = append(args, DatabaseName)
}
return this.ExecuteArray(sql, args)
}
// Deletes the specified Channel.
func (this *SQCloud) RemoveChannel(Channel string) error {
return this.ExecuteArray("REMOVE CHANNEL ?", []interface{}{Channel})
}
// PAuth returns the auth details for pubsub
func (this *SQCloud) GetPAuth() (string, string) {
if this.psub == nil {
return "", ""
}
return this.psub.uuid, this.psub.secret
}