Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/sqlparse/filters/output.py: 16%

73 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1# 

2# Copyright (C) 2009-2020 the sqlparse authors and contributors 

3# <see AUTHORS file> 

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 

8from sqlparse import sql, tokens as T 

9 

10 

11class OutputFilter: 

12 varname_prefix = '' 

13 

14 def __init__(self, varname='sql'): 

15 self.varname = self.varname_prefix + varname 

16 self.count = 0 

17 

18 def _process(self, stream, varname, has_nl): 

19 raise NotImplementedError 

20 

21 def process(self, stmt): 

22 self.count += 1 

23 if self.count > 1: 

24 varname = '{f.varname}{f.count}'.format(f=self) 

25 else: 

26 varname = self.varname 

27 

28 has_nl = len(str(stmt).strip().splitlines()) > 1 

29 stmt.tokens = self._process(stmt.tokens, varname, has_nl) 

30 return stmt 

31 

32 

33class OutputPythonFilter(OutputFilter): 

34 def _process(self, stream, varname, has_nl): 

35 # SQL query assignation to varname 

36 if self.count > 1: 

37 yield sql.Token(T.Whitespace, '\n') 

38 yield sql.Token(T.Name, varname) 

39 yield sql.Token(T.Whitespace, ' ') 

40 yield sql.Token(T.Operator, '=') 

41 yield sql.Token(T.Whitespace, ' ') 

42 if has_nl: 

43 yield sql.Token(T.Operator, '(') 

44 yield sql.Token(T.Text, "'") 

45 

46 # Print the tokens on the quote 

47 for token in stream: 

48 # Token is a new line separator 

49 if token.is_whitespace and '\n' in token.value: 

50 # Close quote and add a new line 

51 yield sql.Token(T.Text, " '") 

52 yield sql.Token(T.Whitespace, '\n') 

53 

54 # Quote header on secondary lines 

55 yield sql.Token(T.Whitespace, ' ' * (len(varname) + 4)) 

56 yield sql.Token(T.Text, "'") 

57 

58 # Indentation 

59 after_lb = token.value.split('\n', 1)[1] 

60 if after_lb: 

61 yield sql.Token(T.Whitespace, after_lb) 

62 continue 

63 

64 # Token has escape chars 

65 elif "'" in token.value: 

66 token.value = token.value.replace("'", "\\'") 

67 

68 # Put the token 

69 yield sql.Token(T.Text, token.value) 

70 

71 # Close quote 

72 yield sql.Token(T.Text, "'") 

73 if has_nl: 

74 yield sql.Token(T.Operator, ')') 

75 

76 

77class OutputPHPFilter(OutputFilter): 

78 varname_prefix = '$' 

79 

80 def _process(self, stream, varname, has_nl): 

81 # SQL query assignation to varname (quote header) 

82 if self.count > 1: 

83 yield sql.Token(T.Whitespace, '\n') 

84 yield sql.Token(T.Name, varname) 

85 yield sql.Token(T.Whitespace, ' ') 

86 if has_nl: 

87 yield sql.Token(T.Whitespace, ' ') 

88 yield sql.Token(T.Operator, '=') 

89 yield sql.Token(T.Whitespace, ' ') 

90 yield sql.Token(T.Text, '"') 

91 

92 # Print the tokens on the quote 

93 for token in stream: 

94 # Token is a new line separator 

95 if token.is_whitespace and '\n' in token.value: 

96 # Close quote and add a new line 

97 yield sql.Token(T.Text, ' ";') 

98 yield sql.Token(T.Whitespace, '\n') 

99 

100 # Quote header on secondary lines 

101 yield sql.Token(T.Name, varname) 

102 yield sql.Token(T.Whitespace, ' ') 

103 yield sql.Token(T.Operator, '.=') 

104 yield sql.Token(T.Whitespace, ' ') 

105 yield sql.Token(T.Text, '"') 

106 

107 # Indentation 

108 after_lb = token.value.split('\n', 1)[1] 

109 if after_lb: 

110 yield sql.Token(T.Whitespace, after_lb) 

111 continue 

112 

113 # Token has escape chars 

114 elif '"' in token.value: 

115 token.value = token.value.replace('"', '\\"') 

116 

117 # Put the token 

118 yield sql.Token(T.Text, token.value) 

119 

120 # Close quote 

121 yield sql.Token(T.Text, '"') 

122 yield sql.Token(T.Punctuation, ';')