On Wed, Jan 2, 2019 at 5:45 AM Fischers Fritz <[email protected]> wrote:
>
> I am writing an information retrieval tool.
>
> I want to add a feature by which one can save queries by name and then
> run them later by specifying the name rather than the whole query again.

hi there -

overall, the general approach is to capture the logical structure of
the query being created and to persist that as a tokenized tree
structure, since that's what any SQL query or really any programming
language construct really is.    A SQLAlchemy Core expression already
exists as such a parse tree which you can traverse using the visitors
API:  
https://github.com/sqlalchemy/sqlalchemy/blob/master/lib/sqlalchemy/sql/visitors.py
  an ORM Query object creates one of these structures too but
currently is not in an API-neutral visitable form, you need to get the
Core expression version of it using query.statement, but this loses
ORM-specific information like eager loaders, polymorphic directives
and things like that.   There's a long term issue to provide more
publicly supported visibility into the internals of an ORM Query, vs.
just the select() construct, at
https://github.com/sqlalchemy/sqlalchemy/issues/3225 but this is not
on any near term roadmap.

The more accurate answer here relies upon the specifics of your
application, because if your system has a means by which users
"create" queries, that would be how they are also "saved".   How do
users "create" these queries, are they writing Python code?   if so,
then save the python code.  Are they using a graphical tool?  then
persist a tokenized version of the graphical structure.    this all
depends on the specifics of where the queries are coming from in the
first place, e.g. in that your application has already devised some
way of capturing user intent (even if it means they write code in a
text box), that's where you'd want to create a persistence system as
well which mirrors that intent.

There is a "serializer" extension in SQLAlchemy that pickles Core
select() and ORM Query objects.   However, here are the reasons I
don't recommend it:  1. the pickle format of a Query changes across
SQLAlchemy releases as the internals change, and this extension was
intended more for a short-term caching use case (which doesn't
actually exist)   2. there are at least a few cases and likely more
where Python pickle can't handle the complexity of the structure and
throws a stack trace 3.  because the extension doesn't really work and
nobody uses it, I will be seeking to remove it in a future SQLAlchemy
release.     if you really want to use it, it's at
https://docs.sqlalchemy.org/en/latest/core/serializer.html?highlight=serializer#module-sqlalchemy.ext.serializer

The advised approach would be that you define your own persistence
format based on capturing the intent of your users, since your tool is
already capturing their intent somehow and converting that into an ORM
(Core?) query, that's where you'd also define an alternate format of
persisting that intent.



>
> For example, maybe I make an elaborate query that finds documents
> mentioning "SQLAlchemy" or "Alembic" in some PostgreSQL tsvector field
> and with a value between 2018-12-01 and 2018-12-31 in some date field.
> How do I serialize these filters?
>
> I can of course serialize everything before I represent it as SQLAlchemy
> objects, but I don't think that should be necessary.
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example.  See  http://stackoverflow.com/help/mcve for a full 
> description.
> ---
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to