Skip to content

Commit

Permalink
Implemented cursor and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Bader committed Sep 3, 2013
1 parent eb8c4d6 commit b8b1258
Show file tree
Hide file tree
Showing 15 changed files with 525 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ parts/
develop-eggs/
.installed.cfg
crate.egg-info
dist
build
45 changes: 37 additions & 8 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
===================
Crate Python Client
===================

.. image:: http://www.crate-technology.com/assets/img/logo.png
:width: 155px
:height: 45px
:alt: Crate-Technology
:target: http://www.crate-technology.com/

Overview
========

The crate client distribution is a nativ python
client offering tools for interacting with the
crate database.
crate server.

Installation
============

Installing via pip
------------------

To install the crate client via `pip
<https://pypi.python.org/pypi/pip>`_
use the following command::

$ pip install crate

To update use::
$ pip install -U crate

Installing via easy_install
---------------------------

If you prefer easy_install which is provided by
`setuptools <https://pypi.python.org/pypi/setuptools/1.1>`_
use the following command::

$ easy_install crate

For details about usage, please refer to the
documentation found under the ``docs`` directory.
To update use::

Developer specific documentation can be found
in the ``DEVELOP.rst`` document.
$ easy_install -U crate
Empty file removed docs/__init__.py
Empty file.
Empty file removed docs/index.txt
Empty file.
25 changes: 16 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@

from setuptools import setup
import crate
import os


if os.path.isfile('README.rst'):
readme = open('README.rst').read()
else:
readme = ''
def read(path):
return open(os.path.join(os.path.dirname(__file__), path)).read()

long_description = (
read('README.rst')
+ '\n' +
read('src/crate/client/client.txt')
+ '\n')

packages = [
'crate',
'crate.client'
]

setup(
name='crate',
url='https://github.com/crate/crate-python',
version=crate.__version__,
author='Lovely Systems',
author_email='office@lovelysystems.com',
package_dir={'': 'src'},
description=crate.__doc__.strip(),
long_description=readme,
description='Crate client',
long_description=long_description,
platforms=['any'],
packages=['crate'],
packages=packages,
extras_require=dict(
test=['lovely.testlayers']),
install_requires=[
Expand Down
8 changes: 8 additions & 0 deletions src/crate/client/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

apilevel = "2.0"

#TODO: correct values after finshed implementation
threadsafety = 1
paramstyle = "qmark"

from .connection import connect
28 changes: 28 additions & 0 deletions src/crate/client/connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

from .cursor import Cursor


class Connection(object):

def __init__(self, servers, client):
self.client = client

def cursor(self):
return Cursor(self)

def close(self):
pass

def commit(self):
pass

def _connect(self, hosts):
pass

def _endpoint(self):
pass


def connect(servers=None, crate_client=None):
return Connection(servers, crate_client)

12 changes: 12 additions & 0 deletions src/crate/client/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@



class Client(object):

def __init__(self):
self.response = {}

def sql(self, stmt=None):
return self.response


115 changes: 115 additions & 0 deletions src/crate/client/cursor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# -*- encoding: utf-8 -*-

from .exceptions import ProgrammingError


class Cursor():

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

def execute(self, sql):
self._result = self.connection.client.sql(sql)
self.rows = iter(self._result["rows"])
self._closed = False

def executemany(self, sql):
raise NotImplementedError

def fetchone(self):
return self.next()

def next(self):
try:
return self._next()
except StopIteration:
return None

def fetchmany(self, count=None):
if count is None:
count = self.arraysize
if count == 0:
return self.fetchall()
result = []
for i in range(count):
try:
result.append(self._next())
except StopIteration:
pass
return result

def fetchall(self):
result = []
iterate = True
while iterate:
try:
result.append(self._next())
except StopIteration:
iterate = False
return result

def close(self):
self._closed = True
self._result = None

def commit(self):
"""
Transactions not supported, nothing to do here.
"""
pass

def setinputsizes(self, sizes):
"""
This can be used before a call to .execute*() to predefine memory areas
for the operation's parameters.
sizes is specified as a sequence — one item for each input parameter.
The item should be a Type Object that corresponds to the input that will be used,
or it should be an integer specifying the maximum length of a string parameter.
If the item is None, then no predefined memory area will be reserved for
that column (this is useful to avoid predefined areas for large inputs).
This method would be used before the .execute*() method is invoked.
Implementations are free to have this method do nothing and users are
free to not use it.
"""
pass

def setoutputsize(self, size, column=None):
"""
Set a column buffer size for fetches of large columns (e.g. LONGs, BLOBs, etc.).
The column is specified as an index into the result sequence.
Not specifying the column will set the default size for all large columns in the cursor.
This method would be used before the .execute*() method is invoked.
Implementations are free to have this method do nothing and users are free to not use
it.
"""
pass

@property
def rowcount(self):
if not self._closed:
return len(self._result["rows"])
return -1

def _next(self):
if not self._closed:
return self.rows.next()
else:
raise ProgrammingError

@property
def description(self):
if self._closed:
return

description = []
for col in self._result["cols"]:
description.append((col,
None,
None,
None,
None,
None,
None))
return tuple(description)
Loading

0 comments on commit b8b1258

Please # to comment.