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

This is an example of bulk create and retrieve 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"
18recordLength = 11
19keyFormat = "<B"
20
21# If the incorrect number of arguments were given.
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# If integerValue is out of range.
28if (integerValue < min_x) or (integerValue > max_x):
29 sys.exit("Usage: " + os.path.basename(sys.argv[0]) + " uint8_value")
30
31btrieveClient = btrievePython.BtrieveClient()
32assert(btrieveClient != None)
33
34btrieveFileAttributes = btrievePython.BtrieveFileAttributes()
35assert(btrieveFileAttributes != None)
36
37rc = btrieveFileAttributes.SetFixedRecordLength(recordLength)
38assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFileAttributes::SetFixedRecordLength():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
39
40rc = btrieveClient.FileCreate(btrieveFileAttributes, btrieveFileName, btrievePython.Btrieve.CREATE_MODE_OVERWRITE)
41assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
42
43btrieveFile = btrievePython.BtrieveFile()
44assert(btrieveFile != None)
45
46rc = btrieveClient.FileOpen(btrieveFile, btrieveFileName, None, btrievePython.Btrieve.OPEN_MODE_NORMAL)
47assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileOpen():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
48
49
50btrieveBulkCreatePayload = btrievePython.BtrieveBulkCreatePayload()
51assert(btrieveBulkCreatePayload != None)
52
53
54
55btrieveBulkCreateResult = btrievePython.BtrieveBulkCreateResult()
56assert(btrieveFile != None)
57
58
59# For all the values of x.
60for i in range(min_x, max_x + 1):
61 record = struct.pack(recordFormat, i, i * i, math.sqrt(i))
62
63
64 rc = btrieveBulkCreatePayload.AddRecord(record)
65 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveBulkCreatePayload::AddRecord():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
66
67
68
69rc = btrieveFile.BulkCreate(btrieveBulkCreatePayload, btrieveBulkCreateResult)
70assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveBulkCreatePayload::BulkCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
71
72
73
74rc = btrieveBulkCreateResult.GetRecordCount()
75assert(rc == (max_x - min_x + 1)), "Error: BtrieveBulkCreateResult::GetRecordCount():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveBulkCreateResult.GetLastStatusCode()))
76
77
78recordCount = rc
79
80for i in range(1, recordCount + 1):
81
82 rc = btrieveBulkCreateResult.GetRecordCursorPosition(i)
83 assert(rc >= 0), "Error: BtrieveBulkCreateResult::GetRecordCursorPosition():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
84
85
86# print record
87record = struct.pack(recordFormat, 0, 0, 0)
88key = struct.pack(keyFormat, integerValue)
89
90
91btrieveFilter = btrievePython.BtrieveFilter()
92assert(btrieveFilter != None)
93
94
95
96btrieveBulkRetrieveAttributes = btrievePython.BtrieveBulkRetrieveAttributes()
97assert(btrieveBulkRetrieveAttributes != None)
98
99
100
101btrieveBulkRetrieveResult = btrievePython.BtrieveBulkRetrieveResult()
102assert(btrieveBulkRetrieveResult != None)
103
104
105
106rc = btrieveFilter.SetField(0, 1, btrievePython.Btrieve.DATA_TYPE_UNSIGNED_BINARY)
107assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFilter::SetField():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
108
109
110
111rc = btrieveFilter.SetComparison(btrievePython.Btrieve.COMPARISON_EQUAL)
112assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFilter::SetComparison():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
113
114
115
116rc = btrieveFilter.SetComparisonConstant(key)
117assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFilter::SetComparisonConstant():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
118
119
120
121rc = btrieveBulkRetrieveAttributes.AddField(0, recordLength)
122assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: btrieveBulkRetrieveAttributes::AddField():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
123
124
125
126rc = btrieveBulkRetrieveAttributes.AddFilter(btrieveFilter)
127assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveBulkRetrieveAttributes::AddFilter():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
128
129
130
131rc = btrieveBulkRetrieveAttributes.SetMaximumRecordCount(1)
132assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveBulkRetrieveAttributes::SetMaximumRecordCount():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
133
134
135
136rc = btrieveBulkRetrieveAttributes.SetMaximumRejectCount(max_x - min_x + 1)
137assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveBulkRetrieveAttributes::SetMaximumRejectCount():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
138
139
140
141rc = btrieveBulkRetrieveAttributes.SetSkipCurrentRecord(False)
142assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveBulkRetrieveAttributes::SetSkipCurrentRecord():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
143
144
145
146rc = btrieveFile.RecordRetrieveFirst(btrievePython.Btrieve.INDEX_NONE, record)
147assert(rc == recordLength), "Error: BtrieveFile::RecordRetrieveFirst():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
148
149
150
151rc = btrieveFile.BulkRetrieveNext(btrieveBulkRetrieveAttributes, btrieveBulkRetrieveResult)
152assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::BulkRetrieveNext():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
153
154
155
156rc = btrieveBulkRetrieveResult.GetRecord(1, record)
157assert(rc == recordLength), "Error: BtrieveBulkRetrieveResult::GetRecord():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveBulkRetrieveResult.GetLastStatusCode()))
158
159
160
161rc = btrieveBulkRetrieveResult.GetRecordCount()
162assert(rc == 1), "Error: BtrieveBulkRetrieveResult::GetRecordCount():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveBulkRetrieveResult.GetLastStatusCode()))
163
164
165
166rc = btrieveBulkRetrieveResult.GetRecordCursorPosition(1)
167assert(rc >= 0), "Error: BtrieveBulkRetrieveResult::GetRecordCursorPosition():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveBulkRetrieveResult.GetLastStatusCode()))
168
169
170
171rc = btrieveBulkRetrieveResult.GetRecordLength(1)
172assert(rc == recordLength), "Error: BtrieveBulkRetrieveResult::GetRecordLength():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveBulkRetrieveResult.GetLastStatusCode()))
173
174
175print ("record:", struct.unpack(recordFormat, record))
176
177rc = btrieveClient.FileClose(btrieveFile)
178assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileClose():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
179
180rc = btrieveClient.FileDelete(btrieveFileName)
181assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileDelete():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))