Skip to content
Snippets Groups Projects

Merge concept version of database-lib

+ 53
3
@@ -24,17 +24,32 @@ logger = logging.getLogger()
class AbstractDatabase(abc.ABC):
"""
The AbstractDatabase class represents any database. It manages the
database connection and cursor.
Attributes:
connection:
Database connection.
cursor:
Database cursor.
"""
def __init__(self):
self.connection = None
self.cursor = None
@abc.abstractmethod
def connect(self):
"""
Abstract connection to the database.
"""
pass
def commit_and_close(self):
"""
Commits the executed statements and closes the database connection
Commits the executed statements and closes the database connection.
"""
self.connection.commit()
@@ -42,8 +57,9 @@ class AbstractDatabase(abc.ABC):
def close(self):
"""
Closes the database connection
Closes the database connection.
"""
self.connection.close()
@@ -126,7 +142,40 @@ class SpatialiteDatabase(AbstractDatabase):
self.cursor = self.connection.cursor()
class PostgisDatabase(AbstractDatabase):
class PostGISDatabase(AbstractDatabase):
"""
The PostgisDatabase class represents a Postgis database. It manages the
database connection and cursor.
Args:
host (str):
Host of the database.
dbname (str):
Name of the database.
port (str):
Port of the database.
username (str):
Username for the database connection.
password (str):
Password for the database connection.
Attributes:
host (str):
Host of the database.
dbname (str):
Name of the database.
port (str):
Port of the database.
username (str):
Username for the database connection.
password (str):
Password for the database connection.
connection (psycopg2.Connection):
Database connection.
cursor (psycopg2.Cursor):
Database cursor.
"""
def __init__(self, host, dbname, port, username, password):
super().__init__()
self.host = host
@@ -140,6 +189,7 @@ class PostgisDatabase(AbstractDatabase):
Create a database connection and a cursor with psycopg2 with the
given credentials and include the connection and cursor as new attributes.
"""
connection_string = (
f"host={self.host} "
f"dbname={self.dbname} "
Loading