Simplified PostgreSQL client built upon Psycopg2
Project description
Queries is a BSD licensed opinionated wrapper of the psycopg2 library for interacting with PostgreSQL.
The popular psycopg2 package is a full-featured python client. Unfortunately as a developer, you’re often repeating the same steps to get started with your applications that use it. Queries aims to reduce the complexity of psycopg2 while adding additional features to make writing PostgreSQL client applications both fast and easy. Check out the Usage section below to see how easy it can be.
Key features include:
Simplified API
Support of Python 2.6+ and 3.2+
PyPy support via psycopg2ct
Asynchronous support for Tornado
Connection information provided by URI
Query results delivered as a generator based iterators
Automatically registered data-type support for UUIDs, Unicode and Unicode Arrays
Ability to directly access psycopg2 connection and cursor objects
Internal connection pooling
Documentation
Documentation is available at https://queries.readthedocs.org
Installation
Queries is available via pypi and can be installed with easy_install or pip:
pip install queries
Usage
Queries provides a session based API for interacting with PostgreSQL. Simply pass in the URI of the PostgreSQL server to connect to when creating a session:
session = queries.Session("postgresql://postgres@localhost:5432/postgres")
Queries built-in connection pooling will re-use connections when possible, lowering the overhead of connecting and reconnecting.
When specifying a URI, if you omit the username and database name to connect with, Queries will use the current OS username for both. You can also omit the URI when connecting to connect to localhost on port 5432 as the current OS user, connecting to a database named for the current user. For example, if your username is fred and you omit the URI when issuing queries.query the URI that is constructed would be postgresql://fred@localhost:5432/fred.
If you’d rather use individual values for the connection, the queries.uri() method provides a quick and easy way to create a URI to pass into the various methods.
>>> queries.uri("server-name", 5432, "dbname", "user", "pass")
'postgresql://user:pass@server-name:5432/dbname'
Environment Variables
Currently Queries uses the following environment variables for tweaking various configuration values. The supported ones are:
QUERIES_MAX_POOL_SIZE - Modify the maximum size of the connection pool (default: 1)
Using the queries.Session class
To execute queries or call stored procedures, you start by creating an instance of the queries.Session class. It can act as a context manager, meaning you can use it with the with keyword and it will take care of cleaning up after itself. For more information on the with keyword and context managers, see PEP343.
In addition to both the queries.Session.query and queries.Session.callproc methods that are similar to the simple API methods, the queries.Session class provides access to the psycopg2 connection and cursor objects.
Using queries.Session.query
The following example shows how a queries.Session object can be used as a context manager to query the database table:
>>> import pprint
>>> import queries
>>>
>>> with queries.Session() as session:
... for row in session.query('SELECT * FROM names'):
... pprint.pprint(row)
...
{'id': 1, 'name': u'Jacob'}
{'id': 2, 'name': u'Mason'}
{'id': 3, 'name': u'Ethan'}
Using queries.Session.callproc
This example uses queries.Session.callproc to execute a stored procedure and then pretty-prints the single row results as a dictionary:
>>> import pprint
>>> import queries
>>> with queries.Session() as session:
... results = session.callproc('chr', [65])
... pprint.pprint(results.as_dict())
...
{'chr': u'A'}
Asynchronous Queries with Tornado
In addition to providing a Pythonic, synchronous client API for PostgreSQL, Queries provides a very similar asynchronous API for use with Tornado. The only major difference API difference between queries.TornadoSession and queries.Session is the TornadoSession.query and TornadoSession.callproc methods return the entire result set instead of acting as an iterator over the results. The following example uses TornadoSession.query in an asynchronous Tornado web application to send a JSON payload with the query result set.
from tornado import gen, ioloop, web
import queries
class MainHandler(web.RequestHandler):
def initialize(self):
self.session = queries.TornadoSession()
@gen.coroutine
def get(self):
results = yield self.session.query('SELECT * FROM names')
self.finish({'data': results.items()})
results.free()
application = web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
ioloop.IOLoop.instance().start()
Inspiration
Queries is inspired by Kenneth Reitz’s awesome work on requests.
History
Queries is a fork and enhancement of pgsql_wrapper, which can be found in the main GitHub repository of Queries as tags prior to version 1.2.0.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file queries-1.7.2.tar.gz
.
File metadata
- Download URL: queries-1.7.2.tar.gz
- Upload date:
- Size: 16.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e91b7a3c4d8a2a23cfdd354a30efca64ca50884226d0a7523f05988e1677458 |
|
MD5 | a723ce124f949b37027460fd0ff6aa91 |
|
BLAKE2b-256 | a6cb89ce7621b9ef7c09924974e746e79ffd794a17b33721ca2cc90adc62207e |