Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: sitescripts/filterhits/db.py

Issue 4615801646612480: Issue 395 - Filter hits statistics backend (Closed)
Patch Set: Created Dec. 19, 2014, 1:16 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sitescripts/filterhits/db.py
diff --git a/sitescripts/filterhits/db.py b/sitescripts/filterhits/db.py
new file mode 100644
index 0000000000000000000000000000000000000000..f09cd500acb559ba696eeb22df860daf912bf865
--- /dev/null
+++ b/sitescripts/filterhits/db.py
@@ -0,0 +1,85 @@
+# coding: utf-8
+
+# This file is part of the Adblock Plus web scripts,
+# Copyright (C) 2006-2014 Eyeo GmbH
+#
+# Adblock Plus is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 3 as
+# published by the Free Software Foundation.
+#
+# Adblock Plus is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+
+import MySQLdb
+
+db = None
+
+def connect(user, password, database):
+ global db
+ if not db:
+ db = MySQLdb.connect(
+ user=user,
+ passwd=password,
+ db=database,
+ use_unicode=True, charset="utf8"
+ )
+ return db
+
+def disconnect():
Sebastian Noack 2015/02/11 16:00:12 It seems the global variable and diconnect() metho
kzar 2015/02/17 10:52:24 Done.
+ global db
+ if db:
+ db.close()
+ db = None
+
+def escape(s):
+ return MySQLdb.escape_string(s)
+
+def query(sql, dict_result=False):
+ """
+ Executes the query given by the provided SQL and returns the results.
+ If dict_result keyword argument is provided + True the results will be
+ returned as a tuple of dictionaries, otherwise a tuple of tuples.
+ """
+ global db
kzar 2015/02/17 10:52:24 Done.
+ try:
+ if dict_result:
+ cursor = db.cursor(MySQLdb.cursors.DictCursor)
+ else:
+ cursor = db.cursor()
+ cursor.execute(sql)
+ results = cursor.fetchall()
+ finally:
+ if cursor:
+ cursor.close()
+ return results
+
+def write(sql):
+ """
+ This writes a given SQL string or iterator of SQL strings to the database.
+ All given SQL will be run as one transaction and rolled back on error.
+ """
+ global db
+
+ if isinstance(sql, str):
Sebastian Noack 2015/02/11 16:00:12 How about always expecting a sequence here, elimin
Sebastian Noack 2015/02/17 14:59:17 You didn't mind to address or reply to this commen
kzar 2015/02/24 18:05:11 It just makes the function nicer to use if you onl
Sebastian Noack 2015/02/26 16:39:25 There is only one call in your whole code where yo
kzar 2015/02/28 19:39:56 Fair enough, I didn't realise that. Done.
+ sql = [sql]
+
+ try:
+ # Commit the changes
Sebastian Noack 2015/02/11 16:00:12 You don't say? This comment doesn't add any inform
kzar 2015/02/17 10:52:24 Done.
+ cursor = db.cursor()
+ for query in sql:
+ if query:
+ [cursor.execute(s) for s in query.split(";") if s]
Sebastian Noack 2015/02/11 16:00:12 I'd rather use a for loop, than list expressions,
kzar 2015/02/17 10:52:24 Done.
+ db.commit()
+ except MySQLdb.Error:
+ # On error roll them back
Sebastian Noack 2015/02/11 16:00:12 Same as above.
kzar 2015/02/17 10:52:24 Done.
+ if db:
Sebastian Noack 2015/02/11 16:00:12 How could a MySQLdb.Error be raised if db didn't e
kzar 2015/02/17 10:52:24 I'm not sure but I seem to remember that this chec
Sebastian Noack 2015/02/26 16:39:25 If a random change you can't explain, fixes an iss
kzar 2015/02/28 19:39:56 Well I agree with you of course but I guess the tr
+ db.rollback()
+ raise
+ finally:
+ if cursor:
Sebastian Noack 2015/02/11 16:00:12 This will result in a NameError if the cursor does
kzar 2015/02/17 10:52:24 Done.
+ cursor.close()

Powered by Google App Engine
This is Rietveld