| 221 | | class PgBYTEA_Adapter(adapters.Pickler): |
|---|
| 222 | | |
|---|
| 223 | | def push(self, value, dbtype): |
|---|
| 224 | | if value is None: |
|---|
| 225 | | return 'NULL' |
|---|
| 226 | | # See http://www.postgresql.org/docs/8.1/interactive/datatype-binary.html |
|---|
| 227 | | value = pickle.dumps(value, 2) |
|---|
| 228 | | def repl(char): |
|---|
| 229 | | o = ord(char) |
|---|
| 230 | | if o <= 31 or o == 39 or o == 92 or o >= 127: |
|---|
| 231 | | return r"\\%03d" % int(oct(o)) |
|---|
| 232 | | return char |
|---|
| 233 | | return "'%s'::bytea" % "".join(map(repl, value)) |
|---|
| 234 | | |
|---|
| 235 | | |
|---|
| | 325 | |
|---|
| | 326 | # ---------------------------- BYTEA Adapters ---------------------------- # |
|---|
| | 327 | |
|---|
| | 328 | |
|---|
| | 329 | class Pg_str_to_BYTEA(Pg_str_to_VARCHAR): |
|---|
| | 330 | """Python str to PostgreSQL bytea adapter. |
|---|
| | 331 | |
|---|
| | 332 | For the most part, Postgres bytea works like Python's str: a sequence |
|---|
| | 333 | of bytes. Certain bytes have to be octal-escaped for consumption by PG. |
|---|
| | 334 | |
|---|
| | 335 | See http://www.postgresql.org/docs/8.1/interactive/datatype-binary.html |
|---|
| | 336 | """ |
|---|
| | 337 | |
|---|
| | 338 | def push(self, value, dbtype): |
|---|
| | 339 | if value is None: |
|---|
| | 340 | return 'NULL' |
|---|
| | 341 | def repl(char): |
|---|
| | 342 | o = ord(char) |
|---|
| | 343 | if o <= 31 or o == 39 or o == 92 or o >= 127: |
|---|
| | 344 | return r"\\%03d" % int(oct(o)) |
|---|
| | 345 | return char |
|---|
| | 346 | return "'%s'::bytea" % "".join(map(repl, value)) |
|---|
| | 347 | |
|---|
| | 348 | |
|---|
| | 349 | class Pg_unicode_to_BYTEA(Pg_unicode_to_VARCHAR): |
|---|
| | 350 | """Python unicode to PostgreSQL bytea adapter. |
|---|
| | 351 | |
|---|
| | 352 | For the most part, Postgres bytea works like Python's str: a sequence |
|---|
| | 353 | of bytes. Certain bytes have to be octal-escaped for consumption by PG. |
|---|
| | 354 | |
|---|
| | 355 | See http://www.postgresql.org/docs/8.1/interactive/datatype-binary.html |
|---|
| | 356 | """ |
|---|
| | 357 | |
|---|
| | 358 | def push(self, value, dbtype): |
|---|
| | 359 | if value is None: |
|---|
| | 360 | return 'NULL' |
|---|
| | 361 | if not isinstance(value, str): |
|---|
| | 362 | value = value.encode(dbtype.encoding) |
|---|
| | 363 | def repl(char): |
|---|
| | 364 | o = ord(char) |
|---|
| | 365 | if o <= 31 or o == 39 or o == 92 or o >= 127: |
|---|
| | 366 | return r"\\%03d" % int(oct(o)) |
|---|
| | 367 | return char |
|---|
| | 368 | return "'%s'::bytea" % "".join(map(repl, value)) |
|---|
| | 369 | |
|---|
| | 370 | |
|---|
| | 371 | class PgBYTEA_Pickler(PgPickler): |
|---|
| | 372 | """Python object to PostgreSQL bytea adapter. |
|---|
| | 373 | |
|---|
| | 374 | For the most part, Postgres bytea works like Python's str: a sequence |
|---|
| | 375 | of bytes. Certain bytes have to be octal-escaped for consumption by PG. |
|---|
| | 376 | |
|---|
| | 377 | See http://www.postgresql.org/docs/8.1/interactive/datatype-binary.html |
|---|
| | 378 | """ |
|---|
| | 379 | |
|---|
| | 380 | def push(self, value, dbtype): |
|---|
| | 381 | if value is None: |
|---|
| | 382 | return 'NULL' |
|---|
| | 383 | |
|---|
| | 384 | value = pickle.dumps(value, 2) |
|---|
| | 385 | |
|---|
| | 386 | def repl(char): |
|---|
| | 387 | o = ord(char) |
|---|
| | 388 | if o <= 31 or o == 39 or o == 92 or o >= 127: |
|---|
| | 389 | return r"\\%03d" % int(oct(o)) |
|---|
| | 390 | return char |
|---|
| | 391 | return "'%s'::bytea" % "".join(map(repl, value)) |
|---|