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

Delta Between Two Patch Sets: sitescripts/filterhits/db.py

Issue 4615801646612480: Issue 395 - Filter hits statistics backend (Closed)
Left Patch Set: Make test log directory path configurable and ensure it's always cleared. Created March 30, 2015, 3:12 p.m.
Right Patch Set: Addressed further comments from Sebastian. Created April 2, 2015, 10:13 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « sitescripts/filterhits/bin/reprocess_logs.py ('k') | sitescripts/filterhits/geometrical_mean.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This file is part of the Adblock Plus web scripts, 3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 Eyeo GmbH 4 # Copyright (C) 2006-2015 Eyeo GmbH
5 # 5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify 6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as 7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation. 8 # published by the Free Software Foundation.
9 # 9 #
10 # Adblock Plus is distributed in the hope that it will be useful, 10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details. 13 # GNU General Public License for more details.
14 # 14 #
15 # You should have received a copy of the GNU General Public License 15 # You should have received a copy of the GNU General Public License
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17 17
18 import itertools 18 import itertools
19
19 import MySQLdb 20 import MySQLdb
20 21
21 from sitescripts.utils import get_config 22 from sitescripts.utils import get_config
22 23
23 def connect(): 24 def connect():
24 config = get_config() 25 config = get_config()
25 return MySQLdb.connect( 26 return MySQLdb.connect(
26 user=config.get("filterhitstats", "dbuser"), 27 user=config.get("filterhitstats", "dbuser"),
27 passwd=config.get("filterhitstats", "dbpassword"), 28 passwd=config.get("filterhitstats", "dbpassword"),
28 db=config.get("filterhitstats", "database"), 29 db=config.get("filterhitstats", "database"),
29 use_unicode=True, charset="utf8" 30 use_unicode=True, charset="utf8"
30 ) 31 )
31 32
32 def query(db, sql, *params, **kwargs): 33 def query(db, sql, *params, **kwargs):
33 """ 34 """
34 Executes the query given by the provided SQL and returns the results. 35 Executes the query given by the provided SQL and returns the results.
35 If dict_result keyword argument is provided + True the results will be 36 If dict_result keyword argument is provided + True the results will be
36 returned as a tuple of dictionaries, otherwise a tuple of tuples. 37 returned as a tuple of dictionaries, otherwise a tuple of tuples.
37 """ 38 """
38 if kwargs.pop('dict_result', False): 39 if kwargs.get("dict_result"):
39 cursor = db.cursor(MySQLdb.cursors.DictCursor) 40 cursor = db.cursor(MySQLdb.cursors.DictCursor)
40 else: 41 else:
41 cursor = db.cursor() 42 cursor = db.cursor()
42 try: 43 try:
43 cursor.execute(sql, params) 44 cursor.execute(sql, params)
44 db.commit() 45 db.commit()
45 return cursor.fetchall() 46 return cursor.fetchall()
46 finally: 47 finally:
47 cursor.close() 48 cursor.close()
48 49
49 def write(db, queries): 50 def write(db, queries):
50 """ 51 """
51 This writes a given iteratable object of tuples containing SQL 52 This writes a given iteratable object of tuples containing SQL
52 strings and any required parameters to the database. All queries will 53 strings and any required parameters to the database. All queries will
53 be run as one transaction and rolled back on error. 54 be run as one transaction and rolled back on error.
54 """ 55 """
55 try: 56 try:
56 cursor = db.cursor() 57 cursor = db.cursor()
57 try: 58 try:
58 for query in queries: 59 for query in queries:
59 sql, params = query[0], query[1:] 60 sql, params = query[0], query[1:]
60 cursor.execute(sql, params) 61 cursor.execute(sql, params)
61 db.commit() 62 db.commit()
62 finally: 63 finally:
63 cursor.close() 64 cursor.close()
64 except MySQLdb.Error: 65 except MySQLdb.Error:
65 db.rollback() 66 db.rollback()
66 raise 67 raise
LEFTRIGHT

Powered by Google App Engine
This is Rietveld