mirror of https://github.com/dbcli/pgcli
Remove the custom parse_dsn function.
This commit is contained in:
parent
e90f541033
commit
d2c79d8240
|
@ -22,41 +22,6 @@ psycopg2.extensions.register_type(
|
|||
# See http://initd.org/psycopg/articles/2014/07/20/cancelling-postgresql-statements-python/
|
||||
psycopg2.extensions.set_wait_callback(psycopg2.extras.wait_select)
|
||||
|
||||
def _parse_dsn(dsn, default_user, default_password, default_host,
|
||||
default_port):
|
||||
"""
|
||||
This function parses a postgres url to get the different components.
|
||||
|
||||
"""
|
||||
|
||||
user = password = host = port = dbname = None
|
||||
|
||||
if dsn.startswith('postgres://'): # Check if the string is a database url.
|
||||
dsn = dsn[len('postgres://'):]
|
||||
elif dsn.startswith('postgresql://'):
|
||||
dsn = dsn[len('postgresql://'):]
|
||||
|
||||
if '/' in dsn:
|
||||
host, dbname = dsn.split('/', 1)
|
||||
if '@' in host:
|
||||
user, _, host = host.partition('@')
|
||||
if ':' in host:
|
||||
host, _, port = host.partition(':')
|
||||
if user and ':' in user:
|
||||
user, _, password = user.partition(':')
|
||||
|
||||
user = user or default_user
|
||||
password = password or default_password
|
||||
host = host or default_host
|
||||
port = port or default_port
|
||||
dbname = dbname or dsn
|
||||
|
||||
_logger.debug('Parsed connection params:'
|
||||
'dbname: %r, user: %r, password: %r, host: %r, port: %r',
|
||||
dbname, user, password, host, port)
|
||||
|
||||
return (dbname, user, password, host, port)
|
||||
|
||||
class PGExecute(object):
|
||||
|
||||
tables_query = '''SELECT c.relname as "Name" FROM pg_catalog.pg_class c
|
||||
|
@ -77,10 +42,11 @@ class PGExecute(object):
|
|||
ORDER BY 1;"""
|
||||
|
||||
def __init__(self, database, user, password, host, port):
|
||||
(self.dbname, self.user, self.password, self.host, self.port) = \
|
||||
_parse_dsn(database, default_user=user,
|
||||
default_password=password, default_host=host,
|
||||
default_port=port)
|
||||
self.dbname = database
|
||||
self.user = user
|
||||
self.password = password
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.connect()
|
||||
|
||||
def connect(self, database=None, user=None, password=None, host=None,
|
||||
|
|
|
@ -1,47 +1,8 @@
|
|||
# coding=UTF-8
|
||||
|
||||
from pgcli.pgexecute import _parse_dsn
|
||||
from textwrap import dedent
|
||||
from utils import *
|
||||
|
||||
def test__parse_dsn():
|
||||
test_cases = [
|
||||
# Full dsn with all components.
|
||||
('postgres://user:password@host:5432/dbname',
|
||||
('dbname', 'user', 'password', 'host', '5432')),
|
||||
|
||||
# dsn without password.
|
||||
('postgres://user@host:5432/dbname',
|
||||
('dbname', 'user', 'fpasswd', 'host', '5432')),
|
||||
|
||||
# dsn without user or password.
|
||||
('postgres://localhost:5432/dbname',
|
||||
('dbname', 'fuser', 'fpasswd', 'localhost', '5432')),
|
||||
|
||||
# dsn without port.
|
||||
('postgres://user:password@host/dbname',
|
||||
('dbname', 'user', 'password', 'host', '1234')),
|
||||
|
||||
# dsn without password and port.
|
||||
('postgres://user@host/dbname',
|
||||
('dbname', 'user', 'fpasswd', 'host', '1234')),
|
||||
|
||||
# dsn without user, password, port.
|
||||
('postgres://localhost/dbname',
|
||||
('dbname', 'fuser', 'fpasswd', 'localhost', '1234')),
|
||||
|
||||
# dsn without user, password, port or host.
|
||||
('postgres:///dbname',
|
||||
('dbname', 'fuser', 'fpasswd', 'fhost', '1234')),
|
||||
|
||||
# Full dsn with all components but with postgresql:// prefix.
|
||||
('postgresql://user:password@host:5432/dbname',
|
||||
('dbname', 'user', 'password', 'host', '5432')),
|
||||
]
|
||||
|
||||
for dsn, expected in test_cases:
|
||||
assert _parse_dsn(dsn, 'fuser', 'fpasswd', 'fhost', '1234') == expected
|
||||
|
||||
@dbtest
|
||||
def test_conn(executor):
|
||||
run(executor, '''create table test(a text)''')
|
||||
|
|
Loading…
Reference in New Issue