# Path: blazar/db/exceptions.py
# LOG = logging.getLogger(__name__)
# class BlazarDBException(exceptions.BlazarException):
# class BlazarDBDuplicateEntry(BlazarDBException):
# class BlazarDBNotFound(BlazarDBException):
# class BlazarDBInvalidFilter(BlazarDBException):
# class BlazarDBInvalidFilterOperator(BlazarDBException):
# class BlazarDBResourcePropertiesNotEnabled(BlazarDBException):
# class BlazarDBInvalidResourceProperty(BlazarDBException):
# 
# Path: blazar/db/sqlalchemy/models.py
# def _generate_unicode_uuid():
# def MediumText():
# def _id_column():
#     def to_dict(self):
#     def to_dict(self):
#     def to_dict(self):
#     def to_dict(self):
#     def to_dict(self):
#     def to_dict(self):
#     def to_dict(self):
#     def to_dict(self):
#     def to_dict(self, include=None):
# class Lease(mb.BlazarBase):
# class Reservation(mb.BlazarBase):
# class Event(mb.BlazarBase):
# class ResourceProperty(mb.BlazarBase):
# class ComputeHostReservation(mb.BlazarBase):
# class InstanceReservations(mb.BlazarBase):
# class ComputeHostAllocation(mb.BlazarBase):
# class ComputeHost(mb.BlazarBase):
# class ComputeHostExtraCapability(mb.BlazarBase):
# class FloatingIPReservation(mb.BlazarBase):
# class RequiredFloatingIP(mb.BlazarBase):
# class FloatingIPAllocation(mb.BlazarBase):
# class FloatingIP(mb.BlazarBase):


import sys
import sqlalchemy as sa
from oslo_config import cfg
from oslo_db import exception as common_db_exc
from oslo_db.sqlalchemy import session as db_session
from oslo_log import log as logging
from sqlalchemy.sql.expression import asc
from sqlalchemy.sql.expression import desc
from blazar.db import exceptions as db_exc
from blazar.db.sqlalchemy import facade_wrapper
from blazar.db.sqlalchemy import models
def get_backend():
    """The backend is this module itself."""
    return sys.modules[__name__]


def model_query(model, session=None):
    """Query helper.

    :param model: base model to query
    """
    session = session or get_session()

    return session.query(model)


def setup_db():
    try:
        engine = db_session.EngineFacade(cfg.CONF.database.connection,
                                         sqlite_fk=True).get_engine()
        models.Lease.metadata.create_all(engine)
    except sa.exc.OperationalError as e:
        LOG.error("Database registration exception: %s", e)
        return False
    return True


def drop_db():
    try:
        engine = db_session.EngineFacade(cfg.CONF.database.connection,
                                         sqlite_fk=True).get_engine()

# Path: blazar/db/exceptions.py
# LOG = logging.getLogger(__name__)
# class BlazarDBException(exceptions.BlazarException):
# class BlazarDBDuplicateEntry(BlazarDBException):
# class BlazarDBNotFound(BlazarDBException):
# class BlazarDBInvalidFilter(BlazarDBException):
# class BlazarDBInvalidFilterOperator(BlazarDBException):
# class BlazarDBResourcePropertiesNotEnabled(BlazarDBException):
# class BlazarDBInvalidResourceProperty(BlazarDBException):
# 
# Path: blazar/db/sqlalchemy/models.py
# def _generate_unicode_uuid():
# def MediumText():
# def _id_column():
#     def to_dict(self):
#     def to_dict(self):
#     def to_dict(self):
#     def to_dict(self):
#     def to_dict(self):
#     def to_dict(self):
#     def to_dict(self):
#     def to_dict(self):
#     def to_dict(self, include=None):
# class Lease(mb.BlazarBase):
# class Reservation(mb.BlazarBase):
# class Event(mb.BlazarBase):
# class ResourceProperty(mb.BlazarBase):
# class ComputeHostReservation(mb.BlazarBase):
# class InstanceReservations(mb.BlazarBase):
# class ComputeHostAllocation(mb.BlazarBase):
# class ComputeHost(mb.BlazarBase):
# class ComputeHostExtraCapability(mb.BlazarBase):
# class FloatingIPReservation(mb.BlazarBase):
# class RequiredFloatingIP(mb.BlazarBase):
# class FloatingIPAllocation(mb.BlazarBase):
# class FloatingIP(mb.BlazarBase):


# Path: blazar/db/sqlalchemy/api.py
import sys
import sqlalchemy as sa
from oslo_config import cfg
from oslo_db import exception as common_db_exc
from oslo_db.sqlalchemy import session as db_session
from oslo_log import log as logging
from sqlalchemy.sql.expression import asc
from sqlalchemy.sql.expression import desc
from blazar.db import exceptions as db_exc
from blazar.db.sqlalchemy import facade_wrapper
from blazar.db.sqlalchemy import models

def get_backend():
    """The backend is this module itself."""
    return sys.modules[__name__]


def model_query(model, session=None):
    """Query helper.

    :param model: base model to query
    """
    session = session or get_session()

    return session.query(model)


def setup_db():
    try:
        engine = db_session.EngineFacade(cfg.CONF.database.connection,
                                         sqlite_fk=True).get_engine()
        models.Lease.metadata.create_all(engine)
    except sa.exc.OperationalError as e:
        LOG.error("Database registration exception: %s", e)
        return False
    return True


def drop_db():
    try:
        engine = db_session.EngineFacade(cfg.CONF.database.connection,
                                         sqlite_fk=True).get_engine()

        models.Lease.metadata.drop_all(engine)