Skip to content

Commit 31dea66

Browse files
authored
Merge branch 'main' into amjith/type-checking
2 parents c5e1a61 + c1f7911 commit 31dea66

3 files changed

Lines changed: 47 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Upcoming (TBD)
22

3+
### Features
4+
5+
* Add support for opening 'file:' URIs with parameters. [(#234)](https://github.com/dbcli/litecli/pull/234)
6+
37
### Bug Fixes
48

59
* Avoid Click 8.1.* to prevent messing up the pager when the PAGER env var has a string with spaces.

litecli/sqlexecute.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import sqlparse
1919
import os.path
20+
from urllib.parse import urlparse
2021

2122
from .packages import special
2223

@@ -75,20 +76,27 @@ def connect(self, database: str | None = None) -> None:
7576
# Nothing to connect to.
7677
return
7778

78-
db_name = os.path.expanduser(db)
79-
db_dir_name = os.path.dirname(os.path.abspath(db_name))
80-
if not os.path.exists(db_dir_name):
81-
raise Exception("Path does not exist: {}".format(db_dir_name))
79+
location = urlparse(db)
80+
if location.scheme and location.scheme == "file":
81+
uri = True
82+
db_name = db
83+
db_filename = location.path
84+
else:
85+
uri = False
86+
db_filename = db_name = os.path.expanduser(db)
87+
db_dir_name = os.path.dirname(os.path.abspath(db_filename))
88+
if not os.path.exists(db_dir_name):
89+
raise Exception("Path does not exist: {}".format(db_dir_name))
8290

83-
conn = sqlite3.connect(database=db_name, isolation_level=None)
91+
conn = sqlite3.connect(database=db_name, isolation_level=None, uri=uri)
8492
conn.text_factory = lambda x: x.decode("utf-8", "backslashreplace")
8593
if self.conn:
8694
self.conn.close()
8795

8896
self.conn = conn
8997
# Update them after the connection is made to ensure that it was a
9098
# successful connection.
91-
self.dbname = db
99+
self.dbname = db_filename
92100

93101
def run(self, statement: str) -> Iterable[tuple]:
94102
"""Execute the sql in the database and return the results. The results

tests/test_main.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
from unittest.mock import patch
99

1010
import click
11+
import pytest
1112
from click.testing import CliRunner
1213

1314
from litecli.main import cli, LiteCli
1415
from litecli.packages.special.main import COMMANDS as SPECIAL_COMMANDS
15-
from utils import dbtest, run
16+
from utils import dbtest, run, create_db, db_connection
1617

1718
test_dir = os.path.abspath(os.path.dirname(__file__))
1819
project_dir = os.path.dirname(test_dir)
@@ -332,3 +333,30 @@ def test_get_prompt(mock_datetime):
332333
# 12. Windows path
333334
lc.connect("C:\\Users\\litecli\\litecli_test.db")
334335
assert lc.get_prompt(r"\d") == "C:\\Users\\litecli\\litecli_test.db"
336+
337+
338+
@pytest.mark.parametrize(
339+
"uri, expected_dbname",
340+
[
341+
("file:{tmp_path}/test.db", "{tmp_path}/test.db"),
342+
("file:{tmp_path}/test.db?mode=ro", "{tmp_path}/test.db"),
343+
("file:{tmp_path}/test.db?mode=ro&cache=shared", "{tmp_path}/test.db"),
344+
],
345+
)
346+
def test_file_uri(tmp_path, uri, expected_dbname):
347+
"""
348+
Test that `file:` URIs are correctly handled
349+
ref:
350+
https://docs.python.org/3/library/sqlite3.html#sqlite3-uri-tricks
351+
https://www.sqlite.org/c3ref/open.html#urifilenameexamples
352+
"""
353+
# - ensure db exists
354+
db_path = tmp_path / "test.db"
355+
create_db(db_path)
356+
db_connection(db_path)
357+
uri = uri.format(tmp_path=tmp_path)
358+
359+
lc = LiteCli()
360+
lc.connect(uri)
361+
362+
assert lc.get_prompt(r"\d") == expected_dbname.format(tmp_path=tmp_path)

0 commit comments

Comments
 (0)