Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import abc 

2from typing import Any, Dict 

3 

4from pystratum_common.wrapper.Wrapper import Wrapper 

5 

6 

7class RowsWithIndexWrapper(Wrapper): 

8 """ 

9 Parent class wrapper wrapper method generator for stored procedures whose result set must be returned using tree 

10 structure using a combination of non-unique columns. 

11 """ 

12 

13 # ------------------------------------------------------------------------------------------------------------------ 

14 def _return_type_hint(self) -> str: 

15 """ 

16 Returns the return type hint of the wrapper method. 

17 

18 :rtype: str 

19 """ 

20 return 'Dict' 

21 

22 # ------------------------------------------------------------------------------------------------------------------ 

23 def _get_docstring_return_type(self) -> str: 

24 """ 

25 Returns the return type of the wrapper methods the be used in the docstring. 

26 

27 :rtype: str 

28 """ 

29 return 'dict' 

30 

31 # ------------------------------------------------------------------------------------------------------------------ 

32 @abc.abstractmethod 

33 def _write_execute_rows(self, routine: Dict[str, Any]) -> None: 

34 raise NotImplementedError() 

35 

36 # ------------------------------------------------------------------------------------------------------------------ 

37 def _write_result_handler(self, routine: Dict[str, Any]) -> None: 

38 """ 

39 Generates code for calling the stored routine in the wrapper method. 

40 """ 

41 self._write_line('ret = {}') 

42 self._write_execute_rows(routine) 

43 self._write_line('for row in rows:') 

44 

45 num_of_dict = len(routine['columns']) 

46 

47 i = 0 

48 while i < num_of_dict: 

49 value = "row['{0!s}']".format(routine['columns'][i]) 

50 

51 stack = '' 

52 j = 0 

53 while j < i: 

54 stack += "[row['{0!s}']]".format(routine['columns'][j]) 

55 j += 1 

56 line = 'if {0!s} in ret{1!s}:'.format(value, stack) 

57 self._write_line(line) 

58 i += 1 

59 

60 line = 'ret' 

61 for column_name in routine['columns']: 

62 line += "[row['{0!s}']]".format(column_name) 

63 line += '.append(row)' 

64 self._write_line(line) 

65 self._indent_level_down() 

66 

67 i = num_of_dict 

68 while i > 0: 

69 self._write_line('else:') 

70 

71 part1 = '' 

72 j = 0 

73 while j < i - 1: 

74 part1 += "[row['{0!s}']]".format(routine['columns'][j]) 

75 j += 1 

76 part1 += "[row['{0!s}']]".format(routine['columns'][j]) 

77 

78 part2 = '' 

79 j = i - 1 

80 while j < num_of_dict: 

81 if j + 1 != i: 

82 part2 += "{{row['{0!s}']: ".format(routine['columns'][j]) 

83 j += 1 

84 part2 += "[row]" + ('}' * (num_of_dict - i)) 

85 

86 line = "ret{0!s} = {1!s}".format(part1, part2) 

87 self._write_line(line) 

88 self._indent_level_down() 

89 if i > 1: 

90 self._indent_level_down() 

91 i -= 1 

92 

93 self._write_line() 

94 self._write_line('return ret') 

95 

96# ----------------------------------------------------------------------------------------------------------------------