column_queries 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Serge Balyuk for Avenue100 Media Solutions Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
File without changes
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler.setup
5
+ Bundler::GemHelper.install_tasks
6
+
7
+ require 'active_record'
8
+ require 'active_record/base'
9
+ require 'rspec/core/rake_task'
10
+
11
+ desc "Run all examples"
12
+ RSpec::Core::RakeTask.new(:spec) do |t|
13
+ t.rspec_opts = %w[--color]
14
+ end
15
+
16
+ task :prepare_db do
17
+ require './spec/support/db.rb'
18
+ PostgresDatabase.prepare_database
19
+ end
20
+
21
+ task :default => [:prepare_db, :spec]
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
3
+ require "column_queries/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "column_queries"
7
+ s.version = ColumnQueries::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Serge Balyuk"]
10
+ s.email = ["serge@complicated-simplicity.com"]
11
+ s.homepage = "http://github.com/bgipsy/column_queries"
12
+ s.summary = "extensions for ActiveRecord PostgreSQL adapter with pg gem"
13
+ s.description = "faster select_values implementation for ActiveRecord with PostgreSQL pg gem"
14
+
15
+ s.required_rubygems_version = ">= 1.3.7"
16
+
17
+ s.add_development_dependency "rspec", "~> 2.5.0"
18
+
19
+ s.add_runtime_dependency "activerecord", "~> 3.0.4"
20
+ s.add_runtime_dependency "pg", "~> 0.11.0"
21
+
22
+ s.files = `git ls-files`.split("\n")
23
+ s.test_files = `git ls-files -- spec/*`.split("\n")
24
+ s.require_path = 'lib'
25
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_record'
2
+ require 'active_record/base'
3
+ require 'active_record/connection_adapters/postgresql_adapter'
4
+
5
+ module ColumnQueries
6
+ require 'column_queries/postgresql_adapter_extensions'
7
+ require 'column_queries/relation_extensions'
8
+ end
@@ -0,0 +1,14 @@
1
+ module ColumnQueries::PostgreSQLAdapterExtensions
2
+ def select_int_values(sql)
3
+ select_columns_as_int_arrays(sql).first
4
+ end
5
+
6
+ # column_values is quite fast method written in C provided by pg gem
7
+ # which seems like what we need for fetching lenghty id arrays
8
+ def select_columns_as_int_arrays(sql)
9
+ result = execute(sql)
10
+ (0...result.nfields).collect {|i| result.column_values(i).map {|j| j.to_i}}
11
+ end
12
+ end
13
+
14
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send(:include, ColumnQueries::PostgreSQLAdapterExtensions)
@@ -0,0 +1,31 @@
1
+ module ColumnQueries::RealtionExtensions
2
+ def to_int_array(column = nil)
3
+ relation = column.nil? ? self : select(column)
4
+ @klass.connection.select_int_values(relation.arel.to_sql)
5
+ end
6
+
7
+ def to_columns_as_int_arrays(*columns)
8
+ relation = columns.empty? ? self : select(columns)
9
+ @klass.connection.select_columns_as_int_arrays(relation.arel.to_sql)
10
+ end
11
+
12
+ def to_int_groups(keys_column, values_column)
13
+ keys, values = to_columns_as_int_arrays(keys_column, values_column)
14
+ keys.zip(values).inject({}) do |hash, pair|
15
+ value = hash[pair.first]
16
+ if value
17
+ value << pair.last
18
+ else
19
+ hash[pair.first] = [pair.last]
20
+ end
21
+ hash
22
+ end
23
+ end
24
+
25
+ def to_int_hash(keys_column, values_column)
26
+ keys, values = to_columns_as_int_arrays(keys_column, values_column)
27
+ Hash[keys.zip(values)]
28
+ end
29
+ end
30
+
31
+ ActiveRecord::Relation.send(:include, ColumnQueries::RealtionExtensions)
@@ -0,0 +1,3 @@
1
+ module ColumnQueries
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe ColumnQueries::PostgreSQLAdapterExtensions do
4
+
5
+ before :each do
6
+ Book.delete_all
7
+ @books = []
8
+ @books << Book.create!(:title => 'Book 1', :description => 'Lorem ipsum ' * 100, :price_cents => 495)
9
+ @books << Book.create!(:title => 'Book 2', :description => 'Lorem ipsum ' * 100, :price_cents => 999)
10
+ @books << Book.create!(:title => 'Book 3', :description => 'Lorem ipsum ' * 100, :price_cents => 1999)
11
+ @books << Book.create!(:title => 'Book 4', :description => 'Lorem ipsum ' * 100, :price_cents => nil)
12
+ end
13
+
14
+ it "should return array of ints" do
15
+ result = Book.connection.select_int_values("SELECT id FROM books ORDER BY id")
16
+ result.should == @books.map(&:id)
17
+ end
18
+
19
+ it "should return array for each column" do
20
+ book_ids, price_cents = Book.connection.select_columns_as_int_arrays("SELECT id, price_cents FROM books ORDER BY id")
21
+ book_ids.should == @books.map(&:id)
22
+ price_cents.should == @books.map {|b| b.price_cents.to_i}
23
+ end
24
+
25
+ it "should return 0 for NULL values" do
26
+ price_cents = Book.connection.select_int_values("SELECT price_cents FROM books WHERE price_cents IS NULL OR price_cents < 500")
27
+ price_cents.should =~ [0, 495]
28
+ end
29
+
30
+ end
@@ -0,0 +1,113 @@
1
+ require 'spec_helper'
2
+
3
+ describe ColumnQueries::RealtionExtensions do
4
+
5
+ before :each do
6
+ Book.delete_all
7
+ @books = []
8
+ @books << Book.create!(:title => 'Book 1', :description => 'Lorem ipsum ' * 100, :price_cents => 495)
9
+ @books << Book.create!(:title => 'Book 2', :description => 'Lorem ipsum ' * 100, :price_cents => 999)
10
+ @books << Book.create!(:title => 'Book 3', :description => 'Lorem ipsum ' * 100, :price_cents => 1999)
11
+ @books << Book.create!(:title => 'Book 4', :description => 'Lorem ipsum ' * 100, :price_cents => nil)
12
+ end
13
+
14
+ describe "to_int_array" do
15
+ it "should work for scopes" do
16
+ Book.scoped.to_int_array(:id).should == @books.map(&:id)
17
+ end
18
+
19
+ it "should work with named scopes" do
20
+ Book.pricy.to_int_array(:id).should == @books.select {|b| b.price_cents.to_i >= 1000}.map(&:id)
21
+ end
22
+
23
+ it "should work with dynamic scopes" do
24
+ Book.scoped_by_price_cents(999).to_int_array(:id).should == [@books[1].id]
25
+ Book.where('price_cents = 999').to_int_array(:id).should == [@books[1].id]
26
+ end
27
+
28
+ it "should work without arguments given .select() scope" do
29
+ Book.select(:price_cents).to_int_array.should == @books.map {|b| b.price_cents.to_i}
30
+ end
31
+ end
32
+
33
+ describe "to_columns_as_int_arrays" do
34
+ it "should work for scopes" do
35
+ ids, price_cents = Book.scoped.to_columns_as_int_arrays(:id, :price_cents)
36
+ ids.should == @books.map(&:id)
37
+ price_cents.should == @books.map {|b| b.price_cents.to_i}
38
+ end
39
+
40
+ it "should work with named scopes" do
41
+ ids, price_cents = Book.pricy.to_columns_as_int_arrays(:id, :price_cents)
42
+ ids.should == @books.select {|b| b.price_cents.to_i >= 1000}.map(&:id)
43
+ price_cents.should == [1999]
44
+ end
45
+
46
+ it "should work with dynamic scopes" do
47
+ ids, price_cents = Book.scoped_by_price_cents(999).to_columns_as_int_arrays(:id, :price_cents)
48
+ ids.should == [@books[1].id]
49
+ price_cents.should == [999]
50
+ end
51
+
52
+ it "should work without arguments given .select() scope" do
53
+ ids, price_cents = Book.select([:id, :price_cents]).to_columns_as_int_arrays
54
+ ids.should == @books.map(&:id)
55
+ price_cents.should == @books.map {|b| b.price_cents.to_i}
56
+ end
57
+ end
58
+
59
+ describe "to_int_hash" do
60
+ it "should work for scopes" do
61
+ prices = Book.scoped.to_int_hash(:id, :price_cents)
62
+ prices.keys.should =~ @books.map(&:id)
63
+ prices.values.should =~ @books.map {|b| b.price_cents.to_i}
64
+ prices[@books[0].id].should == 495
65
+ prices[@books[1].id].should == 999
66
+ end
67
+
68
+ it "should convert NULL values to 0s" do
69
+ prices = Book.scoped.to_int_hash(:id, :price_cents)
70
+ prices[@books[3].id].should == 0
71
+ end
72
+
73
+ it "should work with named scopes" do
74
+ prices = Book.pricy.to_int_hash(:id, :price_cents)
75
+ prices.should == {@books[2].id => 1999}
76
+ end
77
+
78
+ it "should work with dynamic scopes" do
79
+ prices = Book.scoped_by_price_cents(999).to_int_hash(:id, :price_cents)
80
+ prices.should == {@books[1].id => 999}
81
+ end
82
+ end
83
+
84
+ describe "to_int_groups" do
85
+ before :each do
86
+ Comment.delete_all
87
+ @books.each do |book|
88
+ 3.times { Comment.create!(:book => book, :body => 'Lorem Ipsum ' * 10) }
89
+ end
90
+ end
91
+
92
+ it "should work for scopes" do
93
+ books_comments = Comment.scoped.to_int_groups(:book_id, :id)
94
+ books_comments.keys.should =~ @books.map(&:id)
95
+ @books.each do |book|
96
+ books_comments[book.id].should =~ book.comment_ids
97
+ end
98
+ end
99
+
100
+ it "should work with named scopes" do
101
+ books_comments = Comment.for_pricy_books.to_int_groups(:book_id, :id)
102
+ books_comments.keys.should == [@books[2].id]
103
+ books_comments.values.first.should =~ @books[2].comment_ids
104
+ end
105
+
106
+ it "should work with dynamic scopes" do
107
+ books_comments = Comment.where("book_id IN (SELECT id FROM books WHERE price_cents IS NULL)").to_int_groups(:book_id, :id)
108
+ books_comments.keys.should == [@books[3].id]
109
+ books_comments.values.first.should =~ @books[3].comment_ids
110
+ end
111
+ end
112
+
113
+ end
@@ -0,0 +1,13 @@
1
+ require './lib/column_queries'
2
+
3
+ # Requires supporting ruby files with custom matchers and macros, etc,
4
+ # in spec/support/ and its subdirectories.
5
+ Dir['./spec/support/**/*.rb'].each {|f| require f}
6
+
7
+ RSpec.configure do |config|
8
+
9
+ config.before :all do
10
+ PostgresDatabase.establish_connection
11
+ end
12
+
13
+ end
@@ -0,0 +1,44 @@
1
+ class PostgresDatabase
2
+
3
+ TEST_DB = {
4
+ :username => 'postgres',
5
+ :adapter => 'postgresql',
6
+ :encoding => 'unicode',
7
+ :database => 'column_queries_test',
8
+ :username => 'postgres'
9
+ }
10
+
11
+ def self.prepare_database
12
+ ActiveRecord::Base.establish_connection(TEST_DB.merge(:database => 'postgres'))
13
+ ActiveRecord::Base.connection.drop_database(TEST_DB[:database])
14
+ ActiveRecord::Base.connection.create_database(TEST_DB[:database], TEST_DB)
15
+ ActiveRecord::Base.establish_connection(TEST_DB)
16
+ CreateTestSchema.migrate(:up)
17
+ end
18
+
19
+ def self.establish_connection
20
+ ActiveRecord::Base.establish_connection(TEST_DB)
21
+ end
22
+
23
+ end
24
+
25
+ class CreateTestSchema < ActiveRecord::Migration
26
+ def self.up
27
+ create_table :books do |t|
28
+ t.string :title
29
+ t.integer :price_cents
30
+ t.text :description
31
+ t.timestamps
32
+ end
33
+
34
+ create_table :comments do |t|
35
+ t.references :book
36
+ t.text :body
37
+ t.timestamps
38
+ end
39
+ end
40
+
41
+ def self.down
42
+ raise ActiveRecord::IrreversibleMigration
43
+ end
44
+ end
@@ -0,0 +1,20 @@
1
+ module Generators
2
+
3
+ def generate_books(id_range)
4
+ id_range.each do |i|
5
+ book = Book.new(:title => 'Lorem ipsum', :description => 'Lorem ipsum ' * 100, :price => 9.99)
6
+ book.id = i
7
+ book.save!
8
+
9
+ generate_comments(book, 3)
10
+ end
11
+ Book.connection.reset_pk_sequence!('books')
12
+ end
13
+
14
+ def generate_comments(book, ncomments)
15
+ ncomments.times do
16
+ Comment.create!(:book => book, :body => 'troll it!' * 10)
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,15 @@
1
+ class Book < ActiveRecord::Base
2
+
3
+ has_many :comments
4
+
5
+ scope :pricy, where('price_cents >= 1000')
6
+
7
+ end
8
+
9
+ class Comment < ActiveRecord::Base
10
+
11
+ belongs_to :book
12
+
13
+ scope :for_pricy_books, lambda { where(:book_id => Book.pricy.select(:id).project) }
14
+
15
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: column_queries
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Serge Balyuk
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-06-28 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2152565080 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.5.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2152565080
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ requirement: &2152563520 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 3.0.4
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2152563520
36
+ - !ruby/object:Gem::Dependency
37
+ name: pg
38
+ requirement: &2152562720 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.11.0
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *2152562720
47
+ description: faster select_values implementation for ActiveRecord with PostgreSQL
48
+ pg gem
49
+ email:
50
+ - serge@complicated-simplicity.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .gitignore
56
+ - Gemfile
57
+ - MIT-LICENSE
58
+ - README.md
59
+ - Rakefile
60
+ - column_queries.gemspec
61
+ - lib/column_queries.rb
62
+ - lib/column_queries/postgresql_adapter_extensions.rb
63
+ - lib/column_queries/relation_extensions.rb
64
+ - lib/column_queries/version.rb
65
+ - spec/lib/postgresql_adapter_extensions_spec.rb
66
+ - spec/lib/relation_extensions_spec.rb
67
+ - spec/spec_helper.rb
68
+ - spec/support/db.rb
69
+ - spec/support/generators.rb
70
+ - spec/support/models.rb
71
+ homepage: http://github.com/bgipsy/column_queries
72
+ licenses: []
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ segments:
84
+ - 0
85
+ hash: -1475764798245787251
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: 1.3.7
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 1.8.1
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: extensions for ActiveRecord PostgreSQL adapter with pg gem
98
+ test_files:
99
+ - spec/lib/postgresql_adapter_extensions_spec.rb
100
+ - spec/lib/relation_extensions_spec.rb
101
+ - spec/spec_helper.rb
102
+ - spec/support/db.rb
103
+ - spec/support/generators.rb
104
+ - spec/support/models.rb