1 |
# Copyright (c) 2001, 2002 by Intevation GmbH |
# Copyright (c) 2001, 2002, 2003 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
# |
# |
32 |
Currently this is basically just a wrapper around dbflib. |
Currently this is basically just a wrapper around dbflib. |
33 |
""" |
""" |
34 |
|
|
35 |
|
# Implementation strategy regarding writing to a DBF file: |
36 |
|
# |
37 |
|
# Most of the time Thuban only needs to read from a table and it is |
38 |
|
# important that Thuban can work with read-only files. Therefore the |
39 |
|
# DBF file is opened only for reading initially. Only when |
40 |
|
# write_record is called we try to open the DBF file for writing as |
41 |
|
# well. If that succeeds the read/write DBF file will be used for |
42 |
|
# all IO afterwards. |
43 |
|
# |
44 |
|
# It's important to use the same DBF file object for both reading |
45 |
|
# and writing to make sure that reading a records after writing |
46 |
|
# returns the new values. With two separate objects this wouldn't |
47 |
|
# work because a DBF file object buffers some data |
48 |
|
|
49 |
def __init__(self, filename): |
def __init__(self, filename): |
50 |
self.filename = filename |
self.filename = filename |
51 |
self.dbf = dbflib.DBFFile(filename) |
self.dbf = dbflib.DBFFile(filename) |
52 |
|
|
53 |
|
# If true, self.dbf is open for writing. |
54 |
|
self._writable = 0 |
55 |
|
|
56 |
def Destroy(self): |
def Destroy(self): |
57 |
self.dbf.close() |
self.dbf.close() |
58 |
self.dbf = None |
self.dbf = None |
75 |
type = dbflib_fieldtypes[type] |
type = dbflib_fieldtypes[type] |
76 |
return type, name, width, prec |
return type, name, width, prec |
77 |
|
|
78 |
|
def field_info_by_name(self, fieldName): |
79 |
|
count = self.field_count() |
80 |
|
|
81 |
|
for i in range(count): |
82 |
|
info = self.field_info(i) |
83 |
|
if info[1] == fieldName: |
84 |
|
return info |
85 |
|
|
86 |
|
return None |
87 |
|
|
88 |
def read_record(self, record): |
def read_record(self, record): |
89 |
"""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 |
90 |
""" |
""" |
91 |
return self.dbf.read_record(record) |
return self.dbf.read_record(record) |
92 |
|
|
93 |
|
def write_record(self, record, values): |
94 |
|
"""Write the values into the record |
95 |
|
|
96 |
|
The values parameter may either be a dictionary or a sequence. |
97 |
|
|
98 |
|
If it's a dictionary the keys must be the names of the fields |
99 |
|
and their value must have a suitable type. Only the fields |
100 |
|
actually contained in the dictionary are written. Fields for |
101 |
|
which there's no item in the dict are not modified. |
102 |
|
|
103 |
|
If it's a sequence, all fields must be present in the right |
104 |
|
order. |
105 |
|
""" |
106 |
|
if not self._writable: |
107 |
|
new_dbf = dbflib.DBFFile(self.filename, "r+b") |
108 |
|
self.dbf.close() |
109 |
|
self.dbf = new_dbf |
110 |
|
self._writable = 1 |
111 |
|
self.dbf.write_record(record, values) |
112 |
|
self.dbf.commit() |
113 |
|
|