Sha256: ab457b174634c808ffbab0e5232c86349f1935aadbab0a4664ed06a67339e98e

Contents?: true

Size: 1.09 KB

Versions: 9

Compression:

Stored size: 1.09 KB

Contents

module Clickhouse
  class Connection
    module Query
      class Table

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

        def engine(value)
          @engine = value
        end

        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

      private

        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

      end
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
ch-client-0.0.1 lib/clickhouse/connection/query/table.rb
clickhouse-0.1.10 lib/clickhouse/connection/query/table.rb
clickhouse-0.1.9 lib/clickhouse/connection/query/table.rb
clickhouse-0.1.8 lib/clickhouse/connection/query/table.rb
clickhouse-0.1.4 lib/clickhouse/connection/query/table.rb
clickhouse-0.1.3 lib/clickhouse/connection/query/table.rb
clickhouse-0.1.2 lib/clickhouse/connection/query/table.rb
clickhouse-0.1.1 lib/clickhouse/connection/query/table.rb
clickhouse-0.1.0 lib/clickhouse/connection/query/table.rb