Methods
Public Class
Public Instance
Classes and Modules
Constants
VERSION | = | "1.6.3" |
Public Class methods
ExtendDatabase(db)
rubocop:disable Naming/MethodName
[show source]
# File lib/rodauth/oauth/database_extensions.rb 6 def self.ExtendDatabase(db) 7 Module.new do 8 dataset = db.dataset 9 10 if dataset.supports_returning?(:insert) 11 def __insert_and_return__(dataset, _pkey, params) 12 dataset.returning.insert(params).first 13 end 14 else 15 def __insert_and_return__(dataset, pkey, params) 16 id = dataset.insert(params) 17 if params.key?(pkey) 18 # mysql returns 0 when the primary key is a varchar. 19 id = params[pkey] 20 end 21 dataset.where(pkey => id).first 22 end 23 end 24 25 if dataset.supports_returning?(:update) 26 def __update_and_return__(dataset, params) 27 dataset.returning.update(params).first 28 end 29 else 30 def __update_and_return__(dataset, params) 31 dataset.update(params) 32 dataset.first 33 end 34 end 35 36 if dataset.respond_to?(:supports_insert_conflict?) && dataset.supports_insert_conflict? 37 def __insert_or_update_and_return__(dataset, pkey, unique_columns, params, conds = nil, to_update_extra = nil) 38 to_update = Hash[(params.keys - unique_columns).map { |attribute| [attribute, Sequel[:excluded][attribute]] }] 39 40 to_update.merge!(to_update_extra) if to_update_extra 41 42 dataset = dataset.insert_conflict( 43 target: unique_columns, 44 update: to_update, 45 update_where: conds 46 ) 47 48 __insert_and_return__(dataset, pkey, params) 49 end 50 51 def __insert_or_do_nothing_and_return__(dataset, pkey, unique_columns, params) 52 __insert_and_return__( 53 dataset.insert_conflict(target: unique_columns), 54 pkey, 55 params 56 ) || dataset.where(params).first 57 end 58 else 59 def __insert_or_update_and_return__(dataset, pkey, unique_columns, params, conds = nil, to_update_extra = nil) 60 find_params, update_params = params.partition { |key, _| unique_columns.include?(key) }.map { |h| Hash[h] } 61 62 dataset_where = dataset.where(find_params) 63 record = if conds 64 dataset_where_conds = dataset_where.where(conds) 65 66 # this means that there's still a valid entry there, so return early 67 return if dataset_where.count != dataset_where_conds.count 68 69 dataset_where_conds.first 70 else 71 dataset_where.first 72 end 73 74 if record 75 update_params.merge!(to_update_extra) if to_update_extra 76 77 __update_and_return__(dataset_where, update_params) 78 else 79 __insert_and_return__(dataset, pkey, params) 80 end 81 end 82 83 def __insert_or_do_nothing_and_return__(dataset, pkey, unique_columns, params) 84 find_params = params.slice(*unique_columns) 85 dataset.where(find_params).first || __insert_and_return__(dataset, pkey, params) 86 end 87 end 88 end 89 end
Public Instance methods
__insert_and_return__(dataset, _pkey, params)
[show source]
# File lib/rodauth/oauth/database_extensions.rb 11 def __insert_and_return__(dataset, _pkey, params) 12 dataset.returning.insert(params).first 13 end
__insert_or_do_nothing_and_return__(dataset, pkey, unique_columns, params)
[show source]
# File lib/rodauth/oauth/database_extensions.rb 51 def __insert_or_do_nothing_and_return__(dataset, pkey, unique_columns, params) 52 __insert_and_return__( 53 dataset.insert_conflict(target: unique_columns), 54 pkey, 55 params 56 ) || dataset.where(params).first 57 end
__insert_or_update_and_return__(dataset, pkey, unique_columns, params, conds = nil, to_update_extra = nil)
[show source]
# File lib/rodauth/oauth/database_extensions.rb 37 def __insert_or_update_and_return__(dataset, pkey, unique_columns, params, conds = nil, to_update_extra = nil) 38 to_update = Hash[(params.keys - unique_columns).map { |attribute| [attribute, Sequel[:excluded][attribute]] }] 39 40 to_update.merge!(to_update_extra) if to_update_extra 41 42 dataset = dataset.insert_conflict( 43 target: unique_columns, 44 update: to_update, 45 update_where: conds 46 ) 47 48 __insert_and_return__(dataset, pkey, params) 49 end
__update_and_return__(dataset, params)
[show source]
# File lib/rodauth/oauth/database_extensions.rb 26 def __update_and_return__(dataset, params) 27 dataset.returning.update(params).first 28 end