influxdb-arel 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,27 +1,20 @@
1
1
  require 'influxdb/arel/nodes/node'
2
+ require 'influxdb/arel/nodes/unary'
3
+ require 'influxdb/arel/nodes/binary'
2
4
  require 'influxdb/arel/nodes/now'
3
5
  require 'influxdb/arel/nodes/select_statement'
4
- # require 'influxdb/arel/nodes/update_statement'
5
-
6
- # unary
7
- require 'influxdb/arel/nodes/unary'
6
+ require 'influxdb/arel/nodes/delete_statement'
7
+ require 'influxdb/arel/nodes/attribute'
8
+ require 'influxdb/arel/nodes/table'
8
9
  require 'influxdb/arel/nodes/grouping'
9
10
  require 'influxdb/arel/nodes/time'
10
-
11
-
12
- # binary
13
- require 'influxdb/arel/nodes/binary'
14
11
  require 'influxdb/arel/nodes/duration'
15
12
  require 'influxdb/arel/nodes/equality'
16
13
  require 'influxdb/arel/nodes/in'
17
- # require 'influxdb/arel/nodes/delete_statement'
18
14
  require 'influxdb/arel/nodes/table_alias'
19
15
  require 'influxdb/arel/nodes/infix_operation'
20
-
21
- # nary
22
16
  require 'influxdb/arel/nodes/and'
23
-
24
-
25
17
  require 'influxdb/arel/nodes/function'
26
- # require 'influxdb/arel/nodes/named_function'
27
18
  require 'influxdb/arel/nodes/sql_literal'
19
+ require 'influxdb/arel/nodes/ordering'
20
+ require 'influxdb/arel/nodes/merge'
@@ -0,0 +1,20 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Nodes
4
+ class Attribute < Unary
5
+ include Extensions::Expressions
6
+ include Extensions::Predications
7
+ include Extensions::AliasPredication
8
+ include Extensions::Math
9
+
10
+ def time?
11
+ value.to_s == 'time'
12
+ end
13
+
14
+ def sequence_number?
15
+ value.to_s == 'sequence_number'
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -6,14 +6,16 @@ module Influxdb
6
6
 
7
7
  def initialize(left, right)
8
8
  super()
9
+ left = left.to_s if Symbol === left
10
+ right = right.to_s if Symbol === right
9
11
  self.left = left
10
12
  self.right = right
11
13
  end
12
14
 
13
15
  def initialize_copy(other)
14
16
  super
15
- self.left = left.clone if left
16
- self.right = right.clone if right
17
+ self.left = left.safe_clone if left
18
+ self.right = right.safe_clone if right
17
19
  end
18
20
 
19
21
  def hash
@@ -36,7 +38,6 @@ module Influxdb
36
38
  LessThan
37
39
  LessThanOrEqual
38
40
  Matches
39
- Merge
40
41
  NotEqual
41
42
  Or
42
43
  }.each do |name|
@@ -0,0 +1,37 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Nodes
4
+ class DeleteStatement < Node
5
+ attr_accessor :wheres, :tables, :regexp
6
+
7
+ def initialize
8
+ super
9
+ self.wheres = []
10
+ self.tables = []
11
+ self.regexp = nil
12
+ end
13
+
14
+ def initialize_copy(other)
15
+ super
16
+ self.wheres = wheres.map{|where| where.clone }
17
+ self.tables = tables.map{|table| table.clone }
18
+ self.regexp = regexp.clone
19
+ end
20
+
21
+ def table
22
+ regexp || tables
23
+ end
24
+
25
+ def hash
26
+ [wheres, tables].hash
27
+ end
28
+
29
+ def eql?(other)
30
+ self.class == other.class && wheres == other.wheres && table == other.table
31
+ end
32
+
33
+ alias :== :eql?
34
+ end
35
+ end
36
+ end
37
+ end
@@ -14,15 +14,15 @@ module Influxdb
14
14
  end
15
15
 
16
16
  def time
17
- Arel.time(self)
17
+ Time.new(self)
18
18
  end
19
19
 
20
20
  def ago
21
- Arel.now - self
21
+ Now.new - self
22
22
  end
23
23
 
24
24
  def since
25
- Arel.now + self
25
+ Now.new + self
26
26
  end
27
27
  end
28
28
  end
@@ -2,8 +2,8 @@ module Influxdb
2
2
  module Arel
3
3
  module Nodes
4
4
  class Function < Node
5
- include AliasPredication
6
- include Math
5
+ include Extensions::AliasPredication
6
+ include Extensions::Math
7
7
 
8
8
  attr_accessor :expressions
9
9
 
@@ -2,9 +2,9 @@ module Influxdb
2
2
  module Arel
3
3
  module Nodes
4
4
  class Grouping < Unary
5
- include Predications
6
- include AliasPredication
7
- include Expressions
5
+ include Extensions::Predications
6
+ include Extensions::AliasPredication
7
+ include Extensions::Expressions
8
8
  end
9
9
  end
