[FIX] sql_db: port fix from psycopg/psycopg2#459

NUL characters must not be used in query parameters,
as they will be ignored by libpq, being end-of-string
characters.

Preventing NULs avoids unexpected results from
queries. It is only necessary with psycopg2
versions before 2.7, which includes the upstream
fix.
This commit is contained in:
Olivier Dony 2017-06-13 17:17:45 +02:00
parent 46263eb398
commit eb8d919015
No known key found for this signature in database
GPG Key ID: CD556E25E8A6D0D4
1 changed files with 13 additions and 0 deletions

View File

@ -63,6 +63,19 @@ psycopg2.extensions.register_type(psycopg2.extensions.new_type((700, 701, 1700,)
import tools
from tools import parse_version as pv
if pv(psycopg2.__version__) < pv('2.7'):
from psycopg2._psycopg import QuotedString
def adapt_string(adapted):
"""Python implementation of psycopg/psycopg2#459 from v2.7"""
if '\x00' in adapted:
raise ValueError("A string literal cannot contain NUL (0x00) characters.")
return QuotedString(adapted)
psycopg2.extensions.register_adapter(str, adapt_string)
psycopg2.extensions.register_adapter(unicode, adapt_string)
from tools.func import frame_codeinfo
from datetime import datetime as mdt
from datetime import timedelta