# Path: norm/interface.py
# class IAsyncCursor(Interface):
# 
# 
#     def execute(query, params=None):
#         """
#         Execute the given sql and params.
# 
#         Return a C{Deferred} which will fire with the DB-API results.
#         """
# 
# 
#     def fetchone():
#         pass
# 
# 
#     def fetchall():
#         pass
# 
# 
#     def lastRowId():
#         """
#         Return a C{Deferred} id of the most recently inserted row.
#         """
# 
# 
#     def close():
#         pass
# 
# class IRunner(Interface):
#     """
#     I translate and run operations.
#     """
# 
# 
#     def runQuery(sql, params=None):
#         """
#         Run a query in a one-off transaction, returning the deferred result.
#         """
# 
# 
#     def runOperation(sql, params=()):
#         """
#         Run a query with no results
#         """
# 
# 
#     def runInteraction(function, *args, **kwargs):
#         """
#         Run a function within a database transaction.  The function will be
#         passed an L{IAsyncCursor} as the first arg.
#         """
# 
# 
#     def close():
#         """
#         Close the connections
#         """
# 
# class IPool(Interface):
# 
# 
#     def add(option):
#         """
#         Add a ready option to the pool
#         """
# 
# 
#     def remove(option):
#         """
#         Remove an option from the pool
#         """
# 
# 
#     def get():
#         """
#         Choose the next option; this will fire with a Deferred when the next
#         thing is ready for use.
#         """
# 
# 
#     def done(option):
#         """
#         Accept an option as ready
#         """
# 
# 
#     def list():
#         """
#         List all the things
#         """


from zope.interface import implements
from twisted.internet import defer
from functools import partial
from collections import deque, defaultdict
from norm.interface import IAsyncCursor, IRunner, IPool
    def _runOperation(self, cursor, qry, params):
        return cursor.execute(qry, params)


    def runInteraction(self, function, *args, **kwargs):
        cursor = self.cursorFactory(self.conn.cursor())
        d = defer.maybeDeferred(function, cursor, *args, **kwargs)
        d.addCallback(self._commit)
        d.addErrback(self._rollback)
        return d


    def _commit(self, result):
        self.conn.commit()
        return result


    def _rollback(self, result):
        self.conn.rollback()
        return result


    def close(self):
        return defer.maybeDeferred(self.conn.close)



class ConnectionPool(object):



# Path: norm/interface.py
# class IAsyncCursor(Interface):
# 
# 
#     def execute(query, params=None):
#         """
#         Execute the given sql and params.
# 
#         Return a C{Deferred} which will fire with the DB-API results.
#         """
# 
# 
#     def fetchone():
#         pass
# 
# 
#     def fetchall():
#         pass
# 
# 
#     def lastRowId():
#         """
#         Return a C{Deferred} id of the most recently inserted row.
#         """
# 
# 
#     def close():
#         pass
# 
# class IRunner(Interface):
#     """
#     I translate and run operations.
#     """
# 
# 
#     def runQuery(sql, params=None):
#         """
#         Run a query in a one-off transaction, returning the deferred result.
#         """
# 
# 
#     def runOperation(sql, params=()):
#         """
#         Run a query with no results
#         """
# 
# 
#     def runInteraction(function, *args, **kwargs):
#         """
#         Run a function within a database transaction.  The function will be
#         passed an L{IAsyncCursor} as the first arg.
#         """
# 
# 
#     def close():
#         """
#         Close the connections
#         """
# 
# class IPool(Interface):
# 
# 
#     def add(option):
#         """
#         Add a ready option to the pool
#         """
# 
# 
#     def remove(option):
#         """
#         Remove an option from the pool
#         """
# 
# 
#     def get():
#         """
#         Choose the next option; this will fire with a Deferred when the next
#         thing is ready for use.
#         """
# 
# 
#     def done(option):
#         """
#         Accept an option as ready
#         """
# 
# 
#     def list():
#         """
#         List all the things
#         """


# Path: norm/common.py
from zope.interface import implements
from twisted.internet import defer
from functools import partial
from collections import deque, defaultdict
from norm.interface import IAsyncCursor, IRunner, IPool

    def _runOperation(self, cursor, qry, params):
        return cursor.execute(qry, params)


    def runInteraction(self, function, *args, **kwargs):
        cursor = self.cursorFactory(self.conn.cursor())
        d = defer.maybeDeferred(function, cursor, *args, **kwargs)
        d.addCallback(self._commit)
        d.addErrback(self._rollback)
        return d


    def _commit(self, result):
        self.conn.commit()
        return result


    def _rollback(self, result):
        self.conn.rollback()
        return result


    def close(self):
        return defer.maybeDeferred(self.conn.close)



class ConnectionPool(object):



    implements(IRunner)