<commit_before>from sqlalchemy import create_engine, exc
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.engine.url import URL

import cbpos
logger = cbpos.get_logger(__name__)

from .profile import get_used_profile

# Define default database configuration for different RDBMS's
cbpos.config.set_default('db', 'used', 'default')
cbpos.config.set_default('db', 'echo', '')

_session = None
def session():
    """
    Returns a new session if none is present.
    TODO It is almost useless as it is always the same session that is returned. Check SQLAlchemy documentation in ORM>Session>FAQ for when to create a session.
    """
    global _session
    if _session is None:
        _session = Session()
    return _session

def get_url():
    """
    Return the URL to be used with engine creation based on configuration
    """
    return URL(**dict(get_used_profile()))

def clear():
    """
    Clear the database.
    Note: Drops all tables, does not drop database.
    """
    metadata = cbpos.database.Base.metadata
    metadata.drop_all()

def create():
    """
    Create the database.
    Note: Creates all the tables, does not create the database itself.
    """
    metadata = cbpos.database.Base.metadata
    metadata.create_all()

engine, Base, Session = None, None, None
def init():
    """
    Creates the SQLAlchemy engine, Session class and declarative base using the user's configuration.
    """
    global engine

    if engine is not None:
        logger.warn('Engine is not None')
        return
    
    logger.debug('Starting database...')
    url = get_url()
    try:
        start(url)
    except:
        logger.error('Could not connect to database: %s' % (url,))
        return False
    else:
        return True

def start(url):<commit_msg>Log exception when database connection fails<commit_after>from sqlalchemy import create_engine, exc
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.engine.url import URL

import cbpos
logger = cbpos.get_logger(__name__)

from .profile import get_used_profile

# Define default database configuration for different RDBMS's
cbpos.config.set_default('db', 'used', 'default')
cbpos.config.set_default('db', 'echo', '')

_session = None
def session():
    """
    Returns a new session if none is present.
    TODO It is almost useless as it is always the same session that is returned. Check SQLAlchemy documentation in ORM>Session>FAQ for when to create a session.
    """
    global _session
    if _session is None:
        _session = Session()
    return _session

def get_url():
    """
    Return the URL to be used with engine creation based on configuration
    """
    return URL(**dict(get_used_profile()))

def clear():
    """
    Clear the database.
    Note: Drops all tables, does not drop database.
    """
    metadata = cbpos.database.Base.metadata
    metadata.drop_all()

def create():
    """
    Create the database.
    Note: Creates all the tables, does not create the database itself.
    """
    metadata = cbpos.database.Base.metadata
    metadata.create_all()

engine, Base, Session = None, None, None
def init():
    """
    Creates the SQLAlchemy engine, Session class and declarative base using the user's configuration.
    """
    global engine

    if engine is not None:
        logger.warn('Engine is not None')
        return
    
    logger.debug('Starting database...')
    url = get_url()
    try:
        start(url)
    except Exception as e:
        logger.exception(e)
        logger.error('Could not connect to database: %s' % (url,))
        return False
    else:
        return True

def start(url):
{'fraction_non_alphanumeric': 0.09122525414660246, 'fraction_numerical': 0.0, 'mean_word_length': 3.1133113311331133, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1750054', 'n_tokens_mistral': 1055, 'n_tokens_neox': 1034, 'n_words': 388}