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"
18recordLength = 11
19keyFormat = "<B"
20
21
22if (len(sys.argv) != 2):
23 sys.exit("Usage: " + os.path.basename(sys.argv[0]) + " uint8_value")
24
25integerValue = int(sys.argv[1])
26
27
28if (integerValue < min_x) or (integerValue > max_x):
29 sys.exit("Usage: " + os.path.basename(sys.argv[0]) + " uint8_value")
30
31
32btrieveClient = btrievePython.BtrieveClient()
33assert(btrieveClient != None)
34
35
36
37btrieveFileAttributes = btrievePython.BtrieveFileAttributes()
38assert(btrieveFileAttributes != None)
39
40
41
42rc = btrieveFileAttributes.SetFixedRecordLength(recordLength)
43assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFileAttributes::SetFixedRecordLength():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
44
45
46
47rc = btrieveClient.FileCreate(btrieveFileAttributes, btrieveFileName, btrievePython.Btrieve.CREATE_MODE_OVERWRITE)
48assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
49
50
51
52btrieveFile = btrievePython.BtrieveFile()
53assert(btrieveFile != None)
54
55
56
57rc = btrieveClient.FileOpen(btrieveFile, btrieveFileName, None, btrievePython.Btrieve.OPEN_MODE_NORMAL)
58assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileOpen():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
59
60
61
62for i in range(min_x, max_x + 1):
63 record = struct.pack(recordFormat, i, i * i, math.sqrt(i))
64
65
66 rc = btrieveFile.RecordCreate(record)
67 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::RecordCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
68
69
70
71btrieveKeySegment = btrievePython.BtrieveKeySegment()
72assert(btrieveKeySegment != None)
73
74
75
76rc = btrieveKeySegment.SetField(0, 1, btrievePython.Btrieve.DATA_TYPE_UNSIGNED_BINARY)
77assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveKeySegment::SetField():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
78
79
80
81btrieveIndexAttributes = btrievePython.BtrieveIndexAttributes()
82assert(btrieveIndexAttributes != None)
83
84
85
86rc = btrieveIndexAttributes.AddKeySegment(btrieveKeySegment)
87assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveIndexAttributes::AddKeySegment():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
88
89
90
91rc = btrieveFile.IndexCreate(btrieveIndexAttributes)
92assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::IndexCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
93
94
95
96record = struct.pack(recordFormat, 0, 0, 0)
97key = struct.pack(keyFormat, integerValue)
98
99
100rc = btrieveFile.RecordRetrieve(btrievePython.Btrieve.COMPARISON_EQUAL, btrievePython.Btrieve.INDEX_1, key, record)
101assert(rc == recordLength), "Error: BtrieveFile::RecordRetrieve():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
102
103
104print ("record:", struct.unpack(recordFormat, record))
105
106
107rc = btrieveClient.FileClose(btrieveFile)
108assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileClose():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
109
110
111
112rc = btrieveClient.FileDelete(btrieveFileName)
113assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileDelete():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
114