{ "info": { "author": "Paul Chakravarti", "author_email": "paul.chakravarti@gmail.com", "bugtrack_url": null, "classifiers": [ "Programming Language :: Python :: 2", "Programming Language :: Python :: 3", "Topic :: Database" ], "description": "\n\npgwrap - simple PostgreSQL database wrapper\n-------------------------------------------\n\nThe 'pgwrap' module provides a simple wrapper over psycopg2 supporting a\nPython API for common sql functions.\n\nThis is not intended to provide ORM-like functionality, just to make it\neasier to interact with PostgreSQL from python code for simple use-cases\nand allow direct SQL access for more complex operations.\n\nThe module wraps the excellent 'psycopg2' library and most of the \nfunctionality is provided by this behind the scenes.\n\nThe module provides:\n\n * Simplified handling of connections/cursor\n * Connection pool (provided by psycopg2.pool)\n * Cursor context handler \n * Python API to wrap basic SQL functionality \n * Simple select,update,delete,join methods extending the cursor \n context handler (also available as stand-alone methods which\n create an implicit cursor for simple queries)\n * Query results as dict (using psycopg2.extras.DictCursor)\n * Callable prepared statements\n * Logging support\n * Supports Python 2/3\n\nBasic usage\n-----------\n\n>>> import pgwrap\n>>> db = pgwrap.connection(url='postgres://localhost')\n>>> with db.cursor() as c:\n... c.query('select version()')\n[['PostgreSQL...']]\n>>> v = db.query_one('select version()')\n>>> v\n['PostgreSQL...']\n>>> v.items()\n[('version', 'PostgreSQL...')]\n>>> v['version']\n'PostgreSQL...'\n\nConnection\n----------\n\nThe connection class initialises an internal connection pool and provides\nmethods to return a cursor object or execute SQL queries directly (using an\nimplicit cursor).\n\nThe intention is that a single instance of this class is created at\napplication start up.\n\nCursor\n------\n\nThe module provides a cursor context handler wrapping the psycopg2 cursor.\n\nEntering the cursor context handler will obtain a connection from the\nconnection pool and create a cursor using this connection. When the context\nhandler is exited the associated transaction will be committed, cursor\nclosed, and the connection released back to the connection pool.\n\nThe cursor object uses the psycopg2 'DictCursor' by default (which\nreturns rows as a pseudo python dictionary) however this can be overridden\nby providing a 'cursor_factory' parameter to the constructor.\n\n>>> db = pgwrap.connection(url='postgres://localhost')\n>>> with db.cursor() as c:\n... c.query('select version()')\n[['PostgreSQL...']]\n\nThe cursor context provides the following basic methods:\n\n execute - execute SQL query and return rowcount\n query - execute SQL query and fetch results\n query_one - execute SQL query and fetch first result\n query_dict - execute SQL query and return results as dict\n keyed on specified key (which should be unique)\n commit - Commit transaction (called implicitly on exiting\n context handler)\n rollback - Rollback transaction\n\nIn addition the cursor can use the SQL API methods described below or\naccess the underlying psycopg2 cursor (via the self.cursor attribute).\n\nThe cursor methods are also available as standalone functions which\nrun inside an implicit cursor object.\n\nSQL API\n-------\n\nThe cursor class also provides a simple Python API for common SQL\noperations. The basic methods provides are:\n\n select - single table select (with corresponding select_one,\n select_dict methods)\n join - two table join (with corresponding join_one,\n join_dict methods)\n insert - SQL insert\n update - SQL update\n delete - SQL delete\n\nThe methods can be parameterised to customise the associated query \n(see db module for detail): \n\n where - 'where' clause as dict (column operators can be \n specified using the colunm__operator format) \n\n where = {'name':'abc','status__in':(1,2,3)}\n\n columns - list of columns to be returned - these can \n be real columns or expressions. If spefified\n as a tuple the column is explicitly named\n using the AS operator\n\n columns = ('name',('status > 1','updated'))\n\n order - sort order as list (use 'column__desc' to\n reverse order)\n\n order = ('name__desc',)\n\n limit - row limit (int)\n\n offset - offset (int)\n\n on - join columns (as tuple)\n\n values - insert data as dict\n\n returning - columns to return (string)\n\nThe methods are also available as standalone functions which create an \nimplicit cursor object.\n\nBasic usage:\n\n >>> db.create_table('t1','id serial,name text,count int')\n >>> db.create_table('t2','id serial,t1_id int,value text')\n >>> db.log = sys.stdout\n >>> db.insert('t1',{'name':'abc','count':0},returning='id,name')\n INSERT INTO t1 (name) VALUES ('abc') RETURNING id,name\n [1, 'abc']\n >>> db.insert('t2',{'t1_id':1,'value':'t2'})\n INSERT INTO t2 (t1_id,value) VALUES (1,'t2')\n 1\n >>> db.select('t1')\n SELECT * FROM t1\n [[1, 'abc', 0]]\n >>> db.select_one('t1',where={'name':'abc'},columns=('name','count'))\n SELECT name, count FROM t1 WHERE name = 'abc'\n ['abc', 0]\n >>> db.join(('t1','t2'),columns=('t1.id','t2.value'))\n SELECT t1.id, t2.value FROM t1 JOIN t2 ON t1.id = t2.t1_id\n [[1, 't2']]\n >>> db.insert('t1',{'name':'abc'},returning='id')\n INSERT INTO t1 (name) VALUES ('abc') RETURNING id\n [2]\n >>> db.update('t1',{'name':'xyz'},where={'name':'abc'})\n UPDATE t1 SET name = 'xyz' WHERE name = 'abc'\n 2\n >>> db.update('t1',{'count__func':'count + 1'},where={'count__lt':10},returning=\"id,count\")\n UPDATE t1 SET count = count + 1 WHERE count < 10 RETURNING id,count\n [[1, 1]]\n\nPrepared Statements\n-------------------\n\n Prepared statements can be created using the\n\n connection.prepare(stmt,params,name,call_type) \n\n stmt : prepared statement (with parameters identified \n in the statement using the psql $1,$2... notation)\n params : list of optional parameter types (usually not \n needed - infered by psql)\n name : name for the prepared statement (usually\n autogenerated)\n call_type : method used when instance called as method\n (defaults to 'query')\n\n The constructor returns a PreparedStatement object which can be used\n instead of an sql statement in the connection.execute and\n connection.query_xxx methods.\n\n >>> p = db.prepare('UPDATE t1 SET name = $2 WHERE id = $1')\n PREPARE _pstmt_001 AS UPDATE t1 SET name = $2 WHERE id = $1\n >>> with db.cursor() as c:\n ... c.execute(p,(1,'xxx'))\n EXECUTE _pstmt_001 (1,'xxx')\n\n The PreparedStatement object can also be called directly using the\n execute/query/query_one/query_dict methods. The instance is also\n directly callable using the method type identified in 'call_type'\n\n >>> p = db.prepare('UPDATE t1 SET name = $2 WHERE id = $1')\n PREPARE _pstmt_001 AS UPDATE t1 SET name = $2 WHERE id = $1\n >>> p.execute(1,'xxx')\n EXECUTE _pstmt_001 (1,'xxx')\n >>> p(1,'xxx')\n EXECUTE _pstmt_001 (1,'xxx')\n\nLogging\n-------\n\n To enable logging the connection.log attribute can be set to either an\n instance of logging.Logger or a file-like object (supporting the write\n method).\n\n The log message is generated using the self.logf function (called with \n the cursor object as a parameter). By default this just returns the\n query string however can be customised as needed. A cursor.timestamp\n attribute is available to allow execution time to be tracked.\n\n >>> db.log = sys.stdout\n >>> db.logf = lambda c : '[%f] %s' % (time.time() - c.timestamp,c.query)\n >>> db.query('SELECT * FROM t1')\n [0.000536] SELECT * FROM t1\n\nChangelog\n---------\n\n * 0.1 19-10-2012 Initial import\n * 0.2 20-10-2012 Remove psycopg2 dep in setup.py\n * 0.3 20-10-2012 Remove hstore default for cursor\n * 0.4 21-10-2012 Add logging support \n * 0.5 22-12-2012 Refactor connection class / remove globals\n * 0.6 23-12-2012 Add support for prepared statements\n * 0.7 26-12-2012 Add callable prepared statements & named cursor\n * 0.8 02-02-2019 Support Python 3 (finally)\n\nAuthor\n------\n\n * Paul Chakravarti (paul.chakravarti@gmail.com)\n\nMaster Repository/Issues\n------------------------\n\n * https://github.com/paulchakravarti/pgwrap\n\n", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/paulchakravarti/pgwrap", "keywords": "", "license": "BSD", "maintainer": "", "maintainer_email": "", "name": "pgwrap", "package_url": "https://pypi.org/project/pgwrap/", "platform": "", "project_url": "https://pypi.org/project/pgwrap/", "project_urls": { "Homepage": "https://github.com/paulchakravarti/pgwrap" }, "release_url": "https://pypi.org/project/pgwrap/0.8.1/", "requires_dist": null, "requires_python": "", "summary": "Simple PostgreSQL database wrapper - provides wrapper over psycopg2 supporting a Python API for common sql functions (supports py2/py3)", "version": "0.8.1" }, "last_serial": 4775323, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "f52f7f4c471479f4f45e2f629404f52f", "sha256": "944b3127e711b72aa76b44d0fa63bfc68dd8595a3d802ef04d95ee633ba1dd2c" }, "downloads": -1, "filename": "pgwrap-0.1.tar.gz", "has_sig": false, "md5_digest": "f52f7f4c471479f4f45e2f629404f52f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5451, "upload_time": "2012-10-20T17:03:18", "url": "https://files.pythonhosted.org/packages/35/55/fcf7d5d8ef4509cbc4717140128e74ec66923276792837df55be6ca627e7/pgwrap-0.1.tar.gz" } ], "0.2": [ { "comment_text": "", "digests": { "md5": "1a6c0a472941bf5db9eee6dd5a6bb326", "sha256": "9d0ecccfe335b4df820813ba4e43c4fa6806419c3c5ba736f3deb78b06f944a5" }, "downloads": -1, "filename": "pgwrap-0.2.tar.gz", "has_sig": false, "md5_digest": "1a6c0a472941bf5db9eee6dd5a6bb326", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5384, "upload_time": "2012-10-20T22:18:52", "url": "https://files.pythonhosted.org/packages/77/50/f6841f77ae28862d3a0fd037443000071fdbb251e1993b41f76259e5b89b/pgwrap-0.2.tar.gz" } ], "0.3": [ { "comment_text": "", "digests": { "md5": "9d96d3d5f32b45e04e5e7f018adbe9d9", "sha256": "09b6928fbbc4e65c8970d9b9acd5c010489634a2b142d867288e10b493239af3" }, "downloads": -1, "filename": "pgwrap-0.3.tar.gz", "has_sig": false, "md5_digest": "9d96d3d5f32b45e04e5e7f018adbe9d9", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5314, "upload_time": "2012-10-20T22:52:04", "url": "https://files.pythonhosted.org/packages/31/b8/ce4c16f750e8d97d09caf71ef761f961a0b2978284920ae2ba2f85a2002d/pgwrap-0.3.tar.gz" } ], "0.4": [ { "comment_text": "", "digests": { "md5": "2cc3417c723ae904551059a9e24967d3", "sha256": "04fe88c582bdfb7f4d4defc42af215b35ce65f76f443f7774dbb8ed3adaf4a2c" }, "downloads": -1, "filename": "pgwrap-0.4.tar.gz", "has_sig": false, "md5_digest": "2cc3417c723ae904551059a9e24967d3", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7838, "upload_time": "2012-11-19T20:57:12", "url": "https://files.pythonhosted.org/packages/bb/bb/c776ccfa17ade7d7c5c6d22ba1e29f8ae58843559dfe3e9fa7a47ed21bb3/pgwrap-0.4.tar.gz" } ], "0.6": [ { "comment_text": "", "digests": { "md5": "69c1b59825ad142cc01305f63a38e50c", "sha256": "122e775daebe1aabce610eb335ef036f924b754b43688572d6b1bf347f9ac6a1" }, "downloads": -1, "filename": "pgwrap-0.6.tar.gz", "has_sig": false, "md5_digest": "69c1b59825ad142cc01305f63a38e50c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11221, "upload_time": "2012-12-23T22:28:24", "url": "https://files.pythonhosted.org/packages/1b/73/fc694644cf445f238f7cd0519ffdd181349d6403db7635b01020c8feb94a/pgwrap-0.6.tar.gz" } ], "0.7": [ { "comment_text": "", "digests": { "md5": "11a36783fd32f9cd9aa8e12bd80c48ba", "sha256": "48e9a0a7b8136494736d4aebb477edc176d884f3b67ded6558f7d0f95e67b23f" }, "downloads": -1, "filename": "pgwrap-0.7.tar.gz", "has_sig": false, "md5_digest": "11a36783fd32f9cd9aa8e12bd80c48ba", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11747, "upload_time": "2012-12-26T23:31:47", "url": "https://files.pythonhosted.org/packages/a0/c8/3e2dba2fae744eb6cba2499cfde328f6eb8e5db1a7e90bb567148ab7313d/pgwrap-0.7.tar.gz" } ], "0.8": [ { "comment_text": "", "digests": { "md5": "3d65779d60827ca6fba82fe4cb84af96", "sha256": "516ea109cadd9a79bdd46756b9e96dc29440ada0cb93ef7fa13b6b7cd3294d18" }, "downloads": -1, "filename": "pgwrap-0.8.tar.gz", "has_sig": false, "md5_digest": "3d65779d60827ca6fba82fe4cb84af96", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11984, "upload_time": "2019-02-02T18:26:18", "url": "https://files.pythonhosted.org/packages/c0/4e/41e1c869fdf4513d361651e8642c3308d5150351dccdc35035e4a8a0f081/pgwrap-0.8.tar.gz" } ], "0.8.1": [ { "comment_text": "", "digests": { "md5": "2b80fa2eb7a828cd2c049ebb37d3a86c", "sha256": "bac62f888ab087a38dc79b07c3a3c038be96c09420da8ed7c35c0fe85f2de505" }, "downloads": -1, "filename": "pgwrap-0.8.1.tar.gz", "has_sig": false, "md5_digest": "2b80fa2eb7a828cd2c049ebb37d3a86c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12091, "upload_time": "2019-02-03T18:13:00", "url": "https://files.pythonhosted.org/packages/33/fc/051ebc1fef17e6042f5f4ff023cfd15129c97f97276f2a8c491b636327c3/pgwrap-0.8.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "2b80fa2eb7a828cd2c049ebb37d3a86c", "sha256": "bac62f888ab087a38dc79b07c3a3c038be96c09420da8ed7c35c0fe85f2de505" }, "downloads": -1, "filename": "pgwrap-0.8.1.tar.gz", "has_sig": false, "md5_digest": "2b80fa2eb7a828cd2c049ebb37d3a86c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12091, "upload_time": "2019-02-03T18:13:00", "url": "https://files.pythonhosted.org/packages/33/fc/051ebc1fef17e6042f5f4ff023cfd15129c97f97276f2a8c491b636327c3/pgwrap-0.8.1.tar.gz" } ] }