Abstraction from DBMS, queries without explicit use of SQL, and convenient work with high-level object-containers.
Project description
dbrequest
dbrequest is a library for easy database handling. The library is primarily designed for simple projects where complex SQL queries are not needed and SQLite can be used as the DBMS (although the library supports other DBMS as well).
The library provides an abstraction from the DBMS and allows working with storing, loading, modifying, and deleting object-containers without explicit use of SQL.
Contents
Installation
Installation from the PyPI repository:
$ pip install dbrequest
Installation from a GitHub repository (requires pip version 20 and above).
$ pip install git+https://github.com/korandr/dbrequest.git
Library import:
import dbrequest
Disclaimer
The library is primarily developed for the developer's personal projects and does not prioritize suitability for any projects.
Some restrictions are used in the library (for example, all objects to be saved must have an id of type int), which are convenient for the developer's projects but may not necessarily be suitable for other projects.
Nevertheless, the developer is not against integrating this library into other projects if it solves the necessary tasks.
The library does not adhere to the PEP code style and is unlikely to do so. Objects, methods, and properties are always named using camelCase here. However, an important task is to adhere to the SOLID principles.
Quick Start
For example, let's create a table users
. Describe it in the file create_table.sql
.
create table IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
last_message TEXT DEFAULT NULL
);
Let's initialize the library.
import dbrequest
dbrequest.init(init_script='create_table.sql')
Next, in the file user.py
, let's create a class User
. Inheriting from Savable
will automatically add the _id
field and the id
property to the class, which is the implementation of the dbrequest.ISavable
interface.
from dbrequest import Savable
class User(Savable):
def __init__(self) -> None:
super().__init__()
self._username: str = None
self._last_message: str = None
@property
def username(self) -> str:
return self._username
@property
def lastMessage(self) -> str:
return self._last_message
@username.setter
def username(self, value:str) -> None:
if not isinstance(value, str):
raise TypeError(type(value))
if value == '':
raise ValueError(value)
self._username = value
@lastMessage.setter
def lastMessage(self, value:str) -> None:
if not isinstance(value, str) and not value is None:
raise TypeError(type(value))
self._last_message = value
Now let's create the file user_fields.py
and implement classes in it that will be used by the library to load and save fields of the User
class in the database.
from dbrequest import AbstractField
from user import User
class UserUsernameField(AbstractField):
def getValueFromObject(self, object:User) -> None:
self._value = object.username
def setValueToObject(self, object:User) -> None:
object.username = self._value
class UserLastMessageField(AbstractField):
def getValueFromObject(self, object:User) -> None:
self._value = object.lastMessage
def setValueToObject(self, object:User) -> None:
object.lastMessage = self._value
If needed, these classes can contain additional logic for data processing when exchanging data between the container class and the database. For example, converting non-standard data types to data types supported by the database.
The last step is to create a class for database requests related to the user. Let's create the file user_request.py
.
from dbrequest import AbstractDBRequest
from dbrequest import IdField
from user_fields import *
class UserDBRequest(AbstractDBRequest):
def __init__(self) -> None:
super().__init__()
self._TABLE_NAME = 'users'
self._FIELDS = (
IdField(),
UserUsernameField('username', str),
UserLastMessageField('last_message', str, allowed_none=True)
)
In self._TABLE_NAME
, specify the name of the corresponding table.
Through the property self._FIELDS
, the connection with columns in the database is established. The method takes a tuple of implementations of AbstractField
. The fields should be arranged in the same order as the columns in the database. In the constructor of each field class, specify the column name and its type. If the column can accept a NULL
value, then allowednone=True
must be defined.
The abstraction is created. Now it is convenient to perform operations with the User
class and the database.
from typing import Tuple
from user import User
from user_request import UserDBRequest
user = User()
user.username = 'simple_user'
request = UserDBRequest()
request.save(user)
user: User = request.loadAll(User(), limit=1)[0]
print(user.id)
sameUser = User()
sameUser.id = user.id
request.load(sameUser)
print(sameUser.username)
user.lastMessage = 'Hello world!'
request.update(user)
admin = User()
admin.username = 'admin'
admin.lastMessage = 'Do you want to be banned?'
request.save(admin)
users: Tuple[User] = request.loadAll(User())
for user in users:
print(f'The user who said "{user.lastMessage}" has been deleted')
request.delete(user)
Documentation
The documentation for the library is only available in Russian. If you are genuinely interested in the documentation, I suggest using Google Translate as a browser extension, as it works really well.
Feedback
Developer: Andrey Korovyanskiy | korandrmail@ya.ru
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file dbrequest-0.1.6.tar.gz
.
File metadata
- Download URL: dbrequest-0.1.6.tar.gz
- Upload date:
- Size: 13.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 698bb39ebd852eb317ce862802951432be54bafc0e1f375330703936c4e8a855 |
|
MD5 | cd715f95cbf5829033fade1074f42f52 |
|
BLAKE2b-256 | b7fff45effdd8ad2a7b9d8c4667783f67a3dc0bcf6c3366ba89d76183e6782df |
File details
Details for the file dbrequest-0.1.6-py3-none-any.whl
.
File metadata
- Download URL: dbrequest-0.1.6-py3-none-any.whl
- Upload date:
- Size: 16.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c04017db14e541a8059ef7ad2c52d3e220cac2549e3726566ee6935ef420047 |
|
MD5 | fa23407efd2fcd45a0b919c1c78e4a9e |
|
BLAKE2b-256 | ac1b50f067d29eaa24689a94b8f031be680bd66229b06ebc1361e59041f8c640 |