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

This is an example of index attributes use written in 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
14min_x = 0
15max_x = 255
16btrieveFileName = "squaresAndSquareRoots.btr"
17numberOfRecords = max_x - min_x + 1
18recordFormat = "<BHd1024s"
19recordLength = 11 + 1024
20
21# If the incorrect number of arguments were given.
22if (len(sys.argv) != 1):
23 sys.exit("Usage: " + os.path.basename(sys.argv[0]))
24
25btrieveClient = btrievePython.BtrieveClient()
26assert(btrieveClient != None)
27
28# Create file.
29btrieveFileAttributes = btrievePython.BtrieveFileAttributes()
30assert(btrieveFileAttributes != None)
31
32rc = btrieveFileAttributes.SetFixedRecordLength(recordLength)
33assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFileAttributes::SetFixedRecordLength():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
34
35rc = btrieveClient.FileCreate(btrieveFileAttributes, btrieveFileName, btrievePython.Btrieve.CREATE_MODE_OVERWRITE)
36assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
37
38# Open file.
39btrieveFile = btrievePython.BtrieveFile()
40assert(btrieveFile != None)
41
42rc = btrieveClient.FileOpen(btrieveFile, btrieveFileName, None, btrievePython.Btrieve.OPEN_MODE_NORMAL)
43assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileOpen():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
44
45# Load file.
46# For all the values of x.
47for i in range(min_x, max_x + 1):
48 iSquared = i * i
49 iSquareRoot = math.sqrt(i)
50 record = struct.pack(recordFormat, i, iSquared, iSquareRoot, bytes("The square of %d is %d and its square root is %f." % (i, iSquared, iSquareRoot), "utf-8"))
51
52 rc = btrieveFile.RecordCreate(record)
53 assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::RecordCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
54
55# Create index.
56btrieveKeySegment = btrievePython.BtrieveKeySegment()
57assert(btrieveKeySegment != None)
58
59
60rc = btrieveKeySegment.SetDescendingSortOrder(True)
61assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveKeySegment::SetDescendingSortOrder():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
62
63
64rc = btrieveKeySegment.SetField(11, btrievePython.Btrieve.MAXIMUM_KEY_LENGTH, btrievePython.Btrieve.DATA_TYPE_ZSTRING)
65assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveKeySegment::SetField():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
66
67
68rc = btrieveKeySegment.SetNullKeyMode(btrievePython.Btrieve.NULL_KEY_MODE_ANY_SEGMENTS)
69assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveKeySegment::SetNullKeyMode():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
70
71
72
73rc = btrieveKeySegment.SetNullValue(255)
74assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveKeySegment::SetNullValue():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
75
76
77btrieveIndexAttributes = btrievePython.BtrieveIndexAttributes()
78assert(btrieveIndexAttributes != None)
79
80rc = btrieveIndexAttributes.AddKeySegment(btrieveKeySegment)
81assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveIndexAttributes::AddKeySegment():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
82
83
84rc = btrieveIndexAttributes.SetACSName("u54-msft_enus_0")
85assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveIndexAttributes::SetACSName():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
86
87
88
89rc = btrieveIndexAttributes.SetDuplicateMode(btrievePython.Btrieve.DUPLICATE_MODE_ALLOWED_NONREPEATING)
90assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveIndexAttributes::SetDuplicateMode():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
91
92
93
94rc = btrieveIndexAttributes.SetIndex(btrievePython.Btrieve.INDEX_119)
95assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveIndexAttributes::SetIndex():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
96
97
98
99rc = btrieveIndexAttributes.SetModifiable(False)
100assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveIndexAttributes::SetModifiable():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
101
102
103rc = btrieveFile.IndexCreate(btrieveIndexAttributes)
104assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveFile::IndexCreate():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
105
106# Retrieve records.
107record = bytes(recordLength)
108
109# For all the values of x.
110for i in range(min_x, max_x + 1):
111 if (i == min_x):
112 rc = btrieveFile.RecordRetrieveFirst(btrievePython.Btrieve.INDEX_119, record)
113 assert(rc == recordLength), "Error: BtrieveFile::RecordRetrieveFirst():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
114 else:
115
116 rc = btrieveFile.RecordRetrieveNext(record)
117 assert(rc == recordLength), "Error: BtrieveFile::RecordRetrieveNext():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(btrieveFile.GetLastStatusCode()))
118
119
120 i, iSquared, iSquareRoot, text = struct.unpack_from(recordFormat, record)
121 print("record: ({}, {}, {}, {})".format(i, iSquared, iSquareRoot, str(text, "utf-8")))
122
123
124# Close file.
125rc = btrieveClient.FileClose(btrieveFile)
126assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileClose():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))
127
128# Delete file.
129rc = btrieveClient.FileDelete(btrieveFileName)
130assert(rc == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), "Error: BtrieveClient::FileDelete():%d:%s.\n" % (rc, btrievePython.Btrieve_StatusCodeToString(rc))