Skip to content

Commit e9ccd7d

Browse files
author
Abhishek Singh
committed
v1.0.2 documentation update
1 parent 015b6eb commit e9ccd7d

1 file changed

Lines changed: 30 additions & 39 deletions

File tree

README.md

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Key Features
2020
- Authentication using [ZeroMQ Authentication Protocol (ZAP)](https://rfc.zeromq.org/spec:27/ZAP/)
2121
- Encryption using [CurveZMQ](http://curvezmq.org/)
2222
- Generic authorization policy during server startup
23-
- Schedule regular backups for on-disk database (Not supported on Windows and for Python versions older than 3.7)
23+
- Schedule regular backups for on-disk database (Currently not supported on Windows and for Python versions < 3.7)
2424

2525

2626
# Install
@@ -40,14 +40,11 @@ pip install sqlite_rx
4040

4141
## Server
4242

43-
`SQLiteServer` runs in a single thread and follows an event-driven concurrency model (using `tornado's` event loop) which minimizes the cost of concurrent client connections.
43+
`SQLiteServer` runs in a single thread and follows an event-driven concurrency model (using `tornado's` event loop) which minimizes the cost of concurrent client connections. Following snippet shows how you can start the server process.
4444

4545
```python
46-
import logging.config
47-
from sqlite_rx import get_default_logger_settings
4846
from sqlite_rx.server import SQLiteServer
4947

50-
5148
def main():
5249

5350
# database is a path-like object giving the pathname
@@ -56,7 +53,6 @@ def main():
5653
# You can use ":memory:" to open a database connection to a database
5754
# that resides in RAM instead of on disk
5855

59-
logging.config.dictConfig(get_default_logger_settings(logging.DEBUG))
6056
server = SQLiteServer(database=":memory:",
6157
bind_address="tcp://127.0.0.1:5000")
6258
server.start()
@@ -84,29 +80,22 @@ The `execute` method reacts to the following keyword arguments:
8480

8581
### Instantiate a client
8682

83+
The following snippet shows how you can instantiate an `SQLiteClient` and execute a simple `CREATE TABLE` query.
84+
8785
```python
88-
import logging.config
8986
from sqlite_rx.client import SQLiteClient
90-
from sqlite_rx import get_default_logger_settings
91-
92-
# sqlite_rx comes with a default logger settings.
93-
# You could use as below.
94-
logging.config.dictConfig(get_default_logger_settings(logging.DEBUG))
95-
9687

9788
client = SQLiteClient(connect_address="tcp://127.0.0.1:5000")
98-
```
9989

100-
### CREATE TABLE
90+
with client:
91+
query = "CREATE TABLE stocks (date text, trans text, symbol text, qty real, price real)"
92+
result = client.execute(query)
10193

102-
```python
103-
result = client.execute("CREATE TABLE stocks (date text, trans text, symbol text, qty real, price real)")
104-
pprint(result)
10594
```
10695

10796
```python
10897
{'error': None,
109-
'items': []}
98+
'items': []}
11099
```
111100

112101
### INSERT MANY rows
@@ -143,7 +132,6 @@ purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
143132
result = client.execute("INSERT INTO stocks VALUES (?,?,?,?,?)",
144133
*purchases,
145134
execute_many=True)
146-
pprint(result)
147135

148136
```
149137

@@ -154,10 +142,10 @@ pprint(result)
154142
```
155143

156144
### SELECT with WHERE clause
145+
157146
```python
158147
args = ('IBM',)
159148
result = client.execute("SELECT * FROM stocks WHERE symbol = ?", *args)
160-
pprint(result)
161149

162150
```
163151

@@ -186,7 +174,6 @@ script = '''CREATE TABLE users(id INTEGER PRIMARY KEY, name TEXT, phone TEXT);
186174
('Adam', '5547874'), ('Jack', '5484522');'''
187175

188176
result = client.execute(script, execute_script=True)
189-
pprint(result)
190177

191178
```
192179

@@ -196,11 +183,10 @@ pprint(result)
196183
'lastrowid': 27}
197184
```
198185

199-
Select the rows inserted using the above SQL script
186+
Select rows inserted using the above SQL script
200187

201188
```python
202189
result = client.execute("SELECT * FROM users")
203-
pprint(result)
204190
```
205191

206192
```python
@@ -212,9 +198,9 @@ pprint(result)
212198
```
213199

214200

215-
### DROP a Table
201+
### DROP a table
216202

217-
Note: In the default authorization setting, a client is not allowed to drop any table.
203+
In the default authorization setting, a client is not allowed to drop any table.
218204

219205
```python
220206
result = client.execute("DROP TABLE stocks")
@@ -227,11 +213,11 @@ pprint(result)
227213
'items': []}
228214
```
229215

230-
### SELECT statement; Table not present
216+
###
231217
```python
232-
from pprint import pprint
233-
result = client.execute("SELECT * FROM STUDENTS")
234-
pprint(result)
218+
219+
with client:
220+
result = client.execute("SELECT * FROM STUDENTS")
235221

