def update(self, entity, new_entity): for i in range(len(self._entities)): if self._entities[i] == entity: self._entities[i] = new_entity return raise RepositoryError( '{} was not found in the repository.'.format(entity))
def remove(self, key): if key not in self._entities: raise RepositoryError("Id not found!\n") for i in range(len(self._entities)): if self._entities[i] == key: del self._entities[i] return
def update(self, key): if key not in self._entities: raise RepositoryError("Id not found!\n") for i in range(len(self._entities)): if self._entities[i] == key: self._entities[i] = key return
def create_connection(database_path): connection = None try: connection = sqlite3.connect(database_path) except sqlite3.Error as error: raise RepositoryError( "Database connection error occurred: '{}'\n".format( str(error))) return connection
def execute_query(connection, query): cursor = connection.cursor() try: cursor.execute(query) connection.commit() except sqlite3.Error as error: raise RepositoryError( "Database query execution error occurred: '{}'\n".format( str(error)))
def execute_read_query(connection, query): cursor = connection.cursor() reading_result = None try: cursor.execute(query) reading_result = cursor.fetchall() except sqlite3.Error as error: raise RepositoryError( "Database query execution error occurred: '{}'\n".format( str(error))) return reading_result
def remove(self, user): self.__load_data() super().remove(user) cursor = self.__db_connection.cursor() try: cursor.execute( """DELETE FROM users where email = ? AND username = ?""", (user.email, user.username)) self.__db_connection.commit() except sqlite3.Error as error: raise RepositoryError( "Database query execution error occurred: '{}'\n".format( str(error)))
def insert(self, user): self.__load_data() super().insert(user) cursor = self.__db_connection.cursor() try: cursor.execute( """INSERT INTO users (email, username, key, photo) VALUES (?,?,?,?);""", (user.email, user.username, user.database_key, user.photo)) self.__db_connection.commit() except sqlite3.Error as error: raise RepositoryError( "Database query execution error occurred: '{}'\n".format( str(error)))
def update(self, user, new_user): self.__load_data() super().update(user, new_user) cursor = self.__db_connection.cursor() try: cursor.execute( """UPDATE users SET key = ?, username = ?, email = ?, photo = ? WHERE email = ? AND username = ?""", (new_user.database_key, new_user.username, new_user.email, new_user.photo, user.email, user.username)) self.__db_connection.commit() except sqlite3.Error as error: raise RepositoryError( "Database query execution error occurred: '{}'\n".format( str(error)))
def __read_all_from_file(self): self._entities = IterableStructure() result = [] try: f = open(self.__filename, 'rb') result = pickle.load(f) except EOFError: result = [] except IOError as err: raise RepositoryError(err) if len(result) == 0: return [] for i in range(len(result)): result[i] = result[i].strip() obj = self.__read_object(result[i]) self._entities.append(obj) f.close()
def remove(self, entity): search_result = self.search(entity) if search_result is None: raise RepositoryError( '{} was not found in the repository.'.format(entity)) self._entities.remove(search_result)
def insert(self, entity): if self.search(entity) is not None: raise RepositoryError( '{} was already inserted into the repository.'.format(entity)) self._entities.append(entity)
def search(self, key): if key not in self._entities: raise RepositoryError("Id not found!\n") for x in self._entities: if x == key: return x
def add(self, element): if element in self._entities: raise RepositoryError("Id already exists!\n") self._entities.append(element)