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.

output.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2018 the sqlparse authors and contributors
  4. # <see AUTHORS file>
  5. #
  6. # This module is part of python-sqlparse and is released under
  7. # the BSD License: https://opensource.org/licenses/BSD-3-Clause
  8. from sqlparse import sql, tokens as T
  9. from sqlparse.compat import text_type
  10. class OutputFilter(object):
  11. varname_prefix = ''
  12. def __init__(self, varname='sql'):
  13. self.varname = self.varname_prefix + varname
  14. self.count = 0
  15. def _process(self, stream, varname, has_nl):
  16. raise NotImplementedError
  17. def process(self, stmt):
  18. self.count += 1
  19. if self.count > 1:
  20. varname = u'{f.varname}{f.count}'.format(f=self)
  21. else:
  22. varname = self.varname
  23. has_nl = len(text_type(stmt).strip().splitlines()) > 1
  24. stmt.tokens = self._process(stmt.tokens, varname, has_nl)
  25. return stmt
  26. class OutputPythonFilter(OutputFilter):
  27. def _process(self, stream, varname, has_nl):
  28. # SQL query assignation to varname
  29. if self.count > 1:
  30. yield sql.Token(T.Whitespace, '\n')
  31. yield sql.Token(T.Name, varname)
  32. yield sql.Token(T.Whitespace, ' ')
  33. yield sql.Token(T.Operator, '=')
  34. yield sql.Token(T.Whitespace, ' ')
  35. if has_nl:
  36. yield sql.Token(T.Operator, '(')
  37. yield sql.Token(T.Text, "'")
  38. # Print the tokens on the quote
  39. for token in stream:
  40. # Token is a new line separator
  41. if token.is_whitespace and '\n' in token.value:
  42. # Close quote and add a new line
  43. yield sql.Token(T.Text, " '")
  44. yield sql.Token(T.Whitespace, '\n')
  45. # Quote header on secondary lines
  46. yield sql.Token(T.Whitespace, ' ' * (len(varname) + 4))
  47. yield sql.Token(T.Text, "'")
  48. # Indentation
  49. after_lb = token.value.split('\n', 1)[1]
  50. if after_lb:
  51. yield sql.Token(T.Whitespace, after_lb)
  52. continue
  53. # Token has escape chars
  54. elif "'" in token.value:
  55. token.value = token.value.replace("'", "\\'")
  56. # Put the token
  57. yield sql.Token(T.Text, token.value)
  58. # Close quote
  59. yield sql.Token(T.Text, "'")
  60. if has_nl:
  61. yield sql.Token(T.Operator, ')')
  62. class OutputPHPFilter(OutputFilter):
  63. varname_prefix = '$'
  64. def _process(self, stream, varname, has_nl):
  65. # SQL query assignation to varname (quote header)
  66. if self.count > 1:
  67. yield sql.Token(T.Whitespace, '\n')
  68. yield sql.Token(T.Name, varname)
  69. yield sql.Token(T.Whitespace, ' ')
  70. if has_nl:
  71. yield sql.Token(T.Whitespace, ' ')
  72. yield sql.Token(T.Operator, '=')
  73. yield sql.Token(T.Whitespace, ' ')
  74. yield sql.Token(T.Text, '"')
  75. # Print the tokens on the quote
  76. for token in stream:
  77. # Token is a new line separator
  78. if token.is_whitespace and '\n' in token.value:
  79. # Close quote and add a new line
  80. yield sql.Token(T.Text, ' ";')
  81. yield sql.Token(T.Whitespace, '\n')
  82. # Quote header on secondary lines
  83. yield sql.Token(T.Name, varname)
  84. yield sql.Token(T.Whitespace, ' ')
  85. yield sql.Token(T.Operator, '.=')
  86. yield sql.Token(T.Whitespace, ' ')
  87. yield sql.Token(T.Text, '"')
  88. # Indentation
  89. after_lb = token.value.split('\n', 1)[1]
  90. if after_lb:
  91. yield sql.Token(T.Whitespace, after_lb)
  92. continue
  93. # Token has escape chars
  94. elif '"' in token.value:
  95. token.value = token.value.replace('"', '\\"')
  96. # Put the token
  97. yield sql.Token(T.Text, token.value)
  98. # Close quote
  99. yield sql.Token(T.Text, '"')
  100. yield sql.Token(T.Punctuation, ';')