Btrieve 2
Loading...
Searching...
No Matches
btestvlr.py

This is an example of variable length records based on btest.py .

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
14min_x = 0
15max_x = 255
16btrieveFileName = "squaresAndSquareRoots.btr"
17recordFormat = "<BHd"
18recordFixedSize = 11
19recordMaximumVariableSize = 1024
20recordMaximumTotalSize = recordFixedSize + recordMaximumVariableSize
21keyFormat = "<B"
22
23# If the incorrect number of arguments were given.
24if (len(sys.argv) != 2):
25 sys.exit("Usage: " + os.path.basename(sys.argv[0]) + " uint8_value")
26
27integerValue = int(sys.argv[1])
28
29# If integerValue is out of range.
30if (integerValue < min_x) or (integerValue > max_x):
31 sys.exit("Usage: " + os.path.basename(sys.argv[0]) + " uint8_value")
32
33btrieveClient = btrievePython.BtrieveClient()
34assert(btrieveClient != None)
35
36btrieveFileAttributes = btrievePython.BtrieveFileAttributes()
37assert(btrieveFileAttributes != None)
38
39rc = btrieveFileAttributes.SetFixedRecordLength(recordFixedSize)
40assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFileAttributes::SetFixedRecordLength():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
41
42rc = btrieveFileAttributes.SetVariableLengthRecordsMode(btrievePython.Btrieve.VARIABLE_LENGTH_RECORDS_MODE_YES)
43assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFileAttributes::SetVariableLengthRecordsMode():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
44
45rc = btrieveClient.FileCreate(btrieveFileAttributes, btrieveFileName, btrievePython.Btrieve.CREATE_MODE_OVERWRITE)
46assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
47
48btrieveFile = btrievePython.BtrieveFile()
49assert(btrieveFile != None)
50
51rc = btrieveClient.FileOpen(btrieveFile, btrieveFileName, None, btrievePython.Btrieve.OPEN_MODE_NORMAL)
52assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileOpen():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
53
54# For all the values of x.
55for i in range(min_x, max_x + 1):
56 iSquared = i * i
57 iSquareRoot = math.sqrt(i)
58 text = "The square of " + str(i) + " is " + str(iSquared) + " and its square root is " + str(iSquareRoot) + "."
59 variableRecordFormat = recordFormat + str(len(text)) + "s"
60 record = struct.pack(variableRecordFormat, i, iSquared, iSquareRoot, bytes(text, "utf-8"))
61
62 rc = btrieveFile.RecordCreate(record)
63 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::RecordCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
64
65btrieveKeySegment = btrievePython.BtrieveKeySegment()
66assert(btrieveKeySegment != None)
67
68rc = btrieveKeySegment.SetField(0, 1, btrievePython.Btrieve.DATA_TYPE_UNSIGNED_BINARY)
69assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveKeySegment::SetField():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
70
71btrieveIndexAttributes = btrievePython.BtrieveIndexAttributes()
72assert(btrieveIndexAttributes != None)
73
74rc = btrieveIndexAttributes.AddKeySegment(btrieveKeySegment)
75assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveIndexAttributes::AddKeySegment():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
76
77rc = btrieveFile.IndexCreate(btrieveIndexAttributes)
78assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::IndexCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
79
80# print record
81record = bytes(recordMaximumTotalSize)
82key = struct.pack(keyFormat, integerValue)
83
84rc = btrieveFile.RecordRetrieve(btrievePython.Btrieve.COMPARISON_EQUAL, btrievePython.Btrieve.INDEX_1, key, record)
85assert(rc >= recordFixedSize), "Error: BtrieveFile::RecordRetrieve():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
86
87variableRecordFormat = recordFormat + str(rc - recordFixedSize) + "s"
88i, iSquared, iSquareRoot, text = struct.unpack_from(variableRecordFormat, record)
89print("record: ({}, {}, {}, {})".format(i, iSquared, iSquareRoot, str(text, "utf-8")))
90
91rc = btrieveClient.FileClose(btrieveFile)
92assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileClose():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
93
94rc = btrieveClient.FileDelete(btrieveFileName)
95assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileDelete():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))