module Nativepluck
Copyright 2018 Ohad Dahan, Al Chou
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Constants
- VERSION
Attributes
nativepluck_init[RW]
nativepluck_type_map_for_queries[RW]
nativepluck_type_map_for_results[RW]
original_type_map_for_queries[RW]
original_type_map_for_results[RW]
override_pluck[RW]
Public Class Methods
init_nativepluck()
click to toggle source
# File lib/nativepluck.rb, line 28 def self.init_nativepluck return if @nativepluck_init @nativepluck_type_map_for_results = PG::BasicTypeMapForResults.new ActiveRecord::Base.connection.raw_connection @nativepluck_type_map_for_queries = PG::BasicTypeMapForQueries.new ActiveRecord::Base.connection.raw_connection @original_type_map_for_results = ActiveRecord::Base.connection.raw_connection.type_map_for_results @original_type_map_for_queries = ActiveRecord::Base.connection.raw_connection.type_map_for_queries @nativepluck_init = true end
nativepluck(input)
click to toggle source
# File lib/nativepluck.rb, line 49 def self.nativepluck(input) out = [] begin sql = input.respond_to?(:to_sql) ? input.to_sql : input Nativepluck.set_pg_native_casters results = ActiveRecord::Base.connection.raw_connection.async_exec(sql) results.nfields == 1 ? out = results.column_values(0) : out = results.values ensure results.try(:clear) Nativepluck.return_original_casters end return out end
return_original_casters()
click to toggle source
# File lib/nativepluck.rb, line 43 def self.return_original_casters init_nativepluck ActiveRecord::Base.connection.raw_connection.type_map_for_results = Nativepluck.original_type_map_for_results ActiveRecord::Base.connection.raw_connection.type_map_for_queries = Nativepluck.original_type_map_for_queries end
set_override_pluck(selection)
click to toggle source
# File lib/nativepluck.rb, line 94 def self.set_override_pluck(selection) raise ArgumentError.new("#{__method__}:: Input should be true/false and not #{selection.class}") unless (selection.is_a?(TrueClass) || selection.is_a?(FalseClass)) if selection ::ActiveRecord::Calculations.class_eval do alias_method :orig_pluck, :pluck alias_method :pluck, :nativepluck end ::ActiveRecord::Querying.class_eval do alias_method :orig_pluck, :pluck alias_method :pluck, :nativepluck end else unless Nativepluck.override_pluck raise ArgumentError.new("#{__method__}:: Cannot turn off pluck overriding since it wasn't set yet") end ::ActiveRecord::Calculations.class_eval do alias_method :pluck, :orig_pluck end ::ActiveRecord::Querying.class_eval do alias_method :pluck, :orig_pluck end end Nativepluck.override_pluck = selection end
set_pg_native_casters()
click to toggle source
# File lib/nativepluck.rb, line 37 def self.set_pg_native_casters init_nativepluck ActiveRecord::Base.connection.raw_connection.type_map_for_results = Nativepluck.nativepluck_type_map_for_results ActiveRecord::Base.connection.raw_connection.type_map_for_queries = Nativepluck.nativepluck_type_map_for_queries end