Skip to content

Commit

Permalink
Merge branch 'master' of github.com:crate/crate-python
Browse files Browse the repository at this point in the history
  • Loading branch information
dobe committed Sep 5, 2013
2 parents 0c3869e + 1c42757 commit be21b0c
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 54 deletions.
4 changes: 2 additions & 2 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include LICENSE
include *.rst
recursive-include src *.txt *.rst *.json
global-exclude *.pyc
recursive-include src *.txt *.rst
recursive-exclude src tests*.py
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def read(path):
platforms=['any'],
license='Apache License 2.0',
keywords='crate db api',
packages=find_packages(),
packages=find_packages('src'),
namespace_packages = ['crate'],
extras_require=dict(
test=['lovely.testlayers']
),
Expand Down
4 changes: 2 additions & 2 deletions src/crate/client/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def cursor(self):
if not self._closed:
return Cursor(self)
else:
raise ProgrammingError
raise ProgrammingError("Connection closed")

def close(self):
"""
Expand All @@ -34,7 +34,7 @@ def commit(self):
if not self._closed:
pass
else:
raise ProgrammingError
raise ProgrammingError("Connection closed")


def connect(servers=None, timeout=None, client=None):
Expand Down
6 changes: 3 additions & 3 deletions src/crate/client/connection.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ raise a ``ProgrammingError``::
>>> cursor = connection.cursor()
Traceback (most recent call last):
...
ProgrammingError
ProgrammingError: Connection closed

>>> cursor.execute('')
Traceback (most recent call last):
...
ProgrammingError
ProgrammingError: Connection closed

>>> connection.commit()
Traceback (most recent call last):
...
ProgrammingError
ProgrammingError: Connection closed

>>> connection = connect(client=connection_client_mocked)
>>> cursor = connection.cursor()
Expand Down
45 changes: 11 additions & 34 deletions src/crate/client/cursor.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,29 @@
# -*- encoding: utf-8 -*-
from functools import wraps

from .exceptions import ProgrammingError

def check_connection(f):
"""
Method decorator for checking if the ``Connection`` was closed.
If so raise a ``ProgrammingError``.
"""
def wrapper(*args):
cursor = args[0]
# special case for __init__
if not hasattr(cursor, 'connection'):
if args[1]._closed:
raise ProgrammingError
elif cursor.connection._closed:
raise ProgrammingError
return f(*args)
return wrapper

def for_all_methods(decorator, exclude=()):
"""
Class decorator for applying a method decorator to all method except those
passed by ``exclude`` argument.
"""
def decorate(cls):
for attr in cls.__dict__:
if callable(getattr(cls, attr)) and attr not in exclude:
setattr(cls, attr, decorator(getattr(cls, attr)))
return cls
return decorate

@for_all_methods(check_connection)
class Cursor():
class Cursor(object):

def __init__(self, connection):
self.arraysize = 1
self.connection = connection
self._closed = True
self._closed = False
self._result = None

def execute(self, sql):
"""
Prepare and execute a database operation (query or command).
"""
if self.connection._closed:
raise ProgrammingError("Connection closed")

if self._closed:
raise ProgrammingError("Cursor closed")

self._result = self.connection.client.sql(sql)
if "rows" in self._result:
self.rows = iter(self._result["rows"])
self._closed = False

def executemany(self, sql, seq_of_parameters):
"""
Expand Down Expand Up @@ -130,7 +107,7 @@ def rowcount(self):
"""
This read-only attribute specifies the number of rows that the last .execute*() produced
(for DQL statements like ``SELECT``) or affected (for DML statements like ``UPDATE``
or ``INSERT``).
or ``INSERT``).
"""
if (
self._closed
Expand All @@ -147,7 +124,7 @@ def _next(self):
if not self._closed:
return self.rows.next()
else:
raise ProgrammingError
raise ProgrammingError("Cursor closed")

@property
def description(self):
Expand Down
9 changes: 5 additions & 4 deletions src/crate/client/cursor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ close()
After closing a cursor the connection will be unusable. If any operation is attempted with the
closed connection an ``ProgrammingError`` exception will be raised::

>>> cursor = connection.cursor()
>>> cursor.execute('')
>>> cursor.fetchone()
['North West Ripple', 1]
Expand All @@ -151,19 +152,19 @@ closed connection an ``ProgrammingError`` exception will be raised::
>>> cursor.fetchone()
Traceback (most recent call last):
...
ProgrammingError
ProgrammingError: Cursor closed

>>> cursor.fetchmany()
Traceback (most recent call last):
...
ProgrammingError
ProgrammingError: Cursor closed

>>> cursor.fetchall()
Traceback (most recent call last):
...
ProgrammingError
ProgrammingError: Cursor closed

>>> cursor.next()
Traceback (most recent call last):
...
ProgrammingError
ProgrammingError: Cursor closed
3 changes: 3 additions & 0 deletions src/crate/client/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ class Client(object):
"""

sql_path = '_sql'
"""Crate URI path for issuing SQL statements."""

retry_interval = 30
"""Retry interval for failed servers in seconds."""

default_server = "127.0.0.1:9200"
"""Default server to use if no servers are given on instantiation."""

def __init__(self, servers=None, timeout=None):
if not servers:
Expand Down
13 changes: 7 additions & 6 deletions src/crate/client/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ The following command closes the cursor::
>>> cursor.close()

If a cursor is closed, it will be unusable from this point forward.
If any operation is attempted to a closed cursor an ``ProgrammingError`` will be raised::
If any operation is attempted to a closed cursor an ``ProgrammingError`` will be raised.

>>> cursor.fetchone()
>>> cursor.execute("SELECT * FROM locations")
Traceback (most recent call last):
...
ProgrammingError
ProgrammingError: Cursor closed

Closing the Connection
======================
Expand All @@ -162,17 +162,18 @@ The following command closes the connection::
>>> connection.close()

If a connection is closed, it will be unusable from this point forward.
If any operation is attempted to a closed connection an ``ProgrammingError`` will be raised::
If any operation using the connection is attempted to a closed connection an ``ProgrammingError``
will be raised::

>>> cursor.execute("SELECT * FROM locations")
Traceback (most recent call last):
...
ProgrammingError
ProgrammingError: Connection closed

>>> cursor = connection.cursor()
Traceback (most recent call last):
...
ProgrammingError
ProgrammingError: Connection closed



Expand Down
2 changes: 0 additions & 2 deletions src/crate/testing/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import doctest
import os

from .layer import CrateLayer


def docs_path(*parts):
return os.path.join(os.path.dirname(os.path.dirname(__file__)), *parts)
Expand Down

0 comments on commit be21b0c

Please sign in to comment.