Skip to content
This repository has been archived by the owner on Sep 24, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' into upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
haizaar committed Apr 29, 2015
2 parents 4f1babf + 9213cb4 commit 5b3733b
Show file tree
Hide file tree
Showing 9 changed files with 1,197 additions and 1,173 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -15,7 +15,7 @@ env:
- MOMOKO_PSYCOPG2_IMPL=psycopg2
- MOMOKO_PSYCOPG2_IMPL=psycopg2cffi

install: "pip install --use-mirrors tornado ${MOMOKO_PSYCOPG2_IMPL}"
install: "pip install --use-mirrors tornado ${MOMOKO_PSYCOPG2_IMPL} unittest2"
script: python setup.py test

matrix:
Expand Down
127 changes: 67 additions & 60 deletions examples/gen_example.py
Expand Up @@ -6,6 +6,8 @@
.. _gen: http://www.tornadoweb.org/documentation/gen.html
"""

from __future__ import print_function

import os

import tornado.web
Expand All @@ -23,6 +25,7 @@
db_host = os.environ.get('MOMOKO_TEST_HOST', '')
db_port = os.environ.get('MOMOKO_TEST_PORT', 5432)
enable_hstore = True if os.environ.get('MOMOKO_TEST_HSTORE', False) == '1' else False
enable_json = True if os.environ.get('MOMOKO_TEST_JSON', False) == '1' else False
dsn = 'dbname=%s user=%s password=%s host=%s port=%s' % (
db_database, db_user, db_password, db_host, db_port)

Expand All @@ -40,25 +43,26 @@ def db(self):

class OverviewHandler(BaseHandler):
def get(self):
self.write('''
self.write("""
<ul>
<li><a href="/mogrify">Mogrify</a></li>
<li><a href="/query">A single query</a></li>
<li><a href="/hstore">An hstore query</a></li>
<li><a href="/hstore">A hstore query</a></li>
<li><a href="/json">A JSON query</a></li>
<li><a href="/transaction">A transaction</a></li>
<li><a href="/multi_query">Multiple queries executed with gen.Task</a></li>
<li><a href="/callback_and_wait">Multiple queries executed with gen.Callback and gen.Wait</a></li>
<li><a href="/multi_query">Multiple queries executed by yielding a list</a></li>
<li><a href="/connection">Manual connection management</a></li>
</ul>
''')
""")
self.finish()


class MogrifyHandler(BaseHandler):
@gen.coroutine
def get(self):
try:
sql = yield momoko.Op(self.db.mogrify, 'SELECT %s;', (1,))
self.write('SQL: %s<br>' % sql)
sql = yield self.db.mogrify("SELECT %s;", (1,))
self.write("SQL: %s<br>" % sql)
except Exception as error:
self.write(str(error))

Expand All @@ -69,8 +73,8 @@ class SingleQueryHandler(BaseHandler):
@gen.coroutine
def get(self):
try:
cursor = yield momoko.Op(self.db.execute, 'SELECT pg_sleep(%s);', (1,))
self.write('Query results: %s<br>\n' % cursor.fetchall())
cursor = yield self.db.execute("SELECT pg_sleep(%s);", (1,))
self.write("Query results: %s<br>\n" % cursor.fetchall())
except Exception as error:
self.write(str(error))

Expand All @@ -82,15 +86,29 @@ class HstoreQueryHandler(BaseHandler):
def get(self):
if enable_hstore:
try:
cursor = yield momoko.Op(self.db.execute, "SELECT 'a=>b, c=>d'::hstore;")
self.write('Query results: %s<br>' % cursor.fetchall())
cursor = yield momoko.Op(self.db.execute, "SELECT %s;",
({'e': 'f', 'g': 'h'},))
self.write('Query results: %s<br>' % cursor.fetchall())
cursor = yield self.db.execute("SELECT 'a=>b, c=>d'::hstore;")
self.write("Query results: %s<br>" % cursor.fetchall())
cursor = yield self.db.execute("SELECT %s;", ({"e": "f", "g": "h"},))
self.write("Query results: %s<br>" % cursor.fetchall())
except Exception as error:
self.write(str(error))
else:
self.write("hstore is not enabled")

self.finish()


class JsonQueryHandler(BaseHandler):
@gen.coroutine
def get(self):
if enable_json:
try:
cursor = yield self.db.execute('SELECT \'{"a": "b", "c": "d"}\'::json;')
self.write("Query results: %s<br>" % cursor.fetchall())
except Exception as error:
self.write(str(error))
else:
self.write('hstore is not enabled')
self.write("json is not enabled")

self.finish()

Expand All @@ -99,14 +117,14 @@ class MultiQueryHandler(BaseHandler):
@gen.coroutine
def get(self):
cursor1, cursor2, cursor3 = yield [
momoko.Op(self.db.execute, 'SELECT 1;'),
momoko.Op(self.db.mogrify, 'SELECT 2;'),
momoko.Op(self.db.execute, 'SELECT %s;', (3*1,))
self.db.execute("SELECT 1;"),
self.db.mogrify("SELECT 2;"),
self.db.execute("SELECT %s;", (3*1,))
]

self.write('Query 1 results: %s<br>' % cursor1.fetchall())
self.write('Query 2 results: %s<br>' % cursor2)
self.write('Query 3 results: %s' % cursor3.fetchall())
self.write("Query 1 results: %s<br>" % cursor1.fetchall())
self.write("Query 2 results: %s<br>" % cursor2)
self.write("Query 3 results: %s" % cursor3.fetchall())

self.finish()

Expand All @@ -115,49 +133,23 @@ class TransactionHandler(BaseHandler):
@gen.coroutine
def get(self):
try:
cursors = yield momoko.Op(self.db.transaction, (
'SELECT 1, 12, 22, 11;',
'SELECT 55, 22, 78, 13;',
'SELECT 34, 13, 12, 34;',
'SELECT 23, 12, 22, 23;',
'SELECT 42, 23, 22, 11;',
('SELECT 49, %s, 23, 11;', ('STR',)),
cursors = yield self.db.transaction((
"SELECT 1, 12, 22, 11;",
"SELECT 55, 22, 78, 13;",
"SELECT 34, 13, 12, 34;",
"SELECT 23, 12, 22, 23;",
"SELECT 42, 23, 22, 11;",
("SELECT 49, %s, 23, 11;", ("STR",)),
))

for i, cursor in enumerate(cursors):
self.write('Query %s results: %s<br>' % (i, cursor.fetchall()))
self.write("Query %s results: %s<br>" % (i, cursor.fetchall()))
except Exception as error:
self.write(str(error))

self.finish()


class CallbackWaitHandler(BaseHandler):
@gen.coroutine
def get(self):

self.db.execute('SELECT 42, 12, %s, 11;', (25,),
callback=(yield gen.Callback('q1')))
self.db.execute('SELECT 42, 12, %s, %s;', (23, 56),
callback=(yield gen.Callback('q2')))
self.db.execute('SELECT 465767, 4567, 3454;',
callback=(yield gen.Callback('q3')))

# Separately...
# cursor1 = yield momoko.WaitOp('q1')
# cursor2 = yield momoko.WaitOp('q2')
# cursor3 = yield momoko.WaitOp('q3')

# Or all at once
cursor1, cursor2, cursor3 = yield momoko.WaitAllOps(('q1', 'q2', 'q3'))

self.write('Query 1 results: %s<br>' % cursor1.fetchall())
self.write('Query 2 results: %s<br>' % cursor2.fetchall())
self.write('Query 3 results: %s' % cursor3.fetchall())

self.finish()


class ConnectionQueryHandler(BaseHandler):
def __init__(self, *args, **kwargs):
self.http_connection_closed = False
Expand All @@ -166,12 +158,12 @@ def __init__(self, *args, **kwargs):
@gen.coroutine
def get(self):
try:
connection = yield momoko.Op(self.db.getconn)
connection = yield self.db.getconn()
with self.db.manage(connection):
for i in range(5):
if self.http_connection_closed:
break
cursor = yield momoko.Op(connection.execute, 'SELECT pg_sleep(1);')
cursor = yield connection.execute("SELECT pg_sleep(1);")
self.write('Query %d results: %s<br>\n' % (i+1, cursor.fetchall()))
self.flush()
except Exception as error:
Expand All @@ -191,26 +183,41 @@ def main():
(r'/mogrify', MogrifyHandler),
(r'/query', SingleQueryHandler),
(r'/hstore', HstoreQueryHandler),
(r'/json', JsonQueryHandler),
(r'/transaction', TransactionHandler),
(r'/multi_query', MultiQueryHandler),
(r'/callback_and_wait', CallbackWaitHandler),
(r'/connection', ConnectionQueryHandler),
], debug=True)

ioloop = tornado.ioloop.IOLoop.instance()

application.db = momoko.Pool(
dsn=dsn,
size=1,
max_size=3,
ioloop=ioloop,
setsession=("SET TIME ZONE UTC",),
raise_connect_errors=False,
)

# this is a one way to run ioloop in sync
future = application.db.connect()
ioloop.add_future(future, lambda f: ioloop.stop())
ioloop.start()

if enable_hstore:
application.db.register_hstore()
future = application.db.register_hstore()
# This is the other way to run ioloop in sync
ioloop.run_sync(lambda: future)

if enable_json:
future = application.db.register_json()
# This is the other way to run ioloop in sync
ioloop.run_sync(lambda: future)

http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8888, 'localhost')
tornado.ioloop.IOLoop.instance().start()
ioloop.start()
except KeyboardInterrupt:
print('Exit')

Expand Down
6 changes: 2 additions & 4 deletions momoko/__init__.py
Expand Up @@ -10,11 +10,9 @@
"""

import psycopg2
from psycopg2 import ProgrammingError

from .connection import Pool, Connection
from .exceptions import PoolError
from .utils import Op, WaitOp, WaitAllOps
from .connection import Pool, Connection, connect
from .exceptions import PoolError, PartiallyConnectedError


try:
Expand Down

0 comments on commit 5b3733b

Please sign in to comment.