Пример #1
0
 def update_contest_statistics(result):
     """
     更新比赛统计数据
     :param result:
     :return:
     """
     count = db.select_int('select count(*) from t_ranking where contest_id=? and problem_id=? and username=?',
                           result['contest_id'], result['problem_id'], result['user_id'])
     if count == 0:
         record = dict(contest_id=result['contest_id'], problem_id=result['problem_id'], username=result['user_id'])
         db.insert('t_ranking', **record)
     p_time = db.select_one('select AC_time from t_ranking where contest_id=? and problem_id=? and username=?',
                            result['contest_id'], result['problem_id'], result['user_id']).get('AC_time')
     if p_time == 0:  # 本题还没有AC
         if result['result'] == configs.result_code.AC:  # 本题初次AC
             with db.connection():
                 submit_time = db.select_one('select submit_time from t_submission where submission_id=?',
                                             result['solution_id']).get('submit_time')   # 提交时间, datetime类型
                 date_time = db.select_one('select contest_date, start_time from t_contest where contest_id=?',
                                           result['contest_id'])
                 c_date = date_time.get('contest_date')  # 比赛开始日期, date类型
                 c_time = date_time.get('start_time')    # 比赛开始时间, timedelta类型
                 # 转换为比赛开始时间, datetime类型
                 contest_time = datetime.strptime(c_date.strftime('%Y-%m-%d %H:%M:%S'), '%Y-%m-%d %H:%M:%S') + c_time
                 ac_time = (submit_time - contest_time).total_seconds() / 60  # 本题初次AC所用时间, 单位为分钟, float类型
                 with db.transaction():
                     db.update('update t_ranking set AC_time=? where contest_id=? and problem_id=? and username=?',
                               ac_time, result['contest_id'], result['problem_id'], result['user_id'])   # AC题目所用时间
         else:
             db.update('update t_ranking set wrong_submit = wrong_submit + 1 where contest_id=? and problem_id=? '
                       'and username=?', result['contest_id'], result['problem_id'], result['user_id'])
Пример #2
0
    def count_all(cls): 
        ''' 
        Find by 'select count(pk) from table' and return integer. 
        ''' 
        return db.select_int('select count(`%s`) from `%s`' % (cls.__primary_key__.name, cls.__table__)) 

 	@classmethod 
    def count_by(cls, where, *args): 
        ''' 
        Find by 'select count(pk) from table where ... ' and return int. 
        ''' 
        return db.select_int('select count(`%s`) from `%s` %s' % (cls.__primary_key__.name, cls.__table__, where), *args) 
 
    def update(self): 
        self.pre_update and self.pre_update() 
        L = [] 
        args = [] 
        for k, v in self.__mappings__.iteritems(): 
            if v.updatable: 
                if hasattr(self, k): 
                    arg = getattr(self, k) 
                else: 
                    arg = v.default 
                    setattr(self, k, arg) 
                L.append('`%s`=?' % k) 
                args.append(arg) 
        pk = self.__primary_key__.name 
        args.append(getattr(self, pk)) 
        db.update('update `%s` set %s where %s=?' % (self.__table__, ','.join(L), pk), *args) 
        return self 
Пример #3
0
 def count_by(cls, where, *args):
     """
     Find by 'select count(pk) from table where ... ' and return int.
     """
     return db.select_int(
         "select count(`%s`) from `%s` %s" % (cls.__primary_key__.name, cls.__table__, where), *args
     )
Пример #4
0
 def count_by(cls,where,*args):
     '''
     Find by 'select count(pk) from table where ...' and retutn int.
     :param where:
     :param args:
     :return:
     '''
     return db.select_int('select count(`%s`) from `%s` %s'%(cls.__primary_key__.name,cls.__table__,where),*args)
Пример #5
0
 def count_all(cls):
     """
     执行 select count(pk) from table语句,返回一个数值
     效率:
         count(1) 和 count(主键) 效率相对高
         count(*) 是扫描表的,相对慢
     """
     return db.select_int('select count(`%s`) from `%s`' % (cls.__primary_key__.name, cls.__table__))
