Development of an internal social media platform with personalised dashboards for students
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

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