lib/rodauth/oauth/database_extensions.rb in rodauth-oauth-0.7.4 vs lib/rodauth/oauth/database_extensions.rb in rodauth-oauth-0.8.0

- old
+ new

@@ -1,10 +1,10 @@ # frozen_string_literal: true module Rodauth module OAuth - # rubocop:disable Naming/MethodName, Metrics/ParameterLists + # rubocop:disable Naming/MethodName def self.ExtendDatabase(db) Module.new do dataset = db.dataset if dataset.supports_returning?(:insert) @@ -40,10 +40,18 @@ update_where: conds ) __insert_and_return__(dataset, pkey, params) end + + def __insert_or_do_nothing_and_return__(dataset, pkey, unique_columns, params) + __insert_and_return__( + dataset.insert_conflict(target: unique_columns), + pkey, + params + ) || dataset.where(params).first + end else def __insert_or_update_and_return__(dataset, pkey, unique_columns, params, conds = nil, exclude_on_update = nil) find_params, update_params = params.partition { |key, _| unique_columns.include?(key) }.map { |h| Hash[h] } dataset_where = dataset.where(find_params) @@ -63,11 +71,16 @@ __update_and_return__(dataset_where, update_params) else __insert_and_return__(dataset, pkey, params) end end + + def __insert_or_do_nothing_and_return__(dataset, pkey, unique_columns, params) + find_params = params.select { |key, _| unique_columns.include?(key) } + dataset.where(find_params).first || __insert_and_return__(dataset, pkey, params) + end end end end - # rubocop:enable Naming/MethodName, Metrics/ParameterLists + # rubocop:enable Naming/MethodName end end