# HG changeset patch # User jbe # Date 1478543498 -3600 # Node ID 7a2f28503b7600db88127e3e824d062dc3b9bd29 # Parent 675a9d645a760783ca7279f77585ad559d25aaeb Code cleanup for UPSERT code diff -r 675a9d645a76 -r 7a2f28503b76 libraries/mondelefant/mondelefant.lua --- a/libraries/mondelefant/mondelefant.lua Mon Nov 07 19:24:16 2016 +0100 +++ b/libraries/mondelefant/mondelefant.lua Mon Nov 07 19:31:38 2016 +0100 @@ -1128,7 +1128,18 @@ add(fields, {'"$"', {key}}) add(values, {'?', self._col[key]}) end - local upsert + local returning = { sep = ", " } + if primary_key.json_doc then + returning[1] = { + '("$"->>?)::$ AS "json_key"', + {primary_key.json_doc}, primary_key.key, {primary_key.type} + } + else + for idx, value in ipairs(primary_key) do + returning[idx] = '"' .. value .. '"' + end + end + local db_error, db_result if self._upsert then local upsert_keys = {sep = ", "} if primary_key.json_doc then @@ -1141,48 +1152,56 @@ upsert_keys[idx] = {'"$"', {value}} end end - local upsert_sets = {sep = ", "} - for key in pairs(self._dirty) do - add(upsert_sets, {'"$" = ?', {key}, self._col[key]}) - end - upsert = {{ 'ON CONFLICT ($) DO UPDATE SET $ RETURNING', upsert_keys, upsert_sets }} - else - upsert = 'RETURNING' - end - local returning = { sep = ", " } - if primary_key.json_doc then - returning[1] = { - '("$"->>?)::$ AS "json_key"', - {primary_key.json_doc}, primary_key.key, {primary_key.type} - } - else - for idx, value in ipairs(primary_key) do - returning[idx] = '"' .. value .. '"' + if #fields == 0 then + db_error, db_result = self._connection:try_query( + { + 'INSERT INTO $ DEFAULT VALUES ON CONFLICT ($) DO NOTHING $', + {self._class:get_qualified_table()}, + upsert_keys, + returning + }, + "object" + ) + else + local upsert_sets = {sep = ", "} + for key in pairs(self._dirty) do + add(upsert_sets, {'"$" = ?', {key}, self._col[key]}) + end + db_error, db_result = self._connection:try_query( + { + 'INSERT INTO $ ($) VALUES ($) ON CONFLICT ($) DO UPDATE SET $ RETURNING $', + {self._class:get_qualified_table()}, + fields, + values, + upsert_keys, + upsert_sets, + returning + }, + "object" + ) end - end - local db_error, db_result - if #fields == 0 then - db_error, db_result = self._connection:try_query( - { - 'INSERT INTO $ DEFAULT VALUES $ $', - {self._class:get_qualified_table()}, - upsert, - returning - }, - "object" - ) else - db_error, db_result = self._connection:try_query( - { - 'INSERT INTO $ ($) VALUES ($) $ $', - {self._class:get_qualified_table()}, - fields, - values, - upsert, - returning - }, - "object" - ) + if #fields == 0 then + db_error, db_result = self._connection:try_query( + { + 'INSERT INTO $ DEFAULT VALUES RETURNING $', + {self._class:get_qualified_table()}, + returning + }, + "object" + ) + else + db_error, db_result = self._connection:try_query( + { + 'INSERT INTO $ ($) VALUES ($) RETURNING $', + {self._class:get_qualified_table()}, + fields, + values, + returning + }, + "object" + ) + end end if db_error then return db_error