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 |
|
|
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 ReadRowAsDict(self, index): |
159 |
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: |
160 |
stmt = ("SELECT %s FROM %s;" |
stmt = ("SELECT %s FROM %s;" |
161 |
% (", ".join([c.internal_name for c in self.columns]), |
% (", ".join([c.internal_name for c in self.columns]), |
171 |
assert index >= self.read_record_last_row |
assert index >= self.read_record_last_row |
172 |
|
|
173 |
if index == self.read_record_last_row: |
if index == self.read_record_last_row: |
174 |
result = self.read_record_last_result |
result = self.read_record_last_result |
175 |
else: |
else: |
176 |
for i in range(index - self.read_record_last_row): |
for i in range(index - self.read_record_last_row): |
177 |
result = self.read_record_cursor.fetchone() |
result = self.read_record_cursor.fetchone() |
179 |
self.read_record_last_row = index |
self.read_record_last_row = index |
180 |
return dict(zip(self.orig_names, result)) |
return dict(zip(self.orig_names, result)) |
181 |
|
|
182 |
def field_range(self, colname): |
def ValueRange(self, col): |
183 |
col = self.name_to_column[colname] |
col = self.column_map[col] |
184 |
iname = col.internal_name |
iname = col.internal_name |
185 |
min, max = self.db.execute("SELECT min(%s), max(%s) FROM %s;" |
min, max = self.db.execute("SELECT min(%s), max(%s) FROM %s;" |
186 |
% (iname, iname, self.tablename)) |
% (iname, iname, self.tablename)) |
187 |
converter = type_converter_map[col.type] |
converter = type_converter_map[col.type] |
188 |
return ((converter(min), None), (converter(max), None)) |
return (converter(min), converter(max)) |
189 |
|
|
190 |
def GetUniqueValues(self, colname): |
def UniqueValues(self, col): |
191 |
iname = self.orig_to_internal[colname] |
iname = self.column_map[col].internal_name |
192 |
cursor = self.db.cursor() |
cursor = self.db.cursor() |
193 |
cursor.execute("SELECT %s FROM %s GROUP BY %s;" |
cursor.execute("SELECT %s FROM %s GROUP BY %s;" |
194 |
% (iname, self.tablename, iname)) |
% (iname, self.tablename, iname)) |
216 |
|
|
217 |
def create(self, table): |
def create(self, table): |
218 |
columns = [] |
columns = [] |
219 |
for i in range(table.field_count()): |
for col in table.Columns(): |
220 |
type, name = table.field_info(i)[:2] |
columns.append(ColumnReference(col.name, col.type, |
|
columns.append(ColumnReference(name, type, |
|
221 |
self.db.new_column_name())) |
self.db.new_column_name())) |
222 |
TransientTableBase.create(self, columns) |
TransientTableBase.create(self, columns) |
223 |
|
|
229 |
", ".join(["%%(%s)s" % col.name |
", ".join(["%%(%s)s" % col.name |
230 |
for col in self.columns])) |
for col in self.columns])) |
231 |
cursor = self.db.cursor() |
cursor = self.db.cursor() |
232 |
for i in range(table.record_count()): |
for i in range(table.NumRows()): |
233 |
cursor.execute(insert_template, table.read_record(i)) |
cursor.execute(insert_template, table.ReadRowAsDict(i)) |
234 |
self.db.conn.commit() |
self.db.conn.commit() |
235 |
|
|
236 |
|
|
294 |
self.db.execute(stmt) |
self.db.execute(stmt) |
295 |
|
|
296 |
|
|
297 |
class AutoTransientTable: |
class AutoTransientTable(table.OldTableInterfaceMixin): |
298 |
|
|
299 |
"""Table that copies data to a transient table on demand. |
"""Table that copies data to a transient table on demand. |
300 |
|
|
307 |
self.table = table |
self.table = table |
308 |
self.t_table = None |
self.t_table = None |
309 |
|
|
310 |
def record_count(self): |
def Columns(self): |
311 |
"""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() |
|
312 |
|
|
313 |
def field_info(self, field): |
def Column(self, col): |
314 |
"""Return a tuple (type, name, width, prec) for the field no. field |
return self.table.Column(col) |
315 |
|
|
316 |
type is the data type of the field, name the name, width the |
def NumRows(self): |
317 |
field width in characters and prec the decimal precision. |
return self.table.NumRows() |
|
""" |
|
|
info = self.table.field_info(field) |
|
|
if info: |
|
|
info = info[:2] + (0, 0) |
|
|
return info |
|
|
|
|
|
def field_info_by_name(self, fieldName): |
|
|
info = self.table.field_info_by_name(fieldName) |
|
|
if info: |
|
|
info = info[:2] + (0, 0) |
|
|
return info |
|
318 |
|
|
319 |
def read_record(self, record): |
def NumColumns(self): |
320 |
|
return self.table.NumColumns() |
321 |
|
|
322 |
|
def ReadRowAsDict(self, record): |
323 |
"""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 |
324 |
""" |
""" |
325 |
if self.t_table is not None: |
if self.t_table is not None: |
326 |
return self.t_table.read_record(record) |
return self.t_table.read_record(record) |
327 |
else: |
else: |
328 |
return self.table.read_record(record) |
return self.table.ReadRowAsDict(record) |
|
|
|
|
def write_record(self, record, values): |
|
|
raise NotImplementedError |
|
329 |
|
|
330 |
def copy_to_transient(self): |
def copy_to_transient(self): |
331 |
"""Internal: Create a transient table and copy the data into it""" |
"""Internal: Create a transient table and copy the data into it""" |
339 |
self.copy_to_transient() |
self.copy_to_transient() |
340 |
return self.t_table |
return self.t_table |
341 |
|
|
342 |
def field_range(self, colname): |
def ValueRange(self, col): |
343 |
if self.t_table is None: |
if self.t_table is None: |
344 |
self.copy_to_transient() |
self.copy_to_transient() |
345 |
return self.t_table.field_range(colname) |
(min, row), (max, row) = self.t_table.field_range(col) |
346 |
|
return min, max |
347 |
|
|
348 |
def GetUniqueValues(self, colname): |
def UniqueValues(self, colname): |
349 |
if self.t_table is None: |
if self.t_table is None: |
350 |
self.copy_to_transient() |
self.copy_to_transient() |
351 |
return self.t_table.GetUniqueValues(colname) |
return self.t_table.GetUniqueValues(colname) |