Introduction of AmiDB

Using other peoples code can be a painful experience. One runs into bugs and limitations (this is especially true if one uses frameworks or libraries). To solve this I use mailund's approach, I code my own thing!

I had used SQLObject for Orangoo Feed Reader, Skeletonz and some internal projects. I had also used ZODB for the first version of Orangoo Feed Reader. My conclusion:

  • Using only object-relational mapping sucks BADLY. Using object storage sucks even more than that.

They basically have a lot of limitations and those that bugged me most was expressiveness and lack of control over queries. ZODB performance was really awful.

To solve this I have coded AmiDB. I tried to create a SQL libary that has:

  • The expressiveness and power of pure SQL
  • The smartness and easiness of object-relational mapping

If you ask me then I have mostly succeeded... But maybe a lot of people don't have the same taste like me. I use this library in the newest version of Skeletonz and it works very fine (a lot better than SQLObject). Here comes a little tutorial showing the stuff that AmiDB has to offer.

Setting up a table

Set up following table:

CREATE TABLE person (
  id int(11) NOT NULL auto_increment,
  name varchar(255),
  email varchar(255) DEFAULT '',
  website varchar(255) default '',
  age int(11) default 0,
  PRIMARY KEY (id)
);

Upcoming project of mine will probably be managing the creation and updates of SQL databases. Right now you must use the old way ;)

Importing AmiDB and setting up a connection pool

AmiDB features own connection pool (yay). It's pretty trivial to set up:

import amidb
amidb.user = ""
amidb.password = ""
amidb.db = ""
amidb.host = ""

connection_pool = amidb.ConnectionPool()
db = connection_pool.getConnection()

Now you can use db to insert, query, update etc. the database.

After you are the done with the connection, it is cool to return back the connection to the pool:

connection_pool.releaseConnection(db)

Inserting Amir and Robocop

If you have a person table and can connect to your database, then you are ready to insert a couple of persons:

amir_id = db.insert("person", name="Amir Salihefendic", age=20)
robocop_id = db.insert("person", name="Alex J. Murphy", age=33)

Notice that db.insert is a function that can take arbitrary parameters... Most of the functions one uses in AmiDB can do this. Python ROCKS.

Selecting Amir

To select you can use db.select - which is also a function that can take arbitrary parameters:

amir = db.select("person", name="Amir Salihefendic", as_one=True)
print amir.name

Notice the way name is printed...

Selecting all persons and printing them out

The code speaks for itself:

all_persons = db.select("person")
for person in all_persons:
  print "%s is %i old" % (person.name, person.age)

Updating Amir's email

The code speaks for itself:

db.update("person", "name='Amir Salihefendic'", email="amix@amix.dk")
amir = db.select("person", id=amir_id, as_one=True)
print amir.email

Decorating a query

I really like this. One can enscauplate a query into arbitary objects...:

class Person:
  def getName(self):
    return "My name is %s" % self.name
amir = db.select("person", id=amir_id, obj_deco=Person, as_one=True)
print amir.getName()

This is a really nice feature!

Do a query

The code speaks for itself:

persons = db.query("SELECT * FROM person", True)
for p in persons:
  print "%s's email is '%s'" % (p.name, p.email)

Delete persons

The code speaks for itself:

db.delete("person", name="Amir Salihefendic")
db.delete("person", name="Alex J. Murphy")

Improvements or comments?

Always welcomed! I hope AmiDB gives you some new ideas...

Code · Orangoo · Python 3. May 2006
8 comments so far


You know, my approach is not to just code my own all the time ;-) If I am not satisfied with an interface, I wrap it, like you have done here ... then later, if I am not happy with the performance, I can easily replace the underlying code with something else (possibly my own)...

Damn it Mailund, I thought we were on the same team ;-)

Anyway, now that I think of it I started with:

  • CherryPy as web framework
  • MochiKit as JavaScript library
  • SQLObject as ORM
  • Textile as HTML generator

