Class: ClickHouse::Ast::Statement

Inherits:
Object
  • Object
show all
Defined in:
lib/click_house/ast/statement.rb

Constant Summary collapse

PLACEHOLDER_S =
'%s'
PLACEHOLDER_D =
'%d'
DIGIT_RE =
/\A\d+\Z/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: '') ⇒ Statement

Returns a new instance of Statement.



15
16
17
18
# File 'lib/click_house/ast/statement.rb', line 15

def initialize(name: '')
  @buffer = ''
  @name = name
end

Instance Attribute Details

#casterObject

Returns the value of attribute caster.



13
14
15
# File 'lib/click_house/ast/statement.rb', line 13

def caster
  @caster
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/click_house/ast/statement.rb', line 12

def name
  @name
end

Instance Method Details

#add_argument(st) ⇒ Object

Parameters:



36
37
38
# File 'lib/click_house/ast/statement.rb', line 36

def add_argument(st)
  arguments.push(st)
end

#argument!Object



30
31
32
33
# File 'lib/click_house/ast/statement.rb', line 30

def argument!
  add_argument(Statement.new(name: @buffer))
  @buffer = ''
end

#argument_first!Object



68
69
70
71
# File 'lib/click_house/ast/statement.rb', line 68

def argument_first!
  # TODO: raise an error if multiple arguments
  @argument_first ||= arguments.first
end

#argument_valuesArray

cached argument values to increase the casting perfomance

Returns:

  • (Array)


64
65
66
# File 'lib/click_house/ast/statement.rb', line 64

def argument_values
  @argument_values ||= arguments.map(&:value)
end

#argumentsArray<Statement>

Returns:



58
59
60
# File 'lib/click_house/ast/statement.rb', line 58

def arguments
  @arguments ||= []
end

#buffer?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/click_house/ast/statement.rb', line 53

def buffer?
  !@buffer.empty?
end

#digit?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/click_house/ast/statement.rb', line 79

def digit?
  name.match?(DIGIT_RE)
end

#merge(other) ⇒ Object

Parameters:



41
42
43
44
45
46
47
# File 'lib/click_house/ast/statement.rb', line 41

def merge(other)
  if other.named?
    add_argument(other)
  else
    @arguments = arguments.concat(other.arguments)
  end
end

#name!Object



25
26
27
28
# File 'lib/click_house/ast/statement.rb', line 25

def name!
  @name = @buffer
  @buffer = ''
end

#named?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/click_house/ast/statement.rb', line 49

def named?
  !@name.empty?
end

#placeholderObject



73
74
75
76
77
# File 'lib/click_house/ast/statement.rb', line 73

def placeholder
  return @placeholder if defined?(@placeholder)

  @placeholder = digit? ? PLACEHOLDER_D : PLACEHOLDER_S
end

Parameters:

  • value (String)


21
22
23
# File 'lib/click_house/ast/statement.rb', line 21

def print(value)
  @buffer = "#{@buffer}#{value}"
end

#to_sObject



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/click_house/ast/statement.rb', line 96

def to_s
  out = StringIO.new
  out.print(name.empty? ? 'NO_NAME' : name)
  out.print("<#{@buffer}>") unless @buffer.empty?

  if arguments.any?
    out.print("(#{arguments.join(',')})")
  end

  out.string
end

#valueObject



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/click_house/ast/statement.rb', line 83

def value
  @value ||=
    case placeholder
    when PLACEHOLDER_D
      Integer(name)
    when PLACEHOLDER_S
      # remove leading and trailing quotes
      name[1..-2]
    else
      raise "unknown value extractor for <#{placeholder}>"
    end
end