'Querying any sqla table from this common interface'

def get_sqla_query(self, groupby, metrics, granularity, from_dttm, to_dttm, filter=None, is_timeseries=True, timeseries_limit=15, timeseries_limit_metric=None, row_limit=None, inner_from_dttm=None, inner_to_dttm=None, orderby=None, extras=None, columns=None, form_data=None):

    template_kwargs = {'from_dttm': from_dttm, 'groupby': groupby, 'metrics': metrics, 'row_limit': row_limit, 'to_dttm': to_dttm, 'form_data': form_data}
    template_processor = self.get_template_processor(**template_kwargs)
    db_engine_spec = self.database.db_engine_spec
    if (granularity not in self.dttm_cols):
        granularity = self.main_dttm_col
    time_groupby_inline = db_engine_spec.time_groupby_inline
    cols = {col.column_name: col for col in self.columns}
    metrics_dict = {m.metric_name: m for m in self.metrics}
    if ((not granularity) and is_timeseries):
        raise Exception(_('Datetime    column    not    provided    as    part    table    configuration    and    is    required    by    this    type    of    chart'))
    for m in metrics:
        if (m not in metrics_dict):
            raise Exception(_("Metric    '{}'    is    not    valid".format(m)))
    metrics_exprs = [metrics_dict.get(m).sqla_col for m in metrics]
    timeseries_limit_metric = metrics_dict.get(timeseries_limit_metric)
    timeseries_limit_metric_expr = None
    if timeseries_limit_metric:
        timeseries_limit_metric_expr = timeseries_limit_metric.sqla_col
    if metrics_exprs:
        main_metric_expr = metrics_exprs[0]
    else:
        main_metric_expr = literal_column('COUNT(*)').label('ccount')
    select_exprs = []
    groupby_exprs = []
    if groupby:
        select_exprs = []
        inner_select_exprs = []
        inner_groupby_exprs = []
        for s in groupby:
            col = cols[s]
            outer = col.sqla_col
            inner = col.sqla_col.label((col.column_name + '__'))
            groupby_exprs.append(outer)
            select_exprs.append(outer)
            inner_groupby_exprs.append(inner)
            inner_select_exprs.append(inner)
    elif columns:
        for s in columns:
            select_exprs.append(cols[s].sqla_col)
        metrics_exprs = []
    if granularity:
        dttm_col = cols[granularity]
        time_grain = extras.get('time_grain_sqla')
        time_filters = []
        if is_timeseries:
            timestamp = dttm_col.get_timestamp_expression(time_grain)
            select_exprs += [timestamp]
            groupby_exprs += [timestamp]
        if (db_engine_spec.time_secondary_columns and (self.main_dttm_col in self.dttm_cols) and (self.main_dttm_col != dttm_col.column_name)):
            time_filters.append(cols[self.main_dttm_col].get_time_filter(from_dttm, to_dttm))
        time_filters.append(dttm_col.get_time_filter(from_dttm, to_dttm))
    select_exprs += metrics_exprs
    qry = sa.select(select_exprs)
    tbl = self.get_from_clause(template_processor, db_engine_spec)
    if (not columns):
        qry = qry.group_by(*groupby_exprs)
    where_clause_and = []
    having_clause_and = []
    for flt in filter:
        if (not all([flt.get(s) for s in ['col', 'op', 'val']])):
            continue
        col = flt['col']
        op = flt['op']
        eq = flt['val']
        col_obj = cols.get(col)
        if col_obj:
            if (op in ('in', 'not    in')):
                values = []
                for v in eq:
                    if isinstance(v, basestring):
                        v = v.strip("'").strip('"')
                        if col_obj.is_num:
                            v = utils.string_to_num(v)
                    if (v is not None):
                        values.append(v)
                cond = col_obj.sqla_col.in_(values)
                if (op == 'not    in'):
                    cond = (~ cond)
                where_clause_and.append(cond)
            else:
                if col_obj.is_num:
                    eq = utils.string_to_num(flt['val'])
                if (op == '=='):
                    where_clause_and.append((col_obj.sqla_col == eq))
                elif (op == '!='):
                    where_clause_and.append((col_obj.sqla_col != eq))
                elif (op == '>'):
                    where_clause_and.append((col_obj.sqla_col > eq))
                elif (op == '<'):
                    where_clause_and.append((col_obj.sqla_col < eq))
                elif (op == '>='):
                    where_clause_and.append((col_obj.sqla_col >= eq))
                elif (op == '<='):
                    where_clause_and.append((col_obj.sqla_col <= eq))
                elif (op == 'LIKE'):
                    where_clause_and.append(col_obj.sqla_col.like(eq))
    if extras:
        where = extras.get('where')
        if where:
            where = template_processor.process_template(where)
            where_clause_and += [sa.text('({})'.format(where))]
        having = extras.get('having')
        if having:
            having = template_processor.process_template(having)
            having_clause_and += [sa.text('({})'.format(having))]
    if granularity:
        qry = qry.where(and_(*(time_filters + where_clause_and)))
    else:
        qry = qry.where(and_(*where_clause_and))
    qry = qry.having(and_(*having_clause_and))
    if groupby:
        qry = qry.order_by(desc(main_metric_expr))
    elif orderby:
        for (col, ascending) in orderby:
            direction = (asc if ascending else desc)
            qry = qry.order_by(direction(col))
    if row_limit:
        qry = qry.limit(row_limit)
    if (is_timeseries and timeseries_limit and groupby and (not time_groupby_inline)):
        inner_main_metric_expr = main_metric_expr.label('mme_inner__')
        inner_select_exprs += [inner_main_metric_expr]
        subq = select(inner_select_exprs)
        subq = subq.select_from(tbl)
        inner_time_filter = dttm_col.get_time_filter((inner_from_dttm or from_dttm), (inner_to_dttm or to_dttm))
        subq = subq.where(and_(*(where_clause_and + [inner_time_filter])))
        subq = subq.group_by(*inner_groupby_exprs)
        ob = inner_main_metric_expr
        if (timeseries_limit_metric_expr is not None):
            ob = timeseries_limit_metric_expr
        subq = subq.order_by(desc(ob))
        subq = subq.limit(timeseries_limit)
        on_clause = []
        for (i, gb) in enumerate(groupby):
            on_clause.append((groupby_exprs[i] == column((gb + '__'))))
        tbl = tbl.join(subq.alias(), and_(*on_clause))
    return qry.select_from(tbl)