Пример #6
0
    def dount_by(cls, where, *args):
        '''

        :param where:
        :param args:
        :return:
        '''
        return db.select_int('select count(`%s`) from `%s` %s' %(cls.__primary_key__.name, cls.__table__, where), args)
Пример #7
0
 def count_all(cls, **kwargs):
     '''
     Find by 'select count(pk) from table' and return integer.
     '''
     if kwargs.has_key('sub_name'):
         sub_name = kwargs['sub_name']
     else:
         sub_name = ""
     return db.select_int('select count(`%s`) from `%s%s`' % (cls.__primary_key__.name, cls.__table__, sub_name))
Пример #8
0
    def count_by(cls, where, *args, **kwargs):
        '''
        Find by 'select count(pk) from table where ... ' and return int.
        '''
        if kwargs.has_key('sub_name'):
            sub_name = kwargs['sub_name']
        else:
            sub_name = ""

        return db.select_int('select count(`%s`) from `%s%s` %s' % (cls.__primary_key__.name, cls.__table__, sub_name,  where), *args)
Пример #9
0
 def count_all(cls):
     '''
     >>> class User(Model):
     ...     __table__ = 'user'
     ...     id = IntegerField(primary_key=True)
     ...     name = StringField()
     >>> user7 = User(id=7, name='a')
     >>> user8 = User(id=8, name='a')
     >>> user7.insert()
     {'name': 'a', 'id': 7}
     >>> user8.insert()
     {'name': 'a', 'id': 8}
     >>> User.count_all()
     2L
     '''
     return db.select_int('select count(`%s`) from `%s`' % (cls.__primary_key__, cls.__table__))
Пример #10
0
def find_logs_for_table(kwargs, start=0, size=10):
    sql_count = " select count(*) from logs "
    sql = " select * from `logs` "

    if kwargs:
        where = " where "
        if kwargs.get('created'):
            s = kwargs.get('created').get("start")
            e = kwargs.get('created').get("end")
            kwargs.pop('created')
            where += "created > '%s' and created< '%s' " % (s, e)
        where += ' and '.join(["`%s`=%s" % (k, v) for k, v in kwargs])
        sql_count += where
        sql += where
    sql += ' order by created desc limit ?,?; '
    count = db.select_int(sql_count)
    logs = db.select(sql, start, size)

    result = {
        "count": count,
        "data": logs
    }
    raise gen.Return(result)
Пример #11
0
 def update_contest_ranking(result):
     """
     更新比赛统计数据
     :param result:
     :return:
     """
     rowcount = db.update("show tables like 't_?'", result['contest_id'])
     if rowcount == 0:
         PutTask._create_ranking_table(result)
     count = db.select_int('select count(*) from t_? where username=?', result['contest_id'], result['user_id'])
     if count == 0:
         user = dict(username=result['user_id'])
         db.insert('t_%s' % result['contest_id'], **user)
     p_time = db.select_one('select ?_time from t_? where username=?',
                            result['problem_id'], result['contest_id'], result['user_id'])\
         .get('%s_time' % result['problem_id'])
     if p_time == 0:  # 本题还没有AC
         if result['result'] == configs.result_code.AC:  # 本题初次AC
             with db.connection():
                 submit_time = db.select_one('select submit_time from t_submission where submission_id=?',
                                             result['solution_id']).get('submit_time')   # 提交时间, datetime类型
                 date_time = db.select_one('select contest_date, start_time from t_contest where contest_id=?',
                                           result['contest_id'])
                 c_date = date_time.get('contest_date')  # 比赛开始日期, date类型
                 c_time = date_time.get('start_time')    # 比赛开始时间, timedelta类型
                 # 转换为比赛开始时间, datetime类型
                 contest_time = datetime.strptime(c_date.strftime('%Y-%m-%d %H:%M:%S'), '%Y-%m-%d %H:%M:%S') + c_time
                 ac_time = (submit_time - contest_time).total_seconds()  # 本题初次AC所用时间, 单位为秒, float类型
                 with db.transaction():
                     db.update('update t_? set solved = solved + 1 where username=?',
                               result['contest_id'], result['user_id'])  # AC题数 + 1
                     db.update('update t_? set ?_time=? where username=?',
                               result['contest_id'], result['problem_id'], ac_time, result['user_id'])   # AC题目所用时间
         else:
             db.update('update t_? set ?_wrong = ?_wrong + 1 where username=?',
                       result['contest_id'], result['problem_id'], result['problem_id'], result['user_id'])
