Ejemplo n.º 1
0
 def find_all(cls, where=None, args=None, **kw):
     ' find objects by where clause. '
     sql = [cls.__select__]
     if where:
         sql.append('where')
         sql.append(where)
     if args is None:
         args = []
     order_by = kw.get('orderBy', None)
     if order_by:
         sql.append('order by')
         sql.append(order_by)
     limit = kw.get('limit', None)
     if limit is not None:
         sql.append('limit')
         if isinstance(limit, int):
             sql.append('?')
             args.append(limit)
         elif isinstance(limit, tuple) and len(limit) == 2:
             sql.append('?, ?')
             args.extend(limit)
         else:
             raise ValueError('Invalid limit value: %s' % str(limit))
     rs = select(' '.join(sql), args)
     if not rs:
         return None
     return [cls(**r) for r in rs]
Ejemplo n.º 2
0
 def find_number(cls, select_field, where=None, args=None):
     ' find number by select and where. '
     sql = ['select %s _num_ from `%s`' % (select_field, cls.__table__)]
     if where:
         sql.append('where')
         sql.append(where)
     rs = select(' '.join(sql), args, 1)
     if len(rs) == 0:
         return None
     return rs[0]['_num_']
Ejemplo n.º 3
0
 def find(cls, pk):
     ' find object by primary key. '
     rs = select('%s where `%s`=?' % (cls.__select__, cls.__primary_key__), [pk], 1)
     if rs is None or len(rs) == 0:
         return None
     return cls(**rs[0])