Skip to content

Builder does not support to_sql #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 33 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# Influxdb::Arel
# InfluxDB::Arel

Influxdb::Arel is a SQL AST manager for Influxdb dialect. It simplifies the generation of complex SQL queries.
InfluxDB::Arel is a SQL AST manager for InfluxDB dialect. It simplifies the generation of complex SQL queries.

[![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)
[![Gem Version](https://badge.fury.io/rb/influxdb-arel.svg)](http://badge.fury.io/rb/influxdb-arel)
[![Test Coverage](https://codeclimate.com/github/undr/influxdb-arel/badges/coverage.svg)](https://codeclimate.com/github/undr/influxdb-arel)
[![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) [![Gem Version](https://badge.fury.io/rb/influxdb-arel.svg)](http://badge.fury.io/rb/influxdb-arel) [![Test Coverage](https://codeclimate.com/github/undr/influxdb-arel/badges/coverage.svg)](https://codeclimate.com/github/undr/influxdb-arel)

## Installation

Expand Down Expand Up @@ -34,25 +31,25 @@ $ gem install influxdb-arel
At start you should create a builder:

```ruby
builder = Influxdb::Arel::Builder.new(:events)
builder.to_sql
builder = InfluxDB::Arel::Builder.new(:events)
builder.select.to_sql
# => SELECT * FROM events
```

You can set default table name for the builder. Possible to use both strings and symbols:

```ruby
Influxdb::Arel::Builder.new('events') == Influxdb::Arel::Builder.new(:events)
InfluxDB::Arel::Builder.new('events') == InfluxDB::Arel::Builder.new(:events)
# => true
```

If you want to use convenient shortcuts, such as `10.h.ago` or `1.w` you should require a file with core extensions

```ruby
require 'influxdb/arel/core_extensions'
require 'influx_db/arel/core_extensions'

1.h
# => #<Influxdb::Arel::Nodes::Duration:0x00000102143a68 @left=1, @right="h">
# => #<InfluxDB::Arel::Nodes::Duration:0x00000102143a68 @left=1, @right="h">

1.h.to_sql
# => "1h"
Expand All @@ -66,29 +63,28 @@ require 'influxdb/arel/core_extensions'

A builder has methods for SQL construction.

* Specifying which attributes should be used in the query: `select`.
- Specifying which attributes should be used in the query: `select`.

* Specifying which tables and how they should be used in the query: `from`, `merge` and `join`.
- Specifying which tables and how they should be used in the query: `from`, `merge` and `join`.

* Conditions of query: `where`.
- Conditions of query: `where`.

* Grouping methods: `group` and `fill`.
- Grouping methods: `group` and `fill`.

* Ordering methods: `order`, `asc`, `desc` and `invert_order`.
- Ordering methods: `order`, `asc`, `desc` and `invert_order`.

* Specifying limitations of result set: `limit`.
- Specifying limitations of result set: `limit`.

* The part of continuous queries: `into`.
- The part of continuous queries: `into`.

Most of them accept a block for building part of SQL. Inside a block calling of method will be interpreted depending on current context.
For example:
Most of them accept a block for building part of SQL. Inside a block calling of method will be interpreted depending on current context. For example:

#### In `SELECT`, `WHERE`and `GROUP` contexts:

- All undefined methods will be interpreted as attributes:

```ruby
builder.where{ pp name.is_a?(Influxdb::Arel::Nodes::Attribute) }
builder.where{ pp name.is_a?(InfluxDB::Arel::Nodes::Attribute) }
# true
# => ...
builder.where{ name =~ /undr/ }.to_sql
Expand All @@ -103,17 +99,16 @@ builder.where{ pp a(:name) == name }
# => ...
```

- Method `time` returns `Influxdb::Arel::Nodes::Time` object. (It will be available only in `GROUP` context)

- Method `now` returns `Influxdb::Arel::Nodes::Now` object. (It will be available only in `WHERE` context)
- Method `time` returns `InfluxDB::Arel::Nodes::Time` object. (It will be available only in `GROUP` context)

- Method `now` returns `InfluxDB::Arel::Nodes::Now` object. (It will be available only in `WHERE` context)

#### In `FROM`, `JOIN` and `MERGE` contexts

- All undefined methods will be interpreted as tables:

```ruby
builder.select{ pp events.is_a?(Influxdb::Arel::Nodes::Table) }
builder.select{ pp events.is_a?(InfluxDB::Arel::Nodes::Table) }
# true
# => ...
builder.from{ events }.to_sql
Expand Down Expand Up @@ -155,7 +150,7 @@ builder.from{ o{ regexp } }.to_sql
You can specify attributes or expressions for `SELECT` clause using `select` method.

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

Expand Down Expand Up @@ -216,7 +211,7 @@ builder.from(/.*/).to_sql
# => SELECT * FROM /.*/
```

**Warning:** *You can call method with more then one regexp but only first will be used as table name*
**Warning:** _You can call method with more then one regexp but only first will be used as table name_

```ruby
builder.from(/.*/, /logs\..*/).to_sql
Expand All @@ -237,7 +232,7 @@ You can join two tables using `join` method.
It will join two first tables from tables list if method is called without argument

```ruby
builder = Influxdb::Arel::Builder.new(:table)
builder = InfluxDB::Arel::Builder.new(:table)
builder.from(:table1, :table2).join.to_sql
# => SELECT * FROM table1 INNER JOIN table2
builder.from{ [table1.as(:alias1), table2.as(:alias2)] }.join.to_sql
Expand Down Expand Up @@ -286,7 +281,7 @@ You can merge two tables using `merge` method.
It will merge two first tables from tables list if method is called without argument

```ruby
builder = Influxdb::Arel::Builder.new(:table)
builder = InfluxDB::Arel::Builder.new(:table)
builder.from(:table1, :table2).merge.to_sql
# => SELECT * FROM table1 MERGE table2
builder.from{ [table1.as(:alias1), table2.as(:alias2)] }.merge.to_sql
Expand Down Expand Up @@ -333,7 +328,7 @@ builder.merge(:table1).merge(:table2).to_sql
Grouping of results by specified attributes or expressions, such as `time(10m)`:

```ruby
builder = Influxdb::Arel::Builder.new(:table)
builder = InfluxDB::Arel::Builder.new(:table)
builder.group{ [time(10.m), host] }.to_sql
# => SELECT * FROM table GROUP BY time(10m), host
```
Expand All @@ -360,13 +355,13 @@ Yo can set the ordering of results using `order` method

Possible values:

* `:asc`- Default value. Results will be sorted by ascending order.
* `'asc'`- Results will be sorted by ascending order.
* `:desc`- Results will be sorted by descending order.
* `'desc'`- Results will be sorted by descending order.
- `:asc`- Default value. Results will be sorted by ascending order.
- `'asc'`- Results will be sorted by ascending order.
- `:desc`- Results will be sorted by descending order.
- `'desc'`- Results will be sorted by descending order.

```ruby
builder = Influxdb::Arel::Builder.new(:table)
builder = InfluxDB::Arel::Builder.new(:table)
builder.order(:desc).to_sql
builder.order('desc').to_sql
# => SELECT * FROM table ORDER DESC
Expand Down Expand Up @@ -406,7 +401,7 @@ builder.asc.desc.to_sql
You can specify conditions for query using `where` method

```ruby
builder = Influxdb::Arel::Builder.new(:table)
builder = InfluxDB::Arel::Builder.new(:table)
builder.where(name: 'Undr').to_sql
# => SELECT * FROM table WHERE name = 'Undr'
```
Expand All @@ -430,14 +425,14 @@ builder.where(name: 'Undr').where!{ time.lt(10.h.ago) }.to_sql
You can set a limit for a result set

```ruby
builder = Influxdb::Arel::Builder.new(:cpu_load)
builder = InfluxDB::Arel::Builder.new(:cpu_load)
builder.limit(100).to_sql
# => SELECT * FROM cpu_load LIMIT 100
```

## Contributing

1. Fork it ( https://github.com/undr/influxdb-arel/fork )
1. Fork it ( <https://github.com/undr/influxdb-arel/fork> )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
Expand Down
8 changes: 4 additions & 4 deletions influxdb-arel.gemspec
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'influxdb/arel/version'
require 'influx_db/arel/version'

Gem::Specification.new do |spec|
spec.name = "influxdb-arel"
spec.version = Influxdb::Arel::VERSION
spec.version = InfluxDB::Arel::VERSION
spec.authors = ["undr"]
spec.email = ["[email protected]"]
spec.summary = %q{Influxdb SQL AST manager.}
spec.description = %q{Influxdb::Arel is a SQL AST manager for Influxdb dialect.}
spec.summary = %q{InfluxDB SQL AST manager.}
spec.description = %q{InfluxDB::Arel is a SQL AST manager for InfluxDB dialect.}
spec.homepage = "https://github.com/undr/influxdb-arel"
spec.license = "MIT"

Expand Down
4 changes: 4 additions & 0 deletions lib/influx_db.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'influx_db/arel'

module InfluxDB
end
36 changes: 18 additions & 18 deletions lib/influxdb/arel.rb → lib/influx_db/arel.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
require 'date'
require 'time'
require "influxdb/arel/version"
require 'influxdb/arel/core_extensions'
require 'influxdb/arel/extensions'
require 'influxdb/arel/clauses'
require 'influxdb/arel/builder'

require 'influxdb/arel/visitor'
require 'influxdb/arel/visitor/where_statement'
require 'influxdb/arel/visitor/select_statement'
require 'influxdb/arel/visitor/delete_statement'

require 'influxdb/arel/tree_manager'
require 'influxdb/arel/select_manager'
require 'influxdb/arel/delete_manager'
require 'influxdb/arel/nodes'
require 'influxdb/arel/quoter'

module Influxdb
require "influx_db/arel/version"
require 'influx_db/arel/core_extensions'
require 'influx_db/arel/extensions'
require 'influx_db/arel/clauses'
require 'influx_db/arel/builder'

require 'influx_db/arel/visitor'
require 'influx_db/arel/visitor/where_statement'
require 'influx_db/arel/visitor/select_statement'
require 'influx_db/arel/visitor/delete_statement'

require 'influx_db/arel/tree_manager'
require 'influx_db/arel/select_manager'
require 'influx_db/arel/delete_manager'
require 'influx_db/arel/nodes'
require 'influx_db/arel/quoter'

module InfluxDB
module Arel
extend self

Expand Down
Loading