class Cequel::Schema::TableWriter

Creates a new table schema in the database

Attributes

table[R]

Public Class Methods

apply(keyspace, table) click to toggle source

Creates a new table schema in the database given an object representation of the schema to create

@param (see initialize) @return (see apply)

# File lib/cequel/schema/table_writer.rb, line 14
def self.apply(keyspace, table)
  new(table).apply(keyspace)
end
new(table) click to toggle source

@param keyspace [Keyspace] keyspace in which to create the table @param table [Table] object representation of table schema

# File lib/cequel/schema/table_writer.rb, line 22
def initialize(table)
  @table = table
end

Public Instance Methods

apply(keyspace) click to toggle source

Create the table in the keyspace

@return [void]

@api private

# File lib/cequel/schema/table_writer.rb, line 33
def apply(keyspace)
  statements.each { |statement| keyspace.execute(statement) }
end
statements() click to toggle source
# File lib/cequel/schema/table_writer.rb, line 37
def statements
  [create_statement] + index_statements
end

Private Instance Methods

columns_cql() click to toggle source
# File lib/cequel/schema/table_writer.rb, line 66
def columns_cql
  table.columns.map(&:to_cql).join(', ')
end
create_statement() click to toggle source
# File lib/cequel/schema/table_writer.rb, line 47
def create_statement
  "CREATE TABLE #{table.name} (#{columns_cql}, #{keys_cql})".tap do |cql|
    properties = properties_cql
    cql << " WITH #{properties}" if properties
  end
end
index_statements() click to toggle source
# File lib/cequel/schema/table_writer.rb, line 54
def index_statements
  [].tap do |statements|
    table.data_columns.each do |column|
      if column.indexed?
        statements <<
          "CREATE INDEX #{column.index_name} " \
          "ON #{table.name} (#{column.name})"
      end
    end
  end
end
key_columns_cql() click to toggle source
# File lib/cequel/schema/table_writer.rb, line 70
def key_columns_cql
  table.keys.map(&:to_cql).join(', ')
end
keys_cql() click to toggle source
# File lib/cequel/schema/table_writer.rb, line 74
def keys_cql
  partition_cql = table.partition_key_columns
    .map { |key| key.name }.join(', ')
  if table.clustering_columns.any?
    nonpartition_cql =
      table.clustering_columns.map { |key| key.name }.join(', ')
    "PRIMARY KEY ((#{partition_cql}), #{nonpartition_cql})"
  else
    "PRIMARY KEY ((#{partition_cql}))"
  end
end
properties_cql() click to toggle source
# File lib/cequel/schema/table_writer.rb, line 86
def properties_cql
  properties_fragments = table.properties
    .map { |_, property| property.to_cql }
  properties_fragments << 'COMPACT STORAGE' if table.compact_storage?
  if table.clustering_columns.any?
    clustering_fragment =
      table.clustering_columns.map(&:clustering_order_cql).join(',')
    properties_fragments <<
      "CLUSTERING ORDER BY (#{clustering_fragment})"
  end
  properties_fragments.join(' AND ') if properties_fragments.any?
end