10
10
  end
@@ -2,10 +2,9 @@ module Influxdb
2
2
  module Arel
3
3
  module Nodes
4
4
  class InfixOperation < Binary
5
- include Expressions
6
- include Predications
7
- include AliasPredication
8
- include Math
5
+ include Extensions::Expressions
6
+ include Extensions::Predications
7
+ include Extensions::Math
9
8
 
10
9
  attr_reader :operator
11
10
 
@@ -39,12 +38,20 @@ module Influxdb
39
38
  def initialize(left, right)
40
39
  super(:+, left, right)
41
40
  end
41
+
42
+ def as(name)
43
+ Grouping.new(self).as(name)
44
+ end
42
45
  end
43
46
 
44
47
  class Subtraction < InfixOperation
45
48
  def initialize(left, right)
46
49
  super(:-, left, right)
47
50
  end
51
+
52
+ def as(name)
53
+ Grouping.new(self).as(name)
54
+ end
48
55
  end
49
56
  end
50
57
  end
@@ -0,0 +1,13 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Nodes
4
+ class Merge < Binary
5
+ def initialize(left, right)
6
+ left = left.unalias if left.respond_to?(:unalias)
7
+ right = right.unalias if right.respond_to?(:unalias)
8
+ super(left, right)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -2,17 +2,29 @@ module Influxdb
2
2
  module Arel
3
3
  module Nodes
4
4
  class Node
5
- def or(right)
6
- Nodes::Grouping.new(Nodes::Or.new(self, right))
7
- end
5
+ include Extensions::BooleanPredications
8
6
 
9
- def and(right)
10
- Nodes::And.new([self, right])
11
- end
7
+ ENTENSIONS = {
8
+ alias: :AliasPrediction,
9
+ table_alias: :TableAliasPrediction,
10
+ math: :Math,
11
+ expressions: :Expressions,
12
+ predications: :Predications
13
+ }
12
14
 
13
15
  def to_sql
14
16
  Visitor.new.accept(self)
15
17
  end
18
+
19
+ def extend(*extensions)
20
+ extensions.each do |extension|
21
+ if module_name = EXTENSIONS[extension]
22
+ singleton_class.send(:include, Influxdb::Arel::Extensions.const_get(module_name))
23
+ end
24
+ end
25
+
26
+ self
27
+ end
16
28
  end
17
29
  end
18
30
  end
@@ -2,7 +2,7 @@ module Influxdb
2
2
  module Arel
3
3
  module Nodes
4
4
  class Now < Node
5
- include Math
5
+ include Extensions::Math
6
6
 
7
7
  def eql?(other)
8
8
  self.class == other.class
@@ -0,0 +1,15 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Nodes
4
+ class Ordering < Unary
5
+ alias :direction :expr
6
+
7
+ REVERSALS = { asc: :desc, desc: :asc }.freeze
8
+
9
+ def invert
10
+ Ordering.new(REVERSALS[direction.to_sym])
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -2,14 +2,15 @@ module Influxdb
2
2
  module Arel
3
3
  module Nodes
4
4
  class SelectStatement < Node
5
- attr_accessor :limit, :order, :wheres, :groups, :columns, :series, :join, :merge, :fill, :into
5
+ attr_accessor :limit, :order, :wheres, :groups, :attributes, :tables, :join, :merge, :regexp, :fill, :into
6
6
 
7
7
  def initialize
8
8
  super
9
9
  self.wheres = []
10
10
  self.groups = []
11
- self.columns = []
12
- self.series = []
11
+ self.attributes = []
12
+ self.tables = []
13
+ self.regexp = nil
13
14
  self.merge = nil
14
15
  self.join = nil
15
16
  self.order = nil
@@ -22,10 +23,11 @@ module Influxdb
22
23
  super
23
24
  self.wheres = wheres.map{|where| where.clone }
24
25
  self.groups = groups.map{|group| group.clone }
25
- self.columns = columns.map{|column| column.clone }
26
- self.series = series.map{|series| series.clone }
26
+ self.attributes = attributes.map{|attribute| attribute.clone }
27
+ self.tables = tables.map{|table| table.clone }
27
28
  self.join = join.clone if join
28
29
  self.merge = merge.clone if merge
30
+ self.regexp = regexp.clone if regexp
29
31
  self.order = order.clone if order
30
32
  self.limit = limit.clone if limit
31
33
  self.fill = fill.clone if fill
@@ -33,16 +35,16 @@ module Influxdb
33
35
  end
34
36
 
35
37
  def table
36
- join || merge || series.map(&:unalias).uniq
38
+ join || merge || regexp || tables.map(&:unalias).uniq
37
39
  end
38
40
 
39
41
  def hash
40
- [limit, order, wheres, groups, columns, table, fill, into].hash
42
+ [limit, order, wheres, groups, attributes, table, fill, into].hash
41
43
  end
42
44
 
43
45
  def eql?(other)
44
46
  self.class == other.class &&
