<commit_before># -*- coding: utf-8 -*-

"""

SQL Database
~~~~~~~~~~~~
This module provides IO functions to the relational edge store.

"""

import logging

from sqlalchemy.exc import IntegrityError, OperationalError

from .cache import build_manager

__all__ = [
    'to_database',
    'from_database'
]

log = logging.getLogger(__name__)


def to_database(graph, connection=None, store_parts=False):
    """Stores a graph in a database.

    :param BELGraph graph: A BEL graph
    :param connection: An RFC-1738 database connection string, a pre-built :class:`CacheManager`, or `None`` for 
                        default connection
    :type connection: None or str or pybel.manager.cache.CacheManager
    :param bool store_parts: Should the graph be stored in the edge store?
    """
    manager = build_manager(connection=connection)

    try:
        manager.insert_graph(graph, store_parts=store_parts)
    except IntegrityError:
        manager.session.rollback()
        log.warning('Error storing graph - other graph with same metadata'
                    ' already present. Consider incrementing the version')
    except OperationalError:
        manager.session.rollback()
        log.exception('Error storing graph - operational exception')
    except Exception as e:
        manager.session.rollback()
        raise e


def from_database(name, version, connection=None):
    """Loads a BEL graph from a database.

    :param str name: The name of the graph
    :param str version: The version string of the graph. If not specified, loads most recent graph added with this name
    :param connection: An RFC-1738 database connection string, a pre-built :class:`CacheManager`, or ``None`` 
                        for default connection
    :type connection: None or str or pybel.manager.cache.CacheManager
    :return: A BEL graph loaded from the database
    :rtype: BELGraph
    """
    manager = build_manager(connection=connection)
    return manager.get_network_by_name_version(name, version)

<commit_msg>Add option to not use version when getting from database<commit_after># -*- coding: utf-8 -*-

"""

SQL Database
~~~~~~~~~~~~
This module provides IO functions to the relational edge store.

"""

import logging

from sqlalchemy.exc import IntegrityError, OperationalError

from .cache import build_manager

__all__ = [
    'to_database',
    'from_database'
]

log = logging.getLogger(__name__)


def to_database(graph, connection=None, store_parts=False):
    """Stores a graph in a database.

    :param BELGraph graph: A BEL graph
    :param connection: An RFC-1738 database connection string, a pre-built :class:`CacheManager`, or `None`` for 
                        default connection
    :type connection: None or str or pybel.manager.cache.CacheManager
    :param bool store_parts: Should the graph be stored in the edge store?
    """
    manager = build_manager(connection=connection)

    try:
        manager.insert_graph(graph, store_parts=store_parts)
    except IntegrityError:
        manager.session.rollback()
        log.warning('Error storing graph - other graph with same metadata'
                    ' already present. Consider incrementing the version')
    except OperationalError:
        manager.session.rollback()
        log.exception('Error storing graph - operational exception')
    except Exception as e:
        manager.session.rollback()
        raise e


def from_database(name, version=None, connection=None):
    """Loads a BEL graph from a database. If name and version are given, finds it exactly with
    :meth:`pybel.manager.cache.CacheManager.get_network_by_name_version`. If just the name is given, finds most recent
    with :meth:`pybel.manager.cache.CacheManager.get_network_by_name_version`

    :param str name: The name of the graph
    :param str version: The version string of the graph. If not specified, loads most recent graph added with this name
    :param connection: An RFC-1738 database connection string, a pre-built :class:`CacheManager`, or ``None`` 
                        for default connection
    :type connection: None or str or pybel.manager.cache.CacheManager
    :return: A BEL graph loaded from the database
    :rtype: BELGraph
    """
    manager = build_manager(connection=connection)

    if version is None:
        return manager.get_most_recent_network_by_name(name)

    return manager.get_network_by_name_version(name, version)
{'fraction_non_alphanumeric': 0.0834272829763247, 'fraction_numerical': 0.004058624577226606, 'mean_word_length': 3.323586744639376, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1189364', 'n_tokens_mistral': 1207, 'n_tokens_neox': 1149, 'n_words': 425}