def main(cls, args): db = SimpleDB("studentdb") tx = db.newTx() mdm = db.mdMgr() # Open a scan on the data table. studentplan = TablePlan(tx, "student", mdm) studentscan = studentplan.open() # Open the index on MajorId. indexes = mdm.getIndexInfo("student", tx) ii = indexes.get("majorid") idx = ii.open() # Retrieve all index records having a dataval of 20. idx.beforeFirst(Constant(20)) while idx.next(): # Use the datarid to go to the corresponding STUDENT record. datarid = idx.getDataRid() studentscan.moveToRid(datarid) print(studentscan.getString("sname")) # Close the index and the data table. idx.close() studentscan.close() tx.commit()
def executeModify(self, data, tx): tblname = data.tableName() fldname = data.targetField() p = TablePlan(tx, tblname, self.mdm) p = SelectPlan(p, data.pred()) ii = self.mdm.getIndexInfo(tblname, tx).get(fldname) idx = None if (ii is None) else ii.open() s = p.open() count = 0 while s.next(): # first, update the record newval = data.newValue().evaluate(s) oldval = s.getVal(fldname) s.setVal(data.targetField(), newval) # then update the appropriate index, if it exists if idx is not None: rid = s.getRid() idx.delete(oldval, rid) idx.insert(newval, rid) count += 1 if idx is not None: idx.close() s.close() return count
def test2(): db = SimpleDB("studentdb") mdm = db.mdMgr() tx = db.newTx() p1 = TablePlan(tx, "student", mdm) s = p1.open() while s.next(): print(s.getString("sname"), s.getInt("gradyear"))
def main(cls, args): db = SimpleDB("studentdb") tx = db.newTx() mdm = db.mdMgr() studentplan = TablePlan(tx, "student", mdm) studentscan = studentplan.open() # Create a map containing all indexes for STUDENT. indexes = {} idxinfo = mdm.getIndexInfo("student", tx) for fldname in idxinfo.keySet(): idx = idxinfo.get(fldname).open() indexes[fldname] = idx # Task 1: insert a new STUDENT record for Sam # First, insert the record into STUDENT. studentscan.insert() studentscan.setInt("sid", 11) studentscan.setString("sname", "sam") studentscan.setInt("gradyear", 2023) studentscan.setInt("majorid", 30) # Then insert a record into each of the indexes. datarid = studentscan.getRid() for fldname in indexes.keys(): dataval = studentscan.getVal(fldname) idx = indexes.get(fldname) idx.insert(dataval, datarid) # Task 2: find and delete Joe's record studentscan.beforeFirst() while studentscan.next(): if studentscan.getString("sname") == "joe": # First, delete the index records for Joe. joeRid = studentscan.getRid() for fldname in indexes.keys(): dataval = studentscan.getVal(fldname) idx = indexes.get(fldname) idx.delete(dataval, joeRid) # Then delete Joe's record in STUDENT. studentscan.delete() break # Print the records to verify the updates. studentscan.beforeFirst() while studentscan.next(): print( studentscan.getString("sname") + " " + str(studentscan.getInt("sid"))) studentscan.close() for idx in indexes.values(): idx.close() tx.commit()
def executeInsert(self, data, tx): p = TablePlan(tx, data.tableName(), self.mdm) us = p.open() us.insert() iterator = data.vals().__iter__() for fldname in data.fields(): val = iterator.__next__() us.setVal(fldname, val) us.close() return 1
def executeDelete(self, data, tx): p = TablePlan(tx, data.tableName(), self.mdm) p = SelectPlan(p, data.pred()) us = p.open() count = 0 while us.next(): us.delete() count += 1 us.close() return count
def executeModify(self, data, tx): p = TablePlan(tx, data.tableName(), self.mdm) p = SelectPlan(p, data.pred()) us = p.open() count = 0 while us.next(): val = data.newValue().evaluate(us) us.setVal(data.targetField(), val) count += 1 us.close() return count
def executeDelete(self, data, tx): tblname = data.tableName() p = TablePlan(tx, tblname, self.mdm) p = SelectPlan(p, data.pred()) indexes = self.mdm.getIndexInfo(tblname, tx) s = p.open() count = 0 while s.next(): # first, delete the record's RID from every index rid = s.getRid() for fldname in indexes.keys(): val = s.getVal(fldname) idx = indexes.get(fldname).open() idx.delete(val, rid) idx.close() # then delete the record s.delete() count += 1 s.close() return count
def executeInsert(self, data, tx): tblname = data.tableName() p = TablePlan(tx, tblname, self.mdm) # first, insert the record s = p.open() s.insert() rid = s.getRid() # then modify each field, inserting an index record if appropriate indexes = self.mdm.getIndexInfo(tblname, tx) valIter = data.vals().__iter__() for fldname in data.fields(): val = valIter.__next__() s.setVal(fldname, val) ii = indexes.get(fldname) if ii is not None: idx = ii.open() idx.insert(val, rid) idx.close() s.close() return 1