Howto use the Postgres unaccent function in a query expression
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 25/100
Research direction
Start by reading the PostgreSQL adapter and the expression-registration code in the pyDAL sources, especially the @register_expression usage and Field expression handling mentioned in the issue. Trace how query parameters are expanded and inspect any related adapter or expression tests. Done means establishing a supported way to use unaccent, documenting adapter extension guidance, and clarifying the SQL-injection safety behavior.
Written by the indexing model from the issue text.
Description
Hi!
I would like to add the custom Postgres function unaccent (from the Postgres unaccent extension). Basically it should possible to do a ilike search using the unaccent characters from a text column, for example:
SELECT * from table1 WHERE unaccent(column1) ILIKE unaccent('%search%');
For this I implemented a custom function and also monkey-patched the Field object:
def unaccent(db: DAL, val: str):
def op(val, query_env):
return 'unaccent(%s)' % db._adapter.expand(val, field_type='string', query_env=query_env)
return Expression(db=db, op=op, first=val, type='string')
Field.unaccent = lambda field: Expression(db=field.db, op='unaccent(%s)' % field._rname, type='string')
It basically works for the following query:
search = search.replace('%', '!%')
search = f'%{search}%'
expression = unaccent(db, search)
db(db.table1.column1.unaccent.ilike(expression, escape='!')).select()
Now I have a few questions to the devs:
- Is there a better way how to extend the existing Postgres adapter for this new
unaccentfunction? I saw in the pyDAL sources that there are a few decorators available like the@register_expressionbut I was not able to use them. Should it be possible to inherit the Postgres adapter and add the new functionality? - Is the
unaccentfunction safe in terms of SQL injection? I tried to find out how pyDAL uses parameterized queries, but for me it seams the query parameters are added directly to the SQL statement. But I guess pyDAL does some kind of escaping ...
I guess this issue is interesting for other devs as well how to extend an adapter.
Thanks for your help.
BR Chris
- Dominant language
- Python
- Stars
- 540
- Forks
- 151
- Avg merge
- 12d 14h
- Merged PRs (30d)
- 1
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from web2py/pydal
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
-
Difficulty 3/5 1-2 days Newbie friendliness 50/100
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
mloda-ai/mloda-plugin-template#206 ·
Maintainers usually reply within 1 day
-
integration: websocket_api
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
home-assistant/core#194618 · 2 comments ·
Maintainers usually reply within 1 day
-
Shorten and organize the README around reader tasksPossibly taken @msaad00 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
5hyuns/maya-workflow-kit-for-blender#568 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
letsrevel/revel-backend#1558 ·
Maintainers usually reply within 1 day