Пример #12
0
    def count_all(cls):
        '''
		@summary: 通过'select count(pk) from table'查询,并返回一个整数。
		'''
        return db.select_int('select count(`%s`) from `%s`' %
                             (cls.__primary_key__.name, cls.__table__))
Пример #13
0
 def count_by(cls, where, *args):
     return db.select_int(
         'select count(%s) from %s %s' %
         (cls.__primary_key__, cls.__table__, where), args)
Пример #14
0
 def count_all(cls, *args):
     return db.select_int('select count(%s) from %s' %
                          (cls.__primary_key__, cls.__table__))
Пример #15
0
 def count_all(cls, *args):
     return db.select_int('select count(%s) from %s' % (cls.__primary_key__, cls.__table__))
Пример #16
0
 def count_all(cls):
     """
     Find by 'select count(pk) from table' and return integer.
     """
     return db.select_int("select count(`%s`) from `%s`" % (cls.__primary_key__.name, cls.__table__))
Пример #17
0
 def count_by(cls, where, *args):
     '''
     Find by 'select count(pk) from table where ... ' and return int.
     '''
     return db.select_int('select count(`%s`) from `%s` %s' % (cls.__primary_key__.name, cls.__table__, where), *args)
Пример #18
0
 def count_all(cls):
     '''
     查询记录数量
     :return:
     '''
     return db.select_int('select count(`%s`) from %s' % (cls.__primary_key__.name, cls.__table__))
Пример #19
0
 def count_by(cls, where, *args):
     return db.select_int('select count(%s) from %s %s' % (cls.__primary_key__, cls.__table__, where), args)
Пример #20
0
        args.append(getattr(self, pk))
        db.update('update `%s` set %s where %s=?' % (self.__table__, ','.join(L), pk), *args)
        return self

    def delete(self):
        self.pre_delete and self.pre_delete()
        pk = self.__primary_key__.name
        args = (getattr(self, pk), )
        db.update('delete from `%s` where `%s`=?' % (self.__table__, pk), *args)
        return self

    def insert(self):
        self.pre_insert and self.pre_insert()
        params = {}
        for k, v in self.__mappings__.iteritems():
            if v.insertable:
                if not hasattr(self, k):
                    setattr(self, k, v.default)
                params[v.name] = getattr(self, k)
        db.insert('%s' % self.__table__, **params)
        return self

if __name__=='__main__':
    logging.basicConfig(level=logging.DEBUG)
    db.create_engine('root', 'admin', 'test')
    d = db.select('select * from test1')
    print d
    d = db.select_one('select * from test1')
    print d
    d = db.select_int('select count(*) from test1')
    print d
Пример #21
0
 def count_by(cls, order=None, group=None, limit=None, offset=None, **kwargs):
     return db.select_int(cls.__table__, what='count(%s)' % cls.__primary_key__.name,
         where=kwargs, order=order, group=group, limit=limit, offset=offset)
Пример #22
0
 def count_all(cls):
     return db.select_int(cls.__table__, what='count(%s)' % cls.__primary_key__.name)
Пример #23
0
 def count_all(cls):
     """Count by attribute"""
     sql = 'select count(*) from %s' % cls.__table_name__
     return db.select_int(sql)
