Skip to content

Commit

Permalink
move with_commit into class in db.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ReYaNOW committed Jan 26, 2024
1 parent 3ebc324 commit 537a1af
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions page_analyzer/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@
)


def with_commit(func):
def inner(self, *args, **kwargs):
result = func(self, *args, **kwargs)
self.conn.commit()
return result

return inner


class DbConnectionProcessor:
def __init__(self):
self.conn = psycopg2.connect(DATABASE_URL)
Expand All @@ -37,6 +28,19 @@ def _execute_query(self, request, params=None, fetch_type=None):
case FETCH_TYPES.ALL:
return curs.fetchall()

@staticmethod
def with_commit(func):
def inner(self, *args, **kwargs):
try:
result = func(self, *args, **kwargs)
self.conn.commit()
return result
except psycopg2.Error as e:
self.conn.rollback()
raise e

return inner

def get_all_urls(self):
return self._execute_query(
'SELECT * FROM urls ORDER BY id DESC', fetch_type=FETCH_TYPES.ALL
Expand Down

0 comments on commit 537a1af

Please sign in to comment.