Coverage for pystratum_common/wrapper/RowsWithIndexWrapper.py: 0%

56 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-13 08:46 +0200

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 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 return 'Dict' 

19 

20 # ------------------------------------------------------------------------------------------------------------------ 

21 def _get_docstring_return_type(self) -> str: 

22 """ 

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

24 """ 

25 return 'dict' 

26 

27 # ------------------------------------------------------------------------------------------------------------------ 

28 @abc.abstractmethod 

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

30 raise NotImplementedError() 

31 

32 # ------------------------------------------------------------------------------------------------------------------ 

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

34 """ 

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

36 """ 

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

38 self._write_execute_rows(routine) 

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

40 

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

42 

43 i = 0 

44 while i < num_of_dict: 

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

46 

47 stack = '' 

48 j = 0 

49 while j < i: 

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

51 j += 1 

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

53 self._write_line(line) 

54 i += 1 

55 

56 line = 'ret' 

57 for column_name in routine['columns']: 

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

59 line += '.append(row)' 

60 self._write_line(line) 

61 self._indent_level_down() 

62 

63 i = num_of_dict 

64 while i > 0: 

65 self._write_line('else:') 

66 

67 part1 = '' 

68 j = 0 

69 while j < i - 1: 

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

71 j += 1 

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

73 

74 part2 = '' 

75 j = i - 1 

76 while j < num_of_dict: 

77 if j + 1 != i: 

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

79 j += 1 

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

81 

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

83 self._write_line(line) 

84 self._indent_level_down() 

85 if i > 1: 

86 self._indent_level_down() 

87 i -= 1 

88 

89 self._write_line() 

90 self._write_line('return ret') 

91 

92# ----------------------------------------------------------------------------------------------------------------------