Ejemplo n.º 1
0
 def find_one(cls, **kwargs):
     query = '''
     SELECT
         *
     FROM
         cards
     WHERE
         [condition];
     '''
     log(f'kwargs: {kwargs}')
     cond, args = cls.format_kwargs(kwargs)
     sql = query.replace('[condition]', cond)
     log(f'args: {args}')
     return query_db(sql, args, one=True)
Ejemplo n.º 2
0
    def find_one(cls, **kwargs):
        log('find one by kwargs.')

        query = '''
        SELECT
            *
        FROM
            cards
        WHERE
            [condition];
        '''
        log(f'kwargs: {kwargs}')
        cond = cls.format_kwargs(kwargs)
        sql = query.replace('[condition]', cond)
        args = cls.filter_form(kwargs)
        log(f'args: {args}')
        return query_db(text(sql), args, one=True)
Ejemplo n.º 3
0
    def find_by(cls, **kwargs):
        query = '''
        SELECT
            *
        FROM
            cards
        WHERE
            [condition]
        ORDER BY id DESC
        '''
        # for k, v in kwargs.items():
        #     if k in cls.__fields__:

        # 'SELECT * FROM cards WHERE [contdition]' ->
        # 'SELECT * FROM cards WHERE type=? and created=?'
        cond, args = cls.format_kwargs(kwargs)
        sql = query.replace('[condition]', cond)
        return query_db(sql, args, one=False)
Ejemplo n.º 4
0
 def card_id_in_plan(self, card_id):
     """从 today_plan 中, 找到 id是 card_id 的 card."""
     # debug(f'self.today_plan: {self.today_plan[0].keys()}')
     select_sql = '''
     SELECT
         card_id
     FROM
         plans
     WHERE
         known=0 and card_id=?
     LIMIT 1
     '''
     r = query_db(select_sql, (card_id, ), one=True)
     log(f'查询结果: {r.keys()}')
     if 'card_id' in r.keys():
         log(f"{r['card_id']}, {card_id}")
         return int(card_id) == r['card_id']
     else:
         return False
Ejemplo n.º 5
0
    def find_all(cls):
        """
        数据查询
        """
        name = cls.__name__
        # TODO 过滤掉被删除的元素
        # kwargs['deleted'] = False

        # 构建 query 语句
        query = '''
        SELECT
            *
        FROM
            cards
        ORDER BY id DESC
        '''
        cs = query_db(query)
        if cs is None:
            cs = []
        l_ = [c for c in cs]
        return l_
Ejemplo n.º 6
0
    def find_id_by(cls, **kwargs):
        query = '''
        SELECT
            id
        FROM
            cards
        WHERE
            [condition]
        '''
        # for k, v in kwargs.items():
        #     if k in cls.__fields__:

        # 'SELECT id FROM cards WHERE [contdition]' ->
        # 'SELECT id FROM cards WHERE type=? and created=?'
        cond, args = cls.format_kwargs(kwargs)
        sql = query.replace('[condition]', cond)
        _r = query_db(sql, args, one=False)
        if _r:
            _r = list(_r)
            _r = list(map(lambda i: i['id'], _r))
        return _r
Ejemplo n.º 7
0
 def get_random_id(self):
     """ 查询数据库语句 - plan 中随机找一个 card_id 返回."""
     query = '''
     SELECT
         card_id
     FROM
         plans
     WHERE
         known = 0
     ORDER BY RANDOM()
     LIMIT 1
     '''
     r = query_db(query, one=True)
     # log(f'r: {r.keys()}')
     # r 可能值, 今天没有单词需要复习: None; 有的话,
     # 能找到, 是 <sqlite3.Row>.
     if r:
         if 'card_id' in r.keys():
             return r['card_id']
         else:
             return None
     else:
         return None