- if 'where' in parsed_sql:
- query = []
- start = 0
- end = table.nrows
- for condition in parsed_sql['where'].get_sublists():
- if str(condition) == '"rowid"=:p0':
- start = int(params['p0'])
- end = start + 1
- else:
- translated, params = _translate_condition(table, condition, params)
- query.append(translated)
- if query:
- query = ') & ('.join(query)
- query = '(' + query + ')'
- table_rows = table.where(query, params, start, end)
+ def _cast_param(field, pname):
+ # Cast value to the column type
+ coltype = table.coltypes[field]
+ fcast = None
+ if coltype == 'string':
+ fcast = str
+ elif coltype.startswith('int'):
+ fcast = int
+ elif coltype.startswith('float'):
+ fcast = float
+ if fcast:
+ params[pname] = fcast(params[pname])
+
+ def _translate_where(where):
+ # Translate SQL to PyTables expression
+ expr = ''
+ operator = list(where)[0]
+
+ if operator in ['and', 'or']:
+ subexpr = ["({})".format(_translate_where(q)) for q in where[operator]]
+ expr = " {} ".format(_operators[operator]).join(subexpr)
+ elif where == {'eq': ['rowid', 'p0']}:
+ nonlocal start, end
+ start = int(params['p0'])
+ end = start + 1