45
- columns == other.columns &&
47
+ attributes == other.attributes &&
46
48
  wheres == other.wheres &&
47
49
  groups == other.groups &&
48
50
  table == other.table &&
@@ -2,20 +2,17 @@ module Influxdb
2
2
  module Arel
3
3
  module Nodes
4
4
  class SqlLiteral < String
5
- include Expressions
6
- include Predications
7
- include AliasPredication
5
+ include Extensions::Expressions
6
+ include Extensions::Predications
7
+ include Extensions::AliasPredication
8
+ include Extensions::BooleanPredications
8
9
 
9
10
  def name
10
11
  self
11
12
  end
12
13
 
13
- def unalias
14
- self
15
- end
16
-
17
14
  def eql?(other)
18
- Table.comparable_classes.include?(other.class) && name == other.name
15
+ self.class == other.class && name == other.name
19
16
  end
20
17
  end
21
18
  end
@@ -0,0 +1,19 @@
1
+ module Influxdb
2
+ module Arel
3
+ module Nodes
4
+ class Table < Unary
5
+ include Extensions::JoiningMerging
6
+
7
+ alias :name :value
8
+
9
+ def as(name)
10
+ TableAlias.new(self, name)
11
+ end
12
+
13
+ def unalias
14
+ self
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -2,14 +2,12 @@ module Influxdb
2
2
  module Arel
3
3
  module Nodes
4
4
  class TableAlias < Binary
5
+ include Extensions::JoiningMerging
6
+
5
7
  alias :name :right
6
8
  alias :relation :left
7
9
  alias :table_alias :name
8
10
 
9
- def [](name)
10
- Attribute.new(self, name)
11
- end
12
-
13
11
  def table_name
14
12
  relation.respond_to?(:name) ? relation.name : name
15
13
  end
@@ -7,9 +7,15 @@ module Influxdb
7
7
 
8
8
  def initialize(expr)
9
9
  super()
10
+ expr = expr.to_s if Symbol === expr
10
11
  self.expr = expr
11
12
  end
12
13
 
14
+ def initialize_copy(other)
15
+ super
16
+ self.expr = expr.safe_clone if expr
17
+ end
18
+
13
19
  def hash
14
20
  self.expr.hash
15
21
  end
@@ -25,7 +31,6 @@ module Influxdb
25
31
  Group
26
32
  Limit
27
33
  Fill
28
- Ordering
29
34
  Into
30
35
  }.each do |name|
31
36
  const_set(name, Class.new(Unary))
@@ -0,0 +1,85 @@
1
+ require 'bigdecimal'
2
+
3
+ module Influxdb
4
+ module Arel
5
+ module Quoter
6
+ extend self
7
+
8
+ class Repository
9
+ def initialize
10
+ @types = {}
11
+ end
12
+
13
+ def add(type, &block)
14
+ @types[type] = block
15
+ end
16
+
17
+ def quote(value)
18
+ block = @types[value.class]
19
+ block ? block.call(value) : value.inspect
20
+ end
21
+ end
22
+
23
+ def repository
24
+ @repository ||= Repository.new
25
+ end
26
+
27
+ def repository=(value)
28
+ @repository = value
29
+ end
30
+
31
+ def quote(value)
32
+ repository.quote(value)
33
+ end
34
+
35
+ def add_type(type, &block)
36
+ repository.add(type, &block)
37
+ end
38
+ end
39
+
40
+ Quoter.add_type(String) do |value|
41
+ "'#{value}'"
42
+ end
43
+
44
+ Quoter.add_type(Time) do |value|
45
+ "'#{value.utc.strftime('%Y-%m-%d %H:%M:%S')}'"
46
+ end
47
+
48
+ Quoter.add_type(Date) do |value|
49
+ "'#{value.to_time.utc.strftime('%Y-%m-%d')}'"
50
+ end
51
+
52
+ Quoter.add_type(DateTime) do |value|
53
+ "'#{value.to_time.utc.strftime('%Y-%m-%d %H:%M:%S')}'"
54
+ end
55
+
56
+ Quoter.add_type(BigDecimal) do |value|
57
+ value.to_s('F')
58
+ end
59
+
60
+ Quoter.add_type(NilClass) do |value|
61
+ 'null'
62
+ end
63
+
64
+ Quoter.add_type(Hash) do |value|
65
+ value = value.to_json if value.respond_to?(:to_json)
66
+ value.to_s
67
+ end
68
+
69
+ Quoter.add_type(Nodes::SqlLiteral) do |value|
70
+ value
71
+ end
72
+
73
+ if defined?(ActiveSupport::Multibyte::Chars)
74
+ Quoter.add_type(ActiveSupport::Multibyte::Chars) do |value|
75
+ "'#{value}'"
76
+ end
77
+ end
78
+
79
+ if defined?(ActiveSupport::StringInquirer)
80
+ Quoter.add_type(ActiveSupport::StringInquirer) do |value|
81
+ "'#{value}'"
82
+ end
83
+ end
84
+ end
85
+ end