Skip to content
This repository
Browse code

Changing set_alias to alias

  • Loading branch information...
commit b44cfd601f61b7492da8baed1f73297978df1e2f 1 parent 32860e5
Charles Leifer authored October 05, 2012
16  peewee.py
@@ -183,10 +183,10 @@ def clone(self):
183 183
 
184 184
 class Expr(object):
185 185
     def __init__(self):
186  
-        self.alias = None
  186
+        self._alias = None
187 187
 
188  
-    def set_alias(self, a=None):
189  
-        self.alias = a
  188
+    def alias(self, a=None):
  189
+        self._alias = a
190 190
         return self
191 191
 
192 192
     def asc(self):
@@ -616,8 +616,8 @@ def quote(self, s):
616 616
         return ''.join((self.quote_char, s, self.quote_char))
617 617
 
618 618
     def _add_alias(self, expr_str, expr):
619  
-        if expr.alias:
620  
-            expr_str = ' '.join((expr_str, 'AS', expr.alias))
  619
+        if expr._alias:
  620
+            expr_str = ' '.join((expr_str, 'AS', expr._alias))
621 621
         return expr_str
622 622
 
623 623
     def _max_alias(self, am):
@@ -984,8 +984,8 @@ def construct_instance(self, row):
984 984
 
985 985
             if isinstance(expr, Field):
986 986
                 setattr(instance, expr.name, expr.python_value(value))
987  
-            elif isinstance(expr, Expr) and expr.alias:
988  
-                setattr(instance, expr.alias, value)
  987
+            elif isinstance(expr, Expr) and expr._alias:
  988
+                setattr(instance, expr._alias, value)
989 989
             else:
990 990
                 setattr(instance, cols[i], value)
991 991
 
@@ -1295,7 +1295,7 @@ def naive(self, naive=True):
1295 1295
         self._naive = naive
1296 1296
 
1297 1297
     def annotate(self, rel_model, annotation=None):
1298  
-        annotation = annotation or fn.Count(rel_model._meta.primary_key).set_alias('count')
  1298
+        annotation = annotation or fn.Count(rel_model._meta.primary_key).alias('count')
1299 1299
         query = self.clone()
1300 1300
         query = query.ensure_join(query._query_ctx, rel_model)
1301 1301
         if not query._group_by:
14  playhouse/tests_postgres.py
@@ -44,41 +44,41 @@ def test_storage(self):
44 44
     def test_selecting(self):
45 45
         self.create()
46 46
 
47  
-        sq = Testing.select(Testing.name, Testing.data.keys().set_alias('keys'))
  47
+        sq = Testing.select(Testing.name, Testing.data.keys().alias('keys'))
48 48
         self.assertEqual([(x.name, sorted(x.keys)) for x in sq], [
49 49
             ('t1', ['k1', 'k2']), ('t2', ['k2', 'k3'])
50 50
         ])
51 51
 
52  
-        sq = Testing.select(Testing.name, Testing.data.values().set_alias('vals'))
  52
+        sq = Testing.select(Testing.name, Testing.data.values().alias('vals'))
53 53
         self.assertEqual([(x.name, sorted(x.vals)) for x in sq], [
54 54
             ('t1', ['v1', 'v2']), ('t2', ['v2', 'v3'])
55 55
         ])
56 56
 
57  
-        sq = Testing.select(Testing.name, Testing.data.items().set_alias('mtx'))
  57
+        sq = Testing.select(Testing.name, Testing.data.items().alias('mtx'))
58 58
         self.assertEqual([(x.name, sorted(x.mtx)) for x in sq], [
59 59
             ('t1', [['k1', 'v1'], ['k2', 'v2']]),
60 60
             ('t2', [['k2', 'v2'], ['k3', 'v3']]),
61 61
         ])
62 62
 
63  
-        sq = Testing.select(Testing.name, Testing.data.slice('k2', 'k3').set_alias('kz'))
  63
+        sq = Testing.select(Testing.name, Testing.data.slice('k2', 'k3').alias('kz'))
64 64
         self.assertEqual([(x.name, x.kz) for x in sq], [
65 65
             ('t1', {'k2': 'v2'}),
66 66
             ('t2', {'k2': 'v2', 'k3': 'v3'}),
67 67
         ])
68 68
 
69  
-        sq = Testing.select(Testing.name, Testing.data.slice('k4').set_alias('kz'))
  69
