[Twisted-Python] adbapi.Augmention

Stephen C. Waterbury golux at comcast.net
Thu Nov 13 15:47:42 MST 2003


Jon Dyte wrote:

> the doc string of adbapi.Augmentation mentions just using the ConnectionPool 
> directly.
> 
> "A class which augments a database connector with some functionality.
> This class is now deprecated. Just use the ConnectionPool directly.
> Conventional usage of me is to write methods that look like..."
> 
> Presumbably this means DONT inherit from ConnectionPool like you
> used to inherit from Augmentation( or am I reading that incorrectly)
> and just create a ConnectionPool instance and run the queries through that.

Christopher Armstrong wrote:

> right.

But I think it works okay to subclass ConnectionPool ...
at least, it works for me.  Is there a better way to
do something like the following (which is working fine)?
(Admittedly you need to see my other modules for the full
picture, but you can infer what they would do ... as
you can probably guess, I don't use t.e.row.RowObject.)

----------------------------------------------------------------

# Twisted imports ...
from twisted.enterprise import adbapi
from twisted.internet import defer
from twisted.python import log, reflect
# Pan Galactic imports ...
from pangalactic.utils import sql, utils

class PgerDb(adbapi.ConnectionPool):
     """
     I am PGERDB, implemented using the Twisted Enterprise adbapi
     and pyPgSQL on top of the PGERDB Master Schema.
     """

     def __init__(self, dbapiname='pyPgSQL.PgSQL', database='pgerdb'):
         adbapi.ConnectionPool.__init__(self,
                                        dbapiname,
                                        database=database)
         self.dbname = database

     def doPgerdbQuery(self, table, subtypes, *args, **kw):
         """
         Run a query submitted as a list of args against a
         specified table.

         @type table:  string
         @param table: name of the table to search

         @type subtypes:  boolean
         @param subtypes: whether to search only the specified
                          table (False) or to include all subtype
                          tables (True)

         @type args:   list of tuples
         @param args:  a list of tuples, in which each tuple has the
                       tuple has the form:
                         - element[0] = column name
                         - element[1] = search value
                         - element[2] = operator
                       where I{operator} can be any SQL binary
                       operator.

         @param kw:    (optional kw arg)

                       B{sort}: column(s) to be sorted on -- this
                       can be either a string (a single column
                       name) or a list or tuple of column names
         """
         if args:
             sqlargs = sql.buildSelect(table, subtypes, *args, **kw)
             return self.runQuery(*sqlargs)
         else:
             raise TypeError, 'I need at least one search parameter'

     def getByRowKey(self, rowkey):
         """
         Get the resultset for this rowkey.

         @type rowkey:  list
         @param rowkey: a rowkey (for more about rowkeys, see
                        L{pangalactic.repo.factory.record2RowKey})

         @rtype:   a PgResultSet
         @return:  the result set that matches this rowkey.
         """
         sqlargs = sql.buildSelectFromRowKey(rowkey)
         return self.runQuery(*sqlargs)

     def addRow(self, table, *args, **kw):
         """
         Add a row to the specified table.

         @type table:  string
         @param table: name of the table to search

         @type args:   list
         @param args:  a list of 2-tuples:
                       [(colname, value), ...]

         @type kw:     dictionary
         @param kw:    {colname : value, ...}
         """
         parmdict = kw or dict(args)
         sqlcmd, parms = sql.buildInsert(table, parmdict)
         d = self.runOperation(sqlcmd, *parms)
         logmsg = 'addRow:  1 row added to %s table.' % table
         d.addCallback(lambda x: self._addRowDone(x, logmsg))
         d.addErrback(self.operationError)
         return d

     def _addRowDone(self, d, logmsg):
         """Callback for addRow success.
         """
         log.msg("%s operation %s" % (reflect.qual(self.__class__), logmsg))
         return d

     def addRows(self, records):
         """
         Add a series of rows, in a single transaction, from a
         list of records (for a description of "records", see
         L{pangalactic.repo.factory.extract2Record}).

         @type records:  list
         @param records: a list of records -- see
                         L{pangalactic.utils.factory} for the
                         definition of an extract
         """
         tables = utils.unique([rec[0] for rec in records])
         sqlstmt, parms = sql.buildTransactionalInsert(records)
         d = self.runOperation(sqlstmt, *parms)
         logmsg = 'addRows: rows added to tables %s.' % str(tables)
         d.addCallback(lambda x: self._addRowsConfirm(x, logmsg))
         d.addErrback(self.interactionError)
         return d

     def _addRowsConfirm(self, d, logmsg):
         """
         Callback for addRows success.
         """
         log.msg("%s %s" % (reflect.qual(self.__class__), logmsg))
         return d

     def operationDone(self, done):
         """
         Callback for database operation success.
         """
         log.msg("%s Operation done: %s" % (reflect.qual(self.__class__), done))

     def operationError(self, error):
         """
         Callback for database operation failure.
         """
         log.msg("%s Operation failed: %s" % (reflect.qual(self.__class__), error))
         log.err(error)

     def interactionDone(self, done):
         """
         Callback for database interaction success.
         """
         log.msg("%s Interaction done: %s" % (reflect.qual(self.__class__), done))

     def interactionError(self, error):
         """
         Callback for database interaction failure.
         """
         log.msg("%s Interaction failed: %s" % (reflect.qual(self.__class__), error))
         log.err(error)

----------------------------------------------------------------






More information about the Twisted-Python mailing list