Bot's Database¶
Design Pattern¶
The interaction with the database is done using a variation of the command design pattern, the implementation is inspired and derived from the Practices Of The Python Pro Book..
Click to view the UML diagram of the implementation
Pattern Interfaces¶
Commands Interface¶
The invokers call and execute commands which are concrete classes implementing the ICommand
interface that has an execute()
method.
Click to View the ICommand
Interface
Bases: Protocol
This protocol abstracts the implementation of the predefined database commands classes
Source code in src/database/db_commands.py
13 14 15 16 17 |
|
execute()
¶Implement the command execution
Source code in src/database/db_commands.py
16 17 |
|
Persistence Interface¶
Each command calls a corresponding method in the Persistence class which is a concrete class implementing the IPersistenceLayer
interface controlling the DatabaseManger
class.
Click to View the IPersistenceLayer
Interface
Bases: ABC
An abstracted interface for the database controller
Source code in src/database/persistence.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
Pattern Implementation¶
Concrete Commands¶
After defining the the ICommand
interface, we can easily create concrete command classes that implement it's functionality, the following is an example of the AddUserCommand()
command.
Click to view the AddUserCommand()
concrete class
Bases: ICommand
The command adds a new user to the database using the INSERT INTO SQL statement.
Source code in src/database/db_commands.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
__init__(*, user_id: str, block_type: str) -> None
¶summary : This method gets the data to initiate the command to add a user to the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_id |
str
|
description : User's user_id. |
required |
block_type |
str
|
description : User's block type (temp | perm). |
required |
Source code in src/database/db_commands.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
execute() -> None
¶This method executes the 'INSERT INTO' statement.
Source code in src/database/db_commands.py
38 39 40 41 |
|
Concrete Persistence¶
The UsersDatabase
concrete class is an implementation of the IPersistenceLayer
interface which sits between the command classes and the DatabaseManger
class that executes CURL SQL statements on the database.
The UsersDatabase
class is a lower level of abstraction acting on top of the DatabaseManger
class with a predefined SQL statements designed specifically for the Users and allow groups list tables in the database.
Click to view the UsersDatabase
class
Bases: IPersistenceLayer
This class sits between the database commands and the database manger class
Source code in src/database/persistence.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
|
__init__() -> None
¶summary : This creates the 2 tables 'users' and 'allow_list'
Source code in src/database/persistence.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
add_to_allow_list(group_id: str) -> None
¶summary : This methods adds users to the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
group_id |
str
|
description : group_id to allow |
required |
Source code in src/database/persistence.py
89 90 91 92 93 94 95 96 97 98 99 100 101 |
|
add_user(user_id: str, block_type: str) -> None
¶summary : This methods adds users to the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_id |
str
|
description : user id. |
required |
block_type |
str
|
description : 'perm' for permanently block | 'temp' for temporary block |
required |
Source code in src/database/persistence.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
delete(user_id: str)
¶summary : This method deletes user from the database using his user_id as a criteria.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_id |
str
|
description : user's user_id. |
required |
Source code in src/database/persistence.py
170 171 172 173 174 175 176 177 178 179 |
|
delete_group(group_id: str)
¶summary : This method deletes group from the database using his group_id as a criteria.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
group_id |
str
|
description : group's id. |
required |
Source code in src/database/persistence.py
181 182 183 184 185 186 187 188 189 190 |
|
edit(user_id: str, block_type: str)
¶summary : This method update the records in the database using the user user_id as a criteria.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_id |
str
|
description : user's user_id. |
required |
block_type |
str
|
description : The block type of the user perm for permanent | temp for temporary |
required |
Source code in src/database/persistence.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
|
get_group(group_id: str = None, order_by: str = None) -> list
¶summary : This methods selects group from the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
group_id |
str
|
description, by default None : group_id to filter by. |
None
|
order_by |
str
|
description, by default None : Column name to order the query result by, be default it will be order by the date added. |
None
|
Returns:
Type | Description |
---|---|
list
|
description : A list of tuples containing each records |
Source code in src/database/persistence.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
|
get_user(user_id: str = None, block_type: str = None, order_by: str = None) -> list
¶summary : This methods selects users from the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_id |
str
|
description, by default None : user_id to filter by. |
None
|
order_by |
str
|
description, by default None : Column name to order the query result by, be default it will be order by the date added. |
None
|
Returns:
Type | Description |
---|---|
list
|
description : A list of tuples containing each records |
Source code in src/database/persistence.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
|
The DatabaseManger¶
The DatabaseManger
class initializes the connection with the database (sqlite3), and executes SQL statements on the database using CURL commands.
Click to View the DatabaseManger
class
This class manges the connection to the sqlite database.
Source code in src/database/db_manger.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
__del__() -> None
¶summary : This method closes the connection with the database.
Source code in src/database/db_manger.py
26 27 28 |
|
__init__(database_filename: Path) -> None
¶summary : Initializing a connection with the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
database_filename |
Path
|
description : A path to the database file to connect to, if doesn't exist, it will be created. |
required |
Source code in src/database/db_manger.py
16 17 18 19 20 21 22 23 24 |
|
add(table_name: str, data: dict[str, str]) -> None
¶summary : This method adds data into the database using the 'INSERT INTO' SQL statement.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table_name |
str
|
description : Table name to perform the statement on. |
required |
data |
dict[str, str]
|
description : A dict of strings, specifying the columns names and the columns values to be added to the table. |
required |
Source code in src/database/db_manger.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
create_table(table_name: str, columns: dict[str, str]) -> None
¶summary : This method creates a table in the database if the table is note existing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table_name |
str
|
description : The name of the table to create. |
required |
columns |
dict[str, str]
|
description : A dict holding the columns names as a key and their types as the value |
required |
Source code in src/database/db_manger.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
|
delete(table_name: str, criteria: dict[str, str]) -> None
¶summary : This method deletes data from the database using the 'DELETE' statement.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table_name |
str
|
description : Table name to perform the statement on. |
required |
criteria |
dict[str, str]
|
description : The criteria to use as a filter on the DELETE statement, passed as a dict => {keys(criteria) : values(values)} |
required |
Source code in src/database/db_manger.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
select(table_name: str, criteria: dict[str, str] = None, order_by: str = None) -> Cursor
¶summary: : This method selects data from the database using the 'SELECT' SQL statement and returns back data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table_name |
str
|
description : Table name to perform the statement on. |
required |
criteria |
dict[str, str]
|
description, by default None : The criteria to use as a filter on the SELECT statement, passed as a dict => {keys(criteria) : values(values)} |
None
|
order_by |
str
|
description, by default None : A column name to sort the returned query by, will map to the ORDER BY {order_by} |
None
|
Returns:
Type | Description |
---|---|
Cursor
|
description : A Cursor object containing the data back from the database of any. |
Source code in src/database/db_manger.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
update(table_name: str, criteria: dict[str, str], data: dict[str, str]) -> None
¶summary : This method updates data in the database using the 'UPDATE' statement.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table_name |
str
|
description : Table name to perform the statement on. |
required |
criteria |
dict[str, str]
|
description : The criteria to use as a filter on the UPDATE statement, passed as a dict => {keys(criteria) : values(values)} |
required |
data |
dict[str, str]
|
description : The actual data to update |
required |
Source code in src/database/db_manger.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
|