# 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
        """
        The connection is bad, try the query again.
        """
        retval = original_failure
        try:
            new_conn = yield self.makeConnection()
            m = getattr(new_conn, name)
            retval = yield m(*args, **kwargs)
            self.pool.remove(bad_conn)
            self.pool.add(new_conn)
        except:
            pass
        defer.returnValue(retval)


    def close(self):
        dlist = []
        for item in self.pool.list():
            dlist.append(defer.maybeDeferred(item.close))
        return defer.gatherResults(dlist)        




class NextAvailablePool(object):
    """
    I give you the next available object in the pool.
    """



# 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

        """
        The connection is bad, try the query again.
        """
        retval = original_failure
        try:
            new_conn = yield self.makeConnection()
            m = getattr(new_conn, name)
            retval = yield m(*args, **kwargs)
            self.pool.remove(bad_conn)
            self.pool.add(new_conn)
        except:
            pass
        defer.returnValue(retval)


    def close(self):
        dlist = []
        for item in self.pool.list():
            dlist.append(defer.maybeDeferred(item.close))
        return defer.gatherResults(dlist)        




class NextAvailablePool(object):
    """
    I give you the next available object in the pool.
    """



    implements(IPool)