+        sq = Testing.select(Testing.name, Testing.data.slice('k4').alias('kz'))
70 70
         self.assertEqual([(x.name, x.kz) for x in sq], [
71 71
             ('t1', {}),
72 72
             ('t2', {}),
73 73
         ])
74 74
 
75  
-        sq = Testing.select(Testing.name, Testing.data.exists('k3').set_alias('ke'))
  75
+        sq = Testing.select(Testing.name, Testing.data.exists('k3').alias('ke'))
76 76
         self.assertEqual([(x.name, x.ke) for x in sq], [
77 77
             ('t1', False),
78 78
             ('t2', True),
79 79
         ])
80 80
 
81  
-        sq = Testing.select(Testing.name, Testing.data.defined('k3').set_alias('ke'))
  81
+        sq = Testing.select(Testing.name, Testing.data.defined('k3').alias('ke'))
82 82
         self.assertEqual([(x.name, x.ke) for x in sq], [
83 83
             ('t1', False),
84 84
             ('t2', True),
8  tests.py
@@ -282,7 +282,7 @@ def test_selection(self):
282 282
         sq = SelectQuery(Blog, Blog.pk, Blog.title, Blog.user, User.username).join(User)
283 283
         self.assertSelect(sq, 'blog."pk", blog."title", blog."user_id", users."username"', [])
284 284
 
285  
-        sq = SelectQuery(User, fn.Lower(fn.Substr(User.username, 0, 1)).set_alias('lu'), fn.Count(Blog.pk)).join(Blog)
  285
+        sq = SelectQuery(User, fn.Lower(fn.Substr(User.username, 0, 1)).alias('lu'), fn.Count(Blog.pk)).join(Blog)
286 286
         self.assertSelect(sq, 'Lower(Substr(users."username", ?, ?)) AS lu, Count(blog."pk")', [0, 1])
287 287
 
288 288
         sq = SelectQuery(User, User.username, fn.Count(Blog.select().where(Blog.user == User.id)))
@@ -462,7 +462,7 @@ def test_ordering(self):
462 462
         sq = SelectQuery(User).join(Blog).order_by(User.username.desc(), Blog.title.asc())
463 463
         self.assertOrderBy(sq, 'users."username" DESC, blog."title" ASC', [])
464 464
 
465  
-        base_sq = SelectQuery(User, User.username, fn.Count(Blog.pk).set_alias('count')).join(Blog).group_by(User.username)
  465
+        base_sq = SelectQuery(User, User.username, fn.Count(Blog.pk).alias('count')).join(Blog).group_by(User.username)
466 466
         sq = base_sq.order_by(fn.Count(Blog.pk).desc())
467 467
         self.assertOrderBy(sq, 'Count(blog."pk") DESC', [])
468 468
 
@@ -567,13 +567,13 @@ def test_annotate(self):
567 567
         self.assertWhere(sq, '', [])
568 568
         self.assertGroupBy(sq, 'users."id", users."username"', [])
569 569
 
570  
-        sq = User.select(User.username).annotate(Blog, fn.Sum(Blog.pk).set_alias('sum')).where(User.username == 'foo')
  570
+        sq = User.select(User.username).annotate(Blog, fn.Sum(Blog.pk).alias('sum')).where(User.username == 'foo')
571 571
         self.assertSelect(sq, 'users."username", Sum(blog."pk") AS sum', [])
572 572
         self.assertJoins(sq, ['INNER JOIN "blog" AS blog ON users."id" = blog."user_id"'])
573 573
         self.assertWhere(sq, 'users."username" = ?', ['foo'])
574 574
         self.assertGroupBy(sq, 'users."username"', [])
575 575
 
576  
-        sq = User.select(User.username).annotate(Blog).annotate(Blog, fn.Max(Blog.pk).set_alias('mx'))
  576
+        sq = User.select(User.username).annotate(Blog).annotate(Blog, fn.Max(Blog.pk).alias('mx'))
577 577
         self.assertSelect(sq, 'users."username", Count(blog."pk") AS count, Max(blog."pk") AS mx', [])
578 578
         self.assertJoins(sq, ['INNER JOIN "blog" AS blog ON users."id" = blog."user_id"'])
579 579
         self.assertWhere(sq, '', [])

0 notes on commit b44cfd6

Please sign in to comment.
Something went wrong with that request. Please try again.