53 |
self.close() |
self.close() |
54 |
|
|
55 |
def close(self): |
def close(self): |
56 |
if self.conn: |
if self.conn is not None: |
57 |
self.conn.close() |
self.conn.close() |
58 |
|
self.conn = None |
59 |
|
|
60 |
def new_table_name(self): |
def new_table_name(self): |
61 |
self.num_tables += 1 |
self.num_tables += 1 |
85 |
self.internal_name = internal_name |
self.internal_name = internal_name |
86 |
|
|
87 |
|
|
88 |
class TransientTableBase: |
class TransientTableBase(table.OldTableInterfaceMixin): |
89 |
|
|
90 |
"""Base class for tables in the transient database""" |
"""Base class for tables in the transient database""" |
91 |
|
|
96 |
self.indexed_columns = {} |
self.indexed_columns = {} |
97 |
self.read_record_cursor = None |
self.read_record_cursor = None |
98 |
self.read_record_last_row = None |
self.read_record_last_row = None |
99 |
|
self.read_record_last_result = None |
100 |
|
|
101 |
def create(self, columns): |
def create(self, columns): |
102 |
self.columns = columns |
self.columns = columns |
104 |
self.orig_names = [] |
self.orig_names = [] |
105 |
self.internal_to_orig = {} |
self.internal_to_orig = {} |
106 |
self.orig_to_internal = {} |
self.orig_to_internal = {} |
107 |
|
self.column_map = {} |
108 |
|
|
109 |
# Create the column objects and fill various maps and lists |
# Create the column objects and fill various maps and lists |
110 |
for col in self.columns: |
for index in range(len(self.columns)): |
111 |
|
col = self.columns[index] |
112 |
self.name_to_column[col.name] = col |
self.name_to_column[col.name] = col |
113 |
self.orig_names.append(col.name) |
self.orig_names.append(col.name) |
114 |
self.internal_to_orig[col.internal_name] = col.name |
self.internal_to_orig[col.internal_name] = col.name |
115 |
self.orig_to_internal[col.name] = col.internal_name |
self.orig_to_internal[col.name] = col.internal_name |
116 |
|
self.column_map[col.name] = col |
117 |
|
self.column_map[index] = col |
118 |
|
|
119 |
# Build the CREATE TABLE statement and create the table in the |
# Build the CREATE TABLE statement and create the table in the |
120 |
# database |
# database |
142 |
self.db.execute(stmt) |
self.db.execute(stmt) |
143 |
self.indexed_columns[column] = 1 |
self.indexed_columns[column] = 1 |
144 |
|
|
145 |
def field_count(self): |
def NumColumns(self): |
146 |
return len(self.columns) |
return len(self.columns) |
147 |
|
|
148 |
def field_info(self, i): |
def NumRows(self): |
|
col = self.columns[i] |
|
|
return col.type, col.name, 0, 0 |
|
|
|
|
|
def field_info_by_name(self, name): |
|
|
for col in self.columns: |
|
|
if col.name == name: |
|
|
return col.type, col.name, 0, 0 |
|
|
else: |
|
|
return None |
|
|
|
|
|
def record_count(self): |
|
149 |
result = self.db.execute("SELECT count(*) FROM %s;" % self.tablename) |
result = self.db.execute("SELECT count(*) FROM %s;" % self.tablename) |
150 |
return int(result[0]) |
return int(result[0]) |
151 |
|
|
152 |
def read_record(self, index): |
def Columns(self): |
153 |
|
return self.columns |
154 |
|
|
155 |
|
def Column(self, col): |
156 |
|
return self.column_map[col] |
157 |
|
|
158 |
|
def HasColumn(self, col): |
159 |
|
"""Return whether the table has a column with the given name or index |
160 |
|
""" |
161 |
|
return self.column_map.has_key(col) |
162 |
|
|
163 |
|
def ReadRowAsDict(self, index): |
164 |
if self.read_record_cursor is None or index <self.read_record_last_row: |
if self.read_record_cursor is None or index <self.read_record_last_row: |
165 |
stmt = ("SELECT %s FROM %s;" |
stmt = ("SELECT %s FROM %s;" |
166 |
% (", ".join([c.internal_name for c in self.columns]), |
% (", ".join([c.internal_name for c in self.columns]), |
168 |
self.read_record_cursor = self.db.cursor() |
self.read_record_cursor = self.db.cursor() |
169 |
self.read_record_cursor.execute(stmt) |
self.read_record_cursor.execute(stmt) |
170 |
self.read_record_last_row = -1 |
self.read_record_last_row = -1 |
171 |
for i in range(index - self.read_record_last_row): |
self.read_record_last_result = None |
172 |
result = self.read_record_cursor.fetchone() |
|
173 |
|
# Now we should have a cursor at a position less than or equal |
174 |
|
# to the index so the following if statement will always set |
175 |
|
# result to a suitable value |
176 |
|
assert index >= self.read_record_last_row |
177 |
|
|
178 |
|
if index == self.read_record_last_row: |
179 |
|
result = self.read_record_last_result |
180 |
|
else: |
181 |
|
for i in range(index - self.read_record_last_row): |
182 |
|
result = self.read_record_cursor.fetchone() |
183 |
|
self.read_record_last_result = result |
184 |
self.read_record_last_row = index |
self.read_record_last_row = index |
185 |
result = dict(zip(self.orig_names, result)) |
return dict(zip(self.orig_names, result)) |
|
return result |
|
186 |
|
|
187 |
def field_range(self, colname): |
def ValueRange(self, col): |
188 |
col = self.name_to_column[colname] |
col = self.column_map[col] |
189 |
iname = col.internal_name |
iname = col.internal_name |
190 |
min, max = self.db.execute("SELECT min(%s), max(%s) FROM %s;" |
min, max = self.db.execute("SELECT min(%s), max(%s) FROM %s;" |
191 |
% (iname, iname, self.tablename)) |
% (iname, iname, self.tablename)) |
192 |
converter = type_converter_map[col.type] |
converter = type_converter_map[col.type] |
193 |
return ((converter(min), None), (converter(max), None)) |
return (converter(min), converter(max)) |
194 |
|
|
195 |
def GetUniqueValues(self, colname): |
def UniqueValues(self, col): |
196 |
iname = self.orig_to_internal[colname] |
iname = self.column_map[col].internal_name |
197 |
cursor = self.db.cursor() |
cursor = self.db.cursor() |
198 |
cursor.execute("SELECT %s FROM %s GROUP BY %s;" |
cursor.execute("SELECT %s FROM %s GROUP BY %s;" |
199 |
% (iname, self.tablename, iname)) |
% (iname, self.tablename, iname)) |
221 |
|
|
222 |
def create(self, table): |
def create(self, table): |
223 |
columns = [] |
columns = [] |
224 |
for i in range(table.field_count()): |
for col in table.Columns(): |
225 |
type, name = table.field_info(i)[:2] |
columns.append(ColumnReference(col.name, col.type, |
|
columns.append(ColumnReference(name, type, |
|
226 |
self.db.new_column_name())) |
self.db.new_column_name())) |
227 |
TransientTableBase.create(self, columns) |
TransientTableBase.create(self, columns) |
228 |
|
|
234 |
", ".join(["%%(%s)s" % col.name |
", ".join(["%%(%s)s" % col.name |
235 |
for col in self.columns])) |
for col in self.columns])) |
236 |
cursor = self.db.cursor() |
cursor = self.db.cursor() |
237 |
for i in range(table.record_count()): |
for i in range(table.NumRows()): |
238 |
cursor.execute(insert_template, table.read_record(i)) |
cursor.execute(insert_template, table.ReadRowAsDict(i)) |
239 |
self.db.conn.commit() |
self.db.conn.commit() |
240 |
|
|
241 |
|
|
299 |
self.db.execute(stmt) |
self.db.execute(stmt) |
300 |
|
|
301 |
|
|
302 |
class AutoTransientTable: |
class AutoTransientTable(table.OldTableInterfaceMixin): |
303 |
|
|
304 |
"""Table that copies data to a transient table on demand. |
"""Table that copies data to a transient table on demand. |
305 |
|
|
312 |
self.table = table |
self.table = table |
313 |
self.t_table = None |
self.t_table = None |
314 |
|
|
315 |
def record_count(self): |
def Columns(self): |
316 |
"""Return the number of records""" |
return self.table.Columns() |
|
return self.table.record_count() |
|
|
|
|
|
def field_count(self): |
|
|
"""Return the number of fields in a record""" |
|
|
return self.table.field_count() |
|
317 |
|
|
318 |
def field_info(self, field): |
def Column(self, col): |
319 |
"""Return a tuple (type, name, width, prec) for the field no. field |
return self.table.Column(col) |
320 |
|
|
321 |
type is the data type of the field, name the name, width the |
def HasColumn(self, col): |
322 |
field width in characters and prec the decimal precision. |
"""Return whether the table has a column with the given name or index |
323 |
""" |
""" |
324 |
info = self.table.field_info(field) |
return self.table.HasColumn(col) |
325 |
if info: |
|
326 |
info = info[:2] + (0, 0) |
def NumRows(self): |
327 |
return info |
return self.table.NumRows() |
|
|
|
|
def field_info_by_name(self, fieldName): |
|
|
info = self.table.field_info_by_name(fieldName) |
|
|
if info: |
|
|
info = info[:2] + (0, 0) |
|
|
return info |
|
328 |
|
|
329 |
def read_record(self, record): |
def NumColumns(self): |
330 |
|
return self.table.NumColumns() |
331 |
|
|
332 |
|
def ReadRowAsDict(self, record): |
333 |
"""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 |
334 |
""" |
""" |
335 |
if self.t_table is not None: |
if self.t_table is not None: |
336 |
return self.t_table.read_record(record) |
return self.t_table.ReadRowAsDict(record) |
337 |
else: |
else: |
338 |
return self.table.read_record(record) |
return self.table.ReadRowAsDict(record) |
|
|
|
|
def write_record(self, record, values): |
|
|
raise NotImplementedError |
|
339 |
|
|
340 |
def copy_to_transient(self): |
def copy_to_transient(self): |
341 |
"""Internal: Create a transient table and copy the data into it""" |
"""Internal: Create a transient table and copy the data into it""" |
349 |
self.copy_to_transient() |
self.copy_to_transient() |
350 |
return self.t_table |
return self.t_table |
351 |
|
|
352 |
def field_range(self, colname): |
def ValueRange(self, col): |
353 |
if self.t_table is None: |
if self.t_table is None: |
354 |
self.copy_to_transient() |
self.copy_to_transient() |
355 |
return self.t_table.field_range(colname) |
return self.t_table.ValueRange(col) |
356 |
|
|
357 |
def GetUniqueValues(self, colname): |
def UniqueValues(self, col): |
358 |
if self.t_table is None: |
if self.t_table is None: |
359 |
self.copy_to_transient() |
self.copy_to_transient() |
360 |
return self.t_table.GetUniqueValues(colname) |
return self.t_table.UniqueValues(col) |