Пример #1
0
 def __init__(self):
     """Construct the backend instance by initializing all the collections."""
     self._authinfos = authinfos.DjangoAuthInfoCollection(self)
     self._comments = comments.DjangoCommentCollection(self)
     self._computers = computers.DjangoComputerCollection(self)
     self._groups = groups.DjangoGroupCollection(self)
     self._logs = logs.DjangoLogCollection(self)
     self._nodes = nodes.DjangoNodeCollection(self)
     self._query_manager = DjangoQueryManager(self)
     self._backend_manager = DjangoBackendManager()
     self._users = users.DjangoUserCollection(self)
Пример #2
0
def get_backend_manager(backend):
    """Get an instance of the `BackendManager` for the current backend.

    :param backend: the type of the database backend
    :return: `BackendManager`
    """
    if backend == BACKEND_DJANGO:
        from aiida.backends.djsite.manager import DjangoBackendManager
        return DjangoBackendManager()

    if backend == BACKEND_SQLA:
        from aiida.backends.sqlalchemy.manager import SqlaBackendManager
        return SqlaBackendManager()

    raise Exception(f'unknown backend type `{backend}`')
Пример #3
0
class DjangoBackend(SqlBackend[models.Model]):
    """Django implementation of `aiida.orm.implementation.backends.Backend`."""

    def __init__(self):
        """Construct the backend instance by initializing all the collections."""
        self._authinfos = authinfos.DjangoAuthInfoCollection(self)
        self._comments = comments.DjangoCommentCollection(self)
        self._computers = computers.DjangoComputerCollection(self)
        self._groups = groups.DjangoGroupCollection(self)
        self._logs = logs.DjangoLogCollection(self)
        self._nodes = nodes.DjangoNodeCollection(self)
        self._query_manager = DjangoQueryManager(self)
        self._backend_manager = DjangoBackendManager()
        self._users = users.DjangoUserCollection(self)

    def migrate(self):
        self._backend_manager.migrate()

    @property
    def authinfos(self):
        return self._authinfos

    @property
    def comments(self):
        return self._comments

    @property
    def computers(self):
        return self._computers

    @property
    def groups(self):
        return self._groups

    @property
    def logs(self):
        return self._logs

    @property
    def nodes(self):
        return self._nodes

    @property
    def query_manager(self):
        return self._query_manager

    def query(self):
        return querybuilder.DjangoQueryBuilder(self)

    @property
    def users(self):
        return self._users

    @staticmethod
    def transaction():
        """Open a transaction to be used as a context manager."""
        return transaction.atomic()

    # Below are abstract methods inherited from `aiida.orm.implementation.sql.backends.SqlBackend`

    def get_backend_entity(self, model):
        """Return a `BackendEntity` instance from a `DbModel` instance."""
        return convert.get_backend_entity(model, self)

    @contextmanager
    def cursor(self):
        """Return a psycopg cursor to be used in a context manager.

        :return: a psycopg cursor
        :rtype: :class:`psycopg2.extensions.cursor`
        """
        try:
            yield self.get_connection().cursor()
        finally:
            pass

    def execute_raw(self, query):
        """Execute a raw SQL statement and return the result.

        :param query: a string containing a raw SQL statement
        :return: the result of the query
        """
        with self.cursor() as cursor:
            cursor.execute(query)
            results = cursor.fetchall()

        return results

    @staticmethod
    def get_connection():
        """
        Get the Django connection

        :return: the django connection
        """
        # pylint: disable=import-error,no-name-in-module
        from django.db import connection
        # For now we just return the global but if we ever support multiple Django backends
        # being loaded this should be specific to this backend
        return connection