Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 7290b61

Browse files
committed
Uncomplete vertion of the module: 0.1
1 parent d516164 commit 7290b61

3 files changed

Lines changed: 201 additions & 0 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
from abc import ABC, abstractmethod
2+
import sqlite3
3+
4+
# Abstract Classes
5+
class AbstractDatabase():
6+
@abstractmethod
7+
def connect(self) -> None:
8+
pass
9+
10+
@abstractmethod
11+
def close(self) -> None:
12+
pass
13+
14+
# Interfaces
15+
class I_CustomSelectQuery:
16+
def execute_simple_select_query(self, table_name:str, fields:list[str]) -> list:
17+
pass
18+
19+
20+
# List of Inerfaces
21+
INTERFACES:list = [I_CustomSelectQuery]
22+
23+
24+
# Classes
25+
class Database(AbstractDatabase):
26+
def __init__(self, database_name:str) -> None:
27+
"""
28+
Creates an object of Database type, connecting to a simple SQLite3 database.
29+
30+
Attributes:
31+
- database_name (str): Name of the database.
32+
"""
33+
self.__database_name = database_name
34+
self.__conn = sqlite3.connect(f'{self.__database_name}.db')
35+
36+
@property
37+
def database_name(self) -> str:
38+
return self.__database_name
39+
40+
@property
41+
def conn(self) -> object | None:
42+
return self.__conn
43+
44+
def connect(self):
45+
"""
46+
Connects to a SQLite3 database.
47+
"""
48+
self.conn.connect()
49+
50+
def close(self):
51+
"""
52+
Closes database connection.
53+
"""
54+
self.conn.close()
55+
56+
class Cursor():
57+
def __init__(self, database_object:Database) -> None:
58+
"""
59+
Creates a cursor object in base of the received database_object as parameter.
60+
61+
Attributes:
62+
- database_object (Database): Database connection object to create the cursor.
63+
"""
64+
self.database = database_object
65+
self.__cursor = self.database.__conn.cursor()
66+
67+
@property
68+
def cursor(self) -> object:
69+
return self.__cursor
70+
71+
def execute_query(self, query:str) -> list | None:
72+
"""
73+
The Cursor object executes an SQL query that the user will pass as str, for example 'SELECT * FROM Users WHERE age > 21'.
74+
75+
Attributes:
76+
- query (str): The query that will be executed, it can be of any type (SELECT, UPDATE, DELETE, INSERT, etc.).
77+
"""
78+
try:
79+
result = self.cusor.execute(query)
80+
except Exception as e:
81+
print('Impossible to fetch result data:', e)
82+
finally:
83+
self.cursor.execute(query)
84+
85+
if result:
86+
print('Database query sucessfully executed!')
87+
return result
88+
print('Not result obtained from the database query execution, but it worked sucessfully!')
89+
return None
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from . classes import Cursor, Database
2+
from . classes import INTERFACES
3+
4+
# Custom cursor for most common queries execution.
5+
class CustomCursor(Cursor, INTERFACES.I_CustomSelectQuery):
6+
def __init__(self, database_object:Database):
7+
super().__init__(database_object)
8+
9+
def execute_query(self, query:str):
10+
"""
11+
Executes a simple query of any type wether SELECT, INSERT, CREATE TABLE, DELETE, etc.
12+
This method leverages its class's parent class method with the same name, behavior and attributes.
13+
14+
Attributes:
15+
query (str): The query that will be executed.
16+
"""
17+
super().execute_query(query)
18+
19+
def execute_simple_select_query(self, table_name:str, fields:list[str]) -> list:
20+
table_name = table_name
21+
fields = fields
22+
query = f"""SELECT {[field for field in fields]} FROM {table_name};"""
23+
data = self.execute_query(query)
24+
return data
25+
26+
def create_table(self, table_name:str, fields:list[str]) -> None:
27+
self.execute_query("")
28+
pass
29+
"""Uncompleted yet"""
30+
31+
32+
33+
""" Now with a simple cursor object we can execute the most common queries like the typical 'SELECT', or create a table with ease reducing the SQL statement we have to provide to just parameters. """
34+
my_database = Database('MyDatabase')
35+
my_cursor = CustomCursor(my_database)
36+
37+
# Creating users table
38+
my_cursor.create_table('users')
39+
40+
# Fetching data from database in users table
41+
result = my_cursor.execute_simple_select_query('users', ['username', 'email', 'password', 'age'])
42+
print(result)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
aiohappyeyeballs==2.4.6
2+
aiohttp==3.11.12
3+
aiosignal==1.3.2
4+
annotated-types==0.7.0
5+
anyio==4.8.0
6+
asgiref==3.8.1
7+
attrs==25.1.0
8+
blinker==1.9.0
9+
certifi==2024.8.30
10+
chardet==5.2.0
11+
charset-normalizer==3.4.0
12+
click==8.1.8
13+
colorama==0.4.6
14+
Deprecated==1.2.15
15+
dj-database-url==2.3.0
16+
Django==5.1.6
17+
django-environ==0.12.0
18+
django-oauth2-provider==0.2.6.1
19+
django-redis==5.4.0
20+
djangorestframework==3.15.2
21+
djangorestframework-oauth==1.1.0
22+
djangorestframework_simplejwt==5.4.0
23+
drf-yasg==1.21.9
24+
fastapi==0.115.6
25+
Flask==3.1.0
26+
frozenlist==1.5.0
27+
h11==0.14.0
28+
idna==3.10
29+
inflection==0.5.1
30+
itsdangerous==2.2.0
31+
Jinja2==3.1.5
32+
limits==4.0.0
33+
MarkupSafe==3.0.2
34+
multidict==6.1.0
35+
packaging==24.2
36+
pillow==11.1.0
37+
prettytable==3.14.0
38+
propcache==0.2.1
39+
psutil==7.0.0
40+
psycopg2==2.9.10
41+
psycopg2-binary==2.9.10
42+
pydantic==2.10.5
43+
pydantic_core==2.27.2
44+
PyDirectInput==1.0.4
45+
PyJWT==2.10.1
46+
pynput==1.7.7
47+
python-dotenv==1.0.1
48+
python-magic==0.4.27
49+
python-magic-bin==0.4.14
50+
pytz==2025.1
51+
PyYAML==6.0.2
52+
redis==5.2.1
53+
reportlab==4.3.1
54+
requests==2.32.3
55+
shortuuid==1.0.13
56+
six==1.17.0
57+
slowapi==0.1.9
58+
sniffio==1.3.1
59+
sqlparse==0.5.3
60+
starlette==0.41.3
61+
translation==1.0.5
62+
typing_extensions==4.12.2
63+
tzdata==2025.1
64+
uritemplate==4.1.1
65+
urllib3==2.2.3
66+
uvicorn==0.34.0
67+
wcwidth==0.2.13
68+
Werkzeug==3.1.3
69+
wrapt==1.17.2
70+
yarl==1.18.3

0 commit comments

Comments
 (0)