python-3.xpostgresqlasyncpgpypika

Select values from database function in pypika


Wondering how to select data from stored function in postgresql DBMS by pypika? Is there any function or at least possibility to run own query? For instance, select * from get_accounts(<account_id>)

Searched for these and didn't find any solved issue, didn't find querying from stored procedures/functions


Solution

  • If you want to achieve select * from get_accounts(<account_id>) you can simply define the stored function call as a Function and add it to the from_ clause. If your account_id is for instance 1 you can do:

    >>> from pypika import Query
    >>> from pypika.terms import Function
    >>> account_id = 1
    >>> Query.from_(Function('get_accounts', account_id)).select("*")
    'SELECT * FROM get_accounts(1)'