Change placeholders in a query from %s to $n if testing a raw cursor
(cur: Any, query: str)
| 27 | |
| 28 | |
| 29 | def ph(cur: Any, query: str) -> str: |
| 30 | """Change placeholders in a query from %s to $n if testing a raw cursor""" |
| 31 | from psycopg.raw_cursor import RawCursorMixin |
| 32 | |
| 33 | if not isinstance(cur, RawCursorMixin): |
| 34 | return query |
| 35 | |
| 36 | if "%(" in query: |
| 37 | pytest.skip("RawCursor only supports positional placeholders") |
| 38 | |
| 39 | n = 1 |
| 40 | |
| 41 | def s(m: re.Match[str]) -> str: |
| 42 | nonlocal n |
| 43 | rv = f"${n}" |
| 44 | n += 1 |
| 45 | return rv |
| 46 | |
| 47 | return re.sub(r"(?<!%)(%[bst])", s, query) |
| 48 | |
| 49 | |
| 50 | def my_row_factory( |
no outgoing calls
no test coverage detected