Exemple #1
0
 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))
Exemple #2
0
 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
Exemple #3
0
 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
Exemple #4
0
 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
Exemple #5
0
 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)))
Exemple #6
0
 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
Exemple #7
0
 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)))
Exemple #8
0
 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)))
Exemple #9
0
 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)))
Exemple #10
0
 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()
Exemple #11
0
 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)
Exemple #12
0
 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)
Exemple #13
0
 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
Exemple #14
0
 def add(self, element):
     if element in self._entities:
         raise RepositoryError("Id already exists!\n")
     self._entities.append(element)