| Left: | ||
| Right: | 
| OLD | NEW | 
|---|---|
| 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 MySQLdb | 18 import itertools, MySQLdb | 
| 19 | |
| 20 db = None | |
| 21 | 19 | 
| 22 def connect(user, password, database): | 20 def connect(user, password, database): | 
| 23 global db | 21 return MySQLdb.connect( | 
| 24 if not db: | 22 user=user, | 
| 25 db = MySQLdb.connect( | 23 passwd=password, | 
| 26 user=user, | 24 db=database, | 
| 27 passwd=password, | 25 use_unicode=True, charset="utf8" | 
| 28 db=database, | 26 ) | 
| 29 use_unicode=True, charset="utf8" | |
| 30 ) | |
| 31 return db | |
| 32 | 27 | 
| 33 def disconnect(): | 28 def query(db, sql, *params, **kwargs): | 
| 34 global db | 29 """ | 
| 35 if db: | 30 Executes the query given by the provided SQL and returns the results. | 
| 36 db.close() | 31 If dict_result keyword argument is provided + True the results will be | 
| 37 db = None | 32 returned as a tuple of dictionaries, otherwise a tuple of tuples. | 
| 33 """ | |
| 34 dict_result=kwargs.pop('dict_result', False) | |
| 38 | 35 | 
| 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 | |
| 49 try: | 36 try: | 
| 50 if dict_result: | 37 if dict_result: | 
| 51 cursor = db.cursor(MySQLdb.cursors.DictCursor) | 38 cursor = db.cursor(MySQLdb.cursors.DictCursor) | 
| 52 else: | 39 else: | 
| 53 cursor = db.cursor() | 40 cursor = db.cursor() | 
| 54 cursor.execute(sql) | 41 cursor.execute(sql, params) | 
| 55 results = cursor.fetchall() | 42 results = cursor.fetchall() | 
| 56 finally: | 43 finally: | 
| 57 if cursor: | 44 if cursor: | 
| 58 cursor.close() | 45 cursor.close() | 
| 59 return results | 46 return results | 
| 60 | 47 | 
| 61 def write(sql): | 48 def write(db, queries): | 
| 62 """ | 49 """ | 
| 63 This writes a given SQL string or iterator of SQL strings to the database. | 50 This writes a given SQL string or iterator of tuples containing SQL | 
| 
 
Sebastian Noack
2015/02/17 14:59:17
Iterators (in Python) are objects that implement t
 
kzar
2015/02/24 18:05:11
Done.
 
 | |
| 64 All given SQL will be run as one transaction and rolled back on error. | 51 strings and any required parameters to the database. All queries will | 
| 52 be run as one transaction and rolled back on error. | |
| 65 """ | 53 """ | 
| 66 global db | 54 if isinstance(queries, str): | 
| 67 | 55 queries = ((queries,),) | 
| 68 if isinstance(sql, str): | |
| 69 sql = [sql] | |
| 70 | 56 | 
| 71 try: | 57 try: | 
| 72 # Commit the changes | |
| 73 cursor = db.cursor() | 58 cursor = db.cursor() | 
| 74 for query in sql: | 59 try: | 
| 75 if query: | 60 for query in queries: | 
| 76 [cursor.execute(s) for s in query.split(";") if s] | 61 sql, params = query[0], query[1:] | 
| 
 
Sebastian Noack
2015/02/17 14:59:17
I'd prefer to use a two-dimensional tuple here and
 
kzar
2015/02/24 18:05:11
I like doing it this way for db.query and db.write
 
Sebastian Noack
2015/02/26 16:39:25
I don't agree. So I am leaving it up to Wladimir.
 
 | |
| 77 db.commit() | 62 cursor.execute(sql, params) | 
| 63 db.commit() | |
| 64 finally: | |
| 65 cursor.close() | |
| 78 except MySQLdb.Error: | 66 except MySQLdb.Error: | 
| 79 # On error roll them back | |
| 80 if db: | 67 if db: | 
| 81 db.rollback() | 68 db.rollback() | 
| 82 raise | 69 raise | 
| 83 finally: | |
| 84 if cursor: | |
| 85 cursor.close() | |
| OLD | NEW |