Пример #24
0
 def count_by(cls, where, *args):
     return db.select_int(
         'select count(`%s`) from `%s` %s' %
         (cls, __primary_key__.name, cls.__table__, where), *args)
Пример #25
0
 def count_all(cls):
     '''
     Find by 'select count(pk) from table' and return integer.
     '''
     return db.select_int('select count(`%s`) from `%s`' % (cls.__primary_key__.name, cls.__table__))
Пример #26
0
	def count_by(cls, where, *args):
		"""
		通过select count(pk) from table where ...语句进行查询,返回一个数值
		"""
		return db.select_int('select count(`%s`) from `%s` %s' % (cls.__primary_key__.name, cls.__table__, where), *args)
Пример #27
0
 def count_by(cls, where, *args):
     '''
     通过select count(pk) from table where ...语句进行查询, 返回一个数值       
     Find by 'select count(pk) from table where ... ' and return int.
     '''
     return db.select_int('select count(`%s`) from `%s` %s' % (cls.__primary_key__.name, cls.__table__, where), *args)
Пример #28
0
 def count_all(cls):
     """
     执行 select count(pk) from table 
     """
     return db.select_int('select count(`%s`) from %s' %(cls.__primary_key__.name,cls.__table__))
Пример #29
0
 def count_by(cls, where, *args):
     """
     通过select count(pk) from table where ...语句进行查询, 返回一个数值
     """
     return db.select_int('select count(`%s`) from `%s` %s' % (cls.__primary_key__.name, cls.__table__, where), *args)
Пример #30
0
 def count_all(cls):
     '''
     Find by 'select count(pk) from table' and return integer.
     '''
     return db.select_int('select count(`%s`) from `%s`' % (cls.__primary_key__.name, cls.__table__))
Пример #31
0
#!/usr/bin/env python
Пример #32
0
	def count_all(cls):
		'''select count(pk) from table  返回一个integer'''
		return db.select_int('select count(`%s`) from `%s`' % (cls.__primary_key__.name, cls.__table__))
Пример #33
0
 def count_all(cls):
     """
     find by 'select count(pk) from table's and return integer'
     """
     return db.select_int('elect count("%s") from "%s"'%(cls.__primary_key__.name,cls.__table__))
Пример #34
0
 def count(cls, where, *args):
     '''
     Find by 'select count(*) from where ... ' and return one and only one result.
     '''
     return db.select_int('select count(%s) from %s %s' % (cls.__primary_key__.name, cls.__table__, where), *args) if where else \
            db.select_int('select count(%s) from %s' % (cls.__primary_key__.name, cls.__table__))
Пример #35
0
 def count(cls, where, *args):
     '''
     Find by 'select count(*) from where ... ' and return one and only one result.
     '''
     return db.select_int('select count(%s) from %s %s' % (cls.__primary_key__.name, cls.__table__, where), *args) if where else \
            db.select_int('select count(%s) from %s' % (cls.__primary_key__.name, cls.__table__))
Пример #36
0
    def count_all(cls):
        return db.select_int('select count(`%s`) from `%s`' % (cls.__primary_key__.name,cls.__table__))

    @classmethod
Пример #37
0
 def count_all(cls):
     return db.select_int('select count(%s) from `%s` %s' % (cls.__primary_key__.name, cls.__table__))
Пример #38
0
 def count_all(cls):
     return db.select_int('select count(`%s`) from `%s`' %
                          (cls.__primary_key__.name, cls.__table__))
Пример #39
0
	def count_by(cls, where, *args):
		return db.select_int('select count(`%s`) from `%s` %s' % (cls.__primary_key__.name, cls.__table__, where), *args)
Пример #40
0
 def count_by(cls, where, *args):
     ' find by `select count(pk) from table where ... ` and return int '
     return db.select_int('select count(`%s`) from `%s` %s' % (cls.__primary_key__.name, cls.__table__, where), *args)