-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add UUID type conversion and fix datetime comparison #405
Add UUID type conversion and fix datetime comparison #405
Conversation
piccolo/querystring.py
Outdated
if find_spec("asyncpg"): | ||
from asyncpg.pgproto.pgproto import UUID | ||
else: | ||
from uuid import UUID |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally it would check both of these, because for Postgres, a value could be a uuid.UUID
or asyncpg.pgproto.pgproto.UUID
.
We pass uuid.UUID
into queries like INSERT, but asyncpg returns asyncpg.pgproto.pgproto.UUID
from SELECT. We're most likely to be dealing with uuid.UUID
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
asyncpg
only gets installed if the piccolo[postgres]
is installed, correct? I am not sure I have a good idea on how the case where it hasn't been installed other than moving the find_spec("asyncpg")
into the conversion if-elif-else
scope (l# 103). Is that what you would recommend?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or something like
if find_spec("asyncpg"):
from asyncpg.pgproto.pgproto import UUID as apgUUID
else:
from uuid import UUID
apgUUID = UUID
Then do the appropriate typecheck with an or
on line 102
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that works.
@theelderbeever Thanks for this. The changes are definitely useful. The string representation of a query is more of a convenience, so you can do something like I think this issue has also made it clear that an API is needed for composing custom queries with |
Codecov Report
@@ Coverage Diff @@
## master #405 +/- ##
==========================================
- Coverage 90.84% 90.83% -0.01%
==========================================
Files 103 103
Lines 6826 6833 +7
==========================================
+ Hits 6201 6207 +6
- Misses 625 626 +1
Continue to review full report at Codecov.
|
@theelderbeever Thanks, looks good 👍 |
Reference Issue #404
Motivation
The querystring builder is valuable when trying to write custom complex queries with
VALUES
however, certain values are not converted to sql compatible values properly. This results inTable.raw(query).run()
failing. Changes here aspire to address this issue.Explanation
A couple of type check fixes. The
datetime
comparison in thequerystring
was comparing to the module not the class and wouldn't be picked up. Additionally, the.replace("T", " ")
doesn't work with postgres as far as I can tell and I removed it.There also needs to be a check on
UUID
. When being written to a query string the value should be wrapped in''
.I imagine something similar may need to be done with JSONB, Enum, etc. Maybe there could be some merging of usage with the
piccolo.utils.sql_values.convert_to_sql_value
function.