influxdb-arel 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +8 -0
  5. data/Gemfile +6 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +350 -0
  8. data/Rakefile +6 -0
  9. data/influxdb-arel.gemspec +24 -0
  10. data/lib/influxdb-arel.rb +1 -0
  11. data/lib/influxdb.rb +4 -0
  12. data/lib/influxdb/arel.rb +38 -0
  13. data/lib/influxdb/arel/alias_predication.rb +9 -0
  14. data/lib/influxdb/arel/attributes.rb +1 -0
  15. data/lib/influxdb/arel/attributes/attribute.rb +74 -0
  16. data/lib/influxdb/arel/core_extensions.rb +49 -0
  17. data/lib/influxdb/arel/expressions.rb +73 -0
  18. data/lib/influxdb/arel/math.rb +21 -0
  19. data/lib/influxdb/arel/nodes.rb +27 -0
  20. data/lib/influxdb/arel/nodes/and.rb +32 -0
  21. data/lib/influxdb/arel/nodes/binary.rb +47 -0
  22. data/lib/influxdb/arel/nodes/duration.rb +30 -0
  23. data/lib/influxdb/arel/nodes/equality.rb +11 -0
  24. data/lib/influxdb/arel/nodes/function.rb +47 -0
  25. data/lib/influxdb/arel/nodes/grouping.rb +11 -0
  26. data/lib/influxdb/arel/nodes/in.rb +8 -0
  27. data/lib/influxdb/arel/nodes/infix_operation.rb +51 -0
  28. data/lib/influxdb/arel/nodes/node.rb +19 -0
  29. data/lib/influxdb/arel/nodes/now.rb +15 -0
  30. data/lib/influxdb/arel/nodes/select_statement.rb +59 -0
  31. data/lib/influxdb/arel/nodes/sql_literal.rb +23 -0
  32. data/lib/influxdb/arel/nodes/table_alias.rb +23 -0
  33. data/lib/influxdb/arel/nodes/time.rb +13 -0
  34. data/lib/influxdb/arel/nodes/unary.rb +35 -0
  35. data/lib/influxdb/arel/predications.rb +137 -0
  36. data/lib/influxdb/arel/select_manager.rb +129 -0
  37. data/lib/influxdb/arel/table.rb +219 -0
  38. data/lib/influxdb/arel/tree_manager.rb +30 -0
  39. data/lib/influxdb/arel/version.rb +5 -0
  40. data/lib/influxdb/arel/visitor.rb +287 -0
  41. data/spec/lib/influxdb/arel/core_extensions_spec.rb +49 -0
  42. data/spec/lib/influxdb/arel/nodes/and_spec.rb +17 -0
  43. data/spec/lib/influxdb/arel/nodes/binary_spec.rb +49 -0
  44. data/spec/lib/influxdb/arel/nodes/duration_spec.rb +72 -0
  45. data/spec/lib/influxdb/arel/nodes/equality_spec.rb +5 -0
  46. data/spec/lib/influxdb/arel/nodes/function_spec.rb +69 -0
  47. data/spec/lib/influxdb/arel/nodes/grouping_spec.rb +10 -0
  48. data/spec/lib/influxdb/arel/nodes/in_spec.rb +13 -0
  49. data/spec/lib/influxdb/arel/nodes/now_spec.rb +8 -0
  50. data/spec/lib/influxdb/arel/nodes/sql_literal_spec.rb +28 -0
  51. data/spec/lib/influxdb/arel/nodes/table_alias_spec.rb +36 -0
  52. data/spec/lib/influxdb/arel/nodes/time_spec.rb +5 -0
  53. data/spec/lib/influxdb/arel/nodes/unary_spec.rb +25 -0
  54. data/spec/lib/influxdb/arel/select_manager_spec.rb +459 -0
  55. data/spec/lib/influxdb/arel/table_spec.rb +193 -0
  56. data/spec/lib/influxdb/arel_spec.rb +11 -0
  57. data/spec/spec_helper.rb +20 -0
  58. data/spec/support/examples/binary_node.rb +10 -0
  59. data/spec/support/examples/function_node.rb +14 -0
  60. data/spec/support/examples/node_as.rb +8 -0
  61. data/spec/support/examples/node_expressions.rb +145 -0
  62. data/spec/support/examples/node_math.rb +29 -0
  63. data/spec/support/examples/node_predications.rb +248 -0
  64. data/spec/support/examples/node_to_sql.rb +5 -0
  65. data/spec/support/examples/unary_node.rb +10 -0
  66. data/spec/support/fabrics.rb +21 -0
  67. metadata +177 -0
