{ "info": { "author": "Alberto Alcolea", "author_email": "albertoalcolea@gmail.com", "bugtrack_url": null, "classifiers": [], "description": "dbhelpers\n=========\n\n[![Build Status](https://travis-ci.org/albertoalcolea/dbhelpers.svg?branch=master)](https://travis-ci.org/albertoalcolea/dbhelpers)\n\nDatabase helpers and utilities\n\nThis is not an ORM, is a set of useful utilities to work with raw queries using the Python Database API Specification.\n\n\nInstallation\n------------\n\nThe easiest way to install dbhelpers is with pip:\n\n $ pip install dbhelpers\n\n\nUsage\n-----\n\n### Connection classes\n\nUse a default connection class for your db backend:\n\n```python\nfrom dbhelpers import Psycopg2Connection\n\n# Simple connection\nconn = Psycopg2Connection(db='mydb', user='myuser', passwd='mypass').connect()\n(...)\nconn.close()\n\n# Or using a context manager:\nwith Psycopg2Connection(db='mydb', user='myuser', passwd='mypass') as conn:\n cursor = conn.cursor()\n ...\n```\n\nOr create a custom connection class with your default parameters:\n\n```python\nfrom dbhelpers import MySQLdbConnection\n\nclass customconn(MySQLdbConnection):\n default_user = 'myuser'\n default_passwd = 'mypass'\n default_host = 'localhost'\n default_port = 13306\n default_extra_kwargs = {'charset': 'utf8mb4'}\n\nwith customconn('mydb') as conn:\n cursor = conn.cursor()\n ....\n```\n\n### Helpers\n\nThe package include some useful utilities to work with database cursors.\n\n#### Cursor as a context manager:\n\nThe cursor is executed inside a `with` block. When the block ends the cursor is closed. Also does a `connection.commit()` when the block ends if `commit=True` (True by default).\n\n```python\nfrom dbhelpers import cm_cursor\n\n# With autocommit\nwith customconn('mydb') as conn:\n with cm_cursor(conn) as cursor:\n cursor.execute(\"INSERT INTO mytable (id, status) VALUES (23, 'info')\")\n\n# Disable autocommit\nwith customconn('mydb') as conn:\n with cm_cursor(conn, commit=False) as cursor:\n (...)\n```\n\nIf `commit=True` (default) and an exception is thrown inside the `with` block, `cm_cursor` calls the `conn.rollback()` method instead of `conn.commit()`\n\nIn Python 2.7 and 3.x you can get the connection object and the cursor object of the context managers in a single with statment:\n\n```python\nwith customconn('mydb') as conn, cm_cursor(conn) as cursor:\n # Do something ...\n```\n\n#### Fetchiter\n\n`fetchiter` can be used as a generator for large recordsets:\n\n```python\nfrom dbhelpers import fetchiter\n\nwith customconn('mydb') as conn:\n with cm_cursor(conn) as cursor:\n cursor.execute(\"SELECT * FROM bigtable\")\n for row in fetchiter(cursor):\n # Do something\n```\n\nThe `fetchiter` function does not copy all rows in memory, do sucessive calls in blocks to retrieve all data. The default block size is 1000.\n\nThe `cursor.fetchall()` method can fill the process memory easily if there are a lot of register to return. `fetchiter` do calls to `cursor.fetchmany()` iteratively until there are no more data to return. The `fetchiter` function behaves like an iterator.\n\nYou can get the whole blocks or change the size of the block:\n\n```python\nwith customconn('mydb') as conn:\n with cm_cursor(conn) as cursor:\n cursor.execute(\"SELECT * FROM bigtable\")\n for block in fetchiter(cursor, size=50, batch=True):\n # Do something, block is a tuple with 50 rows\n```\n\n#### PostgreSQL server cursor\n\nAlso, `fetchiter` allows work with PostgreSQL server cursors previously declared.\n\nInstead of the standard `fetchiter` behavior, which do a query to a server, the server calculates the whole recordset, and `fetchiter` retrieve the results iteratively to avoid fill the process memory, a server cursor runs the pseudo-iterator on a Postgres server and calculates the partial recordset in blocks iteratively. \n\n```python\nfrom dbhelpers import fetchiter\n\nwith customconn('mydb') as conn:\n with cm_cursor(conn) as cursor:\n cursor.execute(\"DECLARE C CURSOR FOR SELECT * FROM bigtable\")\n for row in fetchiter(cursor, server_cursor='C'):\n # Do something\n cursor.execute(\"CLOSE C\")\n```\n\n`fetchiter` can return the server cursor results as the above example (as an interator or as a block), an you can change the block size. The default block size is 1000.\n\n#### Rows as NamedTuples\n\n`fetchone_nt`, `fetchmany_nt`, `fetchall_nt` `fetchiter_nt` returns the rows as NamedTuples:\n\n```python\nfrom dbhelpers import fetchone_nt, fetchmany_nt, fetchall_nt\n\nwith customconn('mydb') as conn:\n with cm_cursor(conn) as cursor:\n cursor.execute(\"SELECT id, status FROM mytable WHERE id = 23\")\n row = fetchone_nt(cursor)\n # Now, row is a NamedTuple with each column mapped as an attribute:\n # >>> row.id\n # 32\n # >>> row.status\n # 'warning'\n```", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://albertoalcolea.com", "keywords": null, "license": "Apache 2.0", "maintainer": null, "maintainer_email": null, "name": "dbhelpers", "package_url": "https://pypi.org/project/dbhelpers/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/dbhelpers/", "project_urls": { "Download": "UNKNOWN", "Homepage": "http://albertoalcolea.com" }, "release_url": "https://pypi.org/project/dbhelpers/0.1.0.4/", "requires_dist": null, "requires_python": null, "summary": "Database helpers and utilities .", "version": "0.1.0.4" }, "last_serial": 1803580, "releases": { "0.1.0.3": [ { "comment_text": "", "digests": { "md5": "0b89b0a2a8a9438a2ee088f181f0d4a5", "sha256": "fe729d0812db82c14d26d9384564e922c04014b5ce297268e0a4a2e93fc28bcd" }, "downloads": -1, "filename": "dbhelpers-0.1.0.3.tar.gz", "has_sig": false, "md5_digest": "0b89b0a2a8a9438a2ee088f181f0d4a5", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6829, "upload_time": "2015-10-31T00:48:59", "url": "https://files.pythonhosted.org/packages/71/ab/3349a0b60671e24cde9c87e421f3a81e82bbe9b226f33eaa6afab20cbcc0/dbhelpers-0.1.0.3.tar.gz" } ], "0.1.0.4": [ { "comment_text": "", "digests": { "md5": "f2124c59ecffae0c7c000809bc3a51e9", "sha256": "c0e37b717a07f5ae9ad171a170954a17be6c530b2f7a70d805294c6eb7710236" }, "downloads": -1, "filename": "dbhelpers-0.1.0.4.tar.gz", "has_sig": false, "md5_digest": "f2124c59ecffae0c7c000809bc3a51e9", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6971, "upload_time": "2015-11-05T23:09:15", "url": "https://files.pythonhosted.org/packages/f9/de/f13e05295adcdc9211a0c8688edde23d629895307879f6404566029442c3/dbhelpers-0.1.0.4.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "f2124c59ecffae0c7c000809bc3a51e9", "sha256": "c0e37b717a07f5ae9ad171a170954a17be6c530b2f7a70d805294c6eb7710236" }, "downloads": -1, "filename": "dbhelpers-0.1.0.4.tar.gz", "has_sig": false, "md5_digest": "f2124c59ecffae0c7c000809bc3a51e9", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6971, "upload_time": "2015-11-05T23:09:15", "url": "https://files.pythonhosted.org/packages/f9/de/f13e05295adcdc9211a0c8688edde23d629895307879f6404566029442c3/dbhelpers-0.1.0.4.tar.gz" } ] }