Permalink
Browse files

Make db.scalar able to handle SQL with params

  • Loading branch information...
1 parent f282f66 commit ec0527a02ee983a05634457cb48dee7af078cb09 @dstufft committed Feb 28, 2014
Showing with 10 additions and 13 deletions.
  1. +2 −2 warehouse/db.py
  2. +8 −11 warehouse/packaging/models.py
View
@@ -24,9 +24,9 @@ def scalar(query, default=None):
A helper function that takes a query and returns a function that will query
the database and return a scalar.
"""
- def scalar_inner(model, **kwargs):
+ def scalar_inner(model, *args, **kwargs):
with model.engine.connect() as conn:
- val = conn.execute(query).scalar()
+ val = conn.execute(query, *args, **kwargs).scalar()
if default is not None and val is None:
return default
@@ -98,17 +98,14 @@ def get_top_projects(self, num=None):
with self.engine.connect() as conn:
return [tuple(r) for r in conn.execute(query, limit=num)]
- def get_project(self, name):
- query = \
- """ SELECT name
- FROM packages
- WHERE normalized_name = lower(
- regexp_replace(%(name)s, '_', '-', 'ig')
- )
- """
-
- with self.engine.connect() as conn:
- return conn.execute(query, name=name).scalar()
+ get_project = db.scalar(
+ """ SELECT name
+ FROM packages
+ WHERE normalized_name = lower(
+ regexp_replace(%s, '_', '-', 'ig')
+ )
+ """
+ )
def get_projects_for_user(self, username):
query = \

0 comments on commit ec0527a

Please sign in to comment.