(Data): Small extensions to core data interfaces.
Add `LEQ` condition type. Ensure that batch updates don't fire with nothing to update. Add `cast_row` to `update_many` for handling typed `NULL`s.
This commit is contained in:
@@ -45,6 +45,21 @@ class GEQ(Condition):
|
|||||||
values.append(item)
|
values.append(item)
|
||||||
|
|
||||||
|
|
||||||
|
class LEQ(Condition):
|
||||||
|
__slots__ = ('value',)
|
||||||
|
|
||||||
|
def __init__(self, value):
|
||||||
|
self.value = value
|
||||||
|
|
||||||
|
def apply(self, key, values, conditions):
|
||||||
|
item = self.value
|
||||||
|
if isinstance(item, (list, tuple)):
|
||||||
|
raise ValueError("Cannot apply LEQ condition to a list!")
|
||||||
|
else:
|
||||||
|
conditions.append("{} <= {}".format(key, _replace_char))
|
||||||
|
values.append(item)
|
||||||
|
|
||||||
|
|
||||||
class Constant(Condition):
|
class Constant(Condition):
|
||||||
__slots__ = ('value',)
|
__slots__ = ('value',)
|
||||||
|
|
||||||
|
|||||||
@@ -113,7 +113,8 @@ class Row:
|
|||||||
try:
|
try:
|
||||||
yield self._pending
|
yield self._pending
|
||||||
finally:
|
finally:
|
||||||
self.update(**self._pending)
|
if self._pending:
|
||||||
|
self.update(**self._pending)
|
||||||
self._pending = None
|
self._pending = None
|
||||||
|
|
||||||
def _refresh(self):
|
def _refresh(self):
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ def upsert(table, constraint, cursor=None, **values):
|
|||||||
return cursor.fetchone()
|
return cursor.fetchone()
|
||||||
|
|
||||||
|
|
||||||
def update_many(table, *values, set_keys=None, where_keys=None, cursor=None):
|
def update_many(table, *values, set_keys=None, where_keys=None, cast_row=None, cursor=None):
|
||||||
cursor = cursor or conn.cursor()
|
cursor = cursor or conn.cursor()
|
||||||
|
|
||||||
return execute_values(
|
return execute_values(
|
||||||
@@ -134,13 +134,14 @@ def update_many(table, *values, set_keys=None, where_keys=None, cursor=None):
|
|||||||
"""
|
"""
|
||||||
UPDATE {table}
|
UPDATE {table}
|
||||||
SET {set_clause}
|
SET {set_clause}
|
||||||
FROM (VALUES %s)
|
FROM (VALUES {cast_row}%s)
|
||||||
AS {temp_table}
|
AS {temp_table}
|
||||||
WHERE {where_clause}
|
WHERE {where_clause}
|
||||||
RETURNING *
|
RETURNING *
|
||||||
""".format(
|
""".format(
|
||||||
table=table,
|
table=table,
|
||||||
set_clause=', '.join("{0} = _t.{0}".format(key) for key in set_keys),
|
set_clause=', '.join("{0} = _t.{0}".format(key) for key in set_keys),
|
||||||
|
cast_row=cast_row + ',' if cast_row else '',
|
||||||
where_clause=' AND '.join("{1}.{0} = _t.{0}".format(key, table) for key in where_keys),
|
where_clause=' AND '.join("{1}.{0} = _t.{0}".format(key, table) for key in where_keys),
|
||||||
temp_table="_t ({})".format(', '.join(set_keys + where_keys))
|
temp_table="_t ({})".format(', '.join(set_keys + where_keys))
|
||||||
),
|
),
|
||||||
|
|||||||
Reference in New Issue
Block a user