Then after some time, I rewrote them from the ground up into:

  • AmiWeb as web framework
  • AmiJS as JavaScript library
  • AmiDB as ORM
  • AmiFormat as HTML generator

Those frameworks are very minimal and they mimic the preceding frameworks. Kind of insane to actually do this, since it is a lot of work - but who said I was sane?

In an Agatha Christie crime novel it's hard to guess the murder in the middle of the book... Same thing happened to me, in the middle of my work I had no clue that I was doing anything from the ground up. The suddenly I had redone everything... Maybe some aliens had abducted me and taken me to Frogstar World B and into the Total Perspective Vortex - so that I in one instant witnessed the whole creation of the frameworks and myself in relation to them - or maybe not ;-)

...or maybe not, indeed. You might just be mental ;-)

I do not believe

carla

vrv

Dear Amir,
what about SQLite support in AmiDB ? Would it be that hard ?

great, very useful!

عقارات السعودية - عقارات الرياض - عقارات الخرج - عقارات مكة المكرمة - عقارات جدة - عقارات الطائف - عقارات المدينة المنورة - عقارات ينبع - عقارات الدمام - عقارات الخبر - عقارات الأحساء - عقارات القصيم - عقارات عسير - عقارات حائل - عقارات تبوك - عقارات الباحة - عقارات الحدود الشمالية - عقارات الجوف - عقارات جازان - عقارات نجران - عقارات مصر - عقارات القاهرة - عقارات الجيزة - عقارات حلوان - عقارات 6 اكتوبر - عقارات الاسكندرية - عقارات الساحل الشمالي - عقارات البحيرة - عقارات السويس - عقارات الاسماعلية - عقارات بورسعيد - عقارات البحر الاحمر - عقارات مطروح - عقارات جنوب سيناء - عقارات شمال سيناء - عقارات دمياط - عقارات الدقهلية - عقارات كفر الشيخ - عقارات الشرقية - عقارات الغربية - عقارات القليوبية - عقارات المنوفبة - عقارات الفيوم - عقارات قنا - عقارات المنيا - عقارات اسيوط - عقارات بني سويف - عقارات سوهاج - عقارات الوادي الجديد - عقارات الأقصر - عقارات اسوان - عقارات الامارات - عقارات ابوظبي - عقارات العين - عقارات دبي - عقارات جبل علي - عقارات الشارقة - عقارات رأس الخيمة - عقارات عجمان - عقارات أم القيوين - عقارات الفجيرة - الوطن العربي - عقارات الكويت - عقارات عمان - عقارات قطر - عقارات البحرين - عقارات الاردن - عقارات لبنان - عقارات المغرب - عقارات السودان - عقارات سوريا - وظائف - وظائف في السعودية - وظائف في الامارات - وظائف في مصر - وظائف في الكويت - وظائف في عمان - وظائف في قطر - وظائف في البحرين - وظائف في الاردن - وظائف في لبنان - وظائف في المغرب - وظائف في السودان - وظائف في سوريا - خدمات - العروض الجديدة - الرعاية و الإعلان - الدعم الفني - العقارات العام - شراء شقق - شراء شقة - شقق بالتقسيط - شقة بالتقسيط - شقق تمليك - شقة تمليك - شقق سكنية - شقة سكنية - شقق فندقية - شقة فندقية - شقق للايجار - شقة للايجار - شقق للبيع - شقة للبيع - شقق مفروشة - شقة مفروشة - غير مصنف - مطلوب شقق - مطلوب شقة
اهداف الدوري الأنجليزي
اهداف الدوري الأسباني
اهداف دوري ابطال اوروبا
مهارات ولقطات منوعة
اهداف الدوري السعودي
اهداف تصفيات كأس العالم
اهداف الدوري المصري
اهداف الدوري الالماني
أهداف كأس الخليج
هدف تيوب

Post a comment
Commenting on this post has expired.
© 2000-2009 amix. Powered by Skeletonz.