Btrieve 2
読み取り中…
検索中…
一致する文字列を見つけられません
block.py

これは Python で書かれたレコードのロックおよびロック解除の例です。

1import sys
2import struct
3import math
4import os
5import platform
6
7if (platform.system() == "Windows"):
8 sys.path.append("C:\\Program Files\\Actian\\Zen\\bin")
9else:
10 sys.path.append("/usr/local/actianzen/lib64")
11
12import btrievePython
13
14def updateRecord(expectRecordInUse):
15 btrieveClient = btrievePython.BtrieveClient(0,2)
16 assert(btrieveClient != None)
17
18 btrieveFile = btrievePython.BtrieveFile()
19 assert(btrieveFile != None)
20
21 rc = btrieveClient.FileOpen(btrieveFile, btrieveFileName, None, btrievePython.Btrieve.OPEN_MODE_NORMAL)
22 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileOpen():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
23
24 record = struct.pack(recordFormat, 0, 0, 0)
25
26 rc = btrieveFile.RecordRetrieveFirst(btrievePython.Btrieve.INDEX_1, record)
27 assert(rc == recordLength), "Error: BtrieveFile::RecordRetrieveFirst():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
28
29 i, isqr, isqrt = struct.unpack(recordFormat, record)
30 isqr += 1
31 isqrt += 1
32 record = struct.pack(recordFormat, i, isqr, isqrt)
33
34
35 rc = btrieveFile.RecordUpdate(record)
36
37
38 if (expectRecordInUse == True):
39 assert(rc == btrievePython.Btrieve.STATUS_CODE_RECORD_INUSE), "Error: BtrieveFile::RecordUpdate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
40 elif (expectRecordInUse == False):
41 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::RecordUpdate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
42
43 rc = btrieveClient.FileClose(btrieveFile)
44 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileClose():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
45
46 return rc
47
48min_x = 0
49max_x = 255
50btrieveFileName = "squaresAndSquareRoots.btr"
51recordFormat = "<BHd"
52recordLength = 11
53keyFormat = "<B"
54
55# If the incorrect number of arguments were given.
56if (len(sys.argv) != 1):
57 sys.exit("Usage: " + os.path.basename(sys.argv[0]))
58
59btrieveClient = btrievePython.BtrieveClient(0,1)
60assert(btrieveClient != None)
61
62btrieveFileAttributes = btrievePython.BtrieveFileAttributes()
63assert(btrieveFileAttributes != None)
64
65rc = btrieveFileAttributes.SetFixedRecordLength(recordLength)
66assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFileAttributes::SetFixedRecordLength():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
67
68rc = btrieveClient.FileCreate(btrieveFileAttributes, btrieveFileName, btrievePython.Btrieve.CREATE_MODE_OVERWRITE)
69assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
70
71btrieveFile = btrievePython.BtrieveFile()
72assert(btrieveFile != None)
73
74rc = btrieveClient.FileOpen(btrieveFile, btrieveFileName, None, btrievePython.Btrieve.OPEN_MODE_NORMAL)
75assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileOpen():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
76
77# For all the values of x.
78for i in range(min_x, max_x + 1):
79 record = struct.pack(recordFormat, i, i * i, math.sqrt(i))
80
81 rc = btrieveFile.RecordCreate(record)
82 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::RecordCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
83
84btrieveKeySegment = btrievePython.BtrieveKeySegment()
85assert(btrieveKeySegment != None)
86
87rc = btrieveKeySegment.SetField(0, 1, btrievePython.Btrieve.DATA_TYPE_UNSIGNED_BINARY)
88assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveKeySegment::SetField():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
89
90btrieveIndexAttributes = btrievePython.BtrieveIndexAttributes()
91assert(btrieveIndexAttributes != None)
92
93rc = btrieveIndexAttributes.AddKeySegment(btrieveKeySegment)
94assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveIndexAttributes::AddKeySegment():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
95
96rc = btrieveFile.IndexCreate(btrieveIndexAttributes)
97assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::IndexCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
98
99# print record
100record = struct.pack(recordFormat, 0, 0, 0)
101
102
103rc = btrieveFile.RecordRetrieveFirst(btrievePython.Btrieve.INDEX_1, record, btrievePython.Btrieve.LOCK_MODE_SINGLE_WAIT)
104assert(rc == recordLength), "Error: BtrieveFile::RecordRetrieveFirst():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
105
106
107print ("record before update:", struct.unpack(recordFormat, record))
108
109rc = updateRecord(True)
110assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR)
111
112
113rc = btrieveFile.RecordUnlock(btrievePython.Btrieve.UNLOCK_MODE_SINGLE)
114assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::RecordUnlock():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
115
116
117rc = btrieveFile.RecordRetrieveFirst(btrievePython.Btrieve.INDEX_1, record)
118assert(rc == recordLength), "Error: BtrieveFile::RecordRetrieveFirst():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
119
120print ("record after update while locked:", struct.unpack(recordFormat, record))
121
122rc = updateRecord(False)
123assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR)
124
125rc = btrieveFile.RecordRetrieveFirst(btrievePython.Btrieve.INDEX_1, record, btrievePython.Btrieve.LOCK_MODE_SINGLE_WAIT)
126assert(rc == recordLength), "Error: BtrieveFile::RecordRetrieveFirst():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
127
128print ("record after update while unlocked:", struct.unpack(recordFormat, record))
129
130rc = btrieveClient.FileClose(btrieveFile)
131assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileClose():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
132
133rc = btrieveClient.FileDelete(btrieveFileName)
134assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileDelete():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))