Execute the sql in the database and return the results.
(self, statement: str)
| 344 | return None |
| 345 | |
| 346 | def run(self, statement: str) -> Generator[SQLResult, None, None]: |
| 347 | """Execute the sql in the database and return the results.""" |
| 348 | |
| 349 | # Remove spaces and EOL |
| 350 | statement = statement.strip() |
| 351 | if not statement: # Empty string |
| 352 | yield SQLResult() |
| 353 | |
| 354 | # Split the sql into separate queries and run each one. |
| 355 | # Unless it's saving a favorite query, in which case we |
| 356 | # want to save them all together. |
| 357 | if statement.startswith(("\\fs", "/fs")): |
| 358 | components: Iterable[str] = [statement] |
| 359 | else: |
| 360 | components = iocommands.split_queries(statement) |
| 361 | |
| 362 | for sql in components: |
| 363 | # \G is treated specially since we have to set the expanded output. |
| 364 | if sql.endswith("\\G"): |
| 365 | iocommands.set_expanded_output(True) |
| 366 | sql = sql[:-2].strip() |
| 367 | # \g is treated specially since we might want collapsed output when |
| 368 | # auto vertical output is enabled |
| 369 | elif sql.endswith('\\g'): |
| 370 | iocommands.set_expanded_output(False) |
| 371 | iocommands.set_forced_horizontal_output(True) |
| 372 | sql = sql[:-2].strip() |
| 373 | |
| 374 | assert isinstance(self.conn, Connection) |
| 375 | cur = self.conn.cursor() |
| 376 | try: # Special command |
| 377 | _logger.debug("Trying a dbspecial command. sql: %r", sql) |
| 378 | yield from execute(cur, sql) |
| 379 | except CommandNotFound: # Regular SQL |
| 380 | _logger.debug("Regular sql statement. sql: %r", sql) |
| 381 | cur.execute(sql) |
| 382 | while True: |
| 383 | yield self.get_result(cur) |
| 384 | |
| 385 | # PyMySQL returns an extra, empty result set with stored |
| 386 | # procedures. We skip it (rowcount is zero and no |
| 387 | # description). |
| 388 | if not cur.nextset() or (not cur.rowcount and cur.description is None): |
| 389 | break |
| 390 | |
| 391 | def get_result(self, cursor: Cursor) -> SQLResult: |
| 392 | """Get the current result's data from the cursor.""" |
no test coverage detected