Class: Clickhouse::Connection::Query::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/clickhouse/connection/query/table.rb

Instance Method Summary collapse

Constructor Details

#initialize(name) {|_self| ... } ⇒ Table

Returns a new instance of Table.

Yields:

  • (_self)

Yield Parameters:



6
7
8
9
10
# File 'lib/clickhouse/connection/query/table.rb', line 6

def initialize(name)
  @name = name
  @columns = []
  yield self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/clickhouse/connection/query/table.rb', line 35

def method_missing(name, *args)
  type = name.to_s
          .gsub(/(^.|_\w)/) {
            $1.upcase
          }
          .gsub("Uint", "UInt")
          .delete("_")

  type << "(#{args[1]})" if args[1]
  @columns << [args[0].to_s, type]
end

Instance Method Details

#engine(value) ⇒ Object



12
13
14
# File 'lib/clickhouse/connection/query/table.rb', line 12

def engine(value)
  @engine = value
end

#to_sqlObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/clickhouse/connection/query/table.rb', line 16

def to_sql
  raise Clickhouse::InvalidQueryError, "Missing table engine" unless @engine
  length = @columns.collect{|x| x[0].to_s.size}.max

  sql = []
  sql << "CREATE TABLE #{@name} ("

  @columns.each_with_index do |(name, type), index|
    sql << "  #{name.ljust(length, " ")} #{type}#{"," unless index == @columns.size - 1}"
  end

  sql << ")"
  sql << "ENGINE = #{@engine}"

  sql.join("\n")
end