influxdb-arel 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -0
- data/Gemfile +1 -0
- data/README.md +218 -124
- data/lib/influxdb/arel.rb +25 -16
- data/lib/influxdb/arel/builder.rb +209 -0
- data/lib/influxdb/arel/clauses.rb +6 -0
- data/lib/influxdb/arel/clauses/base.rb +34 -0
- data/lib/influxdb/arel/clauses/expressions.rb +75 -0
- data/lib/influxdb/arel/clauses/from_clause.rb +45 -0
- data/lib/influxdb/arel/clauses/group_clause.rb +34 -0
- data/lib/influxdb/arel/clauses/select_clause.rb +37 -0
- data/lib/influxdb/arel/clauses/where_clause.rb +102 -0
- data/lib/influxdb/arel/core_extensions.rb +5 -21
- data/lib/influxdb/arel/delete_manager.rb +50 -0
- data/lib/influxdb/arel/extensions.rb +6 -0
- data/lib/influxdb/arel/extensions/alias_predication.rb +11 -0
- data/lib/influxdb/arel/extensions/boolean_predications.rb +15 -0
- data/lib/influxdb/arel/extensions/expressions.rb +75 -0
- data/lib/influxdb/arel/extensions/joining_merging.rb +21 -0
- data/lib/influxdb/arel/extensions/math.rb +23 -0
- data/lib/influxdb/arel/extensions/predications.rb +144 -0
- data/lib/influxdb/arel/nodes.rb +7 -14
- data/lib/influxdb/arel/nodes/attribute.rb +20 -0
- data/lib/influxdb/arel/nodes/binary.rb +4 -3
- data/lib/influxdb/arel/nodes/delete_statement.rb +37 -0
- data/lib/influxdb/arel/nodes/duration.rb +3 -3
- data/lib/influxdb/arel/nodes/function.rb +2 -2
- data/lib/influxdb/arel/nodes/grouping.rb +3 -3
- data/lib/influxdb/arel/nodes/infix_operation.rb +11 -4
- data/lib/influxdb/arel/nodes/merge.rb +13 -0
- data/lib/influxdb/arel/nodes/node.rb +18 -6
- data/lib/influxdb/arel/nodes/now.rb +1 -1
- data/lib/influxdb/arel/nodes/ordering.rb +15 -0
- data/lib/influxdb/arel/nodes/select_statement.rb +10 -8
- data/lib/influxdb/arel/nodes/sql_literal.rb +5 -8
- data/lib/influxdb/arel/nodes/table.rb +19 -0
- data/lib/influxdb/arel/nodes/table_alias.rb +2 -4
- data/lib/influxdb/arel/nodes/unary.rb +6 -1
- data/lib/influxdb/arel/quoter.rb +85 -0
- data/lib/influxdb/arel/select_manager.rb +111 -64
- data/lib/influxdb/arel/tree_manager.rb +15 -5
- data/lib/influxdb/arel/version.rb +1 -1
- data/lib/influxdb/arel/visitor.rb +96 -126
- data/lib/influxdb/arel/visitor/delete_statement.rb +32 -0
- data/lib/influxdb/arel/visitor/select_statement.rb +59 -0
- data/lib/influxdb/arel/visitor/where_statement.rb +14 -0
- data/spec/lib/influxdb/arel/builder_spec.rb +173 -0
- data/spec/lib/influxdb/arel/core_extensions_spec.rb +0 -21
- data/spec/lib/influxdb/arel/nodes/and_spec.rb +6 -9
- data/spec/lib/influxdb/arel/nodes/attribute_spec.rb +21 -0
- data/spec/lib/influxdb/arel/nodes/binary_spec.rb +0 -4
- data/spec/lib/influxdb/arel/nodes/duration_spec.rb +1 -0
- data/spec/lib/influxdb/arel/nodes/in_spec.rb +1 -0
- data/spec/lib/influxdb/arel/nodes/infix_operation_spec.rb +17 -0
- data/spec/lib/influxdb/arel/nodes/merge_spec.rb +25 -0
- data/spec/lib/influxdb/arel/nodes/now_spec.rb +1 -0
- data/spec/lib/influxdb/arel/nodes/ordering_spec.rb +19 -0
- data/spec/lib/influxdb/arel/nodes/sql_literal_spec.rb +1 -5
- data/spec/lib/influxdb/arel/nodes/table_alias_spec.rb +3 -10
- data/spec/lib/influxdb/arel/nodes/table_spec.rb +8 -0
- data/spec/lib/influxdb/arel/nodes/unary_spec.rb +0 -4
- data/spec/lib/influxdb/arel/quoter/repository_spec.rb +10 -0
- data/spec/lib/influxdb/arel/quoter_spec.rb +33 -0
- data/spec/lib/influxdb/arel/select_manager_spec.rb +356 -156
- data/spec/lib/influxdb/arel_spec.rb +22 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/support/examples/binary_node.rb +1 -0
- data/spec/support/examples/function_node.rb +1 -0
- data/spec/support/examples/infix_operation_node.rb +15 -0
- data/spec/support/examples/node_boolean_predications.rb +21 -0
- data/spec/support/examples/node_joining_merging.rb +53 -0
- data/spec/support/examples/unary_node.rb +1 -0
- data/spec/support/fabrics.rb +3 -3
- metadata +49 -11
- data/lib/influxdb/arel/alias_predication.rb +0 -9
- data/lib/influxdb/arel/attributes.rb +0 -1
- data/lib/influxdb/arel/attributes/attribute.rb +0 -74
- data/lib/influxdb/arel/expressions.rb +0 -73
- data/lib/influxdb/arel/math.rb +0 -21
- data/lib/influxdb/arel/predications.rb +0 -137
- data/lib/influxdb/arel/table.rb +0 -219
- data/spec/lib/influxdb/arel/table_spec.rb +0 -193
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea2a6904d2c1edcc21371849fa2562c29e1d28c8
|
4
|
+
data.tar.gz: 8fba9de6c9530759f08830d244efb398f4cc4408
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 19647b7de298d43a84914ef4323ea715fcd173c0e3bae565b5b7c534148a1e33eef3c0911ee61898deb8ab5b518cc87378c2eb591059f81d580e8730709a6ddd
|
7
|
+
data.tar.gz: 4d1ed64baede232e081db834ca8e946e3c034658da4719666c2a70b23565f5152440f8b98c473bbefa0588cd5d821f078cf8f333e5a524454a10575e004cb663
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,10 @@
|
|
2
2
|
|
3
3
|
Influxdb::Arel is a SQL AST manager for Influxdb dialect. It simplifies the generation of complex SQL queries.
|
4
4
|
|
5
|
-
[![Build Status](https://travis-ci.org/undr/influxdb-arel.svg?branch=master)](https://travis-ci.org/undr/influxdb-arel)
|
5
|
+
[![Build Status](https://travis-ci.org/undr/influxdb-arel.svg?branch=master)](https://travis-ci.org/undr/influxdb-arel)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/undr/influxdb-arel/badges/gpa.svg)](https://codeclimate.com/github/undr/influxdb-arel)
|
7
|
+
[![Gem Version](https://badge.fury.io/rb/influxdb-arel.svg)](http://badge.fury.io/rb/influxdb-arel)
|
8
|
+
[![Test Coverage](https://codeclimate.com/github/undr/influxdb-arel/badges/coverage.svg)](https://codeclimate.com/github/undr/influxdb-arel)
|
6
9
|
|
7
10
|
## Installation
|
8
11
|
|
@@ -28,24 +31,22 @@ $ gem install influxdb-arel
|
|
28
31
|
|
29
32
|
### Introduction
|
30
33
|
|
31
|
-
At start you should create a
|
34
|
+
At start you should create a builder:
|
32
35
|
|
33
36
|
```ruby
|
34
|
-
|
35
|
-
|
37
|
+
builder = Influxdb::Arel::Builder.new(:events)
|
38
|
+
builder.to_sql
|
36
39
|
# => SELECT * FROM events
|
37
40
|
```
|
38
41
|
|
39
|
-
You can use both
|
42
|
+
You can set default table name for the builder. Possible to use both strings and symbols:
|
40
43
|
|
41
44
|
```ruby
|
42
|
-
Influxdb::Arel::
|
45
|
+
Influxdb::Arel::Builder.new('events') == Influxdb::Arel::Builder.new(:events)
|
43
46
|
# => true
|
44
47
|
```
|
45
48
|
|
46
|
-
|
47
|
-
|
48
|
-
If you want to use convenient shortcuts, such as `10.h.ago` or `1.w.time` you should require file with core extensions
|
49
|
+
If you want to use convenient shortcuts, such as `10.h.ago` or `1.w` you should require a file with core extensions
|
49
50
|
|
50
51
|
```ruby
|
51
52
|
require 'influxdb/arel/core_extensions'
|
@@ -56,84 +57,179 @@ require 'influxdb/arel/core_extensions'
|
|
56
57
|
1.h.to_sql
|
57
58
|
# => "1h"
|
58
59
|
|
59
|
-
1.h.time
|
60
|
-
# => #<Influxdb::Arel::Nodes::Time:0x0000010282f728 @expr=#<Influxdb::Arel::Nodes::Duration:0x0000010282f868 @left=1, @right="h">>
|
61
|
-
|
62
|
-
1.h.time.to_sql
|
63
|
-
# => "time(1h)"
|
64
|
-
|
65
60
|
1.h.ago.to_sql
|
66
61
|
# => "(now() - 1h)"
|
67
62
|
|
68
63
|
1.h.since.to_sql
|
69
64
|
# => "(now() + 1h)"
|
65
|
+
```
|
70
66
|
|
71
|
-
|
72
|
-
# => true
|
67
|
+
A builder has methods for SQL construction.
|
73
68
|
|
74
|
-
|
75
|
-
# => true
|
69
|
+
* Specifying which attributes should be used in the query: `select`.
|
76
70
|
|
77
|
-
:
|
78
|
-
# => true
|
71
|
+
* Specifying which tables and how they should be used in the query: `from`, `merge` and `join`.
|
79
72
|
|
80
|
-
:
|
81
|
-
|
73
|
+
* Conditions of query: `where`.
|
74
|
+
|
75
|
+
* Grouping methods: `group` and `fill`.
|
76
|
+
|
77
|
+
* Ordering methods: `order`, `asc`, `desc` and `invert_order`.
|
78
|
+
|
79
|
+
* Specifying limitations of result set: `limit`.
|
80
|
+
|
81
|
+
* The part of continuous queries: `into`.
|
82
|
+
|
83
|
+
Most of them accept a block for building part of SQL. Inside a block calling of method will be interpreted depending on current context.
|
84
|
+
For example:
|
82
85
|
|
83
|
-
|
84
|
-
# => #<Influxdb::Arel::Nodes::As:0x00000101218f70 @left="MEAN(value)", @right="user_events">
|
86
|
+
#### In `SELECT`, `WHERE`and `GROUP` contexts:
|
85
87
|
|
86
|
-
|
87
|
-
# => "MEAN(value) AS user_events"
|
88
|
+
- All undefined methods will be interpreted as attributes:
|
88
89
|
|
89
|
-
|
90
|
-
|
90
|
+
```ruby
|
91
|
+
builder.where{ pp name.is_a?(Influxdb::Arel::Nodes::Attribute) }
|
92
|
+
# true
|
93
|
+
# => ...
|
94
|
+
builder.where{ name =~ /undr/ }.to_sql
|
95
|
+
# => SELECT * FROM table WHERE name =~ /undr/
|
96
|
+
```
|
91
97
|
|
92
|
-
|
93
|
-
|
98
|
+
- Method `a` returns attribute node.
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
builder.where{ pp a(:name) == name }
|
102
|
+
# true
|
103
|
+
# => ...
|
94
104
|
```
|
95
105
|
|
96
|
-
|
106
|
+
- Method `time` returns `Influxdb::Arel::Nodes::Time` object. (It will be available only in `GROUP` context)
|
97
107
|
|
98
|
-
|
108
|
+
- Method `now` returns `Influxdb::Arel::Nodes::Now` object. (It will be available only in `WHERE` context)
|
99
109
|
|
100
|
-
|
110
|
+
|
111
|
+
#### In `FROM`, `JOIN` and `MERGE` contexts
|
112
|
+
|
113
|
+
- All undefined methods will be interpreted as tables:
|
101
114
|
|
102
115
|
```ruby
|
103
|
-
events.
|
104
|
-
#
|
116
|
+
builder.select{ pp events.is_a?(Influxdb::Arel::Nodes::Table) }
|
117
|
+
# true
|
118
|
+
# => ...
|
119
|
+
builder.from{ events }.to_sql
|
120
|
+
# => SELECT * FROM events
|
105
121
|
```
|
106
122
|
|
107
|
-
-
|
123
|
+
- Method `t` returns table node.
|
108
124
|
|
109
125
|
```ruby
|
110
|
-
|
111
|
-
#
|
126
|
+
builder.from{ pp t(:table) == table }
|
127
|
+
# true
|
128
|
+
# => ...
|
112
129
|
```
|
113
130
|
|
114
|
-
-
|
131
|
+
- Method `join` used for joining tables (available only in `JOIN` context).
|
115
132
|
|
116
133
|
```ruby
|
117
|
-
|
118
|
-
# => SELECT * FROM
|
134
|
+
builder.from{ join(table.as(:alias1), table.as(:alias2)) }.to_sql
|
135
|
+
# => SELECT * FROM table AS alias1 INNER JOIN table AS alias2
|
136
|
+
```
|
137
|
+
|
138
|
+
- Method `merge` used for merging tables (available only in `JOIN` context).
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
builder.from{ merge(table1, table2) }.to_sql
|
142
|
+
# => SELECT * FROM table1 MERGE table2
|
143
|
+
```
|
144
|
+
|
145
|
+
Also, into the block will be available `o` method. It used for access to outer scope. For example:
|
146
|
+
|
147
|
+
```ruby
|
148
|
+
regexp = /events\..*/
|
149
|
+
builder.from{ o{ regexp } }.to_sql
|
150
|
+
# => SELECT * FROM /events\..*/
|
151
|
+
```
|
152
|
+
|
153
|
+
### `SELECT` clause
|
154
|
+
|
155
|
+
You can specify attributes or expressions for `SELECT` clause using `select` method.
|
156
|
+
|
157
|
+
```ruby
|
158
|
+
builder = Influxdb::Arel::Builder.new(:cpu_load)
|
159
|
+
builder.to_sql
|
160
|
+
# => SELECT * FROM cpu_load
|
161
|
+
|
162
|
+
builder.select{ (system + user).as(:sum) }.to_sql
|
163
|
+
# => SELECT (system + user) AS sum FROM cpu_load
|
164
|
+
|
165
|
+
builder.select{
|
166
|
+
[mean(idle).as(:idle_mean), mean(user).as(:user_mean)]
|
167
|
+
}.group{ time(1.d) }.to_sql
|
168
|
+
# => SELECT MEAN(idle) AS idle_mean, MEAN(user) AS user_mean FROM cpu_load GROUP BY time(1d)
|
119
169
|
```
|
120
170
|
|
121
|
-
|
171
|
+
It might be convenient to use aliases for complex expressions, such as functions or some mathematical expressions.
|
122
172
|
|
123
|
-
|
173
|
+
Chaining `select` method will add attributes or expressions to the set. If you want to override expressions when use `select!` method.
|
124
174
|
|
125
175
|
```ruby
|
126
|
-
|
176
|
+
builder.select(:name).select(:age).to_sql
|
177
|
+
# => SELECT name, age FROM table
|
178
|
+
builder.select(:name).select!(:age).to_sql
|
179
|
+
# => SELECT age FROM table
|
180
|
+
```
|
181
|
+
|
182
|
+
### `FROM` clause
|
183
|
+
|
184
|
+
You can specify tables for query using `from` method.
|
185
|
+
|
186
|
+
Possible to call method:
|
187
|
+
|
188
|
+
- With strings or symbols as arguments
|
189
|
+
|
190
|
+
```ruby
|
191
|
+
builder.from('events', :errors).to_sql
|
192
|
+
# => SELECT * FROM events, errors
|
193
|
+
```
|
194
|
+
|
195
|
+
- With block
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
builder.from{ errors }.to_sql
|
127
199
|
# => SELECT * FROM errors
|
200
|
+
|
201
|
+
builder.from{ [errors, :events] }.to_sql
|
202
|
+
# => SELECT * FROM errors, events
|
203
|
+
```
|
204
|
+
|
205
|
+
- You can mix both
|
206
|
+
|
207
|
+
```ruby
|
208
|
+
builder.from(:events){ errors }.to_sql
|
209
|
+
# => SELECT * FROM events, errors
|
128
210
|
```
|
129
211
|
|
130
212
|
- With regexp object
|
131
213
|
|
132
214
|
```ruby
|
133
|
-
|
215
|
+
builder.from(/.*/).to_sql
|
216
|
+
# => SELECT * FROM /.*/
|
217
|
+
```
|
218
|
+
|
219
|
+
**Warning:** *You can call method with more then one regexp but only first will be used as table name*
|
220
|
+
|
221
|
+
```ruby
|
222
|
+
builder.from(/.*/, /logs\..*/).to_sql
|
134
223
|
# => SELECT * FROM /.*/
|
135
224
|
```
|
136
225
|
|
226
|
+
Chaining this method will replace previous `FORM` definition.
|
227
|
+
|
228
|
+
```ruby
|
229
|
+
builder.from(:table1).from{ table2 }.to_sql
|
230
|
+
# => SELECT * FROM table2
|
231
|
+
```
|
232
|
+
|
137
233
|
### Joining tables
|
138
234
|
|
139
235
|
You can join two tables using `join` method.
|
@@ -141,103 +237,121 @@ You can join two tables using `join` method.
|
|
141
237
|
It will join two first tables from tables list if method is called without argument
|
142
238
|
|
143
239
|
```ruby
|
144
|
-
|
145
|
-
|
240
|
+
builder = Influxdb::Arel::Builder.new(:table)
|
241
|
+
builder.from(:table1, :table2).join.to_sql
|
146
242
|
# => SELECT * FROM table1 INNER JOIN table2
|
243
|
+
builder.from{ [table1.as(:alias1), table2.as(:alias2)] }.join.to_sql
|
244
|
+
# => SELECT * FROM table1 AS alias1 INNER JOIN table2 AS alias2
|
147
245
|
```
|
148
246
|
|
149
|
-
It will
|
247
|
+
It will raise error if method is called without argument and tables list contains less than two table.
|
150
248
|
|
151
249
|
```ruby
|
152
|
-
|
153
|
-
# =>
|
250
|
+
builder.join.to_sql
|
251
|
+
# => IllegalSQLConstruct: Ambiguous joining clause
|
154
252
|
```
|
155
253
|
|
156
254
|
It will join first table from tables list with given table if argument exists.
|
157
255
|
|
158
256
|
```ruby
|
159
|
-
|
257
|
+
builder.join(:table2).to_sql
|
160
258
|
# => SELECT * FROM table INNER JOIN table2
|
161
259
|
```
|
162
260
|
|
261
|
+
And it will raise error if number of tables is more than two.
|
262
|
+
|
163
263
|
```ruby
|
164
|
-
|
165
|
-
# =>
|
264
|
+
builder.from(:table1, :table2).join(:table3).to_sql
|
265
|
+
# => IllegalSQLConstruct: Ambiguous joining clause
|
166
266
|
```
|
167
267
|
|
168
268
|
Also, you can define alias for each joined table. It would be useful for self joining table.
|
169
269
|
|
170
270
|
```ruby
|
171
|
-
|
271
|
+
builder.from{ table.as(:table_one).join(table.as(:table_two)) }.to_sql
|
172
272
|
# => SELECT * FROM table AS table_one INNER JOIN table AS table_two
|
173
273
|
```
|
174
274
|
|
175
275
|
Chaining this method will replace previous join definition.
|
176
276
|
|
177
277
|
```ruby
|
178
|
-
table1
|
179
|
-
table.join(table1).join(table1.as('alias')).to_sql
|
278
|
+
builder.join(:table1).join{ table1.as(:alias) }.to_sql
|
180
279
|
# => SELECT * FROM table INNER JOIN table1 AS alias
|
181
280
|
```
|
182
281
|
|
183
282
|
### Merging tables
|
184
283
|
|
185
|
-
You can merge tables using `merge` method.
|
284
|
+
You can merge two tables using `merge` method.
|
186
285
|
|
187
|
-
It will merge two first tables from tables list if method is called without argument
|
286
|
+
It will merge two first tables from tables list if method is called without argument
|
188
287
|
|
189
288
|
```ruby
|
190
|
-
|
191
|
-
|
289
|
+
builder = Influxdb::Arel::Builder.new(:table)
|
290
|
+
builder.from(:table1, :table2).merge.to_sql
|
291
|
+
# => SELECT * FROM table1 MERGE table2
|
292
|
+
builder.from{ [table1.as(:alias1), table2.as(:alias2)] }.merge.to_sql
|
192
293
|
# => SELECT * FROM table1 MERGE table2
|
193
294
|
```
|
194
295
|
|
195
|
-
It will
|
296
|
+
It will raise error if method is called without argument and tables list contains less than two table.
|
196
297
|
|
197
298
|
```ruby
|
198
|
-
|
199
|
-
# =>
|
299
|
+
builder.join.to_sql
|
300
|
+
# => IllegalSQLConstruct: Ambiguous merging clause
|
200
301
|
```
|
201
302
|
|
202
303
|
It will merge first table from tables list with given table if argument exists.
|
203
304
|
|
204
305
|
```ruby
|
205
|
-
|
306
|
+
builder.merge(:table2).to_sql
|
206
307
|
# => SELECT * FROM table MERGE table2
|
308
|
+
```
|
207
309
|
|
208
|
-
|
209
|
-
|
310
|
+
And it will raise error if number of tables is more than two.
|
311
|
+
|
312
|
+
```ruby
|
313
|
+
builder.from(:table1, :table2).merge(:table3).to_sql
|
314
|
+
# => IllegalSQLConstruct: Ambiguous merging clause
|
315
|
+
```
|
316
|
+
|
317
|
+
Also, you can define alias for each table, but it doesn't influence on result. Because aliases make sense only for joining tables.
|
318
|
+
|
319
|
+
```ruby
|
320
|
+
builder.from{ table1.as(:table1).merge(table1.as(:table2)) }.to_sql
|
321
|
+
# => SELECT * FROM table1 MERGE table2
|
210
322
|
```
|
211
323
|
|
212
324
|
Chaining this method will replace previous merge definition.
|
213
325
|
|
214
326
|
```ruby
|
215
|
-
|
327
|
+
builder.merge(:table1).merge(:table2).to_sql
|
216
328
|
# => SELECT * FROM table MERGE table2
|
217
329
|
```
|
218
330
|
|
219
331
|
### Grouping of results
|
220
332
|
|
221
|
-
Grouping of results by specified
|
333
|
+
Grouping of results by specified attributes or expressions, such as `time(10m)`:
|
222
334
|
|
223
335
|
```ruby
|
224
|
-
|
225
|
-
|
336
|
+
builder = Influxdb::Arel::Builder.new(:table)
|
337
|
+
builder.group{ [time(10.m), host] }.to_sql
|
226
338
|
# => SELECT * FROM table GROUP BY time(10m), host
|
227
339
|
```
|
228
340
|
|
229
341
|
If you want to fill intervals with no data you should call `fill` method:
|
230
342
|
|
231
343
|
```ruby
|
232
|
-
|
344
|
+
builder.group{ [time(10.m), host] }.fill(0).to_sql
|
233
345
|
# => SELECT * FROM table GROUP BY time(10m), host fill(0)
|
234
346
|
```
|
235
347
|
|
236
|
-
Chaining this method will add expression to the grouping set.
|
348
|
+
Chaining this method will add expression to the grouping set. If you want to override expressions when use `group!` method.
|
237
349
|
|
238
350
|
```ruby
|
239
|
-
|
351
|
+
builder.group{ time(10.m) }.group(:host).to_sql
|
240
352
|
# => SELECT * FROM table GROUP BY time(10m), host
|
353
|
+
builder.group{ time(10.m) }.group!(:host).to_sql
|
354
|
+
# => SELECT * FROM table GROUP BY host
|
241
355
|
```
|
242
356
|
|
243
357
|
### Ordering of results
|
@@ -252,83 +366,63 @@ Possible values:
|
|
252
366
|
* `'desc'`- Results will be sorted by descending order.
|
253
367
|
|
254
368
|
```ruby
|
255
|
-
|
256
|
-
|
257
|
-
|
369
|
+
builder = Influxdb::Arel::Builder.new(:table)
|
370
|
+
builder.order(:desc).to_sql
|
371
|
+
builder.order('desc').to_sql
|
258
372
|
# => SELECT * FROM table ORDER DESC
|
259
373
|
|
260
|
-
|
261
|
-
|
374
|
+
builder.order(:asc).to_sql
|
375
|
+
builder.order('asc').to_sql
|
262
376
|
# => SELECT * FROM table ORDER ASC
|
263
377
|
```
|
264
378
|
|
265
379
|
As well it's possible to use `asc` and `desc` methods
|
266
380
|
|
267
381
|
```ruby
|
268
|
-
|
269
|
-
# => SELECT * FROM table
|
270
|
-
|
271
|
-
# => SELECT * FROM table
|
382
|
+
builder.asc.to_sql
|
383
|
+
# => SELECT * FROM table ORDER ASC
|
384
|
+
builder.desc.to_sql
|
385
|
+
# => SELECT * FROM table ORDER DESC
|
386
|
+
```
|
387
|
+
|
388
|
+
Inverting of the order direction:
|
389
|
+
|
390
|
+
```ruby
|
391
|
+
builder.asc.invert_order.to_sql
|
392
|
+
# => SELECT * FROM table ORDER DESC
|
393
|
+
builder.desc.invert_order.to_sql
|
394
|
+
# => SELECT * FROM table ORDER ASC
|
272
395
|
```
|
273
396
|
|
274
397
|
Chaining this methods will replace previous order definition.
|
275
398
|
|
276
399
|
```ruby
|
277
|
-
|
278
|
-
# => SELECT * FROM table
|
400
|
+
builder.asc.desc.to_sql
|
401
|
+
# => SELECT * FROM table ORDER DESC
|
279
402
|
```
|
280
403
|
|
281
404
|
### Query conditions
|
282
405
|
|
283
|
-
You can specify conditions for
|
406
|
+
You can specify conditions for query using `where` method
|
284
407
|
|
285
408
|
```ruby
|
286
|
-
|
287
|
-
|
409
|
+
builder = Influxdb::Arel::Builder.new(:table)
|
410
|
+
builder.where(name: 'Undr').to_sql
|
288
411
|
# => SELECT * FROM table WHERE name = 'Undr'
|
289
412
|
```
|
290
413
|
|
291
414
|
```ruby
|
292
|
-
|
415
|
+
builder.where{ name.eq('Undr').or(name.eq('Andrei')) }.to_sql
|
293
416
|
# => SELECT * FROM table WHERE name = 'Undr' OR name = 'Andrei'
|
294
417
|
```
|
295
418
|
|
296
|
-
Chaining this method will add expression to the condition set. They will join using `AND` boolean expression.
|
419
|
+
Chaining this method will add expression to the condition set. They will join using `AND` boolean expression. If you want to override conditions when use `where!` method.
|
297
420
|
|
298
421
|
```ruby
|
299
|
-
|
422
|
+
builder.where(name: 'Undr').where{ time.lt(10.h.ago) }.to_sql
|
300
423
|
# => SELECT * FROM table WHERE name = 'Undr' AND time < (now() - 10h)
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
### SELECT clause
|
305
|
-
|
306
|
-
You can specify columns or expressions for SELECT clause using `column` method. By default, it's `*`.
|
307
|
-
|
308
|
-
```ruby
|
309
|
-
table = Influxdb::Arel::Table.new('cpu_load')
|
310
|
-
table.to_sql
|
311
|
-
# => SELECT * FROM cpu_load
|
312
|
-
|
313
|
-
table.column((table[:system] + table[:user]).as(:sum)).to_sql
|
314
|
-
# => SELECT (system + user) AS sum FROM cpu_load
|
315
|
-
|
316
|
-
table.column(
|
317
|
-
table[:idle].mean.as(:idle_mean),
|
318
|
-
table[:user].mean.as(:user_mean)
|
319
|
-
).group(1.d.time).to_sql
|
320
|
-
# => SELECT MEAN(idle) AS idle_mean, MEAN(user) AS user_mean FROM cpu_load GROUP BY time(1d)
|
321
|
-
```
|
322
|
-
|
323
|
-
It might be convenient to use aliases for complex expressions, such as functions or some mathematical expressions. Also the aliasing needed when joining tables. For example:
|
324
|
-
|
325
|
-
```ruby
|
326
|
-
alias1 = table.as('table1')
|
327
|
-
alias2 = table.as('table2')
|
328
|
-
table.from(alias1, alias2).
|
329
|
-
column(alias1[:idle], alias2[:idle]).
|
330
|
-
join.to_sql
|
331
|
-
# => SELECT table1.idle, table2.idle FROM table AS table1 INNER JOIN table AS table2
|
424
|
+
builder.where(name: 'Undr').where!{ time.lt(10.h.ago) }.to_sql
|
425
|
+
# => SELECT * FROM table WHERE time < (now() - 10h)
|
332
426
|
```
|
333
427
|
|
334
428
|
### Limits
|
@@ -336,8 +430,8 @@ table.from(alias1, alias2).
|
|
336
430
|
You can set a limit for a result set
|
337
431
|
|
338
432
|
```ruby
|
339
|
-
|
340
|
-
|
433
|
+
builder = Influxdb::Arel::Builder.new(:cpu_load)
|
434
|
+
builder.limit(100).to_sql
|
341
435
|
# => SELECT * FROM cpu_load LIMIT 100
|
342
436
|
```
|
343
437
|
|