class Cequel::Schema::TableDescDsl
Attributes
Public Class Methods
Initialize a new instance
table_name
- The name of the table being described.
# File lib/cequel/schema/table_desc_dsl.rb, line 31 def initialize(table_name) @table_name = table_name @columns = [] @properties = [] @is_compact_storage = false @is_view = false @has_part_key = false end
Public Instance Methods
Describe a column of the table
name - The name of the column. type - The type of the column. Either a `Cequel::Type` or a symbol.
See `Cequel::Type`.
options
:index - name of a secondary index to apply to the column, or `true` to infer an index name by convention
# File lib/cequel/schema/table_desc_dsl.rb, line 88 def column(name, type, options = {}) columns << DataColumn.new(name, type(type), figure_index_name(name, options.fetch(:index, nil))) end
Direct that this table use “compact storage”. This is primarily useful for backwards compatibility with legacy CQL2 table schemas.
@return [void]
# File lib/cequel/schema/table_desc_dsl.rb, line 143 def compact_storage @is_compact_storage = true end
Returns a Table
object built by evaluating the provided block.
Yields nothing but block is instance_evaled so it as access to
all the methods of the instance.
# File lib/cequel/schema/table_desc_dsl.rb, line 44 def eval(&desc_block) instance_eval(&desc_block) table end
Describe (one of) the key(s) of the table.
name - The name of the column type - The type of the column. Either a `Cequel::Type` or a symbol.
See `Cequel::Type`.
clustering_order - `:asc` or `:desc`. Only meaningful for cluster
keys. Leave nil for partition keys.
# File lib/cequel/schema/table_desc_dsl.rb, line 69 def key(name, type, clustering_order = nil) columns << if has_partition_key? ClusteringColumn.new(name, type(type), clustering_order) else (fail ArgumentError, "Can't set clustering order for partition key #{name}") if clustering_order PartitionKey.new(name, type(type)) end end
Describe a column of type list.
name - The name of the column. type - The type of the elements of this column. Either a
`Cequel::Type` or a symbol. See `Cequel::Type`.
# File lib/cequel/schema/table_desc_dsl.rb, line 99 def list(name, type) columns << List.new(name, type(type)) end
Describe a column of type map.
name - The name of the column. key_type - The type of the keys of this column. Either a
`Cequel::Type` or a symbol. See `Cequel::Type`.
value_type - The type of the values of this column. Either a
`Cequel::Type` or a symbol. See `Cequel::Type`.
# File lib/cequel/schema/table_desc_dsl.rb, line 120 def map(name, key_type, value_type) columns << Map.new(name, type(key_type), type(value_type)) end
Indicates that this is a materialized view.
@return [void]
# File lib/cequel/schema/table_desc_dsl.rb, line 151 def materialized_view self.is_view = true end
Describe (one of) the partition key(s) of the table.
name - The name of the column. type - The type of the column. Either a `Cequel::Type` or a symbol.
See `Cequel::Type`.
# File lib/cequel/schema/table_desc_dsl.rb, line 56 def partition_key(name, type) columns << PartitionKey.new(name, type(type)) end
Describe a column of type set.
name - The name of the column. type - The type of the members of this column. Either a
`Cequel::Type` or a symbol. See `Cequel::Type`.
# File lib/cequel/schema/table_desc_dsl.rb, line 109 def set(name, type) columns << Set.new(name, type(type)) end
# File lib/cequel/schema/table_desc_dsl.rb, line 155 def table Table.new(table_name, is_view).tap do |tab| columns.each do |c| tab.add_column c end properties.each do |p| tab.add_property p end tab.compact_storage = is_compact_storage end end
Describe property of the table.
name - name of property. value - value of property.
See `STORAGE_PROPERTIES` List
of storage property names See cassandra.apache.org/doc/cql3/CQL.html#createTableOptions
list of CQL3 table storage properties
# File lib/cequel/schema/table_desc_dsl.rb, line 133 def with(name, value) properties << TableProperty.build(name, value) end
Protected Instance Methods
# File lib/cequel/schema/table_desc_dsl.rb, line 183 def figure_index_name(column_name, idx_opt) case idx_opt when true :"#{table_name}_#{column_name}_idx" when false, nil nil else idx_opt end end
# File lib/cequel/schema/table_desc_dsl.rb, line 173 def has_partition_key? columns.any?{|c| c.partition_key? } end
# File lib/cequel/schema/table_desc_dsl.rb, line 177 def type(type) type = :int if type == :enum ::Cequel::Type[type] end