Coverage for rdflib_ocdm/counter_handler/sqlite_counter_handler.py: 89%

46 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-11-01 22:02 +0000

1#!/usr/bin/python 

2# -*- coding: utf-8 -*- 

3# Copyright (c) 2023, Arcangelo Massari <arcangelo.massari@unibo.it> 

4# 

5# Permission to use, copy, modify, and/or distribute this software for any purpose 

6# with or without fee is hereby granted, provided that the above copyright notice 

7# and this permission notice appear in all copies. 

8# 

9# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH 

10# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 

11# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, 

12# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 

13# DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 

14# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 

15# SOFTWARE. 

16 

17import sqlite3 

18import urllib.parse 

19 

20from rdflib_ocdm.counter_handler.counter_handler import CounterHandler 

21 

22 

23class SqliteCounterHandler(CounterHandler): 

24 """A concrete implementation of the ``CounterHandler`` interface that persistently stores 

25 the counter values within a SQLite database.""" 

26 

27 def __init__(self, database: str) -> None: 

28 """ 

29 Constructor of the ``SqliteCounterHandler`` class. 

30 

31 :param database: The name of the database 

32 :type info_dir: str 

33 """ 

34 sqlite3.threadsafety = 3 

35 self.con = sqlite3.connect(database, check_same_thread=False) 

36 self.cur = self.con.cursor() 

37 self.cur.execute("""CREATE TABLE IF NOT EXISTS info( 

38 entity TEXT PRIMARY KEY,  

39 count INTEGER)""") 

40 

41 def set_counter(self, new_value: int, entity_name: str) -> None: 

42 """ 

43 It allows to set the counter value of provenance entities. 

44 

45 :param new_value: The new counter value to be set 

46 :type new_value: int 

47 :param entity_name: The entity name 

48 :type entity_name: str 

49 :raises ValueError: if ``new_value`` is a negative integer. 

50 :return: None 

51 """ 

52 entity_name = urllib.parse.quote((str(entity_name))) 

53 if new_value < 0: 

54 raise ValueError("new_value must be a non negative integer!") 

55 self.cur.execute(f"INSERT OR REPLACE INTO info (entity, count) VALUES ('{entity_name}', {new_value})") 

56 self.con.commit() 

57 

58 def read_counter(self, entity_name: str) -> int: 

59 """ 

60 It allows to read the counter value of provenance entities. 

61 

62 :param entity_name: The entity name 

63 :type entity_name: str 

64 :return: The requested counter value. 

65 """ 

66 entity_name = urllib.parse.quote(str(entity_name)) 

67 result = self.cur.execute(f"SELECT count FROM info WHERE entity='{entity_name}'") 

68 rows = result.fetchall() 

69 if len(rows) == 1: 

70 return rows[0][0] 

71 elif len(rows) == 0: 

72 return 0 

73 else: 

74 raise(Exception("There is more than one counter for this entity. The databse id broken")) 

75 

76 def increment_counter(self, entity_name: str) -> int: 

77 """ 

78 It allows to increment the counter value of graph and provenance entities by one unit. 

79 

80 :param entity_name: The entity name 

81 :type entity_name: str 

82 :return: The newly-updated (already incremented) counter value. 

83 """ 

84 cur_count = self.read_counter(entity_name) 

85 count = cur_count + 1 

86 self.set_counter(count, entity_name) 

87 return count 

88 

89 def close(self) -> None: 

90 """ 

91 Closes the database connection. 

92 

93 :return: None 

94 """ 

95 try: 

96 if hasattr(self, 'cur') and self.cur: 96 ↛ 100line 96 didn't jump to line 100 because the condition on line 96 was always true

97 self.cur.close() 

98 except (sqlite3.ProgrammingError, Exception): 

99 pass 

100 try: 

101 if hasattr(self, 'con') and self.con: 101 ↛ exitline 101 didn't return from function 'close' because the condition on line 101 was always true

102 self.con.close() 

103 except (sqlite3.ProgrammingError, Exception): 

104 pass 

105 

106 def __del__(self) -> None: 

107 """ 

108 Destructor that ensures the database connection is closed. 

109 

110 :return: None 

111 """ 

112 self.close() 

113 

114 def __enter__(self): 

115 """ 

116 Context manager entry point. 

117 

118 :return: self 

119 """ 

120 return self 

121 

122 def __exit__(self, exc_type, exc_val, exc_tb) -> None: # noqa: ARG002 

123 """ 

124 Context manager exit point that ensures the database connection is closed. 

125 

126 :param exc_type: Exception type 

127 :param exc_val: Exception value 

128 :param exc_tb: Exception traceback 

129 :return: None 

130 """ 

131 self.close()