On Nov 6, 2008, at 12:28 AM, Adam Ryan wrote:
>
> I can't store the query object in the beaker session because it can't
> be pickled, right?. So is there a way to dump a string representation
> of a query object, then recreate it and associate it with a new SQLA
> Session?
>
> Or am I missing the obvious? Thanks again for the help.
>
OK, after some cobbling I have a great solution for this, I built a
custom pickler/unpickler which stores key objects sparsely on the
pickle side, can rebind back to any metadata/session/engine on the
unpickle side. So you can pickle a whole Query object, or whatever,
with no problem. The size of the string is still a few thousand
characters but not nearly as huge as it would be if it was pickling
the whole map of Table and mapper objects associated.
Check out the svn trunk, and usage is as follows (this is from the
docstring):
from sqlalchemy.ext.serializer import loads, dumps
metadata = MetaData(bind=some_engine)
Session = scoped_session(sessionmaker())
# ... define mappers
query =
Session
.query
(MyClass).filter(MyClass.somedata=='foo').order_by(MyClass.sortkey)
# pickle the query
serialized = dumps(query)
# unpickle. Pass in metadata + scoped_session, both are optional
depending on what you're deserializing
query2 = loads(serialized, metadata, Session)
print query2.all()
this is all brand new code and several structures have special
serialization procedures. If you're playing with it and get errors,
we probably need to add more structures to the list of special-needs
objects. The unit tests are fleshing out a good deal of scenarios so
far but I can think of more which haven't been tested yet.
For usage with Beaker, you're going to want to do this serialization
procedure by hand and then place the resulting string in the session,
then use the specialized loads() on the way out.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---