-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add compile_query as a helper function in lms.db._util
Change the approach on the previous commit, instead of a method on the session, just use a helper method importable from lms.db.
- Loading branch information
Showing
5 changed files
with
47 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from sqlalchemy import Select | ||
from sqlalchemy.dialects import postgresql | ||
from sqlalchemy.orm import Query | ||
|
||
|
||
def compile_query(query: Query | Select, literal_binds: bool = True) -> str: | ||
""" | ||
Return the SQL representation of `query` for postgres. | ||
:param literal_binds: Whether or not replace the query parameters by their values. | ||
""" | ||
if isinstance(query, Query): | ||
# Support for SQLAlchemy 1.X style queryies, eg: db.query(Model).filter_by() | ||
statement = query.statement | ||
else: | ||
# SQLALchemy 2.X style, eg: select(Model).where() | ||
statement = query | ||
|
||
return str( | ||
statement.compile( | ||
dialect=postgresql.dialect(), | ||
compile_kwargs={"literal_binds": literal_binds}, | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from sqlalchemy import select | ||
|
||
from lms.db import compile_query | ||
from lms.models import Event | ||
|
||
|
||
def test_compile_query(db_session): | ||
old_style_query = db_session.query(Event).filter_by(id=0) | ||
new_style_query = select(Event).where(Event.id == 0) | ||
|
||
old_style_query_compiled = compile_query(old_style_query) | ||
new_style_query_complied = compile_query(new_style_query) | ||
|
||
assert old_style_query_compiled == new_style_query_complied | ||
assert ( | ||
old_style_query_compiled | ||
== """SELECT event.id, event.timestamp, event.type_id, event.application_instance_id, event.course_id, event.assignment_id, event.grouping_id | ||
FROM event | ||
WHERE event.id = 0""" # noqa: W291 | ||
) |