-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpubsub.py
More file actions
71 lines (56 loc) · 2.47 KB
/
pubsub.py
File metadata and controls
71 lines (56 loc) · 2.47 KB
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
from typing import Callable, Optional
from sqlitecloud.datatypes import SQLITECLOUD_PUBSUB_SUBJECT, SQLiteCloudConnect
from sqlitecloud.driver import Driver
from sqlitecloud.resultset import SQLiteCloudResultSet
class SQLiteCloudPubSub:
def __init__(self) -> None:
self._driver = Driver()
def listen(
self,
connection: SQLiteCloudConnect,
subject_type: SQLITECLOUD_PUBSUB_SUBJECT,
subject_name: str,
callback: Callable[
[SQLiteCloudConnect, Optional[SQLiteCloudResultSet], Optional[any]], None
],
data: Optional[any] = None,
) -> None:
subject = "TABLE " if subject_type.value == "TABLE" else ""
connection.pubsub_callback = callback
connection.pubsub_data = data
self._driver.execute(f"LISTEN {subject}{subject_name};", connection)
def unlisten(
self,
connection: SQLiteCloudConnect,
subject_type: SQLITECLOUD_PUBSUB_SUBJECT,
subject_name: str,
) -> None:
subject = "TABLE " if subject_type.value == "TABLE" else ""
self._driver.execute(f"UNLISTEN {subject}{subject_name};", connection)
connection.pubsub_callback = None
connection.pubsub_data = None
def create_channel(
self, connection: SQLiteCloudConnect, name: str, if_not_exists: bool = False
) -> None:
if if_not_exists:
self._driver.execute(f"CREATE CHANNEL {name} IF NOT EXISTS;", connection)
else:
self._driver.execute(f"CREATE CHANNEL {name};", connection)
def notify_channel(
self, connection: SQLiteCloudConnect, name: str, data: str
) -> None:
self._driver.execute(f"NOTIFY {name} '{data}';", connection)
def set_pubsub_only(self, connection: SQLiteCloudConnect) -> None:
"""
Close the main socket, leaving only the pub/sub socket opened and ready
to receive incoming notifications from subscripted channels and tables.
Connection is no longer able to send commands.
"""
self._driver.execute("PUBSUB ONLY;", connection)
self._driver.disconnect(connection, only_main_socket=True)
def is_connected(self, connection: SQLiteCloudConnect) -> bool:
return self._driver.is_connected(connection, False)
def list_connections(self, connection: SQLiteCloudConnect) -> SQLiteCloudResultSet:
return SQLiteCloudResultSet(
self._driver.execute("LIST PUBSUB CONNECTIONS;", connection)
)