Commits

Kirill Simonov committed 37862a9

Renamed `.order()` to `.sort()`.

Method `.sort()` now understands order modifiers.

Comments (0)

Files changed (4)

src/htsql/tr/encoder.py

 
     def relate(self):
         space = self.encoder.relate(self.binding.base)
-        order = [(self.encoder.encode(binding), +1)
-                 for binding in self.binding.order]
+        order = []
+        for binding in self.binding.order:
+            code = self.encoder.encode(binding)
+            direction = self.encoder.order(binding)
+            if direction is None:
+                direction = +1
+            order.append((code, direction))
         limit = self.binding.limit
         offset = self.binding.offset
         return OrderedSpace(space, order, limit, offset, self.binding)

src/htsql/tr/fn/function.py

 
 class OrderMethod(ProperMethod):
 
-    named('order')
+    named('sort')
 
     parameters = [
             Parameter('this'),

test/input/pgsql.yaml

         - uri: /{date('2010-07-28')+1,date('2010-07-28')-1,
                  date('2010-07-28')-date('2009-07-28')}
 
-  # Order and Limit functions.
+  # Sort and Limit functions.
   - title: Sorting and paging
     tests:
     - uri: /school
     - uri: /school.limit(1)
     - uri: /school.limit(2,5)
     - uri: /course?department='acc'
-    - uri: /course.order(credits)?department='acc'
-    - uri: /course.order(credits).limit(1,1)?department='acc'
+    - uri: /course.sort(credits)?department='acc'
+    - uri: /course.sort(credits).limit(1,1)?department='acc'
 
   # The `as` operator.
   - title: Title decorator
     - uri: /school{(name+) as Title}
     # Non-unique explicit ordering is complemented with the default ordering.
     - uri: /course{department+,title,credits-}?number<200
+    # Same using the `sort` method.
+    - uri: /course.sort(department+,credits-){department,title,credits}?number<200
 
   # Simple (non-aggregate) filters.
   - title: Simple filters

test/output/pgsql.yaml

          ----
          /course?department='acc'
          SELECT "course"."department", "course"."number", "course"."title", "course"."credits", "course"."description" FROM "ad"."course" AS "course" WHERE ("course"."department" = 'acc') ORDER BY 1 ASC, 2 ASC
-    - uri: /course.order(credits)?department='acc'
+    - uri: /course.sort(credits)?department='acc'
       status: 200 OK
       headers:
       - [Content-Type, text/plain; charset=UTF-8]
       body: |2
-         | (course.order(credits)?department='acc')                                                      |
+         | (course.sort(credits)?department='acc')                                                       |
         -+-----------------------------------------------------------------------------------------------+-
          | department | number | title                      | credits | description                      |
         -+------------+--------+----------------------------+---------+----------------------------------+-
                                                                                                   (6 rows)
 
          ----
-         /course.order(credits)?department='acc'
+         /course.sort(credits)?department='acc'
          SELECT "course"."department", "course"."number", "course"."title", "course"."credits", "course"."description" FROM (SELECT "course"."department", "course"."number", "course"."title", "course"."credits", "course"."description" FROM "ad"."course" AS "course" WHERE ("course"."department" = 'acc') ORDER BY 4 ASC, 1 ASC, 2 ASC) AS "course" ORDER BY 4 ASC, 1 ASC, 2 ASC
-    - uri: /course.order(credits).limit(1,1)?department='acc'
+    - uri: /course.sort(credits).limit(1,1)?department='acc'
       status: 200 OK
       headers:
       - [Content-Type, text/plain; charset=UTF-8]
       body: |2
-         | (course.order(credits).limit(1,1)?department='acc')                                           |
+         | (course.sort(credits).limit(1,1)?department='acc')                                            |
         -+-----------------------------------------------------------------------------------------------+-
          | department | number | title                      | credits | description                      |
         -+------------+--------+----------------------------+---------+----------------------------------+-
                                                                                                    (1 row)
 
          ----
-         /course.order(credits).limit(1,1)?department='acc'
+         /course.sort(credits).limit(1,1)?department='acc'
          SELECT "course"."department", "course"."number", "course"."title", "course"."credits", "course"."description" FROM (SELECT "course"."department", "course"."number", "course"."title", "course"."credits", "course"."description" FROM (SELECT "course"."department", "course"."number", "course"."title", "course"."credits", "course"."description" FROM "ad"."course" AS "course" ORDER BY 4 ASC, 1 ASC, 2 ASC) AS "course" WHERE ("course"."department" = 'acc') ORDER BY 4 ASC, 1 ASC, 2 ASC LIMIT 1 OFFSET 1) AS "course" ORDER BY 4 ASC, 1 ASC, 2 ASC
   - id: title-decorator
     tests:
          ----
          /course{department+,title,credits-}?number<200
          SELECT "course"."department", "course"."title", "course"."credits" FROM "ad"."course" AS "course" WHERE ("course"."number" < 200) ORDER BY 1 ASC, 3 DESC, 1 ASC, "course"."number" ASC
+    - uri: /course.sort(department+,credits-){department,title,credits}?number<200
+      status: 200 OK
+      headers:
+      - [Content-Type, text/plain; charset=UTF-8]
+      body: |2
+         | (course.sort(department+,credits-)?number<200)          |
+        -+---------------------------------------------------------+-
+         | department | title                            | credits |
+        -+------------+----------------------------------+---------+-
+         | acc        | Practical Bookkeeping            |       2 |
+         | artstd     | Spring Basket Weaving Workshop   |         |
+         | astro      | The Solar System                 |       3 |
+         | astro      | Solar System Lab                 |       2 |
+         | astro      | Telescope Workshop               |       1 |
+         | be         | Introduction to Biomedical       |       3 |
+         :            : Engineering                      :         :
+         | chem       | Principles of Chemistry          |       3 |
+         | chem       | Organic Chemistry I              |       3 |
+         | chem       | Organic Chemistry Laboratory I   |       2 |
+         | comp       | Introduction to Computer Science |       3 |
+         | ee         | Exploration of Electrical        |       3 |
+         :            : Engineering                      :         :
+         | eng        | Introduction to Composition      |       3 |
+         | eng        | College Newspaper                |       2 |
+         | hist       | The United States in World       |       3 |
+         :            : History                          :         :
+         | me         | Introduction to Mechanical       |       3 |
+         :            : Engineering                      :         :
+         | mth        | College Algebra                  |       5 |
+         | poli       | American Government and Politics |       3 |
+         | psych      | General Psychology               |       3 |
+         | tched      | Theory and Practice of Early     |       3 |
+         :            : Childhood Education              :         :
+         | tched      | Methods of Early Science         |       3 |
+         :            : Education                        :         :
+                                                           (20 rows)
+
+         ----
+         /course.sort(department+,credits-){department,title,credits}?number<200
+         SELECT "course"."department", "course"."title", "course"."credits" FROM (SELECT "course"."department", "course"."title", "course"."credits", "course"."number" FROM "ad"."course" AS "course" WHERE ("course"."number" < 200) ORDER BY 1 ASC, 3 DESC, 4 ASC) AS "course" ORDER BY 1 ASC, 3 DESC, "course"."number" ASC
   - id: simple-filters
     tests:
     - uri: /school?code='ns'