Class: Influxdb::Arel::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/influxdb/arel/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_table = nil) ⇒ Builder

Returns a new instance of Builder.



6
7
8
# File 'lib/influxdb/arel/builder.rb', line 6

def initialize(default_table = nil)
  @default_table = default_table
end

Instance Attribute Details

#default_tableObject

Returns the value of attribute default_table.



4
5
6
# File 'lib/influxdb/arel/builder.rb', line 4

def default_table
  @default_table
end

Instance Method Details

#ascObject

Results will be sorted by ascending order.

builder = Influxdb::Arel::Builder.new(:table)
builder.asc.to_sql
=> SELECT * FROM table ORDER ASC


129
130
131
# File 'lib/influxdb/arel/builder.rb', line 129

def asc
  from(default_table).asc
end

#descObject

Results will be sorted by descending order.

builder = Influxdb::Arel::Builder.new(:table)
builder.desc.to_sql
=> SELECT * FROM table ORDER DESC


139
140
141
# File 'lib/influxdb/arel/builder.rb', line 139

def desc
  from(default_table).desc
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


202
203
204
# File 'lib/influxdb/arel/builder.rb', line 202

def eql?(other)
  self.class == other.class && self.name == other.name
end

#from(*tables, &block) ⇒ Object

Specify tables for query

builder = Influxdb::Arel::Builder.new
builder.from(:table1).to_sql
=> SELECT * FROM table1

builder = Influxdb::Arel::Builder.new
builder.from{ table1.as(:alias1).join(table2.as(:alias2)) }.to_sql
=> SELECT * FROM table1 AS alias1 INNER JOIN table2 AS alias2

builder = Influxdb::Arel::Builder.new
builder.from(/.*/).to_sql
=> SELECT * FROM /.*/

See: Influxdb::Arel::SelectManager#from



26
27
28
# File 'lib/influxdb/arel/builder.rb', line 26

def from(*tables, &block)
  SelectManager.new(*tables, &block)
end

#group(*attributes, &block) ⇒ Object

Grouping results by specified attributes or expressions, such as time(10m)

builder = Influxdb::Arel::Builder.new(:table)
builder.group{ time(10.s), 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 grouping:

builder.group{ time(10.s), host }.fill(0).to_sql
=> SELECT * FROM table GROUP BY time(10m), host fill(0)

See: Influxdb::Arel::SelectManager#group



93
94
95
# File 'lib/influxdb/arel/builder.rb', line 93

def group(*attributes, &block)
  from(default_table).group(*attributes, &block)
end

#hashObject



198
199
200
# File 'lib/influxdb/arel/builder.rb', line 198

def hash
  @default_table.hash
end

#join(*tables) ⇒ Object

Joining of two tables into one.

It will join default table from builder with given table if tables contains one table.

builder = Influxdb::Arel::Builder.new(:table1)
builder.join(:table2).to_sql
=> SELECT * FROM table1 INNER JOIN table2

It will join tables if tables contains two tables.

builder = Influxdb::Arel::Builder.new
builder.join(:table1, :table2).to_sql
=> SELECT * FROM table1 INNER JOIN table2

It will raise exception if table is nil and tables list contains only one table.

builder.join.to_sql
=> IllegalSQLConstruct: Ambiguous merging clause

See: Influxdb::Arel::SelectManager#join



76
77
78
# File 'lib/influxdb/arel/builder.rb', line 76

def join(*tables)
  from(default_table).join(*tables)
end

#limit(amount) ⇒ Object

Set limit for result’s points Example:

builder = Influxdb::Arel::Builder.new(:table)
builder.limit(100).to_sql
=> SELECT * FROM table LIMIT 100


188
189
190
# File 'lib/influxdb/arel/builder.rb', line 188

def limit(amount)
  from(default_table).limit(amount)
end

#merge(*tables) ⇒ Object

Merging of two tables into one.

Tt will merge default table from builder with given table if tables contains one table.

builder = Influxdb::Arel::Builder.new(:table1)
builder.merge(:table2).to_sql
=> SELECT * FROM table1 MERGE table2

It will merge tables if tables contains two tables.

builder = Influxdb::Arel::Builder.new
builder.merge(:table1, :table2).to_sql
=> SELECT * FROM table1 MERGE table2

It will raise exception if table is nil and tables list contains only one table.

builder.merge.to_sql
=> IllegalSQLConstruct: Ambiguous merging clause

See: Influxdb::Arel::SelectManager#merge



51
52
53
# File 'lib/influxdb/arel/builder.rb', line 51

def merge(*tables)
  from(default_table).merge(*tables)
end

#order(expr) ⇒ Object

Set the ordering of results Possible values:

  • :asc Default value. Results will be sorted by ascending order.

  • :desc Default value. Results will be sorted by descending order.

Example:

builder = Influxdb::Arel::Builder.new(:table)
builder.order(:desc).to_sql
builder.order('desc').to_sql
=> SELECT * FROM table ORDER DESC

builder.order(:asc).to_sql
builder.order('asc').to_sql
=> SELECT * FROM table ORDER ASC

See: Influxdb::Arel::SelectManager#order



119
120
121
# File 'lib/influxdb/arel/builder.rb', line 119

def order(expr)
  from(default_table).order(expr)
end

#select(*attributes, &block) ⇒ Object

Specify attributes or expressions for select. Example:

builder = Influxdb::Arel::Builder.new(:cpu_load)
builder.to_sql
=> SELECT * FROM cpu_load

builder.select(:idle){ (system + user).as(:sum) }.to_sql
=> SELECT idle, (system + user) AS sum FROM cpu_load

builder.select{ [mean(idle).as(:idle_mean), mean(user).as(:user_mean)] }.to_sql
=> SELECT MEAN(idle) AS idle_mean, MEAN(user) AS user_mean FROM cpu_load

See: Influxdb::Arel::SelectManager#select



177
178
179
# File 'lib/influxdb/arel/builder.rb', line 177

def select(*attributes, &block)
  from(default_table).select(*attributes, &block)
end

#select_managerObject

Create Influxdb::Arel::SelectManager



194
195
196
# File 'lib/influxdb/arel/builder.rb', line 194

def select_manager
  SelectManager.new(default_table)
end

#where(conditions = nil, &block) ⇒ Object

Specify conditions for selection or deletion query Example:

builder = Influxdb::Arel::Builder.new(:table)
builder.where(name: 'Undr').to_sql
=> SELECT * FROM table WHERE name = 'Undr'

builder.where(name: 'Undr'){ time.lt(10.h.ago) }.to_sql
=> SELECT * FROM table WHERE name = 'Undr' AND time < (now() - 10h)

builder.where{ name.eq('Undr').or(name.eq('Andrei')) }.to_sql
=> SELECT * FROM table WHERE name = 'Undr' OR name = 'Andrei'

See: Influxdb::Arel::SelectManager#where



158
159
160
# File 'lib/influxdb/arel/builder.rb', line 158

def where(conditions = nil, &block)
  from(default_table).where(conditions, &block)
end