9

I am trying to implement a simple database program in python. I get to the point where I have added elements to the db, changed the values, etc.

class db:
    def __init__(self):
            self.database ={}

    def dbset(self, name, value):
            self.database[name]=value

    def dbunset(self, name):
            self.dbset(name, 'NULL')

    def dbnumequalto(self, value):
            mylist = [v for k,v in self.database.items() if v==value]
            return mylist

def main():
    mydb=db()
    cmd=raw_input().rstrip().split(" ")
    while cmd[0]!='end':
            if cmd[0]=='set':
                    mydb.dbset(cmd[1], cmd[2])
            elif cmd[0]=='unset':
                    mydb.dbunset(cmd[1])
            elif cmd[0]=='numequalto':
                    print len(mydb.dbnumequalto(cmd[1]))
            elif cmd[0]=='list':
                    print mydb.database
            cmd=raw_input().rstrip().split(" ")

if __name__=='__main__':
    main()

Now, as a next step I want to be able to do nested transactions within this python code.I begin a set of commands with BEGIN command and then commit them with COMMIT statement. A commit should commit all the transactions that began. However, a rollback should revert the changes back to the recent BEGIN. I am not able to come up with a suitable solution for this.

3 Answers 3

7

A simple approach is to keep a "transaction" list containing all the information you need to be able to roll-back pending changes:

def dbset(self, name, value):
    self.transaction.append((name, self.database.get(name)))
    self.database[name]=value

def rollback(self):
    # undo all changes
    while self.transaction:
        name, old_value = self.transaction.pop()
        self.database[name] = old_value

def commit(self):
    # everything went fine, drop undo information
    self.transaction = []
Sign up to request clarification or add additional context in comments.

Comments

1

If you are doing this as an academic exercise, you might want to check out the Rudimentary Database Engine recipe on the Python Cookbook. It includes quite a few classes to facilitate what you might expect from a SQL engine.

  • Database is used to create database instances without transaction support.
  • Database2 inherits from Database and provides for table transactions.
  • Table implements database tables along with various possible interactions.

Several other classes act as utilities to support some database actions that would normally be supported.

  • Like and NotLike implement the LIKE operator found in other engines.
  • date and datetime are special data types usable for database columns.
  • DatePart, MID, and FORMAT allow information selection in some cases.

In addition to the classes, there are functions for JOIN operations along with tests / demonstrations.

1 Comment

Thanks for the answer. I was looking to implement my own python code for a simple database.
0

This is all available for free in the built in sqllite module. The commits and rollbacks for sqllite are discussed in more detail than I can understand here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.