Skip to content
Browse files

Create a small helper function to abstract SQL fetching

  • Loading branch information...
1 parent a23946b commit ac27d8d22f068ffe7cdd7e78e1844ca8ff8fc137 @dstufft committed Feb 27, 2014
Showing with 16 additions and 6 deletions.
  1. +12 −0 warehouse/db.py
  2. +4 −6 warehouse/packaging/models.py
View
12 warehouse/db.py
@@ -17,3 +17,15 @@
import sqlalchemy
metadata = sqlalchemy.MetaData()
+
+
+def scalar(query):
+ """
+ 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):
+ with model.engine.connect() as conn:
+ return conn.execute(query).scalar()
+
+ return scalar_inner
View
10 warehouse/packaging/models.py
@@ -19,7 +19,7 @@
import urlparse
import logging
-from warehouse import models
+from warehouse import db, models
from warehouse.packaging.tables import ReleaseDependencyKind
@@ -28,11 +28,9 @@
class Model(models.Model):
- def get_project_count(self):
- query = "SELECT COUNT(*) FROM packages"
-
- with self.engine.connect() as conn:
- return conn.execute(query).scalar()
+ get_project_count = db.scalar(
+ "SELECT COUNT(*) FROM packages"
+ )
def get_download_count(self):
query = "SELECT SUM(downloads) FROM release_files"

0 comments on commit ac27d8d

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