Skip to content

Commit

Permalink
add binding support to SQLEngine execute
Browse files Browse the repository at this point in the history
* added unit test
  • Loading branch information
fzakaria committed Sep 25, 2023
1 parent 7e0ae66 commit 96c1f8a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
14 changes: 9 additions & 5 deletions sqlelf/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys
from collections import OrderedDict
from dataclasses import dataclass
from typing import Any, Dict, Iterator, TextIO
from typing import Any, Dict, Iterator, Optional, TextIO

import apsw
import apsw.shell
Expand All @@ -22,11 +22,15 @@ def shell(self, stdin: TextIO = sys.stdin) -> apsw.shell.Shell:
shell.command_prompt(["sqlelf> "]) # type: ignore[no-untyped-call]
return shell

def execute_raw(self, sql: str) -> apsw.Cursor:
return self.connection.execute(sql)
def execute_raw(
self, sql: str, bindings: Optional["apsw.Bindings"] = None
) -> apsw.Cursor:
return self.connection.execute(sql, bindings=bindings)

def execute(self, sql: str) -> Iterator[dict[str, Any]]:
cursor = self.execute_raw(sql)
def execute(
self, sql: str, bindings: Optional["apsw.Bindings"] = None
) -> Iterator[dict[str, Any]]:
cursor = self.execute_raw(sql, bindings=bindings)
try:
description = cursor.getdescription()
column_names = [n for n, _ in description]
Expand Down
19 changes: 19 additions & 0 deletions tests/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,22 @@ def test_non_existent_file() -> None:
engine = sql.make_sql_engine(["/doesnotexist"])
result = list(engine.execute("SELECT * FROM elf_headers LIMIT 1"))
assert len(result) == 0


def test_select_with_bindings() -> None:
engine = sql.make_sql_engine(["/bin/ls", "/bin/cat"])
result = list(
engine.execute(
"""
SELECT * FROM elf_version_requirements
WHERE path = :path
LIMIT 1
""",
{"path": "/bin/ls"},
)
)
assert len(result) == 1
assert "path" in result[0]
assert result[0]["path"] == "/bin/ls"
assert "file" in result[0]
assert "name" in result[0]

0 comments on commit 96c1f8a

Please sign in to comment.