Skip to content

Commit 4b369a6

Browse files
committed
Fix improper error handling of execute_query method when QueryItem is used: #82
Signed-off-by: Tsuyoshi Hombashi <[email protected]>
1 parent 466ce91 commit 4b369a6

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

simplesqlite/core.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,18 +335,21 @@ def execute_query(
335335
import time
336336

337337
self.check_connection()
338-
if typepy.is_null_string(query):
338+
339+
queryStr = str(query)
340+
341+
if typepy.is_null_string(queryStr):
339342
return None
340343

341344
if self.debug_query or self.global_debug_query:
342-
logger.debug(query)
345+
logger.debug(queryStr)
343346

344347
exec_start_time = time.time()
345348

346349
assert self.connection # to avoid type check error
347350

348351
try:
349-
result = self.connection.execute(str(query))
352+
result = self.connection.execute(queryStr)
350353
except (sqlite3.OperationalError, sqlite3.IntegrityError) as e:
351354
if caller is None:
352355
caller = logging.getLogger().findCaller()
@@ -358,18 +361,18 @@ def execute_query(
358361
"failed to execute query at {:s}({:d}) {:s}".format(
359362
file_path, line_no, func_name
360363
),
361-
f" - query: {MultiByteStrDecoder(query).unicode_str}",
364+
f" - query: {MultiByteStrDecoder(queryStr).unicode_str}",
362365
f" - msg: {e}",
363366
f" - db: {self.database_path}",
364367
]
365368
)
366369
)
367370

368371
if self.__is_profile:
369-
self.__dict_query_count[str(query)] += 1
372+
self.__dict_query_count[queryStr] += 1
370373

371374
elapse_time = time.time() - exec_start_time
372-
self.__dict_query_totalexectime[str(query)] += elapse_time
375+
self.__dict_query_totalexectime[queryStr] += elapse_time
373376

374377
return result
375378

test/test_simplesqlite.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,18 @@ def test_smoke_view(self, con):
151151
result = con.select(select="*", table_name="view1")
152152
assert result is not None
153153

154+
def test_execute_query(self, con):
155+
from simplesqlite.query import Select
156+
157+
result = con.execute_query(Select("*", TEST_TABLE_NAME))
158+
assert result is not None
159+
160+
def test_exception_query(self, con):
161+
from simplesqlite.query import Select
162+
163+
with pytest.raises(OperationalError):
164+
con.execute_query(Select("*", "invalid_table"))
165+
154166
@pytest.mark.parametrize(
155167
["attr", "table_name", "expected"],
156168
[

0 commit comments

Comments
 (0)