Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adapters and converters for list, dict, and object #58

Closed
Jaymon opened this issue Dec 26, 2017 · 1 comment
Closed

adapters and converters for list, dict, and object #58

Jaymon opened this issue Dec 26, 2017 · 1 comment

Comments

@Jaymon
Copy link
Owner

Jaymon commented Dec 26, 2017

in Postgres, this can be accomplished by doing:

class DictType(object):
    def adapt(self, val):
        pout.v("adapt")
        pout.v(val)
        # return a python dict converted to something sql safe

    def convert(self, val):
        pout.v("convert")
        pout.v(val)
        # return back to a python dict from sql safe syntax


d = DictType()
psycopg2.extensions.register_type(
       psycopg2.extensions.new_type(psycopg2.BINARY.values, "BINARY", d.convert)
)
psycopg2.extensions.register_adapter(dict, d.adapt)

Here are the adapting docs and the converting docs.

the register_adapter docs will be handy also.

Postgres already has support for dict and list types (also) depending on Postgres installation and stuff, so that could be a viable alternative also though it would be different than what SQLite would have to do, which might cause issues, though I'm not sure how often you would really switch from postgres to sqlite or vice versa.

SQLite has similar adapters/converters:

d = DictType()
sqlite3.register_adapter(dict, d.adapt)
sqlite3.register_converter('BINARY', d.convert)

I was thinking of doing something similar to Postgres's binary syntax where the converted values would be prepended with something like \dict or \list and then I would do something similar to the ObjectField where the value is pickled and then base64 encoded. So the converter could check to see if the string started with \dict or whatnot and if it was then it would convert back to the object, this way it could co-exist with other binary or text fields that it gets run against.

Really, being able to do:

foo = Field(dict)
foo = Field(list)
foo = Field(object)

and having everything just work is something I've always wanted to solve but I've never quite liked any of the solutions I've come up with

.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant