Skip to content
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Change log

#. pyexcel-io plugin interface has been rewritten. PyInstaller user will be
impacted. please read 'Packaging with Pyinstaller' in the documentation.
#. new query set reader plugin. pyexcel<=0.6.4 has used intrusive way of getting
query set source done. it is against the plugin interface.

0.5.20 - 17.7.2019
--------------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ releases:
details:
- pyexcel-io plugin interface has been rewritten. PyInstaller user will be impacted.
please read 'Packaging with Pyinstaller' in the documentation.
- new query set reader plugin. pyexcel<=0.6.4 has used intrusive way of getting query set
source done. it is against the plugin interface.
version: 0.6.0
date: tbd
- changes:
Expand Down
1 change: 1 addition & 0 deletions pyexcel_io/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
FILE_FORMAT_XLSM = "xlsm"
DB_SQL = "sql"
DB_DJANGO = "django"
DB_QUERYSET = "queryset"
KEYWORD_TSV_DIALECT = "excel-tab"
KEYWORD_LINE_TERMINATOR = "lineterminator"

Expand Down
6 changes: 5 additions & 1 deletion pyexcel_io/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
:license: New BSD License, see LICENSE for more details
"""
from pyexcel_io.plugins import NewIOPluginInfoChain
from pyexcel_io.constants import DB_SQL, DB_DJANGO
from pyexcel_io.constants import DB_SQL, DB_DJANGO, DB_QUERYSET

NewIOPluginInfoChain(__name__).add_a_reader(
relative_plugin_class_path="exporters.queryset.QueryReader",
locations=["file", "memory", "content"],
file_types=[DB_QUERYSET],
).add_a_reader(
relative_plugin_class_path="exporters.django.DjangoBookReader",
locations=["file", "memory", "content"],
file_types=[DB_DJANGO],
Expand Down
20 changes: 20 additions & 0 deletions pyexcel_io/database/exporters/queryset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from pyexcel_io.database.querysets import QuerysetsReader
from pyexcel_io.plugin_api.abstract_reader import IReader


class QueryReader(IReader):
def __init__(self, query_sets, _, column_names=None, **keywords):
self.query_sets = query_sets
self.column_names = column_names
self.keywords = keywords
self.content_array = [
QuerysetsReader(
self.query_sets, self.column_names, **self.keywords
)
]

def read_sheet(self, index):
return self.content_array[index]

def close(self):
pass
2 changes: 2 additions & 0 deletions pyexcel_io/database/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import datetime
from itertools import chain

from pyexcel_io.constants import DEFAULT_SHEET_NAME
from pyexcel_io.plugin_api.abstract_sheet import Sheet


class QuerysetsReader(Sheet):
""" turn querysets into an array """

def __init__(self, query_sets, column_names):
self.name = DEFAULT_SHEET_NAME
self.__column_names = column_names
self.__query_sets = query_sets

Expand Down
9 changes: 9 additions & 0 deletions tests/test_sql_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
)
from sqlalchemy.ext.declarative import declarative_base
from pyexcel_io.database.querysets import QuerysetsReader
from pyexcel_io.database.exporters.queryset import QueryReader
from pyexcel_io.database.exporters.sqlalchemy import (
SQLBookReader,
SQLTableReader,
Expand Down Expand Up @@ -226,6 +227,14 @@ def test_one_table(self):
reader = QuerysetsReader(query_sets, self.data[0])
results = reader.to_array()
assert list(results) == self.results

query_sets = mysession.query(Pyexcel).all()
query_reader = QueryReader(query_sets, None, column_names=self.data[0])
result = query_reader.read_all()
for key in result:
result[key] = list(result[key])
eq_(result, {"pyexcel_sheet1": self.results})
query_reader.close()
mysession.close()

def test_update_existing_row(self):
Expand Down