influxdb-arel 0.0.1 → 0.1.0

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.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +2 -0
  3. data/Gemfile +1 -0
  4. data/README.md +218 -124
  5. data/lib/influxdb/arel.rb +25 -16
  6. data/lib/influxdb/arel/builder.rb +209 -0
  7. data/lib/influxdb/arel/clauses.rb +6 -0
  8. data/lib/influxdb/arel/clauses/base.rb +34 -0
  9. data/lib/influxdb/arel/clauses/expressions.rb +75 -0
  10. data/lib/influxdb/arel/clauses/from_clause.rb +45 -0
  11. data/lib/influxdb/arel/clauses/group_clause.rb +34 -0
  12. data/lib/influxdb/arel/clauses/select_clause.rb +37 -0
  13. data/lib/influxdb/arel/clauses/where_clause.rb +102 -0
  14. data/lib/influxdb/arel/core_extensions.rb +5 -21
  15. data/lib/influxdb/arel/delete_manager.rb +50 -0
  16. data/lib/influxdb/arel/extensions.rb +6 -0
  17. data/lib/influxdb/arel/extensions/alias_predication.rb +11 -0
  18. data/lib/influxdb/arel/extensions/boolean_predications.rb +15 -0
  19. data/lib/influxdb/arel/extensions/expressions.rb +75 -0
  20. data/lib/influxdb/arel/extensions/joining_merging.rb +21 -0
  21. data/lib/influxdb/arel/extensions/math.rb +23 -0
  22. data/lib/influxdb/arel/extensions/predications.rb +144 -0
  23. data/lib/influxdb/arel/nodes.rb +7 -14
  24. data/lib/influxdb/arel/nodes/attribute.rb +20 -0
  25. data/lib/influxdb/arel/nodes/binary.rb +4 -3
  26. data/lib/influxdb/arel/nodes/delete_statement.rb +37 -0
  27. data/lib/influxdb/arel/nodes/duration.rb +3 -3
  28. data/lib/influxdb/arel/nodes/function.rb +2 -2
  29. data/lib/influxdb/arel/nodes/grouping.rb +3 -3
  30. data/lib/influxdb/arel/nodes/infix_operation.rb +11 -4
  31. data/lib/influxdb/arel/nodes/merge.rb +13 -0
  32. data/lib/influxdb/arel/nodes/node.rb +18 -6
  33. data/lib/influxdb/arel/nodes/now.rb +1 -1
  34. data/lib/influxdb/arel/nodes/ordering.rb +15 -0
  35. data/lib/influxdb/arel/nodes/select_statement.rb +10 -8
  36. data/lib/influxdb/arel/nodes/sql_literal.rb +5 -8
  37. data/lib/influxdb/arel/nodes/table.rb +19 -0
  38. data/lib/influxdb/arel/nodes/table_alias.rb +2 -4
  39. data/lib/influxdb/arel/nodes/unary.rb +6 -1
  40. data/lib/influxdb/arel/quoter.rb +85 -0
  41. data/lib/influxdb/arel/select_manager.rb +111 -64
  42. data/lib/influxdb/arel/tree_manager.rb +15 -5
  43. data/lib/influxdb/arel/version.rb +1 -1
  44. data/lib/influxdb/arel/visitor.rb +96 -126
  45. data/lib/influxdb/arel/visitor/delete_statement.rb +32 -0
  46. data/lib/influxdb/arel/visitor/select_statement.rb +59 -0
  47. data/lib/influxdb/arel/visitor/where_statement.rb +14 -0
  48. data/spec/lib/influxdb/arel/builder_spec.rb +173 -0
  49. data/spec/lib/influxdb/arel/core_extensions_spec.rb +0 -21
  50. data/spec/lib/influxdb/arel/nodes/and_spec.rb +6 -9
  51. data/spec/lib/influxdb/arel/nodes/attribute_spec.rb +21 -0
  52. data/spec/lib/influxdb/arel/nodes/binary_spec.rb +0 -4
  53. data/spec/lib/influxdb/arel/nodes/duration_spec.rb +1 -0
  54. data/spec/lib/influxdb/arel/nodes/in_spec.rb +1 -0
  55. data/spec/lib/influxdb/arel/nodes/infix_operation_spec.rb +17 -0
  56. data/spec/lib/influxdb/arel/nodes/merge_spec.rb +25 -0
  57. data/spec/lib/influxdb/arel/nodes/now_spec.rb +1 -0
  58. data/spec/lib/influxdb/arel/nodes/ordering_spec.rb +19 -0
  59. data/spec/lib/influxdb/arel/nodes/sql_literal_spec.rb +1 -5
  60. data/spec/lib/influxdb/arel/nodes/table_alias_spec.rb +3 -10
  61. data/spec/lib/influxdb/arel/nodes/table_spec.rb +8 -0
  62. data/spec/lib/influxdb/arel/nodes/unary_spec.rb +0 -4
  63. data/spec/lib/influxdb/arel/quoter/repository_spec.rb +10 -0
  64. data/spec/lib/influxdb/arel/quoter_spec.rb +33 -0
  65. data/spec/lib/influxdb/arel/select_manager_spec.rb +356 -156
  66. data/spec/lib/influxdb/arel_spec.rb +22 -0
  67. data/spec/spec_helper.rb +15 -0
  68. data/spec/support/examples/binary_node.rb +1 -0
  69. data/spec/support/examples/function_node.rb +1 -0
  70. data/spec/support/examples/infix_operation_node.rb +15 -0
  71. data/spec/support/examples/node_boolean_predications.rb +21 -0
  72. data/spec/support/examples/node_joining_merging.rb +53 -0
  73. data/spec/support/examples/unary_node.rb +1 -0
  74. data/spec/support/fabrics.rb +3 -3
  75. metadata +49 -11
  76. data/lib/influxdb/arel/alias_predication.rb +0 -9
  77. data/lib/influxdb/arel/attributes.rb +0 -1
  78. data/lib/influxdb/arel/attributes/attribute.rb +0 -74
  79. data/lib/influxdb/arel/expressions.rb +0 -73
  80. data/lib/influxdb/arel/math.rb +0 -21
  81. data/lib/influxdb/arel/predications.rb +0 -137
  82. data/lib/influxdb/arel/table.rb +0 -219
  83. 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: 93f531a5801d11ca235b597b784dbbaa4913c095
