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.

24 lines
710 B

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