User Tools

Site Tools


rmsaccesshighapiexample

This is an old revision of the document!


import struct
from vms.rmsdef import RMS__SUC, RMS__RNF, RMS__OK_DUP
from vms.fabdef import FAB_M_PUT, FAB_M_GET, FAB_M_DEL, FAB_M_UPD, \
       FAB_M_SHRPUT, FAB_M_SHRGET, FAB_M_SHRDEL, FAB_M_SHRUPD
from vms.rms.IndexedFile import IndexedFile
from construct import *
 
TestRec = Struct("TestRec", String("f1", 5), SLInt32("f2"), String("f3", 11))
 
class TestFile(IndexedFile):
    Name = 'test.dat'
    def __init__(self):
        IndexedFile.__init__(self, TestFile.Name, TestRec)
 
    def primary_keynum(self):
        return 0
 
    def keyval(self, rec, keynum):
        if keynum == 0:
            return rec.f1
        elif keynum == 1:
            return rec.f2
        else:
            raise "Invalid keynum parameter"
 
    def pack_key(self, keynum, keyval):
        if keynum == 0:
            return struct.pack("=5s", keyval)
        elif keynum == 1:
            return struct.pack("=i", keyval)
        else:
           raise KeyError
 
if __name__=='__main__':
    f = TestFile()
    print
    print 'Clear file'
    f.deleteall()
 
    print
    print 'Insert a few records'
    rec = Container(f1 = 'AA5AA', f2 = 5, f3 = '1234567890A')
    f.put(rec)
    rec = Container(f1 = 'BB2BB', f2 = 2, f3 = '1234567890B')
    f.put(rec)
    rec = Container(f1 = 'CC4CC', f2 = 4, f3 = '1234567890C')
    f.put(rec)
    rec = Container(f1 = 'DD1DD', f2 = 1, f3 = '1234567890D')
    f.put(rec)
    rec = Container(f1 = 'EE1EE', f2 = 2, f3 = '1234567890E')
    f.put(rec)
    rec = Container(f1 = 'FF3FF', f2 = 3, f3 = '1234567890F')
    f.put(rec)
 
    print
    print 'Initial records using an iterator, primary key order'
    for rec in f:
        print rec.f1, rec.f2, rec.f3
 
    print
    print 'Initial records using an iterator, secondary key order'
    f.iterator_keynum(1)
    for rec in f:
        print rec.f1, rec.f2, rec.f3
 
    print
    print 'Update all records using an iterator'
    # iterator key is automatically reset to primary key
    for rec in f:
        rec.f2 = rec.f2 + 10
        f.update_current(rec)
 
    print
    print 'Updated records using an iterator'
    for rec in f:
        print rec.f1, rec.f2, rec.f3
 
    print
    print 'build a list of all records'
    lst = f.fetchall(0)
    print len(lst)
 
    print
    print 'delete a record using secondary key value 14'
    f.delete(1, 14)
 
    print
    print 'Records using an iterator, secondary key order'
    f.iterator_keynum(1)
    for rec in f:
        print rec.f1, rec.f2, rec.f3
 
    print 'delete all records using secondary key value 12'
    f.open(acc = FAB_M_GET + FAB_M_DEL)
    f.usekey(1)
    f.rewind()
    f.find(None, f.pack_key(1, 12))
    for rec in f:
        if rec.f2 == 12:
           f.delete_current()
        else:
           break
    f.close()
 
    print
    print 'Records using an iterator, primary key order'
    for rec in f:
        print rec.f1, rec.f2, rec.f3

Result:

Reset file

Insert a few records

Initial records using an iterator, primary key order
AA5AA 5 1234567890A
BB2BB 2 1234567890B
CC4CC 4 1234567890C
DD1DD 1 1234567890D
EE1EE 2 1234567890E
FF3FF 3 1234567890F

Initial records using an iterator, secondary key order
DD1DD 1 1234567890D
BB2BB 2 1234567890B
EE1EE 2 1234567890E
FF3FF 3 1234567890F
CC4CC 4 1234567890C
AA5AA 5 1234567890A

Update all records using an iterator

Updated records using an iterator
AA5AA 15 1234567890A
BB2BB 12 1234567890B
CC4CC 14 1234567890C
DD1DD 11 1234567890D
EE1EE 12 1234567890E
FF3FF 13 1234567890F

build a list of all records
6

delete a record using secondary key value 14

Records using an iterator, secondary key order
DD1DD 11 1234567890D
BB2BB 12 1234567890B
EE1EE 12 1234567890E
FF3FF 13 1234567890F
AA5AA 15 1234567890A
delete all records using secondary key value 12

Records using an iterator, primary key order
AA5AA 15 1234567890A
DD1DD 11 1234567890D
FF3FF 13 1234567890F
rmsaccesshighapiexample.1459835572.txt.gz · Last modified: 2016/04/05 07:52 by jfp