Tarek Ziadé avatar Tarek Ziadé committed eddb7dd

added a hit counter

Comments (0)

Files changed (1)

     id = Column(Integer, primary_key=True)
     short_url = Column(String(10), nullable=False)
     long_url = Column(Text(), nullable=False)
+    hits = Column(Integer, default=0)
 
 
 short = Short.__table__
 """)
 
 
+HIT = text("""\
+update
+    short
+set
+    hits = hits + 1
+where
+    short_url = :short_url
+""")
+
+
 class SQLStorage(object):
 
     def __init__(self, sqluri):
         self.engine.execute(ADD, short_url=new_short, long_url=long_url)
         return new_short
 
-    def get_long_url(self, short_url):
+    def get_long_url(self, short_url, hit=True):
         res = self.engine.execute(GET_LONG, short_url=short_url)
         res = res.fetchone()
         if res is None:
             return None
+        if hit:
+            # let's increment the hit
+            self.engine.execute(HIT, short_url=short_url)
         return res.long_url
 
     def get_short_url(self, long_url):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.