Demo for Postgres or SQLite3

Issue #26 resolved
Ray Ch
created an issue

Can you please provide an example for membership or a simple blog engine with Postgres or SQLite3.

Comments (3)

  1. Andriy Kornatskyy repo owner

    The simplest version for sqlite session is in guestbook demo config and use in view.

    Here are more "advanced" versions:

    mock:

    def mock_sessions():
        from wheezy.core.db import NullSession
        return {'ro': NullSession, 'rw': NullSession}
    

    sqlite3:

    def sqlite_sessions():
        from wheezy.core.db import Session
        from wheezy.core.pooling import LazyPool
        from sqlite3 import connect
    
        def create_factory(read_only):
            def factory(c):
                if not c:
                    c = connect(config.runtime.connection_string)
                    if read_only:
                        # v3.8.0.2
                        c.cursor().execute('PRAGMA query_only=ON;')
                return c
            return factory
    
        pool_ro = LazyPool(create_factory(read_only=True), 1)
        pool_rw = LazyPool(create_factory(read_only=False), 1)
        return {
            'ro': lambda: Session(pool_ro),
            'rw': lambda: Session(pool_rw)
        }
    

    postgresql:

    def postgresql_sessions():
        from wheezy.core.db import Session
        from wheezy.core.pooling import LazyPool
        import psycopg2
        import psycopg2.extensions
    
        def create_factory():
            def factory(c):
                if not c or c.closed:
                    c = psycopg2.connect(config.runtime.connection_string)
                return c
            return factory
    
        class Psycopg2Session(Session):
    
            def __init__(self, pool, readonly):
                super(Psycopg2Session, self).__init__(pool)
                self.readonly = readonly
    
            def on_active(self, connection):
                connection.set_session(readonly=self.readonly)
    
        psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
        pool = LazyPool(create_factory(), 1)
        return {
            'ro': lambda: Psycopg2Session(pool, readonly=True),
            'rw': lambda: Psycopg2Session(pool, readonly=False)
        }
    
  2. Andriy Kornatskyy repo owner

    The repository implementation as following:

    mock (see source here):

    class MembershipRepository(object):
    
        def __init__(self, session):
            # ensure session is entered
            session.cursor()
    
        def has_account(self, username):
            return username in db['user']
    

    sql:

    class MembershipRepository(object):
    
        def __init__(self, session):
            self.session = session
    
        def has_account(self, username):
            c = self.session.cursor()
            c.callproc('membership.has_account',
                       (username,))
            return c.fetchone()[0] or ''
    
  3. Log in to comment