Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import defaultdict
- class Schema:
- def __init__(self, columns, index = []):
- if len(columns) != len(set(columns)):
- return "Duplicate columns"
- self.columns = columns
- self.index = index
- self.colIndexMap = dict()
- for i, col in enumerate(columns):
- self.colIndexMap[col] = i
- def getColNumber(self):
- return len(self.columns)
- def getColumnIndex(self, key):
- return self.colIndexMap[key]
- class Table:
- def __init__(self, schema):
- self.schema = schema
- self.data = dict()
- self.rowCount = 1
- self.indexMap = dict()
- for index in schema.index:
- self.indexMap[index] = defaultdict(list)
- def insert(self, data):
- colNum = self.schema.getColNumber()
- if colNum != len(data):
- return "Number of columns passed doesnt match the schema"
- self.data[self.rowCount] = data
- self.rowCount += 1
- for index in self.schema.index:
- indexCol = self.schema.getColumnIndex(index)
- val = data[indexCol]
- self.indexMap[index][val].append(data)
- #print("after insertion indexmap", self.indexMap)
- return self.rowCount - 1
- def findByIndex(self, index, val):
- return self.indexMap[index][val]
- def delete(self, id):
- if id not in self.data:
- return "Id does not exist"
- del self.data[id]
- return True
- def getById(self, id):
- if id not in self.data:
- return "Id does not exist"
- return self.data[id]
- def updateById(self, id, toUpdate):
- if id not in self.data:
- return "Id does not exist"
- cols = self.data[id]
- for key in toUpdate:
- cols[ self.schema.getColumnIndex(key) ] = toUpdate[key]
- return True
- def getAll(self):
- return self.data
- def updateSchema(self, newSchema):
- currentCols = set(self.schema.columns)
- newCols = set(newSchema.columns)
- for k in self.data:
- newData = [""] * newSchema.getColNumber()
- for x in newCols:
- val = None
- if x in currentCols:
- #print("updating existing", x, self.schema.getColumnIndex(x))
- val = self.data[k][ self.schema.getColumnIndex(x) ]
- else:
- # this is a new column
- val = "" # default value for new column
- #print("new index", x, newSchema.getColumnIndex(x), len(newData))
- newData[ newSchema.getColumnIndex(x) ] = val
- self.data[k] = newData
- self.schema = newSchema
- class DB:
- def __init__(self):
- self.db = dict()
- def createTable(self, tableName, schema):
- if tableName in self.db:
- return "Table name already exist"
- self.db[tableName] = Table(schema)
- return "Table Created successfully"
- def getTable(self, tableName):
- if tableName not in self.db:
- return "Table name already exist"
- return self.db[tableName]
- schema1 = Schema(["rollno", "name", "email", "zip_code"], ["zip_code"])
- db = DB()
- db.createTable("users", schema1)
- users = db.getTable("users")
- for i in range(5):
- v = i & 1
- users.insert([i+1, "name " + str(i), "useremail" + str(i), "2080" + str(v)])
- # print("After insertion")
- print(users.getAll())
- print(users.findByIndex("zip_code", "20800"))
- print(users.findByIndex("zip_code", "20801"))
- # schema2 = Schema(["email", "phone"])
- # users.updateSchema(schema2)
- # print("After updation")
- # print(users.getAll())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement