Create a cursor and execute the SQL request in 'query'. Return a list of tuples by default e.g. [(valueA, valueB,),(valueAA,valueBB,), etc] If ld != [], load in a dictionary all results according to ld (column names) and return a dictionary e.g. [{'column1':'valueA',
(self, query=None, ld=[], isquery=True, getColumnNames=False, stringOnly=False)
| 190 | logging.debug("Impossible to close the connection to the database: {0}".format(e)) |
| 191 | |
| 192 | def __execThisQuery__(self, query=None, ld=[], isquery=True, getColumnNames=False, stringOnly=False): |
| 193 | ''' |
| 194 | Create a cursor and execute the SQL request in 'query'. |
| 195 | Return a list of tuples by default e.g. [(valueA, valueB,),(valueAA,valueBB,), etc] |
| 196 | If ld != [], load in a dictionary all results according to ld (column names) and return a dictionary e.g. |
| 197 | [{'column1':'valueA', 'column2':'valueB'}, etc] |
| 198 | If getColumns is enabled (not by default), returns columns in first results. Incompatible with ld otpion. |
| 199 | If stringOnly enabled, try to convert all columns as string when required (e.g. datetime) or convert to repr() |
| 200 | if impossible. Incompatible with ld option. |
| 201 | ''' |
| 202 | results = [] |
| 203 | if ld!=[] and (getColumnNames==True or stringOnly==True): |
| 204 | logging.error("INTERNAL BUG: ld option is not compatible with getColumnNames or stringOnly in __execThisQuery__(). Fix that quickly!") |
| 205 | cursor = self.args['dbcon'].cursor() |
| 206 | try: |
| 207 | if self.args['show_sql_requests'] == True: logging.info("SQL request executed: {0}".format(query)) |
| 208 | cursor.execute(query) |
| 209 | except Exception as e: |
| 210 | logging.info("Impossible to execute the query {0}: '{1}'".format(repr(query), self.cleanError(e))) |
| 211 | if self.ERROR_NOT_CONNECTED in str(e): |
| 212 | status = self.__retryConnect__(nbTry=3) |
| 213 | if status == None : |
| 214 | return ErrorSQLRequest("Disconnected. Impossible to re-establish a connection to the database server !") |
| 215 | else : |
| 216 | return self.__execThisQuery__(query=query,ld=ld,isquery=isquery) |
| 217 | else : |
| 218 | return ErrorSQLRequest(e) |
| 219 | if isquery==True : |
| 220 | try : |
| 221 | cursor.arraysize = 256 |
| 222 | results = cursor.fetchall() |
| 223 | except Exception as e: |
| 224 | if self.cleanError(e) == "not a query": |
| 225 | logging.debug("{0} is not a query, returning [] as result".format(query)) |
| 226 | return [()] |
| 227 | else: |
| 228 | logging.warning("Impossible to fetch all the rows of the query {0}: `{1}`".format(query, self.cleanError(e))) |
| 229 | return ErrorSQLRequest(e) |
| 230 | if stringOnly == True: |
| 231 | logging.debug("Converting all columns as string if required...") |
| 232 | resultsAsString = [] |
| 233 | for aR in results: |
| 234 | aResultAsString = [] |
| 235 | for v in aR: |
| 236 | vString = None |
| 237 | try: |
| 238 | vString = str(v) |
| 239 | except Exception: |
| 240 | logging.debug("Impossible to convert {0} as string for request {1}".format(repr(aR),repr(query))) |
| 241 | vString = repr(v) |
| 242 | aResultAsString.append(vString) |
| 243 | resultsAsString.append(aResultAsString) |
| 244 | results = resultsAsString |
| 245 | if getColumnNames==True: |
| 246 | logging.debug("Extracting column names and returning them in results...") |
| 247 | columnNames = [row[0] for row in cursor.description] |
| 248 | results = [tuple(columnNames)] + results |
| 249 | else : |
no test coverage detected