# Path: regenesis/core.py
# def get_catalog(catalog_name):
# 
# Path: regenesis/database.py
# def get_fact_table(cube_name):
# def load_cube(cube, update=False):
# 
# Path: regenesis/database.py
# def get_fact_table(cube_name):
# def load_cube(cube, update=False):


from sqlalchemy import func, select, and_
from sqlalchemy.sql.expression import bindparam
from regenesis.core import engine, app
from regenesis.database import cube_table, value_table, statistic_table
from regenesis.database import dimension_table, reference_table, get_fact_table
from pprint import pprint

def get_cubes(cube_name=None):
    ct = cube_table.table.alias('cube')
    st = statistic_table.table.alias('statistic')
    q = ct.join(st, st.c.name==ct.c.statistic_name)
    q = q.select(use_labels=True)
    if cube_name is not None:
        q = q.where(ct.c.name==cube_name)
    return list(engine.query(q))


def get_dimensions(cube_name):
    rt = reference_table.table.alias('ref')
    dt = dimension_table.table.alias('dim')
    q = rt.join(dt, rt.c.dimension_name==dt.c.name)
    q = q.select(use_labels=True)
    q = q.where(rt.c.cube_name==cube_name)
    res = engine.query(q)
    return list(res)

def get_all_dimensions():
    return list(dimension_table)

def get_all_statistics():
    return list(statistic_table)

def query_cube(cube_name, readable=True):
    cube = get_cube(cube_name)
    dimensions = get_dimensions(cube_name)


# Path: regenesis/core.py
# def get_catalog(catalog_name):
# 
# Path: regenesis/database.py
# def get_fact_table(cube_name):
# def load_cube(cube, update=False):
# 
# Path: regenesis/database.py
# def get_fact_table(cube_name):
# def load_cube(cube, update=False):


# Path: regenesis/queries.py
from sqlalchemy import func, select, and_
from sqlalchemy.sql.expression import bindparam
from regenesis.core import engine, app
from regenesis.database import cube_table, value_table, statistic_table
from regenesis.database import dimension_table, reference_table, get_fact_table
from pprint import pprint


def get_cubes(cube_name=None):
    ct = cube_table.table.alias('cube')
    st = statistic_table.table.alias('statistic')
    q = ct.join(st, st.c.name==ct.c.statistic_name)
    q = q.select(use_labels=True)
    if cube_name is not None:
        q = q.where(ct.c.name==cube_name)
    return list(engine.query(q))


def get_dimensions(cube_name):
    rt = reference_table.table.alias('ref')
    dt = dimension_table.table.alias('dim')
    q = rt.join(dt, rt.c.dimension_name==dt.c.name)
    q = q.select(use_labels=True)
    q = q.where(rt.c.cube_name==cube_name)
    res = engine.query(q)
    return list(res)

def get_all_dimensions():
    return list(dimension_table)

def get_all_statistics():
    return list(statistic_table)

def query_cube(cube_name, readable=True):
    cube = get_cube(cube_name)
    dimensions = get_dimensions(cube_name)


    fact_table = get_fact_table(cube_name).table.alias('fact')