Skip to content

Commit

Permalink
Fix DB API connect with default params
Browse files Browse the repository at this point in the history
  • Loading branch information
xzkostyan committed Mar 18, 2021
1 parent b3913e0 commit 50dcc16
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
3 changes: 2 additions & 1 deletion clickhouse_driver/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ class Connection(object):

def __init__(
self, host, port=None,
database='default', user='default', password='',
database=defines.DEFAULT_DATABASE,
user=defines.DEFAULT_USER, password=defines.DEFAULT_PASSWORD,
client_name=defines.CLIENT_NAME,
connect_timeout=defines.DBMS_DEFAULT_CONNECT_TIMEOUT_SEC,
send_receive_timeout=defines.DBMS_DEFAULT_TIMEOUT_SEC,
Expand Down
7 changes: 5 additions & 2 deletions clickhouse_driver/dbapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Warning, Error, DataError, DatabaseError, ProgrammingError, IntegrityError,
InterfaceError, InternalError, NotSupportedError, OperationalError
)
from .. import defines

apilevel = '2.0'

Expand All @@ -11,8 +12,10 @@
paramstyle = 'pyformat'


def connect(dsn=None, user=None, password=None, host=None, port=None,
database=None, **kwargs):
def connect(dsn=None, host=None,
user=defines.DEFAULT_USER, password=defines.DEFAULT_PASSWORD,
port=defines.DEFAULT_PORT, database=defines.DEFAULT_DATABASE,
**kwargs):
"""
Create a new database connection.
Expand Down
7 changes: 5 additions & 2 deletions clickhouse_driver/dbapi/connection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ..client import Client
from .. import defines
from .cursor import Cursor
from .errors import InterfaceError

Expand All @@ -13,8 +14,10 @@ class Connection(object):
See parameters description in
:data:`~clickhouse_driver.connection.Connection`.
"""
def __init__(self, dsn=None, user=None, password=None, host=None,
port=None, database=None, **kwargs):
def __init__(self, dsn=None, host=None,
user=defines.DEFAULT_USER, password=defines.DEFAULT_PASSWORD,
port=defines.DEFAULT_PORT, database=defines.DEFAULT_DATABASE,
**kwargs):
self.cursors = []

self.dsn = dsn
Expand Down
4 changes: 4 additions & 0 deletions clickhouse_driver/defines.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

DEFAULT_DATABASE = 'default'
DEFAULT_USER = 'default'
DEFAULT_PASSWORD = ''

DEFAULT_PORT = 9000
DEFAULT_SECURE_PORT = 9440

Expand Down
9 changes: 9 additions & 0 deletions tests/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ def test_from_dsn(self):
self.assertEqual(cursor.fetchall(), [(1, )])
connection.close()

def test_connect_default_params(self):
connection = connect(host=self.host)
cursor = connection.cursor()

rv = cursor.execute('SELECT 1')
self.assertIsNone(rv)
self.assertEqual(cursor.fetchall(), [(1, )])
connection.close()

def test_execute_fetchone(self):
with self.created_cursor() as cursor:
cursor.execute('SELECT number FROM system.numbers LIMIT 4')
Expand Down

0 comments on commit 50dcc16

Please sign in to comment.