3 from collections
import OrderedDict
5 from datetime
import datetime
50 ORDER_DIRS =
Enum([
'ASC',
'DESC'], [
'ASC',
'DESC'])
59 self.
name_lookup = OrderedDict( zip(header_row, range(len(col_datatypes))) )
71 for i
in range(1, len(rows)):
72 db.csv_insert(rows[i])
83 file_in = open(file_list[0],
'rb')
84 reader = csv.reader(file_in)
88 for cur_file
in file_list:
89 file_in = open(cur_file,
'rb')
90 reader = csv.reader(file_in)
91 cur_header = reader.next()
93 if db.header_row == cur_header:
99 raise Exception(
'File %s has different column headers than the other files.' % (cur_file))
106 col_sql =
'id INTEGER PRIMARY KEY AUTOINCREMENT'
108 col_sql +=
', %s%d %s NULL' % (CSVDatabase.COL_PREFIX, i, CSVDatabase.DATA_TYPES[self.
col_datatypes[i]])
110 data_sql =
'CREATE TABLE ' + CSVDatabase.TABLE_NAME +
'(' + col_sql +
')'
112 self.cursor.execute(data_sql)
119 return super(CSVDatabase, self).
insert(
121 map(
lambda num:
'%s%d' % (CSVDatabase.COL_PREFIX, num), range(len(self.
col_datatypes))), [csv_row]
140 order_by_indices=
None,
147 if col_indices ==
None:
151 for index
in col_indices:
152 name =
'%s%d' % (CSVDatabase.COL_PREFIX, index)
156 if fcn_indices
and fcns:
157 for i
in range(len(fcn_indices)):
158 for j
in range(len(col_indices)):
159 if fcn_indices[i] == col_indices[j]:
160 cols[j] =
'%s(%s)' % (fcns[i], cols[j])
163 if group_by_index !=
None:
164 group_by =
'%s%d' % (CSVDatabase.COL_PREFIX, group_by_index)
167 if order_by_indices !=
None:
169 for i
in range(len(order_by_indices)):
170 index = order_by_indices[i]
171 order_by +=
'%s%d %s' % (CSVDatabase.COL_PREFIX, index, order_by_dirs[i])
172 if i < len(order_by_indices) - 1:
175 return super(CSVDatabase, self).
select(
176 CSVDatabase.TABLE_NAME,
178 where_cond=where_cond,
192 if col_names ==
None:
193 col_indices = self.name_lookup.values()
195 col_indices = [self.
name_lookup[name]
for name
in col_names]
206 if where_body
and where_cols:
208 for col
in where_cols:
209 db_col_names.append(
'%s%d' % (CSVDatabase.COL_PREFIX, self.
name_lookup[col]) )
210 where_cond = where_body % tuple(db_col_names)
252 group_by_index =
None
256 order_by_indices =
None
262 col_indices=col_indices,
263 where_cond=where_cond,
265 order_by_indices=order_by_indices,
266 order_by_dirs=order_by_dirs,
268 fcn_indices=fcn_indices,
270 group_by_index=group_by_index
281 cols = map(
lambda num:
'%s%d' % (CSVDatabase.COL_PREFIX, num), col_indices)
283 return super(CSVDatabase, self).
update(
284 CSVDatabase.TABLE_NAME,
286 where_cond=where_cond,
301 return super(CSVDatabase, self).
delete(
302 CSVDatabase.TABLE_NAME,
340 def csv_update_by_name(self, col_names, where_body=None, where_cols=None, params=[], dump_sql=False):
342 if col_names ==
None:
343 col_indices = self.name_lookup.values()
345 col_indices = [self.
name_lookup[name]
for name
in col_names]
348 if where_body
and where_cols:
350 for col
in where_cols:
351 db_col_names.append(
'%s%d' % (CSVDatabase.COL_PREFIX, self.
name_lookup[col]) )
352 where_cond = where_body % tuple(db_col_names)
357 return '%s%d' % (CSVDatabase.COL_PREFIX, self.
name_lookup[name])
367 self.cursor.execute(
'ALTER TABLE %s ADD COLUMN %s %s NULL' % (
368 CSVDatabase.TABLE_NAME,
369 '%s%d' % (CSVDatabase.COL_PREFIX, col_index),
370 CSVDatabase.DATA_TYPES[datatype],
373 self.col_datatypes.append(datatype)
374 self.header_row.append(name)
384 for i
in range(len(row)):
385 if not (i
in col_indices):
386 filtered_row.append(row[i])
396 file_out = open(path,
'wb')
397 writer = csv.writer(file_out)
404 writer.writerow(header)
408 writer.writerow(cur_row)