From aac80b201af309f9e05fc36008ed965197719ada Mon Sep 17 00:00:00 2001 From: Jared Lewis Date: Fri, 21 Apr 2017 12:30:42 -0400 Subject: [PATCH 1/2] Respect return_models in upsert method when building upsert sql --- docs/release_notes.rst | 4 ++++ querybuilder/query.py | 27 +++++++++++++++++++++------ querybuilder/version.py | 2 +- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/docs/release_notes.rst b/docs/release_notes.rst index 20145e1..34cf902 100644 --- a/docs/release_notes.rst +++ b/docs/release_notes.rst @@ -1,6 +1,10 @@ Release Notes ============= +v0.14.3 +------- +* Respect return_models in upsert method when building upsert sql + v0.14.2 ------- * Fix upsert to use the proper prepare method on django fields diff --git a/querybuilder/query.py b/querybuilder/query.py index 5663e27..045c729 100644 --- a/querybuilder/query.py +++ b/querybuilder/query.py @@ -1185,7 +1185,15 @@ def get_update_sql(self, rows): return self.sql, sql_args - def get_upsert_sql(self, rows, unique_fields, update_fields, auto_field_name=None, only_insert=False): + def get_upsert_sql( + self, + rows, + unique_fields, + update_fields, + auto_field_name=None, + only_insert=False, + return_rows=True + ): """ Generates the postgres specific sql necessary to perform an upsert (ON CONFLICT) @@ -1235,22 +1243,22 @@ def get_upsert_sql(self, rows, unique_fields, update_fields, auto_field_name=Non row_values_sql = ', '.join(row_values) if update_fields: - self.sql = 'INSERT INTO {0} ({1}) VALUES {2} ON CONFLICT ({3}) DO UPDATE SET {4} RETURNING {5}'.format( + self.sql = 'INSERT INTO {0} ({1}) VALUES {2} ON CONFLICT ({3}) DO UPDATE SET {4} {5}'.format( self.tables[0].get_identifier(), all_field_names_sql, row_values_sql, unique_field_names_sql, update_fields_sql, - '*' + 'RETURNING *' if return_rows else '' ) else: - self.sql = 'INSERT INTO {0} ({1}) VALUES {2} ON CONFLICT ({3}) {4} RETURNING {5}'.format( + self.sql = 'INSERT INTO {0} ({1}) VALUES {2} ON CONFLICT ({3}) {4} {5}'.format( self.tables[0].get_identifier(), all_field_names_sql, row_values_sql, unique_field_names_sql, 'DO UPDATE SET {0}=EXCLUDED.{0}'.format(unique_fields[0].column), - '*' + 'RETURNING *' if return_rows else '' ) return self.sql, sql_args @@ -1737,7 +1745,13 @@ def upsert(self, rows, unique_fields, update_fields, return_rows=False, return_m return_value = [] if rows: - sql, sql_args = self.get_upsert_sql(rows, unique_fields, update_fields, auto_field_name=auto_field_name) + sql, sql_args = self.get_upsert_sql( + rows, + unique_fields, + update_fields, + auto_field_name=auto_field_name, + return_rows=return_models + ) # get the cursor to execute the query cursor = self.get_cursor() @@ -1755,6 +1769,7 @@ def upsert(self, rows, unique_fields, update_fields, return_rows=False, return_m update_fields, auto_field_name=auto_field_name, only_insert=True, + return_rows=return_models ) # get the cursor to execute the query diff --git a/querybuilder/version.py b/querybuilder/version.py index c41af0b..62ea085 100644 --- a/querybuilder/version.py +++ b/querybuilder/version.py @@ -1 +1 @@ -__version__ = '0.14.2' +__version__ = '0.14.3' From aed5870b2fd2fa08a3b3a7f590bb93bba4c61b53 Mon Sep 17 00:00:00 2001 From: Jared Lewis Date: Fri, 21 Apr 2017 12:37:05 -0400 Subject: [PATCH 2/2] Fix tests --- querybuilder/query.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/querybuilder/query.py b/querybuilder/query.py index 045c729..0340ea1 100644 --- a/querybuilder/query.py +++ b/querybuilder/query.py @@ -1750,7 +1750,7 @@ def upsert(self, rows, unique_fields, update_fields, return_rows=False, return_m unique_fields, update_fields, auto_field_name=auto_field_name, - return_rows=return_models + return_rows=return_rows or return_models ) # get the cursor to execute the query @@ -1769,7 +1769,7 @@ def upsert(self, rows, unique_fields, update_fields, return_rows=False, return_m update_fields, auto_field_name=auto_field_name, only_insert=True, - return_rows=return_models + return_rows=return_rows or return_models ) # get the cursor to execute the query