Package: dbrequest
dbrequestdbrequest 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.Read this in RussianContentsInstallationDisclaimerQuick StartDocumentationFeedbackInstallationInstallation from the PyPI repository:$pipinstalldbrequestInstallation from a GitHub repository (requires pip version 20 and above).$pipinstallgit+https://github.com/korandr/dbrequest.gitLibrary import:importdbrequestDisclaimerThe 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 StartFor example, let's create a tableusers. Describe it in the filecreate_table.sql.createtableIFNOTEXISTSusers(idINTEGERPRIMARYKEYAUTOINCREMENT,usernameTEXTNOTNULLUNIQUE,last_messageTEXTDEFAULTNULL);Let's initialize the library.importdbrequestdbrequest.init(init_script='create_table.sql')Next, in the fileuser.py, let's create a classUser. Inheriting fromSavablewill automatically add the_idfield and theidproperty to the class, which is the implementation of thedbrequest.ISavableinterface.fromdbrequestimportSavableclassUser(Savable):def__init__(self)->None:super().__init__()self._username:str=Noneself._last_message:str=None@propertydefusername(self)->str:returnself._username@propertydeflastMessage(self)->str:returnself._last_message@username.setterdefusername(self,value:str)->None:ifnotisinstance(value,str):raiseTypeError(type(value))ifvalue=='':raiseValueError(value)self._username=value@lastMessage.setterdeflastMessage(self,value:str)->None:ifnotisinstance(value,str)andnotvalueisNone:raiseTypeError(type(value))self._last_message=valueNow let's create the fileuser_fields.pyand implement classes in it that will be used by the library to load and save fields of theUserclass in the database.fromdbrequestimportAbstractFieldfromuserimportUserclassUserUsernameField(AbstractField):defgetValueFromObject(self,object:User)->None:self._value=object.usernamedefsetValueToObject(self,object:User)->None:object.username=self._valueclassUserLastMessageField(AbstractField):defgetValueFromObject(self,object:User)->None:self._value=object.lastMessagedefsetValueToObject(self,object:User)->None:object.lastMessage=self._valueIf 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 fileuser_request.py.fromdbrequestimportAbstractDBRequestfromdbrequestimportIdFieldfromuser_fieldsimport*classUserDBRequest(AbstractDBRequest):def__init__(self)->None:super().__init__()self._TABLE_NAME='users'self._FIELDS=(IdField(),UserUsernameField('username',str),UserLastMessageField('last_message',str,allowed_none=True))Inself._TABLE_NAME, specify the name of the corresponding table.Through the propertyself._FIELDS, the connection with columns in the database is established. The method takes a tuple of implementations ofAbstractField. 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 aNULLvalue, thenallowednone=Truemust be defined.The abstraction is created. Now it is convenient to perform operations with theUserclass and the database.fromtypingimportTuplefromuserimportUserfromuser_requestimportUserDBRequestuser=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.idrequest.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())foruserinusers:print(f'The user who said "{user.lastMessage}" has been deleted')request.delete(user)See the code from the exampleDocumentationThe documentation for the library is onlyavailable in Russian. If you are genuinely interested in the documentation, I suggest using Google Translate as a browser extension, as it works really well.FeedbackDeveloper: Andrey Korovyanskiy |korandrmail@ya.ru