@@ -35,22 +35,21 @@ def connect(self):
3535
3636 def execute_query (self , query , dataframe = False ):
3737 """
38- Executes an arbitrary query against the given database connection.
38+ Executes an arbitrary read query against the given database connection.
3939
4040 Parameters
4141 ----------
4242 query: str
43- The query to execute
43+ The query to execute, assumed to be a read query
4444 dataframe: bool, optional
4545 Whether the data will be returned as a pandas DataFrame. Defaults
4646 to False, which means the data is returned as a list of tuples.
4747
4848 Returns
4949 -------
5050 None or sequence
51- None if is_write_query is True. A list of tuples or a pandas
52- DataFrame (based on the dataframe input) if is_write_query is
53- False.
51+ A list of tuples or a pandas DataFrame (based on the `dataframe`
52+ input)
5453 """
5554 self .logger .info ('Querying {} database' .format (self .database ))
5655 self .logger .debug ('Executing query {}' .format (query ))
@@ -82,7 +81,12 @@ def execute_transaction(self, queries):
8281 ----------
8382 queries: list<tuple>
8483 A list of tuples containing a query and the values to be used if
85- the query is parameterized (or None if it's not)
84+ the query is parameterized (or None if it's not). The values can
85+ be for a single insert query -- e.g. execute_transaction(
86+ "INSERT INTO x VALUES (%s, %s)", (1, "a"))
87+ or for multiple -- e.g execute_transaction(
88+ "INSERT INTO x VALUES (%s, %s)", [(1, "a"), (2, "b")])
89+
8690 """
8791 self .logger .info ('Executing transaction against {} database' .format (
8892 self .database ))
@@ -91,7 +95,13 @@ def execute_transaction(self, queries):
9195 cursor .execute ('BEGIN TRANSACTION;' )
9296 for query in queries :
9397 self .logger .debug ('Executing query {}' .format (query ))
94- cursor .execute (query [0 ], query [1 ])
98+ if query [1 ] is not None and all (
99+ isinstance (el , tuple ) or isinstance (el , list )
100+ for el in query [1 ]
101+ ):
102+ cursor .executemany (query [0 ], query [1 ])
103+ else :
104+ cursor .execute (query [0 ], query [1 ])
95105 cursor .execute ('END TRANSACTION;' )
96106 self .conn .commit ()
97107 except Exception as e :
0 commit comments