12 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
13 |
|
|
14 |
import dbflib |
import dbflib |
15 |
|
from Thuban.common import * |
16 |
|
|
17 |
# the field types supported by a Table instance. |
# the field types supported by a Table instance. |
18 |
FIELDTYPE_INT = "int" |
#FIELDTYPE_INT = "int" |
19 |
FIELDTYPE_STRING = "string" |
#FIELDTYPE_STRING = "string" |
20 |
FIELDTYPE_DOUBLE = "double" |
#FIELDTYPE_DOUBLE = "double" |
21 |
|
|
22 |
|
|
23 |
# map the dbflib constants for the field types to our constants |
# map the dbflib constants for the field types to our constants |
33 |
Currently this is basically just a wrapper around dbflib. |
Currently this is basically just a wrapper around dbflib. |
34 |
""" |
""" |
35 |
|
|
36 |
|
# Implementation strategy regarding writing to a DBF file: |
37 |
|
# |
38 |
|
# Most of the time Thuban only needs to read from a table and it is |
39 |
|
# important that Thuban can work with read-only files. Therefore the |
40 |
|
# DBF file is opened only for reading initially. Only when |
41 |
|
# write_record is called we try to open the DBF file for writing as |
42 |
|
# well. If that succeeds the dbf read/write DBF file will be used |
43 |
|
# for all IO afterwards. |
44 |
|
# |
45 |
|
# It's important to use the same DBF file object for both reading |
46 |
|
# and writing to make sure that reading a records after writing |
47 |
|
# returns the new values. With two separate objects this wouldn't |
48 |
|
# work because a DBF file object buffers some data |
49 |
|
|
50 |
def __init__(self, filename): |
def __init__(self, filename): |
51 |
self.filename = filename |
self.filename = filename |
52 |
self.dbf = dbflib.DBFFile(filename, "r+b") |
self.dbf = dbflib.DBFFile(filename) |
53 |
|
|
54 |
|
# If true, self.dbf is open for writing. |
55 |
|
self._writable = 0 |
56 |
|
|
57 |
def Destroy(self): |
def Destroy(self): |
58 |
self.dbf.close() |
self.dbf.close() |
76 |
type = dbflib_fieldtypes[type] |
type = dbflib_fieldtypes[type] |
77 |
return type, name, width, prec |
return type, name, width, prec |
78 |
|
|
79 |
|
def field_info_by_name(self, fieldName): |
80 |
|
count = self.field_count() |
81 |
|
|
82 |
|
for i in range(count): |
83 |
|
info = self.field_info(i) |
84 |
|
if info[1] == fieldName: |
85 |
|
return info |
86 |
|
|
87 |
|
return None |
88 |
|
|
89 |
def read_record(self, record): |
def read_record(self, record): |
90 |
"""Return the record no. record as a dict mapping field names to values |
"""Return the record no. record as a dict mapping field names to values |
91 |
""" |
""" |
104 |
If it's a sequence, all fields must be present in the right |
If it's a sequence, all fields must be present in the right |
105 |
order. |
order. |
106 |
""" |
""" |
107 |
|
if not self._writable: |
108 |
|
new_dbf = dbflib.DBFFile(self.filename, "r+b") |
109 |
|
self.dbf.close() |
110 |
|
self.dbf = new_dbf |
111 |
|
self._writable = 1 |
112 |
self.dbf.write_record(record, values) |
self.dbf.write_record(record, values) |
113 |
self.dbf.commit() |
self.dbf.commit() |
114 |
|
|