4
- data.tar.gz: 3dee249e2b4273d429e5c368c80e5d36ad093255
3
+ metadata.gz: ea2a6904d2c1edcc21371849fa2562c29e1d28c8
4
+ data.tar.gz: 8fba9de6c9530759f08830d244efb398f4cc4408
5
5
  SHA512:
6
- metadata.gz: 2166d603b4dfc1be19b9513298968f24b712c75459e775f518c6a013439e95849ce629fd3594aea6f943c7e1a82c22de8d48de8868893125f423acf656cf6af7
7
- data.tar.gz: 5f34262b49084e14b4ac57f380eb0c873f8ff9955dd653a7ebf6ed694812c88fa67f772cc59e953a4aab0c3f26653e202cbb879c0fb22463349f00e926083968
6
+ metadata.gz: 19647b7de298d43a84914ef4323ea715fcd173c0e3bae565b5b7c534148a1e33eef3c0911ee61898deb8ab5b518cc87378c2eb591059f81d580e8730709a6ddd
7
+ data.tar.gz: 4d1ed64baede232e081db834ca8e946e3c034658da4719666c2a70b23565f5152440f8b98c473bbefa0588cd5d821f078cf8f333e5a524454a10575e004cb663
@@ -1,4 +1,6 @@
1
1
  language: ruby
2
+ env:
3
+ - CODECLIMATE_REPO_TOKEN=daca1501f7334dfeb4429d55c57aadcf14754e813d1ba1e5965a1f087cf9ee55
2
4
  rvm:
3
5
  - 1.9.3
4
6
  - 2.0.0
data/Gemfile CHANGED
@@ -4,3 +4,4 @@ source 'https://rubygems.org'
4
4
  gemspec
5
5
 
6
6
  gem 'pry'
