Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

capture queryset as list of dictionaries. #102

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@ The transaction is created when the first SQL statement is executed.
exits the *with* context and the queries succeed, otherwise
`trino.dbapi.Connection.rollback()` will be called.

# Fetch as json
If you need to fetch your queryset as a list of dictionaries (with column names as keys) you can use the alternative for fetchall method on the cursor.

```python
import trino
conn = trino.dbapi.connect(
host='localhost',
port=8080,
user='the-user',
catalog='the-catalog',
schema='the-schema',
)
cur = conn.cursor()
cur.execute('SELECT * FROM system.runtime.nodes')
rows = cur.fetchjson()
```


# Development

## Getting Started With Development
Expand All @@ -129,9 +147,21 @@ For development purpose, pip can reference the code you are modifying in a
$ pip install -e .[tests]
```


That way, you do not need to run `pip install` again to make your changes
applied to the *virtualenv*.

If you use debian or redhat linux distributions. you may need to install following to be able to install pip packages:

for Debian/Ubuntu/etc:
```
$ sudo apt-get install gcc python-dev libkrb5-dev
```

for RHEL/CentOS/etc:
```
$ sudo yum install gcc python-devel krb5-devel krb5-workstation python-devel
```
Comment on lines +154 to +164
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend separating a PR since it's unrelated to dictionary change.

When the code is ready, submit a Pull Request.

## Code Style
Expand Down
5 changes: 5 additions & 0 deletions trino/dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,11 @@ def cancel(self):
def close(self):
self._connection.close()

def fetchjson(self):
columns = [col['name'] for col in self._query.columns]
rows = self.fetchall()
return [dict(zip(columns, item)) for item in rows]
Comment on lines +469 to +472
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we implement like this, there is less benefit because users can achieve the same thing easily with the existing code and cannot use in case of fetchone. Could you take a look at other library's implementation I shared in the issue?



Date = datetime.date
Time = datetime.time
Expand Down