Permalink
Browse files

Inet contains queries

  • Loading branch information...
1 parent c872181 commit 20dda03b18b2636368fb83da97a622197bf941de @danmcclain danmcclain committed Feb 8, 2013
View
@@ -8,6 +8,7 @@ rvm:
before_script:
- psql -c 'create database postgres_ext_test;' -U postgres
- cp spec/dummy/config/database.yml.example spec/dummy/config/database.yml
+ - RAILS_ENV=test rake db:migrate
notifications:
email:
@@ -8,13 +8,44 @@ def initialize(scope)
end
def overlap(opts)
- arel_table = @scope.engine.arel_table
opts.each do |key, value|
@scope = @scope.where(arel_table[key].array_overlap(value))
end
@scope
end
+ def contained_within(opts)
+ opts.each do |key, value|
+ @scope = @scope.where(arel_table[key].contained_within(value))
+ end
+
+ @scope
+ end
+
+ def contained_within_or_equals(opts)
+ opts.each do |key, value|
+ @scope = @scope.where(arel_table[key].contained_within_or_equals(value))
+ end
+
+ @scope
+ end
+
+ def contains(opts)
+ opts.each do |key, value|
+ @scope = @scope.where(arel_table[key].contains(value))
+ end
+
+ @scope
+ end
+
+ def contains_or_equals(opts)
+ opts.each do |key, value|
+ @scope = @scope.where(arel_table[key].contains_or_equals(value))
+ end
+
+ @scope
+ end
+
def any(opts)
equality_to_function('ANY', opts)
end
@@ -25,9 +56,11 @@ def all(opts)
private
- def equality_to_function(function_name, opts)
- arel_table = @scope.engine.arel_table
+ def arel_table
+ @arel_table ||= @scope.engine.arel_table
+ end
+ def equality_to_function(function_name, opts)
opts.each do |key, value|
any_function = Arel::Nodes::NamedFunction.new(function_name, [arel_table[key]])
predicate = Arel::Nodes::Equality.new(value, any_function)
@@ -2,11 +2,9 @@ class CreatePeople < ActiveRecord::Migration
def change
create_table :people do |t|
t.inet :ip
- t.cidr :subnet, :subnet2
- t.integer :arrayzerd, :array => true
- t.inet :inet_arrayzerd, :array => true
- t.string :str_arrayzerd, :array => true, :limit => 5
- t.string :test
+ t.cidr :subnet
+ t.inet :tag_ids, :array => true
+ t.string :tags, :array => true
t.timestamps
end
View
@@ -14,17 +14,21 @@
ActiveRecord::Schema.define(:version => 20120501163758) do
add_extension "hstore"
+ add_extension "pg_trgm"
+ add_extension "citext"
create_table "people", :force => true do |t|
t.inet "ip"
t.cidr "subnet"
- t.cidr "subnet2"
- t.integer "arrayzerd", :array => true
- t.inet "inet_arrayzerd", :array => true
- t.string "str_arrayzerd", :limit => 5, :array => true
- t.string "test"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.inet "tag_ids", :array => true
+ t.string "tags", :array => true
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ create_table "sanity_tests", :force => true do |t|
+ t.string "tags", :array => true
+ t.integer "tag_ids", :array => true
end
end
@@ -1,41 +1,26 @@
require 'spec_helper'
-describe 'where array clauses' do
- let!(:adapter) { ActiveRecord::Base.connection }
-
- before do
- adapter.create_table :overlap_arel_arrays, :force => true do |t|
- t.string :tags, :array => true
- t.integer :tag_ids, :array => true
- end
-
- class OverlapArelArray < ActiveRecord::Base
- attr_accessible :tags, :tags_ids
- end
- end
- after do
- adapter.drop_table :overlap_arel_arrays
- end
- let(:equality_regex) { %r{\"overlap_arel_arrays\"\.\"tags\" = '\{\"working\"\}'} }
- let(:overlap_regex) { %r{\"overlap_arel_arrays\"\.\"tag_ids\" && '\{1,2\}'} }
- let(:any_regex) { %r{2 = ANY\(\"overlap_arel_arrays\"\.\"tag_ids\"\)} }
- let(:all_regex) { %r{2 = ALL\(\"overlap_arel_arrays\"\.\"tag_ids\"\)} }
+describe 'Array queries' do
+ let(:equality_regex) { %r{\"people\"\.\"tags\" = '\{\"working\"\}'} }
+ let(:overlap_regex) { %r{\"people\"\.\"tag_ids\" && '\{1,2\}'} }
+ let(:any_regex) { %r{2 = ANY\(\"people\"\.\"tag_ids\"\)} }
+ let(:all_regex) { %r{2 = ALL\(\"people\"\.\"tag_ids\"\)} }
describe '.where(:array_column => [])' do
it 'returns an array string instead of IN ()' do
- query = OverlapArelArray.where(:tags => ['working']).to_sql
+ query = Person.where(:tags => ['working']).to_sql
query.should match equality_regex
end
end
describe '.where.overlap(:column => value)' do
it 'generates the appropriate where clause' do
- query = OverlapArelArray.where.overlap(:tag_ids => [1,2])
+ query = Person.where.overlap(:tag_ids => [1,2])
query.to_sql.should match overlap_regex
end
it 'allows chaining' do
- query = OverlapArelArray.where.overlap(:tag_ids => [1,2]).where(:tags => ['working']).to_sql
+ query = Person.where.overlap(:tag_ids => [1,2]).where(:tags => ['working']).to_sql
query.should match overlap_regex
query.should match equality_regex
@@ -44,12 +29,12 @@ class OverlapArelArray < ActiveRecord::Base
describe '.where.any(:column => value)' do
it 'generates the appropriate where clause' do
- query = OverlapArelArray.where.any(:tag_ids => 2)
+ query = Person.where.any(:tag_ids => 2)
query.to_sql.should match any_regex
end
it 'allows chaining' do
- query = OverlapArelArray.where.any(:tag_ids => 2).where(:tags => ['working']).to_sql
+ query = Person.where.any(:tag_ids => 2).where(:tags => ['working']).to_sql
query.should match any_regex
query.should match equality_regex
@@ -58,12 +43,12 @@ class OverlapArelArray < ActiveRecord::Base
describe '.where.all(:column => value)' do
it 'generates the appropriate where clause' do
- query = OverlapArelArray.where.all(:tag_ids => 2)
+ query = Person.where.all(:tag_ids => 2)
query.to_sql.should match all_regex
end
it 'allows chaining' do
- query = OverlapArelArray.where.all(:tag_ids => 2).where(:tags => ['working']).to_sql
+ query = Person.where.all(:tag_ids => 2).where(:tags => ['working']).to_sql
query.should match all_regex
query.should match equality_regex
@@ -0,0 +1,32 @@
+require 'spec_helper'
+
+describe 'Contains queries' do
+ let(:contained_within_regex) { %r{\"people\"\.\"ip\" << '127.0.0.1/24'} }
+ let(:contained_within_equals_regex) { %r{\"people\"\.\"ip\" <<= '127.0.0.1/24'} }
+ let(:contains_regex) { %r{\"people\"\.\"ip\" >> '127.0.0.1'} }
+ let(:contains_equals_regex) { %r{\"people\"\.\"ip\" >>= '127.0.0.1'} }
+ describe '.where.contained_within(:column, value)' do
+ it 'generates the appropriate where clause' do
+ query = Person.where.contained_within(:ip => '127.0.0.1/24')
+ query.to_sql.should match contained_within_regex
+ end
+ end
+ describe '.where.contained_within_or_equals(:column, value)' do
+ it 'generates the appropriate where clause' do
+ query = Person.where.contained_within_or_equals(:ip => '127.0.0.1/24')
+ query.to_sql.should match contained_within_equals_regex
+ end
+ end
+ describe '.where.contains(:column, value)' do
+ it 'generates the appropriate where clause' do
+ query = Person.where.contains(:ip => '127.0.0.1')
+ query.to_sql.should match contains_regex
+ end
+ end
+ describe '.where.contained_within_or_equals(:column, value)' do
+ it 'generates the appropriate where clause' do
+ query = Person.where.contains_or_equals(:ip => '127.0.0.1')
+ query.to_sql.should match contains_equals_regex
+ end
+ end
+end

0 comments on commit 20dda03

Please sign in to comment.