Class: Influxdb::Arel::Table
- Inherits:
-
Object
- Object
- Influxdb::Arel::Table
- Defined in:
- lib/influxdb/arel/table.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
(also: #table_name)
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](name) ⇒ Object
Get attribute.
-
#alias(name) ⇒ Object
(also: #as)
Specify alias for table Example:.
-
#column(*things) ⇒ Object
Specify columns or expressions for select.
- #eql?(other) ⇒ Boolean (also: #==)
- #from(*tables) ⇒ Object
-
#group(*columns) ⇒ Object
Grouping results by specified columns or expressions, such as
time(10m). - #hash ⇒ Object
-
#initialize(name) ⇒ Table
constructor
A new instance of Table.
-
#join(table = nil) ⇒ Object
Joining of two series.
-
#merge(table = nil) ⇒ Object
Merging of two series into one.
- #now ⇒ Object
-
#order(expr) ⇒ Object
Set the ordering of results Possible values:.
- #select_manager ⇒ Object
- #sql(raw_sql) ⇒ Object
- #star ⇒ Object
- #table_alias ⇒ Object
-
#take(amount) ⇒ Object
Set limit for result’s points Example:.
- #time(duration) ⇒ Object
- #unalias ⇒ Object
-
#where(condition) ⇒ Object
Specify conditions for selection or deletion query Example:.
Constructor Details
#initialize(name) ⇒ Table
Returns a new instance of Table.
14 15 16 |
# File 'lib/influxdb/arel/table.rb', line 14 def initialize(name) @name = name.to_s end |
Instance Attribute Details
#name ⇒ Object Also known as: table_name
Returns the value of attribute name.
10 11 12 |
# File 'lib/influxdb/arel/table.rb', line 10 def name @name end |
Class Method Details
Instance Method Details
#[](name) ⇒ Object
Get attribute
176 177 178 |
# File 'lib/influxdb/arel/table.rb', line 176 def [](name) Attribute.new(self, name) end |
#alias(name) ⇒ Object Also known as: as
Specify alias for table Example:
Influxdb::Arel::Table.new('table').as('alias_table').to_sql
=> table AS alias_table
24 25 26 |
# File 'lib/influxdb/arel/table.rb', line 24 def alias(name) Nodes::TableAlias.new(self, name) end |
#column(*things) ⇒ Object
Specify columns or expressions for select. Example:
table = Influxdb::Arel::Table.new('cpu_load')
table.to_sql
=> SELECT * FROM cpu_load
table.column((table[:system] + table[:user]).as(:sum)).to_sql
=> SELECT (system + user) AS sum FROM cpu_load
table.column(table[:idle].mean.as(:idle_mean), table[:user].mean.as(:user_mean)).to_sql
=> SELECT MEAN(idle) AS idle_mean, MEAN(user) AS user_mean FROM cpu_load
159 160 161 |
# File 'lib/influxdb/arel/table.rb', line 159 def column(*things) from(self).column(*things) end |
#eql?(other) ⇒ Boolean Also known as: ==
188 189 190 |
# File 'lib/influxdb/arel/table.rb', line 188 def eql?(other) self.class.comparable_classes.include?(self.class) && self.name == other.name end |
#from(*tables) ⇒ Object
30 31 32 |
# File 'lib/influxdb/arel/table.rb', line 30 def from(*tables) SelectManager.new(*tables) end |
#group(*columns) ⇒ Object
Grouping results by specified columns or expressions, such as time(10m)
table = Influxdb::Arel::Table.new('table')
table.group(table.time(10.m), table[:host]).to_sql
=> SELECT * FROM table GROUP BY time(10m), host
If you want to fill intervals with no data you shoult call fill method after:
table.group(10.m.time, table[:host]).fill(0).to_sql
=> SELECT * FROM table GROUP BY time(10m), host fill(0)
101 102 103 |
# File 'lib/influxdb/arel/table.rb', line 101 def group(*columns) from(self).group(*columns) end |
#hash ⇒ Object
184 185 186 |
# File 'lib/influxdb/arel/table.rb', line 184 def hash @name.hash end |
#join(table = nil) ⇒ Object
Joining of two series.
If table is nil it will join two first tables from tables list.
table = Influxdb::Arel::Table.new('table')
table.from('table1', 'table2').join.to_sql
=> SELECT * FROM table1 INNER JOIN table2
If table is nil and tables list contains only one table it will change nothing.
table.join.to_sql
=> SELECT * FROM table
If table exists it will join first table from tables list with given table.
table.join('table2').to_sql
=> SELECT * FROM table INNER JOIN table2
table.from('table1', 'table2').join('table3').to_sql
=> SELECT * FROM table1 INNER JOIN table3
Aliases. You can define alias for each joined table. It would be usefull for self joining table.
table.from(table.as(:table_one)).join(table.as(:table_two)).to_sql
=> SELECT * FROM table AS table_one INNER JOIN table AS table_two
86 87 88 |
# File 'lib/influxdb/arel/table.rb', line 86 def join(table = nil) from(self).join(table) end |
#merge(table = nil) ⇒ Object
Merging of two series into one.
If table is nil it will merge two first tables from tables list.
table = Influxdb::Arel::Table.new('table')
table.from('table1', 'table2').merge.to_sql
=> SELECT * FROM table1 MERGE table2
If table is nil and tables list contains only one table it will change nothing.
table.merge.to_sql
=> SELECT * FROM table
If table exists it will merge first table from tables list with given table.
table.merge('table2').to_sql
=> SELECT * FROM table MERGE table2
table.from('table1', 'table2').merge('table3').to_sql
=> SELECT * FROM table1 MERGE table3
55 56 57 |
# File 'lib/influxdb/arel/table.rb', line 55 def merge(table = nil) from(self).merge(table) end |
#order(expr) ⇒ Object
Set the ordering of results Possible values:
-
:ascDefault value. Results will be sorted by ascending order. -
:descDefault value. Results will be sorted by descending order.
Example:
table = Influxdb::Arel::Table.new('table')
table.order(:desc).to_sql
table.order('desc').to_sql
=> SELECT * FROM table ORDER DESC
table.order(:asc).to_sql
table.order('asc').to_sql
=> SELECT * FROM table ORDER ASC
125 126 127 |
# File 'lib/influxdb/arel/table.rb', line 125 def order(expr) from(self).order(expr) end |
#select_manager ⇒ Object
180 181 182 |
# File 'lib/influxdb/arel/table.rb', line 180 def select_manager SelectManager.new end |
#sql(raw_sql) ⇒ Object
202 203 204 |
# File 'lib/influxdb/arel/table.rb', line 202 def sql(raw_sql) Arel.sql(raw_sql) end |
#table_alias ⇒ Object
194 195 196 |
# File 'lib/influxdb/arel/table.rb', line 194 def table_alias nil end |
#take(amount) ⇒ Object
Set limit for result’s points Example:
table = Influxdb::Arel::Table.new('cpu_load')
table.take(100).to_sql
=> SELECT * FROM table LIMIT 100
170 171 172 |
# File 'lib/influxdb/arel/table.rb', line 170 def take(amount) from(self).take(amount) end |
#time(duration) ⇒ Object
214 215 216 |
# File 'lib/influxdb/arel/table.rb', line 214 def time(duration) Arel.time(duration) end |
#unalias ⇒ Object
198 199 200 |
# File 'lib/influxdb/arel/table.rb', line 198 def unalias self end |
#where(condition) ⇒ Object
Specify conditions for selection or deletion query Example:
table = Influxdb::Arel::Table.new('table')
table.where(table[:name].eq('Undr')).to_sql
=> SELECT * FROM table WHERE name = 'Undr'
table.where(table[:name].eq('Undr')).where(table[:time].lt(10.h.ago).to_sql
=> SELECT * FROM table WHERE name = 'Undr' AND time < (now() - 10h)
table.where(table[:name].eq('Undr').or(table[:name].eq('Andrei'))).to_sql
=> SELECT * FROM table WHERE name = 'Undr' OR name = 'Andrei'
142 143 144 |
# File 'lib/influxdb/arel/table.rb', line 142 def where(condition) from(self).where(condition) end |