|
1 |
| -"""Executing raw SQL queries against an SQLAlchemy engine.""" |
| 1 | +"""Execute raw SQL queries against an SQLAlchemy engine.""" |
2 | 2 | from typing import List, Optional
|
3 | 3 |
|
4 | 4 | from sqlalchemy import engine, text
|
5 | 5 |
|
| 6 | +from sqlalchemy_tutorial.logger import LOGGER |
| 7 | + |
6 | 8 |
|
7 | 9 | def fetch_job_listings(db: engine) -> Optional[List[dict]]:
|
8 |
| - """Select rows from database and parse as list of dicts.""" |
| 10 | + """ |
| 11 | + Select rows from database and parse as list of dicts. |
| 12 | +
|
| 13 | + :param db: Engine object representing a SQL database. |
| 14 | + :type db: engine |
| 15 | +
|
| 16 | + :returns: Optional[List[dict]] |
| 17 | + """ |
9 | 18 | results = db.execute(
|
10 | 19 | "SELECT job_id, agency, business_title, \
|
11 | 20 | salary_range_from, salary_range_to \
|
12 | 21 | FROM nyc_jobs ORDER BY RAND();"
|
13 | 22 | )
|
14 |
| - print(f"Selected {results.rowcount} rows.") |
15 | 23 | rows = [dict(row) for row in results.fetchall()]
|
| 24 | + LOGGER.info( |
| 25 | + f"Selected {results.rowcount} rows: \ |
| 26 | + {rows}" |
| 27 | + ) |
16 | 28 | return rows
|
17 | 29 |
|
18 | 30 |
|
19 | 31 | def update_job_listing(db: engine) -> Optional[List[dict]]:
|
20 |
| - """Update row in database with problematic characters escaped.""" |
| 32 | + """ |
| 33 | + Update row in database with problematic characters escaped. |
| 34 | +
|
| 35 | + :param db: Engine object representing a SQL database. |
| 36 | + :type db: engine |
| 37 | +
|
| 38 | + :returns: Optional[List[dict]] |
| 39 | + """ |
21 | 40 | result = db.execute(
|
22 | 41 | text(
|
23 | 42 | "UPDATE nyc_jobs SET business_title = 'Senior QA Scapegoat 🏆', \
|
24 | 43 | job_category = 'Information? <>!#%%Technology!%%#^&%* & Telecom' \
|
25 | 44 | WHERE job_id = 229837;"
|
26 | 45 | )
|
27 | 46 | )
|
| 47 | + LOGGER.info( |
| 48 | + f"Selected {result.rowcount} row: \ |
| 49 | + {result}" |
| 50 | + ) |
28 | 51 | return result.rowcount
|
0 commit comments