webmcp
changeset 465:7a2f28503b76
Code cleanup for UPSERT code
author | jbe |
---|---|
date | Mon Nov 07 19:31:38 2016 +0100 (2016-11-07) |
parents | 675a9d645a76 |
children | 2751b6b81c23 |
files | libraries/mondelefant/mondelefant.lua |
line diff
1.1 --- a/libraries/mondelefant/mondelefant.lua Mon Nov 07 19:24:16 2016 +0100 1.2 +++ b/libraries/mondelefant/mondelefant.lua Mon Nov 07 19:31:38 2016 +0100 1.3 @@ -1128,7 +1128,18 @@ 1.4 add(fields, {'"$"', {key}}) 1.5 add(values, {'?', self._col[key]}) 1.6 end 1.7 - local upsert 1.8 + local returning = { sep = ", " } 1.9 + if primary_key.json_doc then 1.10 + returning[1] = { 1.11 + '("$"->>?)::$ AS "json_key"', 1.12 + {primary_key.json_doc}, primary_key.key, {primary_key.type} 1.13 + } 1.14 + else 1.15 + for idx, value in ipairs(primary_key) do 1.16 + returning[idx] = '"' .. value .. '"' 1.17 + end 1.18 + end 1.19 + local db_error, db_result 1.20 if self._upsert then 1.21 local upsert_keys = {sep = ", "} 1.22 if primary_key.json_doc then 1.23 @@ -1141,48 +1152,56 @@ 1.24 upsert_keys[idx] = {'"$"', {value}} 1.25 end 1.26 end 1.27 - local upsert_sets = {sep = ", "} 1.28 - for key in pairs(self._dirty) do 1.29 - add(upsert_sets, {'"$" = ?', {key}, self._col[key]}) 1.30 - end 1.31 - upsert = {{ 'ON CONFLICT ($) DO UPDATE SET $ RETURNING', upsert_keys, upsert_sets }} 1.32 - else 1.33 - upsert = 'RETURNING' 1.34 - end 1.35 - local returning = { sep = ", " } 1.36 - if primary_key.json_doc then 1.37 - returning[1] = { 1.38 - '("$"->>?)::$ AS "json_key"', 1.39 - {primary_key.json_doc}, primary_key.key, {primary_key.type} 1.40 - } 1.41 - else 1.42 - for idx, value in ipairs(primary_key) do 1.43 - returning[idx] = '"' .. value .. '"' 1.44 + if #fields == 0 then 1.45 + db_error, db_result = self._connection:try_query( 1.46 + { 1.47 + 'INSERT INTO $ DEFAULT VALUES ON CONFLICT ($) DO NOTHING $', 1.48 + {self._class:get_qualified_table()}, 1.49 + upsert_keys, 1.50 + returning 1.51 + }, 1.52 + "object" 1.53 + ) 1.54 + else 1.55 + local upsert_sets = {sep = ", "} 1.56 + for key in pairs(self._dirty) do 1.57 + add(upsert_sets, {'"$" = ?', {key}, self._col[key]}) 1.58 + end 1.59 + db_error, db_result = self._connection:try_query( 1.60 + { 1.61 + 'INSERT INTO $ ($) VALUES ($) ON CONFLICT ($) DO UPDATE SET $ RETURNING $', 1.62 + {self._class:get_qualified_table()}, 1.63 + fields, 1.64 + values, 1.65 + upsert_keys, 1.66 + upsert_sets, 1.67 + returning 1.68 + }, 1.69 + "object" 1.70 + ) 1.71 end 1.72 - end 1.73 - local db_error, db_result 1.74 - if #fields == 0 then 1.75 - db_error, db_result = self._connection:try_query( 1.76 - { 1.77 - 'INSERT INTO $ DEFAULT VALUES $ $', 1.78 - {self._class:get_qualified_table()}, 1.79 - upsert, 1.80 - returning 1.81 - }, 1.82 - "object" 1.83 - ) 1.84 else 1.85 - db_error, db_result = self._connection:try_query( 1.86 - { 1.87 - 'INSERT INTO $ ($) VALUES ($) $ $', 1.88 - {self._class:get_qualified_table()}, 1.89 - fields, 1.90 - values, 1.91 - upsert, 1.92 - returning 1.93 - }, 1.94 - "object" 1.95 - ) 1.96 + if #fields == 0 then 1.97 + db_error, db_result = self._connection:try_query( 1.98 + { 1.99 + 'INSERT INTO $ DEFAULT VALUES RETURNING $', 1.100 + {self._class:get_qualified_table()}, 1.101 + returning 1.102 + }, 1.103 + "object" 1.104 + ) 1.105 + else 1.106 + db_error, db_result = self._connection:try_query( 1.107 + { 1.108 + 'INSERT INTO $ ($) VALUES ($) RETURNING $', 1.109 + {self._class:get_qualified_table()}, 1.110 + fields, 1.111 + values, 1.112 + returning 1.113 + }, 1.114 + "object" 1.115 + ) 1.116 + end 1.117 end 1.118 if db_error then 1.119 return db_error