예제 #1
0
class Orator(object):

    def __init__(self, app=None):
        self.Model = BaseModel
        self.cli = None
        self._db = None

        if app is not None:
            self.init_app(app)

    def init_app(self, app):
        if 'ORATOR_DATABASES' not in app.config:
            raise RuntimeError('Missing "ORATOR_DATABASES" configuration')

        # Register request hooks
        self.register_handlers(app)

        # Getting config databases
        self._config = app.config['ORATOR_DATABASES']

        # Initializing database manager
        self._db = DatabaseManager(self._config)

        self.Model.set_connection_resolver(self._db)

        # Setting current page resolver
        def current_page_resolver():
            return int(request.args.get('page', 1))

        Paginator.current_page_resolver(current_page_resolver)

        # Setting commands
        self.init_commands()

    def init_commands(self):
        self.cli = Application(orator_application.get_name(),
                               orator_application.get_version())
        
        self.cli.add(InstallCommand(self))
        self.cli.add(MigrateCommand(self))
        self.cli.add(MigrateMakeCommand(self))
        self.cli.add(RollbackCommand(self))
        self.cli.add(StatusCommand(self))
        self.cli.add(ResetCommand(self))

    def register_handlers(self, app):
        teardown = app.teardown_appcontext

        @teardown
        def disconnect(_):
            return self._db.disconnect()

    def __getattr__(self, item):
        return getattr(self._db, item)
예제 #2
0
class Orator(object):
    def __init__(self, app=None):
        self.Model = BaseModel
        self.cli = None
        self._db = None

        if app is not None:
            self.init_app(app)

    def init_app(self, app):
        if 'ORATOR_DATABASES' not in app.config:
            raise RuntimeError('Missing "ORATOR_DATABASES" configuration')

        # Register request hooks
        self.register_handlers(app)

        # Getting config databases
        self._config = app.config['ORATOR_DATABASES']

        # Initializing database manager
        self._db = DatabaseManager(self._config)

        self.Model.set_connection_resolver(self._db)

        # Setting current page resolver
        def current_page_resolver():
            return int(request.args.get('page', 1))

        Paginator.current_page_resolver(current_page_resolver)

        # Setting commands
        self.init_commands()

    def init_commands(self):
        self.cli = Application(orator_application.get_name(),
                               orator_application.get_version())

        self.cli.add(InstallCommand(self))
        self.cli.add(MigrateCommand(self))
        self.cli.add(MigrateMakeCommand(self))
        self.cli.add(RollbackCommand(self))
        self.cli.add(StatusCommand(self))
        self.cli.add(ResetCommand(self))

    def register_handlers(self, app):
        teardown = app.teardown_appcontext

        @teardown
        def disconnect(_):
            return self._db.disconnect()

    def __getattr__(self, item):
        return getattr(self._db, item)
예제 #3
0
class DatabaseBaseMixin:
    """
    Generic mixin for all Steps related to Database
    """
    connection_config: t.Dict[str, str]
    __db: t.Optional[DatabaseManager] = None
    query: t.Optional[QueryBuilder] = None
    data_field: t.Optional[str] = None
    table_name: t.Optional[str] = None
    pk_field: t.Optional[str] = None

    def __init__(
        self,
        table_name: t.Optional[str] = None,
        data_field: t.Optional[str] = None,
        pk_field: str = 'id',
        where: t.Optional[tuple] = None,
        join: t.Optional[tuple] = None,
        select: t.Optional[tuple] = None
    ):

        self.data_field = data_field if data_field is not None else self.data_field
        self.table_name = table_name if table_name is not None else self.table_name
        self.pk_field = pk_field if pk_field is not None else self.pk_field

        if self.table_name is None:
            raise StepInitializationException('`table_name` is missing')

        self.where_clause = where
        self.join_clause = join
        self.select_clause = select

    def set_table(self, table_name: str) -> QueryBuilder:
        """
        :param table_name:
        :return: Orator Query builder
        """
        self.query = self.__db.table(table_name)

        return self.query

    def set_select(self, select: t.Optional[tuple] = None) -> QueryBuilder:
        """
        Sets columns for selecting. See Orator docs for detailed info
        :param select:
        :return: Orator Query builder
        """
        if select is not None:
            return self.query.select(*select)

    def set_where(self, where: t.Optional[tuple] = None) -> QueryBuilder:
        """
        Sets where clause. See Orator docs for detailed info

        :param where:
        :return: Orator Query builder
        """
        if where is not None:
            return self.query.where(*where)

    def set_join(self, _join: t.Optional[tuple] = None) -> QueryBuilder:
        """
        Sets join clause. See Orator docs for detailed info.

        :param _join:
        :return: Orator Query builder
        """
        if _join is not None:
            return self.query.join(*_join)

    def create_connection(self) -> None:
        """
        Creates connection to database if it is None
        """
        if self.__db is None:
            self.__db = DatabaseManager(self.connection_config)

    def clear_connection(self):
        """
        Clears connection
        """
        self.__db.disconnect()