radically simplify upsert_many; refs #352.
This commit is contained in:
parent
5e09aba401
commit
2b1947e407
@ -264,28 +264,15 @@ class Table(object):
|
|||||||
def upsert_many(self, rows, keys, chunk_size=1000, ensure=None, types=None):
|
def upsert_many(self, rows, keys, chunk_size=1000, ensure=None, types=None):
|
||||||
"""
|
"""
|
||||||
Sorts multiple input rows into upserts and inserts. Inserts are passed
|
Sorts multiple input rows into upserts and inserts. Inserts are passed
|
||||||
to insert_many and upserts are updated.
|
to insert and upserts are updated.
|
||||||
|
|
||||||
See :py:meth:`upsert() <dataset.Table.upsert>` and
|
See :py:meth:`upsert() <dataset.Table.upsert>` and
|
||||||
:py:meth:`insert_many() <dataset.Table.insert_many>`.
|
:py:meth:`insert_many() <dataset.Table.insert_many>`.
|
||||||
"""
|
"""
|
||||||
keys = ensure_list(keys)
|
# Removing a bulk implementation in 5e09aba401. Doing this one by one
|
||||||
|
# is incredibly slow, but doesn't run into issues with column creation.
|
||||||
to_insert = []
|
|
||||||
to_update = []
|
|
||||||
for row in rows:
|
for row in rows:
|
||||||
if self.find_one(**{key: row.get(key) for key in keys}):
|
self.upsert(row, keys, ensure=ensure, types=types)
|
||||||
# Row exists - update it.
|
|
||||||
to_update.append(row)
|
|
||||||
else:
|
|
||||||
# Row doesn't exist - insert it.
|
|
||||||
to_insert.append(row)
|
|
||||||
|
|
||||||
# Insert non-existing rows.
|
|
||||||
self.insert_many(to_insert, chunk_size, ensure, types)
|
|
||||||
|
|
||||||
# Update existing rows.
|
|
||||||
self.update_many(to_update, keys, chunk_size, ensure, types)
|
|
||||||
|
|
||||||
def delete(self, *clauses, **filters):
|
def delete(self, *clauses, **filters):
|
||||||
"""Delete rows from the table.
|
"""Delete rows from the table.
|
||||||
@ -414,7 +401,7 @@ class Table(object):
|
|||||||
return self.table.c[column] == value
|
return self.table.c[column] == value
|
||||||
if op in ("!=", "<>", "not"):
|
if op in ("!=", "<>", "not"):
|
||||||
return self.table.c[column] != value
|
return self.table.c[column] != value
|
||||||
if op in ("in"):
|
if op in ("in",):
|
||||||
return self.table.c[column].in_(value)
|
return self.table.c[column].in_(value)
|
||||||
if op in ("between", ".."):
|
if op in ("between", ".."):
|
||||||
start, end = value
|
start, end = value
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user