Serialize a dictionary into an hstore literal. Keys and values must both be strings (except None for values).
(val: _HSTORE_VAL)
| 402 | |
| 403 | |
| 404 | def _serialize_hstore(val: _HSTORE_VAL) -> str: |
| 405 | """Serialize a dictionary into an hstore literal. Keys and values must |
| 406 | both be strings (except None for values). |
| 407 | |
| 408 | """ |
| 409 | |
| 410 | def esc(s: Optional[str], position: str) -> str: |
| 411 | if position == "value" and s is None: |
| 412 | return "NULL" |
| 413 | elif isinstance(s, str): |
| 414 | return '"%s"' % s.replace("\\", "\\\\").replace('"', r"\"") |
| 415 | else: |
| 416 | raise ValueError( |
| 417 | "%r in %s position is not a string." % (s, position) |
| 418 | ) |
| 419 | |
| 420 | return ", ".join( |
| 421 | "%s=>%s" % (esc(k, "key"), esc(v, "value")) for k, v in val.items() |
| 422 | ) |