Skip to content

Commit e55a376

Browse files
committed
refactor _execute_sql to fix python <3.11 error and add custom exception
Signed-off-by: Grant Ramsay <seapagan@gmail.com>
1 parent fbe5b1e commit e55a376

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

sqliter/exceptions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,9 @@ class TableDeletionError(SqliterError):
139139
"""Raised when a table cannot be deleted from the database."""
140140

141141
message_template = "Failed to delete the table: '{}'"
142+
143+
144+
class SqlExecutionError(SqliterError):
145+
"""Raised when an SQL execution fails."""
146+
147+
message_template = "Failed to execute SQL: '{}'"

sqliter/sqliter.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
RecordInsertionError,
2222
RecordNotFoundError,
2323
RecordUpdateError,
24+
SqlExecutionError,
2425
TableCreationError,
2526
TableDeletionError,
2627
)
@@ -273,6 +274,14 @@ def create_table(
273274
raise TableCreationError(table_name) from exc
274275

275276
def _execute_sql(self, sql: str) -> None:
277+
"""Execute an SQL statement.
278+
279+
Args:
280+
sql: The SQL statement to execute.
281+
282+
Raises:
283+
SqlExecutionError: If the SQL execution fails.
284+
"""
276285
if self.debug:
277286
self._log_sql(sql, [])
278287

@@ -281,8 +290,8 @@ def _execute_sql(self, sql: str) -> None:
281290
cursor = conn.cursor()
282291
cursor.execute(sql)
283292
conn.commit()
284-
except sqlite3.Error as exc:
285-
raise TableCreationError(sql) from exc
293+
except (sqlite3.Error, sqlite3.Warning) as exc:
294+
raise SqlExecutionError(sql) from exc
286295

287296
def drop_table(self, model_class: type[BaseDBModel]) -> None:
288297
"""Drop the table associated with the given model class.

tests/test_execute_sql.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pytest
66

77
from sqliter import SqliterDB
8-
from sqliter.exceptions import TableCreationError
8+
from sqliter.exceptions import SqlExecutionError
99

1010

1111
class TestExecuteSQL:
@@ -39,7 +39,7 @@ def test_execute_sql_error(self) -> None:
3939
"""Test SQL execution with an error."""
4040
# Missing closing parenthesis...
4141
invalid_sql = "CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT"
42-
with pytest.raises(TableCreationError):
42+
with pytest.raises(SqlExecutionError):
4343
self.db._execute_sql(invalid_sql)
4444

4545
@patch("sqliter.sqliter.SqliterDB._log_sql")
@@ -73,7 +73,7 @@ def test_execute_sql_multiple_statements(self) -> None:
7373
CREATE TABLE test_multi (id INTEGER PRIMARY KEY, name TEXT);
7474
INSERT INTO test_multi (name) VALUES ('test');
7575
"""
76-
with pytest.raises(TableCreationError) as exc_info:
76+
with pytest.raises(SqlExecutionError) as exc_info:
7777
self.db._execute_sql(sql)
7878

7979
assert "You can only execute one statement at a time." in str(
@@ -86,7 +86,7 @@ def test_execute_sql_with_parameters(self) -> None:
8686
"CREATE TABLE test_params (id INTEGER PRIMARY KEY, name TEXT)"
8787
)
8888
sql = "INSERT INTO test_params (name) VALUES (?)"
89-
with pytest.raises(TableCreationError):
89+
with pytest.raises(SqlExecutionError):
9090
self.db._execute_sql(
9191
sql
9292
) # This should fail as _execute_sql doesn't support parameters

0 commit comments

Comments
 (0)