Ejemplo n.º 1
0
Archivo: db.py Proyecto: jjnsn/caravela
  def select(self,cols="*"):
    if cols:
      if cols == '*':
        cols = ','.join(self.__schema__)

      self.col_exps = codd.parse(
        "({})".format(cols), 
        get_value=self.get_value
      ).func_closure[0].cell_contents


      #self.col_exps = [
      #  codd.parse(col, get_value=self.get_value)
      #  for col in cols
      #]

      self.reducers = [
        (pos, self.aggregates[col.__name__])
        for pos, col in enumerate(self.col_exps)
        if col.__name__ in self.aggregates
      ]
 
      self.schema = [
        getattr(c, '__name__', "col{}".format(i))
        for i,c in enumerate(self.col_exps)
      ]
      result_class = self.result_class = namedtuple("row", self.schema)

    return self
Ejemplo n.º 2
0
Archivo: db.py Proyecto: jjnsn/caravela
  def _order_key(self, attr, direction="desc"):
    direction = direction.lower()
    get_key = codd.parse(attr)

    if direction == "desc":
      return get_key
    elif direction == "asc":
      def invert(record, ctx):
        value = get_key(record,ctx)
        v_type = type(value)
        # TODO: utilize types if/when we get them
        if v_type in (int, float, long, complex):
          return -value
        elif v_type  in ( str,  bytearray):
          return [-b for b in bytearray(value)]
        elif v_type == unicode:
          return [-b for b in bytearray(value.encode('utf-8'))]
        else:
          return None
      return invert
    else:
      raise RuntimeError("direction must be DESC or ASC")