Howto use the Postgres unaccent function in a query expression

Open
#734 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
postgresql, python
Domain
backend, database

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 unaccent function? I saw in the pyDAL sources that there are a few decorators available like the @register_expression but I was not able to use them. Should it be possible to inherit the Postgres adapter and add the new functionality?
  • Is the unaccent function 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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from web2py/pydal

All issues in web2py/pydal

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.