Skip to content

Commit fa725fe

Browse files
laodouyalaodouya
authored andcommitted
Support dsn in connect func.
1 parent d6541be commit fa725fe

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

README.rst

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Documentation
5353

5454
Documentation is available online: http://developers.covenantsql.io/
5555

56-
Key file and database_id can get from: http://developers.covenantsql.io/docs/quickstart
56+
Key file and dsn can get from: http://developers.covenantsql.io/docs/quickstart
5757

5858
For support, please fire a issue at `Github
5959
<https://github.com/CovenantSQL/CovenantSQL/issues/new>`_.
@@ -76,14 +76,13 @@ The following examples make use of a simple table
7676
7777
import pycovenantsql
7878
79-
# user key file location
80-
key = '/path/to/private.key'
8179
82-
# Connect to the database
83-
connection = pycovenantsql.connect(host='localhost',
80+
# Connect to the database with dsn
81+
# host and port are your local CovenantSQL Adapter server
82+
connection = pycovenantsql.connect(
83+
dsn='covenantsql://your_database_id',
84+
host='localhost',
8485
port=11108,
85-
key=key,
86-
database='database_id'
8786
)
8887
8988
try:

example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
print(pycovenantsql.get_client_info())
88

9-
conn = pycovenantsql.connect(dsn='covenant://0a10b74439f2376d828c9a70fd538dac4b69e0f4065424feebc0f5dbc8b34872',
10-
host="adp00.cn.gridb.io", port=7784, database='0a10b74439f2376d828c9a70fd538dac4b69e0f4065424feebc0f5dbc8b34872')
9+
conn = pycovenantsql.connect(dsn='covenantsql://0a10b74439f2376d828c9a70fd538dac4b69e0f4065424feebc0f5dbc8b34872',
10+
host="adp00.cn.gridb.io", port=7784)
1111

1212
cur = conn.cursor()
1313
print(cur.description)

pycovenantsql/connections.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
22
import os
33
import requests
4+
from urllib.parse import urlparse
45
from . import err
56
from .cursors import Cursor
67
from .optionfile import Parser
@@ -83,6 +84,12 @@ def _config(key, arg):
8384
self.port = port or 11108
8485
self.key = key
8586
self.database = database
87+
if self.dsn:
88+
u = urlparse(self.dsn)
89+
scheme = u.scheme.lower()
90+
if scheme == "covenantsql" or scheme == "cql":
91+
if u.hostname:
92+
self.database = u.hostname
8693
if https_pem:
8794
self._cert = (https_pem, self.key)
8895
elif self.key:

0 commit comments

Comments
 (0)