Пример #1
0
    async def start(cls, user: TelegramUser) -> 'Conversation':
        conversation_table = Table(cls._meta.db_table)
        c1 = Table(cls._meta.db_table, alias='C1')
        params = [
            cls._meta.db.executor_class.parameter(None, i) for i in range(4)
        ]

        subquery = (
            Query.from_(c1).select(c1.id).where(
                (((c1.opponent_id == conversation_table.initiator_id) &
                  (c1.initiator_id == params[1]))
                 | ((c1.initiator_id == conversation_table.opponent_id) &
                    (c1.opponent_id == params[2])))
                & c1.finished_at > params[3])  # is recent
        )
        query = (
            Query.from_(conversation_table).select(
                conversation_table.id).limit(1).where(
                    conversation_table.opponent_id.isnull()
                    & conversation_table.finished_at.isnull()
                    & (conversation_table.initiator_id != params[0]
                       )  # not own created
                    & ExistsCriterion(subquery).negate()
                )  # should not have the same recent opponent
        )

        time_limit = datetime.now() - timedelta(
            minutes=config.RECENT_OPPONENT_TIMEOUT)
        connection = Tortoise.get_connection('anon_talks')

        __, suitable_conversations = await connection.execute_query(
            query.get_sql(), ([user.id] * 3) + [time_limit])
        if suitable_conversations:
            conversation = await cls.all().select_related('initiator').get(
                id=suitable_conversations[0][0])
            conversation.opponent = user
            conversation.initiator.status = conversation.opponent.status = TelegramUser.Status.IN_CONVERSATION
            await asyncio.gather(
                conversation.save(
                    update_fields=['opponent_id', 'modified_at']),
                conversation.initiator.save(update_fields=['status']),
                conversation.opponent.save(update_fields=['status']),
            )
        else:
            user.status = TelegramUser.Status.WAITING_OPPONENT
            conversation, __ = await asyncio.gather(
                cls.create(initiator=user),
                user.save(update_fields=['status']),
            )

        return conversation
Пример #2
0
    def get_source_table(self, source_id, as_array=False):
        source = Source.objects.get(pk=source_id)

        table_name = source.active_mirror_name
        schema_name = source.schema

        if as_array:
            return [table_name, schema_name]

        return Table(table_name, schema=schema_name)
Пример #3
0
 def get_count_sql(self, config, estimate=True):
     if estimate:
         stats_table = Table("pg_stat_user_tables")
         table_parts = self.get_source_table(config.get('source'), True)
         table_name = table_parts[0]
         schema_name = table_parts[1]
         return Query.from_(stats_table).select(
             stats_table.n_live_tup).where(
                 stats_table.relname == table_name).where(
                     stats_table.schemaname == schema_name).get_sql()
     query = self.process_config(config)
     query = Query.from_(query)
     return query.select(fn.Count('*')).get_sql()
Пример #4
0
 def copy_(self, table):
     self._copy_table = table if isinstance(table, Table) else Table(table)
Пример #5
0
 def into(self, table):
     self._into_table = table if isinstance(table, Table) else Table(table)
Пример #6
0
 def copy_(self, table: Union[str, Table]) -> "VerticaCopyQueryBuilder":
     self._copy_table = table if isinstance(table, Table) else Table(table)
Пример #7
0
 def into(self, table: Union[str, Table]) -> "MySQLQueryBuilder":
     self._into_table = table if isinstance(table, Table) else Table(table)
Пример #8
0
 def copy_(self, table: Union[str, Table]) -> "VerticaCopyQueryBuilder":
     with copy_if_immutable(self) as this:
         this._copy_table = table if isinstance(table,
                                                Table) else Table(table)
         return this
Пример #9
0
 def into(self, table: Union[str, Table]) -> "MySQLQueryBuilder":
     with copy_if_immutable(self) as this:
         this._into_table = table if isinstance(table,
                                                Table) else Table(table)
         return this
Пример #10
0
 def DocType(table_name: str, *args, **kwargs) -> Table:
     table_name = get_table_name(table_name)
     return Table(table_name, *args, **kwargs)