7
+ gem "codeclimate-test-reporter", group: :test, require: nil
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) [![Code Climate](https://codeclimate.com/github/undr/influxdb-arel/badges/gpa.svg)](https://codeclimate.com/github/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 table which you will work with:
34
+ At start you should create a builder:
32
35
 
33
36
  ```ruby
34
- events = Influxdb::Arel::Table.new(:events)
35
- events.to_sql
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 string and symbol as table name:
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::Table.new('events') == Influxdb::Arel::Table.new(:events)
45
+ Influxdb::Arel::Builder.new('events') == Influxdb::Arel::Builder.new(:events)
43
46
  # => true
44
47
  ```
45
48
 
46
- You will get the same result.
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
- 'time(1s)'.to_arel == Influxdb::Arel::Nodes::SqlLiteral.new('time(1s)')
72
- # => true
67
+ A builder has methods for SQL construction.
73
68
 
74
- 'time(1s)'.to_influxdb_arel == Influxdb::Arel::Nodes::SqlLiteral.new('time(1s)')
75
- # => true
69
+ * Specifying which attributes should be used in the query: `select`.
76
70
 
77
- :events.to_arel == Influxdb::Arel::Table.new('events')
78
- # => true
71
+ * Specifying which tables and how they should be used in the query: `from`, `merge` and `join`.
79
72
 
80
- :events.to_influxdb_arel == Influxdb::Arel::Table.new('events')
81
- # => true
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
- 'MEAN(value)'.as('user_events')
84
- # => #<Influxdb::Arel::Nodes::As:0x00000101218f70 @left="MEAN(value)", @right="user_events">
86
+ #### In `SELECT`, `WHERE`and `GROUP` contexts:
85
87
 
86
- 'MEAN(value)'.as('user_events').to_sql
87
- # => "MEAN(value) AS user_events"
88
+ - All undefined methods will be interpreted as attributes:
88
89
 
89
- :events.as('user_events')
90
- # => #<Influxdb::Arel::Nodes::TableAlias:0x0000010180f8c0 @left=#<Influxdb::Arel::Table:0x0000010180f938 @name="events">, @right="user_events">
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
- :events.as('user_events').to_sql
93
- # => "events AS user_events"
98
+ - Method `a` returns attribute node.
99
+
100
+ ```ruby
101
+ builder.where{ pp a(:name) == name }
102
+ # true
103
+ # => ...
94
104
  ```
95
105
 
96
- ### Setting of table names
106
+ - Method `time` returns `Influxdb::Arel::Nodes::Time` object. (It will be available only in `GROUP` context)
97
107
 
98
- There are several ways to set another table name to table object. You should call `from` method:
108
+ - Method `now` returns `Influxdb::Arel::Nodes::Now` object. (It will be available only in `WHERE` context)
99
109
 
100
- - With strings or symbols
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.from('events', :errors).to_sql
104
- # => SELECT * FROM events, errors
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
- - With table objects
123
+ - Method `t` returns table node.
108
124
 
109
125
  ```ruby
110
- events.from(Influxdb::Arel::Table.new(:errors)).to_sql
111
- # => SELECT * FROM errors
126
+ builder.from{ pp t(:table) == table }
127
+ # true
128
+ # => ...
112
129
  ```
113
130
 
114
- - With sql literal objects
131
+ - Method `join` used for joining tables (available only in `JOIN` context).
115
132
 
116
133
  ```ruby
117
- events.from(Influxdb::Arel::Nodes::SqlLiteral.new('errors')).to_sql
118
- # => SELECT * FROM errors
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
- - With table aliases
171
+ It might be convenient to use aliases for complex expressions, such as functions or some mathematical expressions.
122
172
 
123
- *There will be only a table name without alias in result SQL because aliases are used only when joining tables*
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
- events.from(events.as('user_errors')).to_sql
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
- events.from(/.*/).to_sql
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
- table = Influxdb::Arel::Table.new('table')
145
- table.from('table1', 'table2').join.to_sql
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 change nothing if method is called without argument and tables list contains less than two table.
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
- table.join.to_sql
153
- # => SELECT * FROM table
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
- table.join('table2').to_sql
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
- table.from('table1', 'table2').join('table3').to_sql
165
- # => SELECT * FROM table1 INNER JOIN table3
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
- table.from(table.as(:table_one)).join(table.as(:table_two)).to_sql
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 = Influxdb::Arel::Table.new('table')
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
- table = Influxdb::Arel::Table.new('table')
191
- table.from('table1', 'table2').merge.to_sql
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 change nothing if method is called without argument and tables list contains less than two table.
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
- table.merge.to_sql
199
- # => SELECT * FROM table
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
- table.merge('table2').to_sql
306
+ builder.merge(:table2).to_sql
206
307
  # => SELECT * FROM table MERGE table2
308
+ ```
207
309
 
208
- table.from('table1', 'table2').merge('table3').to_sql
209
- # => SELECT * FROM table1 MERGE table3
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
- table.megre('table1').merge('table2').to_sql
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 columns or expressions, such as `time(10m)`:
333
+ Grouping of results by specified attributes or expressions, such as `time(10m)`:
222
334
 
223
335
  ```ruby
224
- table = Influxdb::Arel::Table.new('table')
225
- table.group(table.time(10.m), table[:host]).to_sql
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
- table.group(10.m.time, table[:host]).fill(0).to_sql
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
- table.group(table.time(10.m)).group(:host).to_sql
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
- table = Influxdb::Arel::Table.new('table')
256
- table.order(:desc).to_sql
257
- table.order('desc').to_sql
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
- table.order(:asc).to_sql
261
- table.order('asc').to_sql
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
- table.where(table[:time].lteq(Time.now)).asc.to_sql
269
- # => SELECT * FROM table WHERE time <= 1412137303000000 ORDER ASC
270
- table.where(table[:time].lteq(Time.now)).desc.to_sql
271
- # => SELECT * FROM table WHERE time <= 1412137303000000 ORDER DESC
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
- table.where(table[:time].lteq(Time.now)).asc.desc.to_sql
278
- # => SELECT * FROM table WHERE time <= 1412137303000000 ORDER DESC
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 selection query using `where` method
406
+ You can specify conditions for query using `where` method
284
407
 
285
408
  ```ruby
286
- table = Influxdb::Arel::Table.new('table')
287
- table.where(table[:name].eq('Undr')).to_sql
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
- table.where(table[:name].eq('Undr').or(table[:name].eq('Andrei'))).to_sql
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
- table.where(table[:name].eq('Undr')).where(table[:time].lt(10.h.ago).to_sql
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
- table = Influxdb::Arel::Table.new('cpu_load')
340
- table.take(100).to_sql
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