-
-def inspect(path):
- "Open file and return tables info"
- h5tables = {}
- views = []
- h5file = tables.open_file(path)
-
- for table in filter(lambda node: not(isinstance(node, tables.group.Group)), h5file):
- colnames = ['value']
- if isinstance(table, tables.table.Table):
- colnames = table.colnames
-
- h5tables[table._v_pathname] = {
- 'name': table._v_pathname,
- 'columns': colnames,
- 'primary_keys': [],
- 'count': int(table.nrows),
- 'label_column': None,
- 'hidden': False,
- 'fts_table': None,
- 'foreign_keys': {'incoming': [], 'outgoing': []},
- }
-
- h5file.close()
- return h5tables, views, _connector_type
-
-def _parse_sql(sql, params):
- # Table name
- sql = re.sub(r'(?i)from \[(.*)]', r'from "\g<1>"', sql)
- # Params
- for param in params:
- sql = sql.replace(":" + param, param)
-
- try:
- parsed = parse(sql)
- except:
- # Propably it's a PyTables expression
- for token in ['group by', 'order by', 'limit', '']:
- res = re.search('(?i)where (.*)' + token, sql)
- if res:
- modified_sql = re.sub('(?i)where (.*)(' + token + ')', r'\g<2>', sql)
- parsed = parse(modified_sql)
- parsed['where'] = res.group(1).strip()
- break
-
- # Always a list of fields
- if type(parsed['select']) is not list:
- parsed['select'] = [parsed['select']]
-
- return parsed
-
-_operators = {
- 'eq': '==',
- 'neq': '!=',
- 'gt': '>',
- 'gte': '>=',
- 'lt': '<',
- 'lte': '<=',
- 'and': '&',
- 'or': '|',
-}
-
-class Connection:
- def __init__(self, path):
- self.path = path
- self.h5file = tables.open_file(path)
-
- def execute(self, sql, params=None, truncate=False, page_size=None, max_returned_rows=None):
- if params is None:
- params = {}
- rows = []