 Issue 4615801646612480:
  Issue 395 - Filter hits statistics backend  (Closed)
    
  
    Issue 4615801646612480:
  Issue 395 - Filter hits statistics backend  (Closed) 
  | Left: | ||
| Right: | 
| LEFT | RIGHT | 
|---|---|
| 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-2014 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 | |
| 19 | |
| 18 import MySQLdb | 20 import MySQLdb | 
| 19 | 21 | 
| 20 db = None | 22 from sitescripts.utils import get_config | 
| 21 | 23 | 
| 22 def connect(user, password, database): | 24 def connect(): | 
| 23 global db | 25 config = get_config() | 
| 24 if not db: | 26 return MySQLdb.connect( | 
| 25 db = MySQLdb.connect( | 27 user=config.get("filterhitstats", "dbuser"), | 
| 26 user=user, | 28 passwd=config.get("filterhitstats", "dbpassword"), | 
| 27 passwd=password, | 29 db=config.get("filterhitstats", "database"), | 
| 28 db=database, | 30 use_unicode=True, charset="utf8" | 
| 29 use_unicode=True, charset="utf8" | 31 ) | 
| 30 ) | |
| 31 return db | |
| 32 | 32 | 
| 33 def disconnect(): | 33 def query(db, sql, *params, **kwargs): | 
| 
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 | 34 """ | 
| 35 if db: | 35 Executes the query given by the provided SQL and returns the results. | 
| 36 db.close() | 36 If dict_result keyword argument is provided + True the results will be | 
| 37 db = None | 37 returned as a tuple of dictionaries, otherwise a tuple of tuples. | 
| 38 """ | |
| 39 if kwargs.get("dict_result"): | |
| 40 cursor = db.cursor(MySQLdb.cursors.DictCursor) | |
| 41 else: | |
| 42 cursor = db.cursor() | |
| 43 try: | |
| 44 cursor.execute(sql, params) | |
| 45 db.commit() | |
| 46 return cursor.fetchall() | |
| 47 finally: | |
| 48 cursor.close() | |
| 38 | 49 | 
| 39 def escape(s): | 50 def write(db, queries): | 
| 40 return MySQLdb.escape_string(s) | |
| 41 | |
| 42 def query(sql, dict_result=False): | |
| 43 """ | 51 """ | 
| 44 Executes the query given by the provided SQL and returns the results. | 52 This writes a given iteratable object of tuples containing SQL | 
| 45 If dict_result keyword argument is provided + True the results will be | 53 strings and any required parameters to the database. All queries will | 
| 46 returned as a tuple of dictionaries, otherwise a tuple of tuples. | 54 be run as one transaction and rolled back on error. | 
| 47 """ | 55 """ | 
| 48 global db | |
| 
kzar
2015/02/17 10:52:24
Done.
 | |
| 49 try: | 56 try: | 
| 50 if dict_result: | 57 cursor = db.cursor() | 
| 51 cursor = db.cursor(MySQLdb.cursors.DictCursor) | 58 try: | 
| 52 else: | 59 for query in queries: | 
| 53 cursor = db.cursor() | 60 sql, params = query[0], query[1:] | 
| 54 cursor.execute(sql) | 61 cursor.execute(sql, params) | 
| 55 results = cursor.fetchall() | 62 db.commit() | 
| 56 finally: | 63 finally: | 
| 57 if cursor: | |
| 58 cursor.close() | 64 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: | 65 except MySQLdb.Error: | 
| 79 # On error roll them back | 66 db.rollback() | 
| 
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 | 67 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() | |
| LEFT | RIGHT |