mercredi 9 mars 2016

Is there a design pattern for an object representing a row from a query result?

Suppose that I make a complex query a database from an application, either by plain SQL or by a an ORM library:

SELECT user.name, book.title, home.address
       FROM user 
       JOIN book on user.book_id = book.id
       JOIN home on book.home_id = home.id;

The result is a list of (name, title, address) tuples.

It is often convenient to initialize an object from each row, a-la:

def Shipping(object):
    def __init__(self, row):
        self.name = row[0]
        self.title = row[1]
        self.address = row[2]

    def action(self):
        return "Sending %s to %s at %s" % (self.book, self.user, self.address)

Is there a design pattern for an object that represents a row in a DB query?

Aucun commentaire:

Enregistrer un commentaire