Skip to content

Commit

Permalink
Merge pull request #63 from uber/ak_transaction
Browse files Browse the repository at this point in the history
fix transaction/commits
  • Loading branch information
alexjikim committed May 8, 2015
2 parents e0482fc + 845ff7a commit 1ea6923
Show file tree
Hide file tree
Showing 8 changed files with 344 additions and 46 deletions.
103 changes: 74 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

[![PyPI version](https://badge.fury.io/py/vertica-python.png)](http://badge.fury.io/py/vertica-python)

0.4.2 breaks some of the older query interfaces (row_handler callback, and connection.query).
0.4.x breaks some of the older query interfaces (row_handler callback, and connection.query).
It replaces the row_handler callback with an iterate() method. Please see examples below

If you are on 0.4.0 - 0.4.3, please upgrade to 0.4.5 as there are various bug fixes

vertica-python is a native Python adapter for the Vertica (http://www.vertica.com) database.

vertica-python is currently in alpha stage; it has been tested for functionality, but does not have a test suite. Please use with caution, and feel free to submit issues and/or pull requests.
vertica-python is currently in beta stage; it has been tested for functionality and has a very basic test suite. Please use with caution, and feel free to submit issues and/or pull requests (after running the unit tests).

vertica-python has been tested with Vertica 6.1.2/7.0.0+ and Python 2.6/2.7.

Expand All @@ -31,50 +33,78 @@ Source code for vertica-python can be found at:

http://github.com/uber/vertica-python


## Run unit tests
# install nose if you don't have it
pip install -r requirements_test.txt

# you will need to have access to a vertica database.
# connection info is in tests/basic_tests.py

# run tests
nosetests


## Usage
**Stream** results:


**Create connection**

```python
from vertica_python import connect

with vertica_python.connect({'host': '127.0.0.1',
'port': 5433,
'user': 'some_user',
'password': 'some_password',
'database': 'a_database'}) as connection:
cur = connection.cursor()
cur.execute("SELECT * FROM a_table LIMIT 2")
for row in cur.iterate():
print(row)
# {'id': 1, 'value': 'something'}
# {'id': 2, 'value': 'something_else'}
conn_info = {'host': '127.0.0.1',
'port': 5433,
'user': 'some_user',
'password': 'some_password',
'database': 'a_database'}

# simple connection, with manual close
connection = vertica_python.connect(conn_info)
# do things
connection.close()

# using with for auto connection closing after usage
with vertica_python.connect(conn_info) as connection:
# do things
```


**Stream query results**:

```python
cur = connection.cursor()
cur.execute("SELECT * FROM a_table LIMIT 2")
for row in cur.iterate():
print(row)
# {'id': 1, 'value': 'something'}
# {'id': 2, 'value': 'something_else'}
```
Streaming is recommended if you want to further process each row, save the results in a non-list/dict format (e.g. Pandas DataFrame), or save the results in a file.

**In-memory** results as list:

**In-memory results as list**:

```python
with vertica_python.connect(...) as connection:
cur = connection.cursor()
cur.execute("SELECT * FROM a_table LIMIT 2")
cur.fetchall()
# [ [1, 'something'], [2, 'something_else'] ]
cur = connection.cursor()
cur.execute("SELECT * FROM a_table LIMIT 2")
cur.fetchall()
# [ [1, 'something'], [2, 'something_else'] ]
```


**In-memory** results as dictionary:
**In-memory results as dictionary**:

```python
with vertica_python.connect(...) as connection:
cur = connection.cursor('dict')
cur.execute("SELECT * FROM a_table LIMIT 2")
cur.fetchall()
cur = connection.cursor('dict')
cur.execute("SELECT * FROM a_table LIMIT 2")
cur.fetchall()
# [ {'id': 1, 'value': 'something'}, {'id': 2, 'value': 'something_else'} ]
connection.close()
```


**Using named parameters** :
**Query using named parameters**:

```python
# Using named parameter bindings requires psycopg2>=2.5.1 which is not includes with the base vertica_python requirements.
Expand All @@ -85,15 +115,30 @@ cur.fetchall()
# [ [1, 'something'], [2, 'something_else'] ]
```

**Insert and commits** :

```python
cur = connection.cursor()

# inline commit
cur.execute("INSERT INTO a_table (a, b) VALUES (1, 'aa'); commit;")

# commit in execution
cur.execute("INSERT INTO a_table (a, b) VALUES (1, 'aa')")
cur.execute("INSERT INTO a_table (a, b) VALUES (2, 'bb')")
cur.execute("commit;")

# connection.commit()
cur.execute("INSERT INTO a_table (a, b) VALUES (1, 'aa')")
connection.commit()
```


**Copy** :

```python
cur = connection.cursor()
cur.copy("COPY test_copy (id, name) from stdin DELIMITER ',' ", "1,foo\n2,bar")

# input stream copy is todo

```


Expand Down
1 change: 1 addition & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nose==1.3.6
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# version should use the format 'x.x.x' (instead of 'vx.x.x')
setup(
name='vertica-python',
version='0.4.4',
version='0.4.5',
description='A native Python client for the Vertica database.',
author='Justin Berka, Alex Kim, Kenneth Tran',
author_email='justin.berka@gmail.com, alex.kim@uber.com, tran@uber.com',
Expand Down
4 changes: 1 addition & 3 deletions vertica_python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
# Main module for this library.

# The version number of this library.
version_info = (0, 4, 3)
version_info = (0, 4, 4)
version_info = (0, 4, 5)

__version__ = '.'.join(map(str, version_info))

Expand All @@ -28,5 +27,4 @@

def connect(options):
"""Opens a new connection to a Vertica database."""

return Connection(options)
Empty file.
Loading

0 comments on commit 1ea6923

Please sign in to comment.