Commit f918e16d authored by Ohad Dahan's avatar Ohad Dahan

Added nativepluck_raw

parent 79d496c5
......@@ -24,6 +24,19 @@ module Nativepluck
ActiveRecord::Base.connection.raw_connection.type_map_for_queries = Nativepluck.original_type_map_for_queries
end
def self.nativepluck_raw(input)
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
module ClassMethods
def nativepluck(*columns, **opts)
raise ArgumentError.new('No columns to pluck were provided') if columns.size == 0
......@@ -39,8 +52,9 @@ module Nativepluck
"
results = ActiveRecord::Base.connection.raw_connection.async_exec(sql)
puts "#{__method__} sql = #{sql}" if opts[:verbose]
columns.size == 1 ? out = results.column_values(0) : out = results.values
results.nfields == 1 ? out = results.column_values(0) : out = results.values
ensure
results.try(:clear)
Nativepluck.return_original_casters
end
return out
......
class AnotherGenericModel < ApplicationRecord
include Nativepluck
belongs_to :genericmodel
end
class Genericmodel < ApplicationRecord
include Nativepluck
has_many :another_generic_models, dependent: :destroy
end
class CreateAnotherGenericModels < ActiveRecord::Migration[5.2]
def change
create_table :another_generic_models do |t|
t.integer :another_integer_col
t.string :another_string_col
t.datetime :another_datetime_col
t.float :another_float_col
t.json :another_json_col
t.jsonb :another_jsonb_col
t.timestamps
end
end
end
class AddReference < ActiveRecord::Migration[5.2]
def change
add_reference :another_generic_models, :genericmodel , index: true, foreign_key: true
end
end
......@@ -10,11 +10,24 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2018_10_20_103358) do
ActiveRecord::Schema.define(version: 2018_11_09_095940) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "another_generic_models", force: :cascade do |t|
t.integer "another_integer_col"
t.string "another_string_col"
t.datetime "another_datetime_col"
t.float "another_float_col"
t.json "another_json_col"
t.jsonb "another_jsonb_col"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "genericmodel_id"
t.index ["genericmodel_id"], name: "index_another_generic_models_on_genericmodel_id"
end
create_table "genericmodels", force: :cascade do |t|
t.integer "integer_col"
t.string "string_col"
......@@ -26,4 +39,5 @@ ActiveRecord::Schema.define(version: 2018_10_20_103358) do
t.datetime "updated_at", null: false
end
add_foreign_key "another_generic_models", "genericmodels"
end
......@@ -21,3 +21,28 @@ genericmodels = []
end
Genericmodel.import genericmodels
another_generic_models = []
genericmodel_ids = Genericmodel.pluck(:id)
1000.times do
genericmodel_id = genericmodel_ids.sample(1).first
json = {
id: rand(100_00),
string: rand_str,
datetime: DateTime.now,
float: rand(100_000) + rand()
}
another_generic_models << AnotherGenericModel.new(
another_integer_col: rand(100_000),
another_float_col: rand(100_000) + rand(),
another_string_col: rand_str,
another_datetime_col: DateTime.now,
another_json_col: json,
another_jsonb_col: json,
genericmodel_id: genericmodel_id
)
end
AnotherGenericModel.import another_generic_models
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
require 'test_helper'
class AnotherGenericModelTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
......@@ -14,11 +14,11 @@ class GenericmodelTest < ActiveSupport::TestCase
end
test 'pluck_permutations' do
LIMIT = 100
row_limit = 100
failed = []
Genericmodel.attribute_names.permutation(2).each do |attrs|
pluck = Genericmodel.limit(100).order(id: :asc).pluck(*attrs)
nativepluck = Genericmodel.nativepluck(*attrs, order: {id: :asc} , limit: LIMIT)
nativepluck = Genericmodel.nativepluck(*attrs, order: {id: :asc} , limit: row_limit)
failed << attrs if pluck != nativepluck
end
assert(failed.empty?, "#{__method__}:: pluck != nativepluck for attributes : #{failed.join(', ')}")
......@@ -35,19 +35,19 @@ class GenericmodelTest < ActiveSupport::TestCase
test 'test_offset' do
LIMIT = 100
row_limit = 100
Range.new(1,100).step(rand(5) + 1).each do |offset|
pluck = Genericmodel.limit(LIMIT).offset(offset).order(id: :asc).pluck(:id)
nativepluck = Genericmodel.nativepluck(:id, order: {id: :asc} , limit: LIMIT, offset: offset)
pluck = Genericmodel.limit(row_limit).offset(offset).order(id: :asc).pluck(:id)
nativepluck = Genericmodel.nativepluck(:id, order: {id: :asc} , limit: row_limit, offset: offset)
assert(pluck == nativepluck, "#{__method__}:: pluck != nativepluck for column :id and offset #{offset}")
end
end
test 'test_group_by' do
LIMIT = 100
pluck = Genericmodel.limit(LIMIT).order(id: :asc).group(:id).pluck(:id)
nativepluck = Genericmodel.nativepluck(:id, order: {id: :asc} , group: [:id], limit: LIMIT )
row_limit = 100
pluck = Genericmodel.limit(row_limit).order(id: :asc).group(:id).pluck(:id)
nativepluck = Genericmodel.nativepluck(:id, order: {id: :asc} , group: [:id], limit: row_limit )
assert(pluck == nativepluck, "#{__method__}:: pluck != nativepluck")
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment