views:

256

answers:

3

Rather than use an ORM, I am considering the following approach in Python and MySQL with no ORM (SQLObject/SQLAlchemy). I would like to get some feedback on whether this seems likely to have any negative long-term consequences since in the short-term view it seems fine from what I can tell.

Rather than translate a row from the database into an object:

  • each table is represented by a class
  • a row is retrieved as a dict
  • an object representing a cursor provides access to a table like so:

    cursor.mytable.get_by_ids(low, high)

  • removing means setting the time_of_removal to the current time

So essentially this does away with the need for an ORM since each table has a class to represent it and within that class, a separate dict represents each row.

Type mapping is trivial because each dict (row) being a first class object in python/blub allows you to know the class of the object and, besides, the low-level database library in Python handles the conversion of types at the field level into their appropriate application-level types.

If you see any potential problems with going down this road, please let me know. Thanks.

+6  A: 

That doesn't do away with the need for an ORM. That is an ORM. In which case, why reinvent the wheel?

Is there a compelling reason you're trying to avoid using an established ORM?

Eevee
Well, mainly I don't want to learn all the ins and outs of an ORM especially when I won't need 90% of the functionality that it defines. It's the same reason I prefer to use Notepad over Microsoft Word most of the time.
tirus
Well, don't learn that 90%, then. It's still astoundingly useful to have it around if you ever DO need it. Incidentally, speaking of text editing, I had about the same attitude when I tried Vim; I learned enough to move around and type and that was about it. But I kept finding more advanced things I wanted to do, went off to learn how to do them efficiently, and now I'm a Vim ninja. A Vimja, if you will.
Eevee
There are major benefits to using a mature ORM such as it being mature, well tested and well documented. Reinventing the wheel only means you start from scratch, yet again, and anyone who picks up your code now how to learn your ORM but without the benefit of vast documentation and tutorials.
Soviut
+2  A: 

You will still be using SQLAlchemy. ResultProxy is actually a dictionary once you go for .fetchmany() or similar.

Use SQLAlchemy as a tool that makes managing connections easier, as well as executing statements. Documentation is pretty much separated in sections, so you will be reading just the part that you need.

iElectric
A: 

web.py has in a decent db abstraction too (not an ORM). Queries are written in SQL (not specific to any rdbms), but your code remains compatible with any of the supported dbs (sqlite, mysql, postresql, and others).

from http://webpy.org/cookbook/select:

myvar = dict(name="Bob")
results = db.select('mytable', myvar, where="name = $name")