Package: haip-database
haip.confighaip.database is a minimalistic async database interface for Python 3Featuresminimalistic:queryanddodb by name: reference your database (sessions) per config-namesql templates: SQL seperated from code. placeholders are escaped automatically.db pools: db connection poolingdict short notation: row.fieldname == row['fieldname']db supported: mysql, oracle, mssql, (ongoing)Getting StartedInstallingpipinstallhaip-databaseor from source:gitclonehttps://github.com/haipdev/database.gitUsageConfig-filesdatabases:mysql_test_db:type:mysqlhost:hostport:portdatabase:databaseusername:usernamepassword:passwordmax_connections:3max_idle_connections:3oracle_test_db:type:oraclehost:hostport:portusername:usernamepassword:passwordservice_name:service_namemssql_test_db:type:mssqldriver:driverhost:hostport:portdatabase:TESTusername:usernamepassword:passwordThe database is identified by the section-name (e.g. 'mysql_test_db').Common optionstype: mysql | oraclehost: hostname/IP of the database server - default='127.0.0.1'port: port the database server is listening at - default depends on the type (mysql=3306, oracle=1521)username: username for loginpassword: password for loginautocommit: true | false - default=truemax_connections: max number of open connections for this database (otherwise you will get DatabasePoolExhaustedExcpetions)max_idle_connections: max number of open idle connectionsMysql optionsdatabase: name of the database on the database-serverPrerequisite: python module "mysql_connector"Oracle optionsservice_name: service-name (higher priority then SID)sid: sidPrerequisite: python module "cx_Oracle"MSSQL optionsdriver: driver definition string e.g. ""{SQL Server}"Prerequisite: python module "pyodbc"Example/path-to-my-config-dir/databases.ymldatabases:testdb:type:mysqlusername:testuserhost:127.0.0.2(optionally you can place e.g. the passwords in seperate files):/path-to-my-config-dir/dev/databases.ymldatabases:testdb:password:testpasswordFunctionsQueryasync def query(db_name, query_template, *values, **args)db_name: the name of the database as defined in the configuration files (main section "databases" - in the example above e.g. "testdb")query_template: the filename of the template-file containing the SQL query. This file must have the suffix ".sql".*values: values for query placeholders**args: the template-vars for the query_template jinja template.This function returns an array of arrays (array of rows).Query example/path-to-my-config-dir/queries/firstname.sqlSELECTfirstname,lastnameFROMusersWHEREfirstname='{{firstname}}importhaip.configasconfigimporthaip.databaseasdatabaseconfig.load('/path-to-my-config-dir','dev')rows=awaitdatabase.query('testdb','queries/firstname.sql',firstname='Reinhard')forrowinrows:firstname=row[0]lastname=row[1]awaitdatabase.shutdown()shortcutsasync def query_assoc(db_name, query_template, **args)Like "query" but returns an array of dicts. e.g. rows[0]['firstname'] or rows[0].firstnameasync def query_first(db_name, query_template, **args)Like "query" but returns only the first row as dict. e.g. row['firstname'] or row.firstname. If no rows found None will be returned.Insert/Updatesasync def do(db_name, query_template, **args)Arguments like "query". This function returns the number of rows effected by this statement.Do example/path-to-my-config-dir/queries/update.sqlUPDATEusersSETfirstname='Test'WHERElastname='{{lastname}}importhaip.configasconfigimporthaip.databaseasdatabaseconfig.load('/path-to-my-config-dir','dev')changes=awaitdatabase.do('testdb','queries/update.sql',lastname='Hainz')print(f'effected rows:{changes}')awaitdatabase.shutdown()Proceduresasync def call(db_name, procedure)db_name: as aboveprocedure: the name of the procedure to be calledPool shutdownasync def shutdown()Close all open connections in the connection pool normally used before closing your application. If you do not close the open connections in the connection pool you will see some warnings on your db servers. So calling this function is not realy necessary but beautifies your code.Running the testsTests are written using pytest and located in the "tests" directory.pytesttestsContributingFeel free to use and enhance this project. Pull requests are welcome.AuthorsReinhard Hainz-Initial work-haipdevLicenseThis project is licensed under the MIT License - see theLICENSEfile for detailsDependencieshaip-confighaip-template