Python Mini ORM

Recently I was researching on python ORMs like SqlAlchemy, SqlObject or the Django Object relational mapper. The truth is, and I read this from a remarked person on the area, that write an ORM from scratch is very simple! You don’t need to have a lot of knowledges about SQL, DataBases Engines or Data Persistance to make your own Object relational mapper…

Obviously I’m talking of a very simple ORM that implements the basics Concept of SQL to Objects. Don’t expect that a Basic ORM written from scratch scale-up like SqlAlchemy or the Django’s ORM. These are very mature projects with years of development and maintance.

Last days I Wrote a very simple ORM and I published it in pastebin.

I want to show you what you can do with this little toy:

 

A Select * from ‘Table’ and return a list of Table_row objects with the correspondent properties that match the table fields using a sqlite engine looks like this:

>>> import sqlite3

>>> from core import DataBase

>>> db = DataBase(provider=sqlite3, db='youdatabase.db')

>>> print db.Table("Person").rows

[<core.Person_Row object at 0x7fa522ad2050>, <core.Person_Row object at 0x7fa522ad2090>, <core.Person_Row object at 0x7fa522ad20d0>]

Awesome! Really? Yes, if you think that the ORM don’t have more than 160 lines of python code.

 

Let’s have more fun. A more Complex query can be:

>>> import sqlite3

>>> from core import DataBase

>>> db = DataBase(provider=sqlite3, db='youdatabase.db')

>>> print [s.album for s in db.Table("Songs").filter("id > 15").filter("year = 2000").order_by("song").rows]

[u'essential selection vol. one', u'The Beatles 1', u'Please']

It returns a list of Strings with the albums of the songs with id greater than 15 that belong to the 2000 year, order by the song name! Look you can use “s.album”. You have an Song_row Object matching the database table.

 

It’s simple to get the column names of the table wich you’re querying:

>>> import sqlite3

>>> from core import DataBase

>>> db = DataBase(provider=sqlite3, db='youdatabase.db')

>>> print [c for c in db.Table("Songs").columns]

[u'id', u'song', u'album', u'year']

That is all for this moment… you can also use Group by statements as easier as the examples in this post.

At the moment this toy ORM only work with import MySQLdb sqlite3 and psycopg2 modules but it’s easy to extend!

 

The project is hosted at GoogleCode and published also in pastebin. Feel Free to modify the code and get a lot of fun with it! Here are the links:

http://pastebin.com/7SZGzZJ2

svn checkout http://py-mini-orm.googlecode.com/svn/trunk/ py-mini-orm-read-only

 

Happy Coding =)

Regards, Juan Manuel

Advertisements

2 thoughts on “Python Mini ORM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s