From 54322694fd7dc2e018d14593d082d5ddca06d762 Mon Sep 17 00:00:00 2001 From: ClodoaldoPinto Date: Sat, 3 Oct 2015 22:18:19 +0000 Subject: [PATCH 1/2] Update extras.py Create an "Insert_Values" adapter --- lib/extras.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/extras.py b/lib/extras.py index 2713d6fc..2f027144 100644 --- a/lib/extras.py +++ b/lib/extras.py @@ -968,6 +968,26 @@ def register_composite(name, conn_or_curs, globally=False, factory=None): return caster +class Insert_Values(list): + def __init__(self, list_of_tuples): + assert isinstance(list_of_tuples, list), 'Insert_Values argument must be a list' + tuples_lenght = set() + for t in list_of_tuples: + assert isinstance(t, tuple), 'The Insert_Values list elements must all be tuples' + tuples_lenght.add(len(t)) + assert len(tuples_lenght) > 0, 'The Insert_Values List must not be empty' + assert len(tuples_lenght) == 1, 'All Insert_Values tuples must have the same length' + self._list_of_tuples = list_of_tuples + def __str__(self): + return str(self._list_of_tuples) + +def adapt_insert_values(values): + l = [_A(t).getquoted() for t in values._list_of_tuples] + return _ext.AsIs(','.join(l)) + +def register_insert_values(): + _ext.register_adapter(Insert_Values, adapt_insert_values) + # expose the json adaptation stuff into the module from psycopg2._json import json, Json, register_json From 46f138d203cd646a468c9a91d7d9796623d04eee Mon Sep 17 00:00:00 2001 From: ClodoaldoPinto Date: Sun, 4 Oct 2015 11:26:16 +0000 Subject: [PATCH 2/2] New Insert_Values Adapter Fix typo in variable name --- lib/extras.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/extras.py b/lib/extras.py index 2f027144..e828a6ce 100644 --- a/lib/extras.py +++ b/lib/extras.py @@ -971,12 +971,12 @@ def register_composite(name, conn_or_curs, globally=False, factory=None): class Insert_Values(list): def __init__(self, list_of_tuples): assert isinstance(list_of_tuples, list), 'Insert_Values argument must be a list' - tuples_lenght = set() + tuples_length = set() for t in list_of_tuples: assert isinstance(t, tuple), 'The Insert_Values list elements must all be tuples' - tuples_lenght.add(len(t)) - assert len(tuples_lenght) > 0, 'The Insert_Values List must not be empty' - assert len(tuples_lenght) == 1, 'All Insert_Values tuples must have the same length' + tuples_length.add(len(t)) + assert len(tuples_length) > 0, 'The Insert_Values List must not be empty' + assert len(tuples_length) == 1, 'All Insert_Values tuples must have the same length' self._list_of_tuples = list_of_tuples def __str__(self): return str(self._list_of_tuples) @@ -988,7 +988,6 @@ def adapt_insert_values(values): def register_insert_values(): _ext.register_adapter(Insert_Values, adapt_insert_values) - # expose the json adaptation stuff into the module from psycopg2._json import json, Json, register_json from psycopg2._json import register_default_json, register_default_jsonb