You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
696 B

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Therp BV <http://therp.nl>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from psycopg2.extensions import ISQLQuote
  5. class IdentifierAdapter(ISQLQuote):
  6. def __init__(self, identifier, quote=True):
  7. self.quote = quote
  8. self.identifier = identifier
  9. def __conform__(self, protocol):
  10. if protocol == ISQLQuote:
  11. return self
  12. def getquoted(self):
  13. def is_identifier_char(c):
  14. return c.isalnum() or c in ['_', '$']
  15. format_string = '"%s"'
  16. if not self.quote:
  17. format_string = '%s'
  18. return format_string % filter(is_identifier_char, self.identifier)