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 = "<BHd128s"
18recordLength = 139
19
20
21if (len(sys.argv) != 2):
22 sys.exit("Usage: " + os.path.basename(sys.argv[0]) + " digit")
23
24integerValue = int(sys.argv[1])
25
26
27if (integerValue < 0) or (integerValue > 9):
28 sys.exit("Usage: " + os.path.basename(sys.argv[0]) + " digit")
29
30key = str(integerValue)
31
32btrieveClient = btrievePython.BtrieveClient()
33assert(btrieveClient != None)
34
35btrieveFileAttributes = btrievePython.BtrieveFileAttributes()
36assert(btrieveFileAttributes != None)
37
38rc = btrieveFileAttributes.SetFixedRecordLength(recordLength)
39assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFileAttributes::SetFixedRecordLength():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
40
41rc = btrieveClient.FileCreate(btrieveFileAttributes, btrieveFileName, btrievePython.Btrieve.CREATE_MODE_OVERWRITE)
42assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
43
44btrieveFile = btrievePython.BtrieveFile()
45assert(btrieveFile != None)
46
47rc = btrieveClient.FileOpen(btrieveFile, btrieveFileName, None, btrievePython.Btrieve.OPEN_MODE_NORMAL)
48assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileOpen():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
49
50
51for i in range(min_x, max_x + 1):
52 iSquared = i * i
53 iSquareRoot = math.sqrt(i)
54
55 if ((i % 2) == 0):
56 text = "The square of %03d is %05d and its square root is %f." % (i, iSquared, iSquareRoot)
57 record = struct.pack(recordFormat, i, iSquared, iSquareRoot, bytes(text, "utf-8"))
58 else:
59 text = "The SQUARE of %03d is %05d and its square root is %f." % (i, iSquared, iSquareRoot)
60 record = struct.pack(recordFormat, 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(11, 128, btrievePython.Btrieve.DATA_TYPE_ZSTRING)
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 = btrieveIndexAttributes.SetACSName("u54-msft_enus_0")
78assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveIndexAttributes::SetACSName():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
79
80rc = btrieveFile.IndexCreate(btrieveIndexAttributes)
81assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::IndexCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
82
83
84record = struct.pack(recordFormat, 0, 0, 0, bytes("", "utf-8"))
85
86btrieveBulkRetrieveAttributes = btrievePython.BtrieveBulkRetrieveAttributes()
87assert(btrieveBulkRetrieveAttributes != None)
88
89btrieveFilter = btrievePython.BtrieveFilter()
90assert(btrieveFilter != None)
91
92
93rc = btrieveFilter.SetComparison(btrievePython.Btrieve.COMPARISON_EQUAL)
94assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFilter::SetComparison():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
95
96rc = btrieveFilter.SetComparisonConstant(bytes(key, "utf-8"))
97assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFilter::SetComparisonConstant():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
98
99rc = btrieveFilter.SetField(25, 1, btrievePython.Btrieve.DATA_TYPE_CHAR)
100assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFilter::SetField():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
101
102
103rc = btrieveFilter.SetConnector(btrievePython.Btrieve.CONNECTOR_AND)
104assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFilter::SetConnector():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
105
106
107rc = btrieveBulkRetrieveAttributes.AddFilter(btrieveFilter)
108assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveBulkRetrieveAttributes::AddFilter():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
109
110
111rc = btrieveFilter.SetField(32, 1, btrievePython.Btrieve.DATA_TYPE_CHAR)
112assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFilter::SetField():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
113
114
115rc = btrieveFilter.SetComparisonField(27)
116assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFilter::SetComparisonField():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
117
118
119rc = btrieveFilter.SetConnector(btrievePython.Btrieve.CONNECTOR_AND)
120assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFilter::SetConnector():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
121
122rc = btrieveBulkRetrieveAttributes.AddFilter(btrieveFilter)
123assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveBulkRetrieveAttributes::AddFilter():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
124
125
126
127rc = btrieveFilter.SetACSMode(btrievePython.Btrieve.ACS_MODE_CASE_INSENSITIVE)
128assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFilter::SetACSMode():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
129
130
131rc = btrieveFilter.SetField(20, 1, btrievePython.Btrieve.DATA_TYPE_CHAR)
132assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFilter::SetField():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
133
134rc = btrieveFilter.SetComparisonConstant(bytes("E", "utf-8"))
135assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFilter::SetComparisonConstant():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
136
137rc = btrieveFilter.SetConnector(btrievePython.Btrieve.CONNECTOR_AND)
138assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFilter::SetConnector():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
139
140rc = btrieveBulkRetrieveAttributes.AddFilter(btrieveFilter)
141assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveBulkRetrieveAttributes::AddFilter():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
142
143
144
145rc = btrieveFilter.SetACSName("u54-msft_enus_0")
146assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFilter::SetACSName():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
147
148
149rc = btrieveFilter.SetComparison(btrievePython.Btrieve.COMPARISON_GREATER_THAN_OR_EQUAL)
150assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFilter::SetComparison():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
151
152c = btrieveFilter.SetField(19, 1, btrievePython.Btrieve.DATA_TYPE_CHAR)
153assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFilter::SetField():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
154
155rc = btrieveFilter.SetComparisonConstant(bytes("R", "utf-8"))
156assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFilter::SetComparisonConstant():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
157
158rc = btrieveFilter.SetConnector(btrievePython.Btrieve.CONNECTOR_LAST)
159assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFilter::SetConnector():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
160
161rc = btrieveBulkRetrieveAttributes.AddFilter(btrieveFilter)
162assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveBulkRetrieveAttributes::AddFilter():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
163
164
165rc = btrieveBulkRetrieveAttributes.AddField(0, recordLength)
166assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveBulkRetrieveAttributes::AddField():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
167
168rc = btrieveBulkRetrieveAttributes.SetMaximumRecordCount(max_x - min_x + 1)
169assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveBulkRetrieveAttributes::SetMaximumRecordCount():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
170
171rc = btrieveBulkRetrieveAttributes.SetMaximumRejectCount(max_x - min_x + 2)
172assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveBulkRetrieveAttributes::SetMaximumRejectCount():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
173
174rc = btrieveBulkRetrieveAttributes.SetSkipCurrentRecord(False)
175assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveBulkRetrieveAttributes::SetSkipCurrentRecord():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
176
177rc = btrieveFile.RecordRetrieveFirst(btrievePython.Btrieve.INDEX_NONE, record)
178assert(rc == recordLength), "Error: BtrieveFile::RecordRetrieveFirst():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
179
180btrieveBulkRetrieveResult = btrievePython.BtrieveBulkRetrieveResult()
181assert(btrieveBulkRetrieveResult != None)
182
183rc = btrieveFile.BulkRetrieveNext(btrieveBulkRetrieveAttributes, btrieveBulkRetrieveResult)
184assert(rc == btrievePython.Btrieve.STATUS_CODE_END_OF_FILE), "Error: BtrieveFile::BulkRetrieveNext():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
185
186recordCount = btrieveBulkRetrieveResult.GetRecordCount()
187assert(rc != -1), "Error: BtrieveBulkRetrieveResult::GetRecordCount():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveBulkRetrieveResult.GetLastStatusCode))
188
189for i in range(1, recordCount + 1):
190 rc = btrieveBulkRetrieveResult.GetRecord(i, record)
191 assert(rc == recordLength), "Error: BtrieveBulkRetrieveResult::GetRecord():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveBulkRetrieveResult.GetLastStatusCode))
192
193 rc = btrieveBulkRetrieveResult.GetRecordCursorPosition(i)
194 assert(rc >= 0), "Error: BtrieveBulkRetrieveResult::GetRecordCursorPosition():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveBulkRetrieveResult.GetLastStatusCode))
195
196 rc = btrieveBulkRetrieveResult.GetRecordLength(i)
197 assert(rc == recordLength), "Error: BtrieveBulkRetrieveResult::GetRecordLength():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveBulkRetrieveResult.GetLastStatusCode))
198
199 i, iSquared, iSquareRoot, text = struct.unpack_from(recordFormat, record)
200 print("record: ({}, {}, {}, {})".format(i, iSquared, iSquareRoot, str(text, "utf-8")))
201
202
203rc = btrieveClient.FileClose(btrieveFile)
204assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileClose():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
205
206rc = btrieveClient.FileDelete(btrieveFileName)
207assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileDelete():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))