I'm essentially building a tool for a group that doesn't know SQLalchemy,
and doesn't want to know or care about it, or even SQL in general. They
need to be able to query a dataset using minimal input, namely only caring
about the input filter conditions (e.g. X > 10). Presumably, they will
need to be able to update and change these parameters without having to
reset or rebuild the query I think. The parameter list is also spread
across multiple tables as well.
My query builder is a mix of SQLalchemy + WTForm-Alchemy, but the
pseudocode is something like
q = Query()
q.createBaseQuery() # this sets the default query =
session.query(ModelClass)
q.set_params(params=params) # define and set the input parameters to
search on, as dictionary of {'parameter_name': 'operand value'} , e.g
{'X': '< 10'}
q.add_conditions() # builds and adds the parameters into a giant filter,
and adds it to the query object, e.g.
myfilt = None
for param in parameters:
if parameter_table not in join, add it to the join:
query = query.join(parameter_table)
parse the input parameter into operand and value
newfilter = construct a new BinaryExpression based on input
if not myfilt:
myfilt = and_(newfilter)
else:
myfit = and_(myfilt, newfilter)
if any filter exists, add it to query:
query = query.filter(myfilt)
results = q.run() # runs the sql query and returns the results with either
query.all(), or query.one(), or query.count(), etc...
So if they want to change the conditions to query with, as far as my
limited understanding goes, they would either have to rebuild the query
from scratch and reapply the filters, or they would have to modify the
values inside the sqlalchemy query object? And it seems like this
bindparam is a nice way to allow for flexible attribute changes without
resetting the query.
Cheers, Brian
On Wednesday, March 2, 2016 at 5:31:09 PM UTC-5, Simon King wrote:
>
> Out of interest, how are you building your query, and why do you need to
> be able to change the values afterwards?
>
> Simon
>
> > On 2 Mar 2016, at 21:59, Brian Cherinka <[email protected] <javascript:>>
> wrote:
> >
> > Thanks, Mike. This is excellent. That did the trick. That's much
> easier than what I was trying to do. Do you know if there is a way to
> auto bindparam every parameter I have in my Declarative Bases, if and when
> they get added into a filter? Basically, I need to allow the user to be
> able to modify any parameter they set after the fact, but I have a crazy
> amount of parameters to explicitly do this for.
> >
> > Cheers, Brian
> >
> > On Wednesday, March 2, 2016 at 4:28:46 PM UTC-5, Mike Bayer wrote:
> >
> >
> >
> >
> > --
> > 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] <javascript:>.
> > To post to this group, send email to [email protected]
> <javascript:>.
> > Visit this group at https://groups.google.com/group/sqlalchemy.
> > For more options, visit https://groups.google.com/d/optout.
>
>
--
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.