Package: zensols.db
Database convenience utilities.A library of database convenience utilities, typically for creation of
temporary files for processing large data.Features:DB-API Interface allows combined SQL rapid prototyping with backing
programmatic usage.Java Beans like persistence.Integration withzensols.util stash.SQLiteintegration.PostgreSQLintegration with thedbutilpglibrary.Pandasdata frame creation, which is agnostic of database provider.DocumentationSee thefull documentation.
TheAPI referenceis also
available.ObtainingThe easist way to install the command line program is via thepipinstaller:pip3installzensols.dbBinaries are also available onpypi.UsageA simple example is detailed below, and also found in therepo.SQL binding fileFirst, create the SQL file, which is used to create and access the database.
Here we can replacename, agewith${cols}and call itperson.sql:-- meta=init_sections=create_tables,create_idx-- name=create_idxcreateindexperson_nameonperson(name);-- name=create_tablescreatetableperson(nametext,ageint);-- name=insert_personinsertintoperson(${cols})values(?,?);-- name=select_people; note that the order is needed for the unit tests onlyselect${cols},rowidasidfrompersonorderbyname;-- name=select_people_by_idselect${cols},rowidasidfrompersonwhereid=?;-- name=update_personupdatepersonsetname=?,age=?whererowid=?;-- name=delete_persondeletefrompersonwhererowid=?;PersisterNext, create the application context with a persister that is the SQL to client
binding and call itapp.conf:# command line interaction[cli]class_name=zensols.cli.ActionCliManagerapps=list: app# the connection manager, which is the DB binding and in our case SQLite[sqlite_conn_manager]class_name=zensols.db.SqliteConnectionManagerdb_file=path: person.db# the persister binds the API to the SQL[person_persister]class_name=zensols.db.DataClassDbPersisterbean_class=class: app.Personsql_file=person.sqlconn_manager=instance: sqlite_conn_managerinsert_name=insert_personselect_name=select_peopleselect_by_id=select_people_by_idupdate_name=update_persondelete_name=delete_person# the application class invoked by the CLI[app]class_name=app.Applicationpersister=instance: person_persisterApplicationDefine thebean, which provides the metadata for the${cols}inperson.sqland can (but not must) be used with the API to CRUD rows:fromdataclassesimportdataclass,fieldfromzensols.dbimportBeanDbPersister@dataclassclassPerson(object):name:str=field()age:int=field()id:int=field(default=None)@dataclassclassApplication(object):"""A people database"""persister:BeanDbPersisterdefdemo(self):# create a row using an instance of a dataclass and return the unique# ID of the inserted rowpaul_id:int=self.persister.insert(Person('Paul',31))# we can also insert by columns in the order given in the dataclassjane_id:int=self.persister.insert_row('Jane',32)# print everyone in the databaseprint(self.persister.get())# delete a rowself.persister.delete(paul_id)print(self.persister.get())# update jane's ageself.persister.update_row(jane_id,'jane',36)# get the updated row we just setjane=self.persister.get_by_id(jane_id)print(f'jane:{jane}')# clean up, which for SQLite deletes the fileself.persister.conn_manager.drop()Create the entry point used on the command line and call itrun.py:fromzensols.cliimportCliHarnessCliHarness(app_config_resource='app.conf').run()Run$./run.py-h
Usage:run.py[options]:

Apeopledatabase.

Options:-h,--helpshowthishelpmessageandexit--versionshowtheprogramversionandexit$./run.py(Person(name='Jane',age=32,id=2),Person(name='Paul',age=31,id=1))(Person(name='Jane',age=32,id=2),)jane:Person(name='jane',age=36,id=2)See theuse casesfor more detailed examples of
how to use the API.ChangelogAn extensive changelog is availablehere.CommunityPlease star this repository and let me know how and where you use this API.
Contributions as pull requests, feedback and any input is welcome.LicenseMIT LicenseCopyright (c) 2020 - 2023 Paul Landes