Skip to content

Commit

Permalink
fix #40: introduce bulk_size for sqlalchemy
Browse files Browse the repository at this point in the history
  • Loading branch information
chfw committed Jul 20, 2017
1 parent 2f482fc commit 7bdcafe
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pyexcel_io/database/importers/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ class PyexcelSQLSkipRowException(Exception):
class SQLTableWriter(SheetWriter):
"""Write to a table
"""
def __init__(self, importer, adapter, auto_commit=True, **keywords):
def __init__(self, importer, adapter, auto_commit=True,
bulk_size=1000, **keywords):
SheetWriter.__init__(self, importer, adapter,
adapter.get_name(), **keywords)
self.__auto_commit = auto_commit
self.__count = 0
self.__bulk_size = bulk_size

def write_row(self, array):
if is_empty_array(array):
Expand Down Expand Up @@ -56,6 +59,10 @@ def _write_row(self, array):
key = name
setattr(obj, key, row[name])
self._native_book.session.add(obj)
if self.__auto_commit and self.__bulk_size != float('inf'):
self.__count += 1
if self.__count % self.__bulk_size == 0:
self._native_book.session.commit()

def close(self):
if self.__auto_commit:
Expand Down

0 comments on commit 7bdcafe

Please sign in to comment.