@@ -0,0 +1,11 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Nodes
4
+ class Grouping < Unary
5
+ include Predications
6
+ include AliasPredication
7
+ include Expressions
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Nodes
4
+ class In < Equality
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,51 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Nodes
4
+ class InfixOperation < Binary
5
+ include Expressions
6
+ include Predications
7
+ include AliasPredication
8
+ include Math
9
+
10
+ attr_reader :operator
11
+
12
+ def initialize(operator, left, right)
13
+ super(left, right)
14
+ @operator = operator
15
+ end
16
+ end
17
+
18
+ class Multiplication < InfixOperation
19
+ def initialize(left, right)
20
+ super(:*, left, right)
21
+ end
22
+
23
+ def as(name)
24
+ Grouping.new(self).as(name)
25
+ end
26
+ end
27
+
28
+ class Division < InfixOperation
29
+ def initialize(left, right)
30
+ super(:/, left, right)
31
+ end
32
+
33
+ def as(name)
34
+ Grouping.new(self).as(name)
35
+ end
36
+ end
37
+
38
+ class Addition < InfixOperation
39
+ def initialize(left, right)
40
+ super(:+, left, right)
41
+ end
42
+ end
43
+
44
+ class Subtraction < InfixOperation
45
+ def initialize(left, right)
46
+ super(:-, left, right)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,19 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Nodes
4
+ class Node
5
+ def or(right)
6
+ Nodes::Grouping.new(Nodes::Or.new(self, right))
7
+ end
8
+
9
+ def and(right)
10
+ Nodes::And.new([self, right])
11
+ end
12
+
13
+ def to_sql
14
+ Visitor.new.accept(self)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Nodes
4
+ class Now < Node
5
+ include Math
6
+
7
+ def eql?(other)
8
+ self.class == other.class
9
+ end
10
+
11
+ alias :== :eql?
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,59 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Nodes
4
+ class SelectStatement < Node
5
+ attr_accessor :limit, :order, :wheres, :groups, :columns, :series, :join, :merge, :fill, :into
6
+
7
+ def initialize
8
+ super
9
+ self.wheres = []
10
+ self.groups = []
11
+ self.columns = []
12
+ self.series = []
13
+ self.merge = nil
14
+ self.join = nil
15
+ self.order = nil
16
+ self.limit = nil
17
+ self.fill = nil
18
+ self.into = nil
19
+ end
20
+
21
+ def initialize_copy(other)
22
+ super
23
+ self.wheres = wheres.map{|where| where.clone }
24
+ self.groups = groups.map{|group| group.clone }
25
+ self.columns = columns.map{|column| column.clone }
26
+ self.series = series.map{|series| series.clone }
27
+ self.join = join.clone if join
28
+ self.merge = merge.clone if merge
29
+ self.order = order.clone if order
30
+ self.limit = limit.clone if limit
31
+ self.fill = fill.clone if fill
32
+ self.into = into.clone if into
33
+ end
34
+
35
+ def table
36
+ join || merge || series.map(&:unalias).uniq
37
+ end
38
+
39
+ def hash
40
+ [limit, order, wheres, groups, columns, table, fill, into].hash
41
+ end
42
+
43
+ def eql?(other)
44
+ self.class == other.class &&
45
+ columns == other.columns &&
46
+ wheres == other.wheres &&
47
+ groups == other.groups &&
48
+ table == other.table &&
49
+ order == other.order &&
50
+ limit == other.limit &&
51
+ fill == other.fill &&
52
+ into == other.into
53
+ end
54
+
55
+ alias :== :eql?
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,23 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Nodes
4
+ class SqlLiteral < String
5
+ include Expressions
6
+ include Predications
7
+ include AliasPredication
8
+
9
+ def name
10
+ self
11
+ end
12
+
13
+ def unalias
14
+ self
15
+ end
16
+
17
+ def eql?(other)
18
+ Table.comparable_classes.include?(other.class) && name == other.name
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Nodes
4
+ class TableAlias < Binary
5
+ alias :name :right
6
+ alias :relation :left
7
+ alias :table_alias :name
8
+
9
+ def [](name)
10
+ Attribute.new(self, name)
11
+ end
12
+
13
+ def table_name
14
+ relation.respond_to?(:name) ? relation.name : name
15
+ end
16
+
17
+ def unalias
18
+ relation
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Nodes
4
+ class Time < Unary
5
+ def initialize(expr)
6
+ expr = SqlLiteral.new(expr) if String === expr
7
+ expr = SqlLiteral.new(expr.to_s) if Symbol === expr
8
+ super(expr)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,35 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Nodes
4
+ class Unary < Node
5
+ attr_accessor :expr
6
+ alias :value :expr
7
+
8
+ def initialize(expr)
9
+ super()
10
+ self.expr = expr
11
+ end
12
+
13
+ def hash
14
+ self.expr.hash
15
+ end
16
+
17
+ def eql?(other)
18
+ self.class == other.class && expr == other.expr
19
+ end
20
+
21
+ alias :== :eql?
22
+ end
23
+
24
+ %w{
25
+ Group
26
+ Limit
27
+ Fill
28
+ Ordering
29
+ Into
30
+ }.each do |name|
31
+ const_set(name, Class.new(Unary))
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,137 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Predications
4
+ def not_eq(other)
5
+ Nodes::NotEqual.new(self, other)
6
+ end
7
+
8
+ def not_eq_any(others)
9
+ grouping_any(:not_eq, others)
10
+ end
11
+
12
+ def not_eq_all(others)
13
+ grouping_all(:not_eq, others)
14
+ end
15
+
16
+ def eq(other)
17
+ Nodes::Equality.new(self, other)
18
+ end
19
+
20
+ def eq_any(others)
21
+ grouping_any(:eq, others)
22
+ end
23
+
24
+ def eq_all(others)
25
+ grouping_all(:eq, others)
26
+ end
27
+
28
+ def in(other)
29
+ case other
30
+ when Range
31
+ if other.begin == -Float::INFINITY && other.end == Float::INFINITY
32
+ Nodes::Equality.new(1, 1)
33
+ elsif other.end == Float::INFINITY
34
+ Nodes::GreaterThanOrEqual.new(self, other.begin)
35
+ elsif other.begin == -Float::INFINITY && other.exclude_end?
36
+ Nodes::LessThan.new(self, other.end)
37
+ elsif other.begin == -Float::INFINITY
38
+ Nodes::LessThanOrEqual.new(self, other.end)
39
+ elsif other.exclude_end?
40
+ left = Nodes::GreaterThanOrEqual.new(self, other.begin)
41
+ right = Nodes::LessThan.new(self, other.end)
42
+ Nodes::And.new([left, right])
43
+ else
44
+ left = Nodes::GreaterThanOrEqual.new(self, other.begin)
45
+ right = Nodes::LessThanOrEqual.new(self, other.end)
46
+ Nodes::And.new([left, right])
47
+ end
48
+ else
49
+ Nodes::In.new(self, other)
50
+ end
51
+ end
52
+
53
+ def matches(other)
54
+ Nodes::Matches.new(self, other)
55
+ end
56
+
57
+ def matches_any(others)
58
+ grouping_any(:matches, others)
59
+ end
60
+
61
+ def matches_all(others)
62
+ grouping_all(:matches, others)
63
+ end
64
+
65
+ def does_not_match(other)
66
+ Nodes::DoesNotMatch.new(self, other)
67
+ end
68
+
69
+ def does_not_match_any(others)
70
+ grouping_any(:does_not_match, others)
71
+ end
72
+
73
+ def does_not_match_all(others)
74
+ grouping_all(:does_not_match, others)
75
+ end
76
+
77
+ def gteq(right)
78
+ Nodes::GreaterThanOrEqual.new(self, right)
79
+ end
80
+
81
+ def gteq_any(others)
82
+ grouping_any(:gteq, others)
83
+ end
84
+
85
+ def gteq_all(others)
86
+ grouping_all(:gteq, others)
87
+ end
88
+
89
+ def gt(right)
90
+ Nodes::GreaterThan.new(self, right)
91
+ end
92
+
93
+ def gt_any(others)
94
+ grouping_any(:gt, others)
95
+ end
96
+
97
+ def gt_all(others)
98
+ grouping_all(:gt, others)
99
+ end
100
+
101
+ def lt(right)
102
+ Nodes::LessThan.new(self, right)
103
+ end
104
+
105
+ def lt_any(others)
106
+ grouping_any(:lt, others)
107
+ end
108
+
109
+ def lt_all(others)
110
+ grouping_all(:lt, others)
111
+ end
112
+
113
+ def lteq(right)
114
+ Nodes::LessThanOrEqual.new(self, right)
115
+ end
116
+
117
+ def lteq_any(others)
118
+ grouping_any(:lteq, others)
119
+ end
120
+
121
+ def lteq_all(others)
122
+ grouping_all(:lteq, others)
123
+ end
124
+
125
+ private
126
+
127
+ def grouping_any(method_id, others)
128
+ nodes = others.map{|expr| send(method_id, expr) }
129
+ Nodes::Grouping.new(nodes.inject{|result, node| Nodes::Or.new(result, node) })
130
+ end
131
+
132
+ def grouping_all(method_id, others)
133
+ Nodes::Grouping.new(Nodes::And.new(others.map{|expr| send(method_id, expr) }))
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,129 @@
1
+ module Influxdb
2
+ module Arel
3
+ class SelectManager < Arel::TreeManager
4
+ def initialize(*tables)
5
+ super()
6
+ @ast = Nodes::SelectStatement.new
7
+ from(*tables)
8
+ end
9
+
10
+ def initialize_copy(other)
11
+ super
12
+ end
13
+
14
+ def limit
15
+ ast.limit
16
+ end
17
+
18
+ alias :taken :limit
19
+
20
+ def wheres
21
+ ast.wheres
22
+ end
23
+
24
+ def group(*columns)
25
+ columns.each do |column|
26
+ column = STRING_OR_SYMBOL_CLASS.include?(column.class) ? Arel.sql(column.to_s) : column
27
+ ast.groups.push(Nodes::Group.new(column))
28
+ end
29
+
30
+ self
31
+ end
32
+
33
+ def fill(value)
34
+ ast.fill = Nodes::Fill.new(value)
35
+ self
36
+ end
37
+
38
+ def from(*series)
39
+ series = series.map do |table|
40
+ case table
41
+ when String, Symbol
42
+ Arel.sql(table.to_s)
43
+ when Regexp
44
+ Arel.sql(table.inspect)
45
+ else
46
+ table
47
+ end
48
+ end.compact
49
+
50
+ ast.series = series unless series.empty?
51
+ self
52
+ end
53
+
54
+ def join(table = nil)
55
+ if table && !series.empty?
56
+ table = STRING_OR_SYMBOL_CLASS.include?(table.class) ? Arel.sql(table.to_s) : table
57
+ ast.join = Nodes::Join.new(series[0], table)
58
+ elsif series.size > 1
59
+ ast.join = Nodes::Join.new(series[0], series[1])
60
+ end
61
+ self
62
+ end
63
+
64
+ def merge(table = nil)
65
+ if table && !series.empty?
66
+ table = STRING_OR_SYMBOL_CLASS.include?(table.class) ? Arel.sql(table.to_s) : table
67
+ ast.merge = Nodes::Merge.new(series[0].unalias, table.unalias)
68
+ elsif series.size > 1
69
+ ast.merge = Nodes::Merge.new(series[0].unalias, series[1].unalias)
70
+ end
71
+ self
72
+ end
73
+
74
+ def column(*columns)
75
+ columns.each do |column|
76
+ column = STRING_OR_SYMBOL_CLASS.include?(column.class) ? Arel.sql(column.to_s) : column
77
+ ast.columns.push(column)
78
+ end
79
+
80
+ self
81
+ end
82
+
83
+ def columns
84
+ ast.columns
85
+ end
86
+
87
+ def columns=(columns)
88
+ self.column(*columns)
89
+ end
90
+
91
+ def order(expr)
92
+ expr = STRING_OR_SYMBOL_CLASS.include?(expr.class) ? Nodes::Ordering.new(expr.to_s) : expr
93
+ ast.order = expr
94
+ self
95
+ end
96
+
97
+ def desc
98
+ ast.order = Nodes::Ordering.new('desc')
99
+ self
100
+ end
101
+
102
+ def asc
103
+ ast.order = Nodes::Ordering.new('asc')
104
+ self
105
+ end
106
+
107
+ def ordering
108
+ ast.order
109
+ end
110
+
111
+ def take(limit)
112
+ ast.limit = limit ? Nodes::Limit.new(limit) : nil
113
+ self
114
+ end
115
+
116
+ alias :limit= :take
117
+
118
+ def into(table)
119
+ table = STRING_OR_SYMBOL_CLASS.include?(table.class) ? Arel.sql(table.to_s) : table
120
+ ast.into = Nodes::Into.new(table)
121
+ self
122
+ end
123
+
124
+ def series
125
+ ast.series
126
+ end
127
+ end
128
+ end
129
+ end