def test_query(self): res = db.query('select name from users limit 5') self.assertEqual(len(res), 5) res = db.query('select name from users limit %s', (100,), many=20) rows = [] for r in res: rows.append(r) self.assertTrue(100, len(rows))
def test_query(self): res = db.query('select name from users limit 5') self.assertEqual(len(res), 5) res = db.query('select name from users limit %s', (100, ), many=20) rows = [] for r in res: rows.append(r) self.assertTrue(100, len(rows))
def paginate(self, page=1, perpage=10): count = self.count() results = db.query( self.SELECT + ' ORDER BY created DESC LIMIT %s OFFSET %s', (perpage, (page - 1) * perpage)) users = [self._load(user) for user in results] return Paginator(users, count, page, perpage, '/user')
def claim(self, limit, age=None): age= age or datetime.now() task_id = gen_task_id() db.execute('UPDATE cron SET task_id=%s, status=%s WHERE task_id IS NULL and next_run<=%s and status<%s LIMIT %s', (task_id, Task.RUNNING, age, Task.RUNNING, limit)) tasks = db.query('SELECT * FROM cron WHERE task_id=%s', (task_id,)) return [self._load(task) for task in tasks]
def claim(self, limit, age=None): age = age or datetime.now() task_id = gen_task_id() db.execute( 'UPDATE cron SET task_id=%s, status=%s WHERE task_id IS NULL and next_run<=%s and status<%s LIMIT %s', (task_id, Task.RUNNING, age, Task.RUNNING, limit)) tasks = db.query('SELECT * FROM cron WHERE task_id=%s', (task_id, )) return [self._load(task) for task in tasks]
def count(self, name, status): select = 'SELECT COUNT(cron_id) FROM cron' where = [] args = [] if status: args.append(status) where.append('status=%s') if name: args.append(name) where.append(' name like %s') if where: where = ' WHERE ' + ' AND '.join(where) else: where = '' return db.query(select + where , args)[0][0]
def count(self, name, status): select = 'SELECT COUNT(cron_id) FROM cron' where = [] args = [] if status: args.append(status) where.append('status=%s') if name: args.append(name) where.append(' name like %s') if where: where = ' WHERE ' + ' AND '.join(where) else: where = '' return db.query(select + where, args)[0][0]
def take(self, name, status=0, page=1, perpage=10): select = 'SELECT * FROM cron' where = [] args = [] if status: args.append(status) where.append('status=%s') if name: args.append(name) where.append(' name like %s') if where: where = ' WHERE ' + ' AND '.join(where) else: where = '' args.extend([perpage, (page - 1) * perpage]) results = db.query(select + where + ' LIMIT %s OFFSET %s', args) return [self._load(task) for task in results]
def find_by_username(self, username): """Return user by username if find in database otherwise None""" data = db.query(self.SELECT + 'WHERE username=%s', (username,)) if data: return self._load(data[0])
def find_by_username(self, username): """Return user by username if find in database otherwise None""" data = db.query(self.SELECT + 'WHERE username=%s', (username, )) if data: return self._load(data[0])
def find_by_email(self, email): """Return user by email if find in database otherwise None""" data = db.query(self.SELECT + 'WHERE email=%s', (email, )) if data: return self._load(data[0])
def find(self, uid): """Find and load the user from database by uid(user id)""" data = db.query(self.SELECT + 'WHERE uid=%s', (uid, )) if data: return self._load(data[0])
def paginate(self, page=1, perpage=10): count = self.count() results = db.query(self.SELECT + ' ORDER BY created DESC LIMIT %s OFFSET %s', (perpage, (page - 1) * perpage)) users = [self._load(user) for user in results] return Paginator(users, count, page, perpage, '/user')
def q(n): for i in range(10): res = db.query('select count(*) from users') self.assertEqual(99, res[0][0])
def find_by_email(self, email): """Return user by email if find in database otherwise None""" data = db.query(self.SELECT + 'WHERE email=%s', (email,)) if data: return self._load(data[0])
def count(self): return db.query('SELECT COUNT(uid) FROM users')[0][0]
def find_by_task_id(self, task_id): results = db.query('SELECT * FROM cron WHERE task_id=%s', (task_id,)) return [self._load(data) for data in results]
def test_excute(self): res = db.execute('insert into users values(%s, %s)', [(10L, 'thomas'), (11L, 'animer')], key='test') res = db.query('SELECT count(*) FROM users WHERE uid>=10', key='test') self.assertEqual(2, res[0][0])
def test_excute(self): res = db.execute('insert into users values(%s, %s)', [(100L, 'thomas'), (101L, 'animer')]) res = db.query('SELECT count(*) FROM users WHERE uid>=100') self.assertEqual(2, res[0][0])
def find_by_task_id(self, task_id): results = db.query('SELECT * FROM cron WHERE task_id=%s', (task_id, )) return [self._load(data) for data in results]
def find(self, uid): """Find and load the user from database by uid(user id)""" data = db.query(self.SELECT + 'WHERE uid=%s', (uid,)) if data: return self._load(data[0])