236222
```
237223

@@ -241,7 +227,11 @@ pprint(result)
241227
'items': []}
242228
```
243229

244-
### Client Clean up
230+
### SQLiteClient clean up
231+
232+
When you use `zeromq` sockets in a programming language like Python, objects get automatically freed for you.
233+
However, if you want to explicitly perform clean up and free the I/O resources, there are 2 options.
234+
You can either call the `cleanup()` method or execute queries in the context of the client i.e. `with` statement.
245235

246236
Call `cleanup()`
247237

@@ -265,7 +255,10 @@ with client:
265255

266256
## Backup
267257

268-
With `sqlite-rx`, database backup can be regularly peformed. The backup function can be configured to run as a background thread during server startup. Use `backup_interval` argument to specify how frequently backup should be performed in seconds.
258+
With `sqlite-rx`, database backup can be scheduled to run regularly during Server startup.
259+
Under the hood, this uses SQLite's Online [Backup](https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.backup) API. The Backup function runs as a daemon thread and it makes backup even while the database is being accessed by other clients.
260+
261+
You can specify `backup_interval` (time in seconds) to control how frequently backup should be performed.
269262

270263
```python
271264

@@ -277,7 +270,6 @@ def main():
277270
# You can use ":memory:" to open a database connection to a database
278271
# that resides in RAM instead of on disk
279272

280-
logging.config.dictConfig(get_default_logger_settings(logging.DEBUG))
281273
server = SQLiteServer(database="main.db",
282274
bind_address="tcp://127.0.0.1:5000",
283275
backup_database='backup.db',
@@ -291,11 +283,11 @@ if __name__ == '__main__':
291283
```
292284
### Constraints
293285
- Requires Python >= 3.7
294-
- Backup is performed using sqlite backup API which was introduced in Python 3.7
286+
- Backup is performed using sqlite3 backup API which was introduced in Python 3.7
295287
- Not supported on Windows
296288

297-
## Generic Default Authorization Policy
298289

290+
## Generic Default Authorization Policy
299291

300292
```python
301293
DEFAULT_AUTH_CONFIG = {
@@ -348,7 +340,7 @@ It is recommended you **do not** override the `SQLITE_PRAGMA` action as the data
348340

349341
`sqlite-server` is a console script to start an SQLiteServer.
350342

351-
```bash
343+
```text
352344
Usage: sqlite-server [OPTIONS]
353345
354346
Options:
@@ -394,7 +386,6 @@ This link also explains how to setup CurveZMQ encryption and ZAP authentication
394386
The following `docker-compose` examples using the docker image [`aosingh/sqlite_rx`](https://hub.docker.com/r/aosingh/sqlite_rx)
395387

396388

397-
398389
`sqlite-server` CLI is used in all the docker examples
399390

400391
## In-memory SQLite Database
@@ -412,7 +403,7 @@ services:
412403

413404
- Note that in the docker container the server listens on port `5000` so, do enable port forwarding on the host machine
414405

415-
## On Disk SQLite Database
406+
## On Disk SQLite Database with Backup
416407

417408
docker volume is used to persist the database file on the host's file system
418409

@@ -423,7 +414,7 @@ services:
423414

424415
sqlite_server:
425416
image: aosingh/sqlite_rx
426-
command: sqlite-server --log-level DEBUG --database /data/database.db
417+
command: sqlite-server --log-level DEBUG --database /data/database.db --backup-database /data/backup.db
427418
ports:
428419
- 5000:5000
429420
volumes:

0 commit comments

Comments
 (0)