Class: OracleSqlParser::Ast::Base

Inherits:
Object
  • Object
show all
Includes:
Util::Parameterizable
Defined in:
lib/oracle-sql-parser/ast/base.rb

Direct Known Subclasses

Array, Hash, Variable

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Parameterizable

#to_parameterized, #to_parameternized

Constructor Details

#initialize(arg) ⇒ Base

Returns a new instance of Base.



59
60
61
62
63
64
# File 'lib/oracle-sql-parser/ast/base.rb', line 59

def initialize(arg)
  if arg.instance_of?(Array) || arg.instance_of?(Hash)
    raise "cant assign #{arg.class} Base.new()"
  end
  @ast = arg
end

Class Method Details

.[](value) ⇒ Object



131
132
133
# File 'lib/oracle-sql-parser/ast/base.rb', line 131

def self.[](value)
  self.new(value)
end

.deep_dup(original) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/oracle-sql-parser/ast/base.rb', line 74

def self.deep_dup(original)
  if original.is_a? OracleSqlParser::Ast::Base
    original.deep_dup
  elsif original.is_a? ::Hash
    ::Hash[ original.map {|k, v| [k, deep_dup(v)]} ]
  elsif original.is_a? ::Array
    original.map {|v| deep_dup(v)}
  elsif original.duplicable?
    original.dup
  else
    original
  end
end

.find_different_value(left, right, &block) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/oracle-sql-parser/ast/base.rb', line 135

def self.find_different_value(left, right, &block)
  if left.class != right.class
    block.call(left, right) if block_given?
    return true
  end

  result = false
  case left
  when Base
    result ||= self.find_different_value(
                  left.instance_variable_get(:@ast),
                  right.instance_variable_get(:@ast),
                  &block)
  when Hash
    (left.keys + right.keys).uniq.each do |key|
      result ||= self.find_different_value(left[key], right[key], &block)
    end
  when OracleSqlParser::Ast::Array
    if left.size == right.size
      left.each_with_index do |value, index|
        result ||= self.find_different_value(value, right[index], &block)
      end
    else
      block.call(left, right) if block_given?
      result = true
    end
  else
    if left != right
      block.call(left, right) if block_given?
      result = true
    end
  end
  result
end

Instance Method Details

#==(value) ⇒ Object



170
171
172
# File 'lib/oracle-sql-parser/ast/base.rb', line 170

def ==(value)
  self.class.find_different_value(self, value) != true
end

#astObject



119
120
121
# File 'lib/oracle-sql-parser/ast/base.rb', line 119

def ast
  raise "do not call ast method"
end

#deep_dupObject



88
89
90
91
92
93
94
# File 'lib/oracle-sql-parser/ast/base.rb', line 88

def deep_dup
  copy = self.class.new
  original_ast = self.instance_variable_get(:@ast)
  copy_ast = self.class.deep_dup(original_ast)
  copy.instance_variable_set(:@ast, copy_ast)
  copy
end

#initialize_copy(original) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/oracle-sql-parser/ast/base.rb', line 66

def initialize_copy(original)
  if @ast.nil?
    self.class.new
  else
    self.class.new(original.instance_variable_get(:@ast).dup)
  end
end

#inspectObject Also known as: to_s



114
115
116
# File 'lib/oracle-sql-parser/ast/base.rb', line 114

def inspect
  "#<#{self.class.name} #{@ast.inspect}>"
end

#map_ast(&block) ⇒ Object



96
97
98
99
100
# File 'lib/oracle-sql-parser/ast/base.rb', line 96

def map_ast(&block)
  duplicated = self.deep_dup
  duplicated.map_ast!(&block)
  duplicated
end

#map_ast!(&block) ⇒ Object



102
103
104
105
106
107
# File 'lib/oracle-sql-parser/ast/base.rb', line 102

def map_ast!(&block)
  if @ast.is_a? OracleSqlParser::Ast::Base
    @ast.map_ast!(&block)
  end
  @ast = block.call(@ast)
end

#remove_nil_values!Object



109
110
111
112
# File 'lib/oracle-sql-parser/ast/base.rb', line 109

def remove_nil_values!
  @ast.remove_nil_values! if @ast.respond_to? :remove_nil_values!
  self
end

#to_sql(options = {}) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/oracle-sql-parser/ast/base.rb', line 123

def to_sql(options ={})
  if @ast.respond_to? :to_sql
    @ast.to_sql(options)
  else
    @ast.to_s
  end
end