rql 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +30 -2
- data/lib/rql/dsl.rb +1 -0
- data/lib/rql/dsl/comparisons.rb +7 -3
- data/lib/rql/dsl/context.rb +1 -0
- data/lib/rql/dsl/functions.rb +9 -0
- data/lib/rql/queryable.rb +1 -1
- data/lib/rql/scope/block_methods.rb +8 -0
- data/lib/rql/scope/param_methods.rb +8 -0
- data/lib/rql/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 377409fcba26dcedaba557963e2b24c98aaeb947
|
4
|
+
data.tar.gz: 161b3d6a9495f23f604c85ee1a6622f925682e5b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0ce38ebc39256f5b07243409eed49efe1501209a41282756f193c812a0f0a74c7425788083ad056c3aca8996b44885088c4bc188091c94d992999c4e738e25c9
|
7
|
+
data.tar.gz: 5717564360f289e9a86bb0f1f204f966d63f366807e786d644b2c28fb7d7835123bb72124f5d63f2ad6ca21797328d065e8fe721a052d97c7dcd83c1532ec160
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# RQL [![Build Status](https://secure.travis-ci.org/liefni/rql.svg)](http://travis-ci.org/liefni/rql) [![Gem Version](https://badge.fury.io/rb/rql.svg)](https://badge.fury.io/rb/rql)
|
2
2
|
|
3
|
-
RQL is a DSL for ActiveRecord designed to allow derived/calculated attributes on models to be used in database queries.
|
3
|
+
RQL is a DSL for Ruby on Rails' ActiveRecord, designed to allow derived/calculated attributes on models to be used in database queries.
|
4
4
|
|
5
5
|
[Api Documentation](https://www.rubydoc.info/gems/rql/)
|
6
6
|
|
@@ -81,6 +81,32 @@ If a derived attribute has not been preloaded then the code will be evaluated in
|
|
81
81
|
accessed. For instance calling `Book.first.leaves` will return `pages / 2` just as a normal method would, without having
|
82
82
|
to query the database.
|
83
83
|
|
84
|
+
### Case Sensitivity and Regexp
|
85
|
+
|
86
|
+
As strings in ruby are case sensitive, to get consistent results when derived attributes are both run is the context of
|
87
|
+
model and in the database you will need to configure your database to be case sensitive.
|
88
|
+
|
89
|
+
To perform case-insensitive queries downcase the values you are comparing e.g.
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
Authors.where{name.downcase == my_string.downcase}
|
93
|
+
```
|
94
|
+
|
95
|
+
Alternatively you can use regexp matching with the =~ operator. This is case sensitive by default but supports the case
|
96
|
+
insensitive flag.
|
97
|
+
|
98
|
+
Case sensitive regexp match:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
Authors.where{name =~ /[A-Za-z]/}
|
102
|
+
```
|
103
|
+
|
104
|
+
Case insensitive match using i flag:
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
Authors.where{name =~ /[A-Z]/i}
|
108
|
+
```
|
109
|
+
|
84
110
|
### Query DSL
|
85
111
|
|
86
112
|
#### Operators
|
@@ -94,6 +120,7 @@ to query the database.
|
|
94
120
|
|< |`Book.rql.where {pages < 200}` |`SELECT * FROM "book" WHERE "book"."pages" < 200` |
|
95
121
|
|>= |`Book.rql.where {pages >= 200}` |`SELECT * FROM "book" WHERE "book"."pages" >= 200` |
|
96
122
|
|<= |`Book.rql.where {pages <= 200}` |`SELECT * FROM "book" WHERE "book"."pages" <= 200` |
|
123
|
+
|=~ |`Book.rql.where {name =~ /^[A-Z]*$/}` |`SELECT * FROM "book" WHERE "book"."name" ~ '^[A-Z]*$'` |
|
97
124
|
|+ |`Book.rql.where {pages + 2 == 200}` |`SELECT * FROM "book" WHERE ("book"."pages" + 2) = 200` |
|
98
125
|
|- |`Book.rql.where {pages - 2 == 200}` |`SELECT * FROM "book" WHERE ("book"."pages" - 2) = 200` |
|
99
126
|
|* |`Book.rql.where {pages * 2 == 200}` |`SELECT * FROM "book" WHERE "book"."pages" * 2 = 200` |
|
@@ -108,7 +135,8 @@ to query the database.
|
|
108
135
|
|`start_with?` |`Book.rql.where {name.start_with?('A')}` |`SELECT * FROM "book" WHERE "book"."name" ILIKE 'A%'` |
|
109
136
|
|`end_with?` |`Book.rql.where {name.end_with?('A')}` |`SELECT * FROM "book" WHERE "book"."name" ILIKE '%A'` |
|
110
137
|
|`include?` |`Book.rql.where {name.include?('A')}` |`SELECT * FROM "book" WHERE "book"."name" ILIKE '%A%'` |
|
111
|
-
|`in
|
138
|
+
|`in?` |`Book.rql.where {name.in?(['A', 'B'])}` |`SELECT * FROM "book" WHERE "book"."name" IN ('A', 'B')`|
|
139
|
+
|`downcase` |`Book.rql.where {name.downcase == 'a'}` |`SELECT * FROM "book" WHERE LOWER("book"."name") = 'a'` |
|
112
140
|
|
113
141
|
|Aggregates |Example |
|
114
142
|
|---------------------------------|----------------------------------------------------------|
|
data/lib/rql/dsl.rb
CHANGED
data/lib/rql/dsl/comparisons.rb
CHANGED
@@ -31,16 +31,20 @@ module Rql
|
|
31
31
|
Context.new(@model, arel.gteq(value))
|
32
32
|
end
|
33
33
|
|
34
|
+
def =~(value)
|
35
|
+
Context.new(@model, arel.matches_regexp(value.source, !value.casefold?))
|
36
|
+
end
|
37
|
+
|
34
38
|
def start_with?(value)
|
35
|
-
Context.new(@model, arel.matches("#{value}%"))
|
39
|
+
Context.new(@model, arel.matches("#{value}%", nil, true))
|
36
40
|
end
|
37
41
|
|
38
42
|
def end_with?(value)
|
39
|
-
Context.new(@model, arel.matches("%#{value}"))
|
43
|
+
Context.new(@model, arel.matches("%#{value}", nil, true))
|
40
44
|
end
|
41
45
|
|
42
46
|
def include?(value)
|
43
|
-
Context.new(@model, arel.matches("%#{value}%"))
|
47
|
+
Context.new(@model, arel.matches("%#{value}%", nil, true))
|
44
48
|
end
|
45
49
|
|
46
50
|
def in?(collection)
|
data/lib/rql/dsl/context.rb
CHANGED
data/lib/rql/queryable.rb
CHANGED
@@ -18,7 +18,7 @@ module Rql
|
|
18
18
|
# @yield the code block to be converted to sql
|
19
19
|
# @return [Rql::Dsl::Context] object wrapping arel for the evaluated block of code
|
20
20
|
def eval_rql(alias_derived_attr = false, &block)
|
21
|
-
Dsl::Base.new(self, alias_derived_attr).instance_eval(&block)
|
21
|
+
Dsl::Base.new(self.unscoped, alias_derived_attr).instance_eval(&block)
|
22
22
|
end
|
23
23
|
|
24
24
|
# Defines a derived attribute on a model
|
@@ -17,6 +17,14 @@ module Rql
|
|
17
17
|
scope.select(*build_attributes(true, &block))
|
18
18
|
end
|
19
19
|
|
20
|
+
# Plucks the specified attributes, creating and array of values
|
21
|
+
#
|
22
|
+
# @yield RQL block defining the attributes to be selected
|
23
|
+
# @return [Array] an array of values if plucking one value or an array of arrays if plucking multiple values
|
24
|
+
def pluck(&block)
|
25
|
+
scope.pluck(*build_attributes(true, &block))
|
26
|
+
end
|
27
|
+
|
20
28
|
# Orders by specified attributes
|
21
29
|
#
|
22
30
|
# @yield RQL block defining the attributes order by
|
@@ -17,6 +17,14 @@ module Rql
|
|
17
17
|
scope.select(*attributes.map{|attr| scope.derived_attributes[attr] ? scope.eval_rql(&scope.derived_attributes[attr]).as(attr).arel : attr})
|
18
18
|
end
|
19
19
|
|
20
|
+
# Plucks the specified attributes, creating and array of values
|
21
|
+
#
|
22
|
+
# @param attributes [Array] the attributes to select
|
23
|
+
# @return [Array] an array of values if plucking one value or an array of arrays if plucking multiple values
|
24
|
+
def pluck(*attributes)
|
25
|
+
scope.pluck(*attributes.map{|attr| scope.derived_attributes[attr] ? scope.eval_rql(&scope.derived_attributes[attr]).as(attr).arel : attr})
|
26
|
+
end
|
27
|
+
|
20
28
|
# Orders by specified attributes
|
21
29
|
#
|
22
30
|
# @param attributes [Array] the attributes to order by. Attributes may be a symbol, sql string or a hash specifying attribute and order.
|
data/lib/rql/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lief Nikkel
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-07-
|
11
|
+
date: 2018-07-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -137,6 +137,7 @@ files:
|
|
137
137
|
- lib/rql/dsl/base.rb
|
138
138
|
- lib/rql/dsl/comparisons.rb
|
139
139
|
- lib/rql/dsl/context.rb
|
140
|
+
- lib/rql/dsl/functions.rb
|
140
141
|
- lib/rql/dsl/logic.rb
|
141
142
|
- lib/rql/dsl/maths.rb
|
142
143
|
- lib/rql/dsl/orders.rb
|