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

Side by Side Diff: sitescripts/filterhits/db.py

Issue 4615801646612480: Issue 395 - Filter hits statistics backend (Closed)
Patch Set: Created Dec. 19, 2014, 1:16 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # coding: utf-8
2
3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2014 Eyeo GmbH
5 #
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
8 # published by the Free Software Foundation.
9 #
10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
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/>.
17
18 import MySQLdb
19
20 db = None
21
22 def connect(user, password, database):
23 global db
24 if not db:
25 db = MySQLdb.connect(
26 user=user,
27 passwd=password,
28 db=database,
29 use_unicode=True, charset="utf8"
30 )
31 return db
32
33 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.
34 global db
35 if db:
36 db.close()
37 db = None
38
39 def escape(s):
40 return MySQLdb.escape_string(s)
41
42 def query(sql, dict_result=False):
43 """
44 Executes the query given by the provided SQL and returns the results.
45 If dict_result keyword argument is provided + True the results will be
46 returned as a tuple of dictionaries, otherwise a tuple of tuples.
47 """
48 global db
kzar 2015/02/17 10:52:24 Done.
49 try:
50 if dict_result:
51 cursor = db.cursor(MySQLdb.cursors.DictCursor)
52 else:
53 cursor = db.cursor()
54 cursor.execute(sql)
55 results = cursor.fetchall()
56 finally:
57 if cursor:
58 cursor.close()
59 return results
60
61 def write(sql):
62 """
63 This writes a given SQL string or iterator of SQL strings to the database.
64 All given SQL will be run as one transaction and rolled back on error.
65 """
66 global db
67
68 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.
69 sql = [sql]
70
71 try:
72 # 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.
73 cursor = db.cursor()
74 for query in sql:
75 if query:
76 [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.
77 db.commit()
78 except MySQLdb.Error:
79 # On error roll them back
Sebastian Noack 2015/02/11 16:00:12 Same as above.
kzar 2015/02/17 10:52:24 Done.
80 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
81 db.rollback()
82 raise
83 finally:
84 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.
85 cursor.close()
OLDNEW

Powered by Google App Engine
This is Rietveld