ansr 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/README.md +35 -0
- data/ansr.gemspec +2 -1
- data/ansr_blacklight/Gemfile +3 -0
- data/ansr_blacklight/README.md +37 -0
- data/ansr_blacklight/ansr_blacklight.gemspec +28 -0
- data/ansr_blacklight/lib/ansr_blacklight.rb +57 -0
- data/ansr_blacklight/lib/ansr_blacklight/arel.rb +6 -0
- data/ansr_blacklight/lib/ansr_blacklight/arel/big_table.rb +57 -0
- data/ansr_blacklight/lib/ansr_blacklight/arel/visitors.rb +6 -0
- data/ansr_blacklight/lib/ansr_blacklight/arel/visitors/query_builder.rb +217 -0
- data/ansr_blacklight/lib/ansr_blacklight/arel/visitors/to_no_sql.rb +14 -0
- data/ansr_blacklight/lib/ansr_blacklight/base.rb +21 -0
- data/ansr_blacklight/lib/ansr_blacklight/connection_adapters/no_sql_adapter.rb +38 -0
- data/ansr_blacklight/lib/ansr_blacklight/model/querying.rb +29 -0
- data/ansr_blacklight/lib/ansr_blacklight/relation.rb +50 -0
- data/ansr_blacklight/lib/ansr_blacklight/relation/solr_projection_methods.rb +55 -0
- data/ansr_blacklight/lib/ansr_blacklight/request_builders.rb +141 -0
- data/ansr_blacklight/lib/ansr_blacklight/solr.rb +4 -0
- data/ansr_blacklight/lib/ansr_blacklight/solr/request.rb +46 -0
- data/ansr_blacklight/lib/ansr_blacklight/solr/response.rb +94 -0
- data/ansr_blacklight/lib/ansr_blacklight/solr/response/group.rb +32 -0
- data/ansr_blacklight/lib/ansr_blacklight/solr/response/group_response.rb +50 -0
- data/ansr_blacklight/lib/ansr_blacklight/solr/response/more_like_this.rb +14 -0
- data/ansr_blacklight/lib/ansr_blacklight/solr/response/pagination_methods.rb +35 -0
- data/ansr_blacklight/lib/ansr_blacklight/solr/response/spelling.rb +92 -0
- data/ansr_blacklight/spec/fixtures/config.yml +0 -0
- data/ansr_blacklight/spec/lib/loaded_relation_spec.rb +223 -0
- data/ansr_blacklight/spec/lib/queryable_relation_spec.rb +133 -0
- data/ansr_blacklight/spec/lib/relation/faceting_spec.rb +475 -0
- data/ansr_blacklight/spec/lib/relation/grouping_spec.rb +159 -0
- data/ansr_blacklight/spec/spec_helper.rb +72 -0
- data/ansr_dpla/Gemfile +3 -0
- data/ansr_dpla/Gemfile.lock +138 -0
- data/ansr_dpla/README.md +2 -2
- data/ansr_dpla/ansr_dpla.gemspec +2 -2
- data/ansr_dpla/lib/ansr_dpla.rb +3 -0
- data/ansr_dpla/lib/ansr_dpla/api.rb +3 -3
- data/ansr_dpla/lib/ansr_dpla/arel.rb +1 -2
- data/ansr_dpla/lib/ansr_dpla/arel/big_table.rb +4 -0
- data/ansr_dpla/lib/ansr_dpla/arel/visitors.rb +6 -0
- data/ansr_dpla/lib/ansr_dpla/arel/visitors/query_builder.rb +188 -0
- data/ansr_dpla/lib/ansr_dpla/arel/visitors/to_no_sql.rb +9 -0
- data/ansr_dpla/lib/ansr_dpla/connection_adapters/no_sql_adapter.rb +58 -0
- data/ansr_dpla/lib/ansr_dpla/model/base.rb +7 -0
- data/ansr_dpla/lib/ansr_dpla/model/querying.rb +6 -10
- data/ansr_dpla/lib/ansr_dpla/relation.rb +61 -0
- data/ansr_dpla/lib/ansr_dpla/request.rb +5 -0
- data/ansr_dpla/spec/lib/api_spec.rb +8 -5
- data/ansr_dpla/spec/lib/item_spec.rb +2 -2
- data/ansr_dpla/spec/lib/relation/facet_spec.rb +27 -19
- data/ansr_dpla/spec/lib/relation/select_spec.rb +10 -8
- data/ansr_dpla/spec/lib/relation/where_spec.rb +1 -1
- data/ansr_dpla/spec/lib/relation_spec.rb +31 -29
- data/ansr_dpla/test/system.rb +4 -2
- data/lib/ansr.rb +7 -0
- data/lib/ansr/arel.rb +3 -0
- data/lib/ansr/arel/big_table.rb +43 -3
- data/lib/ansr/arel/configured_field.rb +19 -0
- data/lib/ansr/arel/nodes.rb +41 -0
- data/lib/ansr/arel/visitors.rb +7 -0
- data/lib/ansr/arel/visitors/context.rb +13 -0
- data/lib/ansr/arel/visitors/query_builder.rb +47 -0
- data/lib/ansr/arel/visitors/to_no_sql.rb +41 -0
- data/lib/ansr/base.rb +29 -1
- data/lib/ansr/configurable.rb +6 -12
- data/lib/ansr/connection_adapters.rb +5 -0
- data/lib/ansr/connection_adapters/no_sql_adapter.rb +80 -0
- data/lib/ansr/dummy_associations.rb +105 -0
- data/lib/ansr/facets.rb +103 -0
- data/lib/ansr/model.rb +17 -107
- data/lib/ansr/model/connection_handler.rb +6 -0
- data/lib/ansr/relation.rb +40 -23
- data/lib/ansr/relation/group.rb +31 -0
- data/lib/ansr/relation/predicate_builder.rb +106 -0
- data/lib/ansr/relation/query_methods.rb +192 -45
- data/lib/ansr/sanitization.rb +5 -18
- data/lib/ansr/utils.rb +89 -0
- data/lib/ansr/version.rb +1 -1
- metadata +73 -25
- data/ansr_dpla/lib/ansr_dpla/arel/connection.rb +0 -81
- data/ansr_dpla/lib/ansr_dpla/arel/query_builder.rb +0 -131
- data/lib/ansr/model/connection.rb +0 -103
@@ -1,131 +0,0 @@
|
|
1
|
-
module Ansr::Dpla
|
2
|
-
module Arel
|
3
|
-
class QueryBuilder
|
4
|
-
attr_reader :query_opts, :aliases, :table
|
5
|
-
def initialize(big_table)
|
6
|
-
@query_opts = {}
|
7
|
-
@aliases = {}
|
8
|
-
@table = big_table
|
9
|
-
end
|
10
|
-
def fields=(value)
|
11
|
-
@query_opts[:fields] = value
|
12
|
-
end
|
13
|
-
def select(arel_nodes=nil)
|
14
|
-
arel_nodes = [arel_nodes].compact unless arel_nodes.respond_to? :each
|
15
|
-
arel_nodes.each.select {|an| ::Arel::SqlLiteral === an}.each do |n|
|
16
|
-
select_val = n.to_s.split(" AS ")
|
17
|
-
add_field(select_val[0])
|
18
|
-
aliases[select_val[0]] = select_val[1] if select_val[1]
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def field_key_from_node(node)
|
23
|
-
table.model.field_name(node)
|
24
|
-
end
|
25
|
-
|
26
|
-
def add_field(field_name)
|
27
|
-
return unless field_name
|
28
|
-
query_opts[:fields] ||= ""
|
29
|
-
query_opts[:fields] << ',' << field_name
|
30
|
-
query_opts[:fields].sub!(/^,/,'')
|
31
|
-
end
|
32
|
-
|
33
|
-
def add_facet(field_name)
|
34
|
-
return unless field_name
|
35
|
-
field_name = Array(field_name).uniq
|
36
|
-
if query_opts[:facets]
|
37
|
-
query_opts[:facets] = (Array(query_opts[:facets]) + field_name).uniq
|
38
|
-
else
|
39
|
-
query_opts[:facets] = field_name[1] ? field_name : field_name[0]
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def where(arel_nodes)
|
44
|
-
arel_nodes = (arel_nodes.respond_to? :each) ? arel_nodes : Array(arel_nodes)
|
45
|
-
arel_nodes.each do |node|
|
46
|
-
case node
|
47
|
-
when ::Arel::Nodes::Equality
|
48
|
-
field_key = field_key_from_node(node.left)
|
49
|
-
if @query_opts[field_key]
|
50
|
-
@query_opts[field_key] = (Array(@query_opts[field_key]) << node.right)
|
51
|
-
else
|
52
|
-
@query_opts[field_key] = node.right
|
53
|
-
end
|
54
|
-
when ::Arel::Nodes::Grouping
|
55
|
-
n = node.expr
|
56
|
-
if ::Arel::Nodes::Binary === n
|
57
|
-
prefix = nil
|
58
|
-
prefix = "NOT" if (::Arel::Nodes::NotEqual === n)
|
59
|
-
prefix = "OR" if (::Arel::Nodes::Or === n)
|
60
|
-
if prefix
|
61
|
-
val = "#{prefix} #{n.right}"
|
62
|
-
else
|
63
|
-
val = n.right
|
64
|
-
end
|
65
|
-
field_key = field_key_from_node(n)
|
66
|
-
if @query_opts[field_key]
|
67
|
-
@query_opts[field_key] = Array(@query_opts[field_key]) << val
|
68
|
-
else
|
69
|
-
@query_opts[field_key] = val
|
70
|
-
end
|
71
|
-
end
|
72
|
-
when ::Arel::Attributes::Attribute # this is the field references
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
def order(*arel_nodes)
|
78
|
-
direction = nil
|
79
|
-
nodes = []
|
80
|
-
arel_nodes.inject(nodes) do |c, n|
|
81
|
-
if ::Arel::Nodes::Ordering === n
|
82
|
-
c << n
|
83
|
-
elsif n.is_a? String
|
84
|
-
_ns = n.split(',')
|
85
|
-
_ns.each do |_n|
|
86
|
-
_p = _n.split(/\s+/)
|
87
|
-
if (_p[1])
|
88
|
-
_p[1] = _p[1].downcase.to_sym
|
89
|
-
else
|
90
|
-
_p[1] = :asc
|
91
|
-
end
|
92
|
-
c << table[_p[0].to_sym].send(_p[1])
|
93
|
-
end
|
94
|
-
end
|
95
|
-
c
|
96
|
-
end
|
97
|
-
nodes.each do |node|
|
98
|
-
if ::Arel::Nodes::Ordering === node
|
99
|
-
if @query_opts[:sort_by]
|
100
|
-
@query_opts[:sort_by] = Array[@query_opts[:sort_by]] << node.expr.name
|
101
|
-
else
|
102
|
-
@query_opts[:sort_by] = node.expr.name
|
103
|
-
end
|
104
|
-
direction = :asc if (::Arel::Nodes::Ascending === node and direction)
|
105
|
-
direction = :desc if (::Arel::Nodes::Descending === node)
|
106
|
-
end
|
107
|
-
end
|
108
|
-
@query_opts[:sort_order] = direction if direction
|
109
|
-
end
|
110
|
-
|
111
|
-
def take(value=nil)
|
112
|
-
if value and (value = value.to_i)
|
113
|
-
raise "Page size cannot be > 500 (#{value}" if value > 500
|
114
|
-
@query_opts[:page_size] = value
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
def skip(value=nil)
|
119
|
-
if value
|
120
|
-
@query_opts[:page] = (value.to_i / (@query_opts[:page_size] || Ansr::Relation::DEFAULT_PAGE_SIZE)) + 1
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
def filters=(values)
|
125
|
-
unless values.empty?
|
126
|
-
@query_opts[:facets] = (values[1]) ? values : values[0]
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|
@@ -1,103 +0,0 @@
|
|
1
|
-
module Ansr
|
2
|
-
module Model
|
3
|
-
class Connection
|
4
|
-
def initialize(klass)
|
5
|
-
@table = klass.table
|
6
|
-
end
|
7
|
-
|
8
|
-
def primary_key(table_name)
|
9
|
-
'id'
|
10
|
-
end
|
11
|
-
|
12
|
-
def limit(arel_node)
|
13
|
-
case arel_node
|
14
|
-
when ::Arel::SelectManager
|
15
|
-
arel_node.limit
|
16
|
-
when ::Arel::Nodes::SelectStatement
|
17
|
-
arel_node.limit && arel_node.limit.expr
|
18
|
-
else
|
19
|
-
nil
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def offset(arel_node)
|
24
|
-
case arel_node
|
25
|
-
when ::Arel::SelectManager
|
26
|
-
arel_node.offset
|
27
|
-
when ::Arel::Nodes::SelectStatement
|
28
|
-
arel_node.offset && arel_node.offset.expr
|
29
|
-
else
|
30
|
-
nil
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def constraints(arel_node)
|
35
|
-
case arel_node
|
36
|
-
when ::Arel::SelectManager
|
37
|
-
arel_node.constraints
|
38
|
-
when ::Arel::Nodes::SelectStatement
|
39
|
-
arel_node.cores.last.wheres
|
40
|
-
else
|
41
|
-
nil
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def projections(arel_node)
|
46
|
-
case arel_node
|
47
|
-
when ::Arel::SelectManager
|
48
|
-
arel_node.projections
|
49
|
-
when ::Arel::Nodes::SelectStatement
|
50
|
-
arel_node.cores.last.projections
|
51
|
-
else
|
52
|
-
nil
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def orders(arel_node)
|
57
|
-
case arel_node
|
58
|
-
when ::Arel::SelectManager
|
59
|
-
arel_node.orders
|
60
|
-
when ::Arel::Nodes::SelectStatement
|
61
|
-
arel_node.orders
|
62
|
-
else
|
63
|
-
nil
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
def schema_cache
|
68
|
-
ActiveRecord::ConnectionAdapters::SchemaCache.new(self)
|
69
|
-
end
|
70
|
-
|
71
|
-
def table_exists?(table_name)
|
72
|
-
true
|
73
|
-
end
|
74
|
-
|
75
|
-
# this is called by the BigTable impl
|
76
|
-
def columns(table_name, *rest)
|
77
|
-
@table.fields.map {|s| ::ActiveRecord::ConnectionAdapters::Column.new(s.to_s, nil, nil)}
|
78
|
-
end
|
79
|
-
|
80
|
-
def sanitize_limit(limit_value)
|
81
|
-
if limit_value.to_s.to_i >= 0
|
82
|
-
limit_value
|
83
|
-
else
|
84
|
-
Ansr::Relation::DEFAULT_PAGE_SIZE
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
def sanitize_filter_name(filter_value)
|
89
|
-
if filter_value.is_a? Array
|
90
|
-
return filter_value.collect {|x| sanitize_filter_name(x)}.compact
|
91
|
-
else
|
92
|
-
if @table.facets.include? filter_value.to_sym
|
93
|
-
return filter_value
|
94
|
-
else
|
95
|
-
raise "#{filter_value} is not a facetable field"
|
96
|
-
#Rails.logger.warn "Ignoring #{filter_value} (not a filterable field)" if Rails.logger
|
97
|
-
#return nil
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|