{ "info": { "author": "Reinhard Hainz", "author_email": "reinhard.hainz@gmail.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3" ], "description": "# haip.config\n\n[![License](https://img.shields.io/github/license/haipdev/database.svg)](LICENSE)\n[![Build Status](https://travis-ci.org/haipdev/database.svg?branch=master)](https://travis-ci.org/haipdev/database)\n\nhaip.database is a minimalistic async database interface for Python 3\n\n## Features\n\n* **minimalistic**: *query* and *do*\n* **db by name**: reference your database (sessions) per config-name\n* **sql templates**: SQL seperated from code. placeholders are escaped automatically.\n* **db pools**: db connection pooling\n* **dict short notation**: row.fieldname == row['fieldname']\n* **db supported**: mysql, oracle, mssql, (ongoing)\n\n## Getting Started\n\n### Installing\n\n```sh\npip install haip-database\n```\n\nor from source:\n\n```sh\ngit clone https://github.com/haipdev/database.git\n```\n\n### Usage\n\n#### Config-files\n\n```yaml\ndatabases:\n mysql_test_db:\n type: mysql\n host: host\n port: port\n database: database\n username: username\n password: password\n max_connections: 3\n max_idle_connections: 3\n\n oracle_test_db:\n type: oracle\n host: host\n port: port\n username: username\n password: password\n service_name: service_name\n\n mssql_test_db:\n type: mssql\n driver: driver\n host: host\n port: port\n database: TEST\n username: username\n password: password\n```\n\nThe database is identified by the section-name (e.g. 'mysql_test_db').\n\n##### Common options\n\n* **type**: mysql | oracle\n* **host**: hostname/IP of the database server - default='127.0.0.1'\n* **port**: port the database server is listening at - default depends on the type (mysql=3306, oracle=1521)\n* **username**: username for login\n* **password**: password for login\n* **autocommit**: true | false - default=true\n* **max_connections**: max number of open connections for this database (otherwise you will get DatabasePoolExhaustedExcpetions)\n* **max_idle_connections**: max number of open idle connections\n\n##### Mysql options\n\n* **database**: name of the database on the database-server\n\nPrerequisite: python module \"mysql_connector\"\n\n##### Oracle options\n\n* **service_name**: service-name (higher priority then SID)\n* **sid**: sid\n\nPrerequisite: python module \"cx_Oracle\"\n\n##### MSSQL options\n\n* **driver**: driver definition string e.g. \"\"{SQL Server}\"\n\nPrerequisite: python module \"pyodbc\"\n\n##### Example\n\n/path-to-my-config-dir/databases.yml\n\n```yaml\ndatabases:\n testdb:\n type: mysql\n username: testuser\n host: 127.0.0.2\n```\n\n(optionally you can place e.g. the passwords in seperate files):\n\n/path-to-my-config-dir/dev/databases.yml\n\n```yaml\ndatabases:\n testdb:\n password: testpassword\n```\n\n#### Functions\n\n#### Query\n\n> async def query(db_name, query_template, *values, **args)\n\n* *db_name*: the name of the database as defined in the configuration files (main section \"databases\" - in the example above e.g. \"testdb\")\n* *query_template*: the filename of the template-file containing the SQL query. This file must have the suffix \".sql\".\n* **values*: values for query placeholders\n* ***args*: the template-vars for the query_template jinja template.\n\nThis function returns an array of arrays (array of rows).\n\n##### Query example\n\n/path-to-my-config-dir/queries/firstname.sql\n\n```sql\nSELECT firstname, lastname\n FROM users\n WHERE firstname = '{{ firstname }}\n```\n\n```python\nimport haip.config as config\nimport haip.database as database\n\nconfig.load('/path-to-my-config-dir', 'dev')\nrows = await database.query('testdb', 'queries/firstname.sql', firstname='Reinhard')\n\nfor row in rows:\n firstname = row[0]\n lastname = row[1]\n\nawait database.shutdown()\n```\n\n##### shortcuts\n\n> async def query_assoc(db_name, query_template, **args)\n\nLike \"query\" but returns an array of dicts. e.g. rows[0]['firstname'] or rows[0].firstname\n\n> async def query_first(db_name, query_template, **args)\n\nLike \"query\" but returns only the first row as dict. e.g. row['firstname'] or row.firstname. If no rows found None will be returned.\n\n#### Insert/Updates\n\n> async def do(db_name, query_template, **args)\n\nArguments like \"query\". This function returns the number of rows effected by this statement.\n\n##### Do example\n\n/path-to-my-config-dir/queries/update.sql\n\n```sql\nUPDATE users\n SET firstname='Test'\nWHERE lastname = '{{ lastname }}\n```\n\n```python\nimport haip.config as config\nimport haip.database as database\n\nconfig.load('/path-to-my-config-dir', 'dev')\nchanges = await database.do('testdb', 'queries/update.sql', lastname='Hainz')\n\nprint(f'effected rows: {changes}')\nawait database.shutdown()\n```\n\n#### Procedures\n\n> async def call(db_name, procedure)\n\n* *db_name*: as above\n* *procedure*: the name of the procedure to be called\n\n#### Pool shutdown\n\n> async def shutdown()\n\nClose 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.\n\n## Running the tests\n\nTests are written using pytest and located in the \"tests\" directory.\n\n```sh\npytest tests\n```\n\n## Contributing\n\nFeel free to use and enhance this project. Pull requests are welcome.\n\n## Authors\n\n* **Reinhard Hainz** - *Initial work* - [haipdev](https://github.com/haipdev)\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details\n\n## Dependencies\n\n* [haip-config](https://github.com/haipdev/config)\n* [haip-template](https://github.com/haipdev/template)\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/haipdev/database", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "haip-database", "package_url": "https://pypi.org/project/haip-database/", "platform": "", "project_url": "https://pypi.org/project/haip-database/", "project_urls": { "Homepage": "https://github.com/haipdev/database" }, "release_url": "https://pypi.org/project/haip-database/0.1.4/", "requires_dist": [ "asgiref", "haip-config", "haip-template" ], "requires_python": "", "summary": "A generic database interface.", "version": "0.1.4" }, "last_serial": 4944259, "releases": { "0.1.4": [ { "comment_text": "", "digests": { "md5": "13bfa48335c5457132f84b3a3fb9dd90", "sha256": "01fbe089f412ad480c2daaba7851696e9d3653153ca4d8351a0c094ca0d4254c" }, "downloads": -1, "filename": "haip_database-0.1.4-py3-none-any.whl", "has_sig": false, "md5_digest": "13bfa48335c5457132f84b3a3fb9dd90", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 8806, "upload_time": "2019-03-15T13:52:19", "url": "https://files.pythonhosted.org/packages/fe/d1/a53000efd8910286921691d7729feb8b39342e5915beb71021c15ecadf8a/haip_database-0.1.4-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "c501fa6c28d8f50c83d59344461d6734", "sha256": "349427f31bd0f921242da867a26b41e3c90dbe9c538799611ba4467556d46170" }, "downloads": -1, "filename": "haip_database-0.1.4.tar.gz", "has_sig": false, "md5_digest": "c501fa6c28d8f50c83d59344461d6734", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5635, "upload_time": "2019-03-15T13:52:20", "url": "https://files.pythonhosted.org/packages/78/68/52fb50ad5d6ac2e94aac58b6ed626cfe0170bd7e9c00aa1a5e151a2e7e24/haip_database-0.1.4.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "13bfa48335c5457132f84b3a3fb9dd90", "sha256": "01fbe089f412ad480c2daaba7851696e9d3653153ca4d8351a0c094ca0d4254c" }, "downloads": -1, "filename": "haip_database-0.1.4-py3-none-any.whl", "has_sig": false, "md5_digest": "13bfa48335c5457132f84b3a3fb9dd90", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 8806, "upload_time": "2019-03-15T13:52:19", "url": "https://files.pythonhosted.org/packages/fe/d1/a53000efd8910286921691d7729feb8b39342e5915beb71021c15ecadf8a/haip_database-0.1.4-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "c501fa6c28d8f50c83d59344461d6734", "sha256": "349427f31bd0f921242da867a26b41e3c90dbe9c538799611ba4467556d46170" }, "downloads": -1, "filename": "haip_database-0.1.4.tar.gz", "has_sig": false, "md5_digest": "c501fa6c28d8f50c83d59344461d6734", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5635, "upload_time": "2019-03-15T13:52:20", "url": "https://files.pythonhosted.org/packages/78/68/52fb50ad5d6ac2e94aac58b6ed626cfe0170bd7e9c00aa1a5e151a2e7e24/haip_database-0.1.4.tar.gz" } ] }