sprockets.mixins.postgresql

Handler mixins that automatically connect a PostgreSQL client session upon initialization.

Version Downloads Status Coverage License

Installation

sprockets.mixins.postgresql is available on the Python Package Index and can be installed via pip or easy_install:

pip install sprockets.mixins.postgresql

API Documentation

PostgreSQL Client Mixins

Sprockets mixins that automatically connects to PostgreSQL using sprockets.clients.postgresql.

Handlers implementing one of the mixins should set an attribute called DBNAME that specifies the database name to connect to. The value of DBNAME will be passed into the creation of the Session or TornadoSession object.

The Session classes wrap the Queries Session or TornadoSession providing environment variable based configuration.

The environment variables should be set using the DBNAME_[VARIABLE] format where [VARIABLE] is one of HOST, PORT, DBNAME, USER, and PASSWORD.

class sprockets.mixins.postgresql.AsyncHandlerMixin

A asynchronous Tornado handler mixin for connecting to PostgreSQL. The mixin automatically creates the database session using the DBNAME attribute of the class as the database name for the TornadoSession object creation.

Using the mixin, the name of the session attribute will be <dbname>_session, automatically created when initializing the object.

Example:

from sprockets.mixins import postgresql
from tornado import web


class FooRequestHandler(postgresql.AsyncHandlerMixin,
                        web.RequestHandler):

    DBNAME = 'foo'

    @web.asynchronous
    def get(self, *args, **kwargs):
        result = yield self.foo_session.query('SELECT * FROM bar')
        self.finish({'data': result.items()})
        result.free()
class sprockets.mixins.postgresql.HandlerMixin

A handler mixin for connecting to PostgreSQL. The mixin automatically creates the database session using the DBNAME attribute of the class as the database name for the Session object creation.

Using the mixin, the name of the session attribute will be <dbname>_session, automatically created when initializing the object.

Example:

from sprockets.mixins import postgresql
from tornado import web


class FooRequestHandler(postgresql.HandlerMixin,
                        web.RequestHandler,):

    DBNAME = 'foo'

    def get(self, *args, **kwargs):
        result = self.foo_session.query('SELECT * FROM bar')
        self.finish({'data': result.items()})

Examples

The following example demonstrates using the HandlerMixin with a synchronous Tornado RequestHandler for a database named postgres:

import os

from sprockets.mixins import postgresql
from tornado import web

os.environ['POSTGRES_HOST'] = 'localhost'
os.environ['POSTGRES_USER'] = 'postgres'
os.environ['POSTGRES_PORT'] = 5432
os.environ['POSTGRES_DBNAME'] = 'postgres'

class PostgresRequestHandler(postgresql.HandlerMixin,
                             web.RequestHandler):

    DBNAME = 'postgres'

    def get(self, *args, **kwargs):
        result = self.foo_session.query('SELECT * FROM bar')
        self.finish({'data': result.items()})

The second example demonstrates using the AsyncHandlerMixin with an asynchronous Tornado RequestHandler for a database named foo:

import os

from sprockets.mixins import postgresql
from tornado import web

os.environ['FOO_HOST'] = 'localhost'
os.environ['FOO_USER'] = 'postgres'
os.environ['FOO_PORT'] = 5432
os.environ['FOO_DBNAME'] = 'foo'
os.environ['FOO_PASSWORD'] = 'bar'

class FooRequestHandler(postgresql.HandlerMixin,
                        web.RequestHandler):

    DBNAME = 'foo'

    @web.asynchronous
    def get(self, *args, **kwargs):
        result = yield self.foo_session.query('SELECT * FROM baz')
        self.finish({'data': result.items()})
        result.free()

Version History

See Version History

Issues

Please report any issues to the Github project at https://github.com/sprockets/sprockets.mixins.postgresql/issues

Source

sprockets.mixins.postgresql source is available on Github at https://github.com/sprockets/sprockets.mixins.postgresql

License

sprockets.mixins.postgresql is released under the 3-Clause BSD license.

Indices and tables