3 from collections import OrderedDict
5 _connector_type = 'pytables'
8 "Open file and return tables info"
11 h5file = tables.open_file(path)
13 for table in filter(lambda node: not(isinstance(node, tables.group.Group)), h5file):
15 if isinstance(table, tables.table.Table):
16 colnames = table.colnames
18 h5tables[table._v_pathname] = {
19 'name': table._v_pathname,
26 'foreign_keys': {'incoming': [], 'outgoing': []},
30 return h5tables, views, _connector_type
33 parsed = sqlparse.parse(sql)
37 for token in stmt.tokens:
39 if current_keyword in parsed_sql and parsed_sql[current_keyword] == '':
40 # Check composed keywords like 'order by'
41 del parsed_sql[current_keyword]
42 current_keyword += " " + str(token)
44 current_keyword = str(token)
45 parsed_sql[current_keyword] = ""
47 if not token.is_whitespace:
48 parsed_sql[current_keyword] += str(token)
52 def __init__(self, path):
54 self.h5file = tables.open_file(path)
56 def execute(self, sql, params=None, truncate=False):
61 parsed_sql = _parse_sql(sql)
62 table = self.h5file.get_node(parsed_sql['from'][1:-1])
64 fields = parsed_sql['select'].split(',')
66 # Use 'where' statement or get all the rows
67 if 'where' in parsed_sql:
70 table_rows = table.iterrows()
72 if len(fields) == 1 and fields[0] == 'count(*)':
73 rows.append(Row({'count(*)': table.nrows}))
75 for table_row in table_rows:
79 row[field] = table_row.nrow
81 for col in table.colnames:
82 row[col] = table_row[col]
84 row[field] = table_row[field]
87 description = ((col,) for col in table.colnames)
90 return rows, truncated, description
94 class Row(OrderedDict):
95 def __getitem__(self, label):
96 if type(label) is int:
97 return super(OrderedDict, self).__getitem__(list(self.keys())[label])