Class: Cequel::Schema::Table
- Inherits:
-
Object
- Object
- Cequel::Schema::Table
- Defined in:
- lib/cequel/schema/table.rb
Overview
An object representation of a CQL3 table schema.
Constant Summary collapse
- STORAGE_PROPERTIES =
%w( bloom_filter_fp_chance caching comment compaction compression dclocal_read_repair_chance gc_grace_seconds read_repair_chance replicate_on_write )
Instance Attribute Summary collapse
-
#clustering_columns ⇒ Array<ClusteringColumn>
readonly
Clustering columns defined on the table.
-
#columns ⇒ Array<Column>
readonly
All columns defined on the table.
-
#compact_storage ⇒ Boolean
writeonly
‘true` if this table is configured with compact storage.
-
#data_columns ⇒ Array<DataColumn,CollectionColumn>
readonly
Data columns and collection columns defined on the table.
-
#name ⇒ Symbol
readonly
The name of the table.
-
#partition_key_columns ⇒ Array<PartitionKey>
readonly
Partition key columns defined on the table.
-
#properties ⇒ Hash
readonly
Storage properties defined on the table.
Instance Method Summary collapse
-
#add_clustering_column(name, type, clustering_order = nil) ⇒ void
Define a clustering column for the table.
-
#add_data_column(name, type, options = {}) ⇒ void
Define a data column on the table.
-
#add_key(name, type, clustering_order = nil) ⇒ void
Define a key column.
-
#add_list(name, type) ⇒ void
Define a list column on the table.
-
#add_map(name, key_type, value_type) ⇒ void
Define a map column on the table.
-
#add_partition_key(name, type) ⇒ void
Define a partition key for the table.
-
#add_property(name, value) ⇒ void
Define a storage property for the table.
-
#add_set(name, type) ⇒ void
Define a set column on the table.
-
#clustering_column(name) ⇒ ClusteringColumn
Clustering column with given name.
-
#clustering_column_count ⇒ Integer
Number of clustering columns.
-
#clustering_column_names ⇒ Array<Symbol>
Names of clustering columns.
-
#column(name) ⇒ Column
Column defined on table with given name.
-
#column_names ⇒ Array<Symbol>
The names of all columns.
-
#compact_storage? ⇒ Boolean
‘true` if this table uses compact storage.
-
#data_column(name) ⇒ DataColumn, CollectionColumn
Data column or collection column with given name.
-
#initialize(name) ⇒ Table
constructor
private
A new instance of Table.
-
#key_column_count ⇒ Integer
Total number of key columns.
-
#key_column_names ⇒ Array<Symbol>
Names of all key columns (partition + clustering).
-
#key_columns ⇒ Array<Column>
All key columns (partition + clustering).
-
#partition_key(name) ⇒ PartitionKey
Partition key column with given name.
-
#partition_key_column_count ⇒ Integer
Number of partition key columns.
-
#partition_key_column_names ⇒ Array<Symbol>
Names of partition key columns.
-
#property(name) ⇒ TableProperty
Property as defined on table.
Constructor Details
#initialize(name) ⇒ Table
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Table.
41 42 43 44 45 46 |
# File 'lib/cequel/schema/table.rb', line 41 def initialize(name) @name = name @partition_key_columns, @clustering_columns, @data_columns = [], [], [] @columns, @columns_by_name = [], {} @properties = ActiveSupport::HashWithIndifferentAccess.new end |
Instance Attribute Details
#clustering_columns ⇒ Array<ClusteringColumn> (readonly)
Returns clustering columns defined on the table.
27 28 29 |
# File 'lib/cequel/schema/table.rb', line 27 def clustering_columns @clustering_columns end |
#columns ⇒ Array<Column> (readonly)
Returns all columns defined on the table.
21 22 23 |
# File 'lib/cequel/schema/table.rb', line 21 def columns @columns end |
#compact_storage=(value) ⇒ Boolean (writeonly)
Returns ‘true` if this table is configured with compact storage.
35 36 37 |
# File 'lib/cequel/schema/table.rb', line 35 def compact_storage=(value) @compact_storage = value end |
#data_columns ⇒ Array<DataColumn,CollectionColumn> (readonly)
Returns data columns and collection columns defined on the table.
30 31 32 |
# File 'lib/cequel/schema/table.rb', line 30 def data_columns @data_columns end |
#name ⇒ Symbol (readonly)
Returns the name of the table.
19 20 21 |
# File 'lib/cequel/schema/table.rb', line 19 def name @name end |
#partition_key_columns ⇒ Array<PartitionKey> (readonly)
Returns partition key columns defined on the table.
24 25 26 |
# File 'lib/cequel/schema/table.rb', line 24 def partition_key_columns @partition_key_columns end |
#properties ⇒ Hash (readonly)
Returns storage properties defined on the table.
32 33 34 |
# File 'lib/cequel/schema/table.rb', line 32 def properties @properties end |
Instance Method Details
#add_clustering_column(name, type, clustering_order = nil) ⇒ void
This method returns an undefined value.
Define a clustering column for the table
92 93 94 95 |
# File 'lib/cequel/schema/table.rb', line 92 def add_clustering_column(name, type, clustering_order = nil) ClusteringColumn.new(name, type(type), clustering_order) .tap { |column| @clustering_columns << add_column(column) } end |
#add_data_column(name, type, options = {}) ⇒ void
This method returns an undefined value.
Define a data column on the table
108 109 110 111 112 113 114 |
# File 'lib/cequel/schema/table.rb', line 108 def add_data_column(name, type, = {}) = {index: } unless .is_a?(Hash) index_name = [:index] index_name = :"#{@name}_#{name}_idx" if index_name == true DataColumn.new(name, type(type), index_name) .tap { |column| @data_columns << add_column(column) } end |
#add_key(name, type, clustering_order = nil) ⇒ void
This method returns an undefined value.
Define a key column. If this is the first key column defined, it will be a partition key; otherwise, it will be a clustering column.
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/cequel/schema/table.rb', line 61 def add_key(name, type, clustering_order = nil) if @partition_key_columns.empty? unless clustering_order.nil? fail ArgumentError, "Can't set clustering order for partition key #{name}" end add_partition_key(name, type) else add_clustering_column(name, type, clustering_order) end end |
#add_list(name, type) ⇒ void
This method returns an undefined value.
Define a list column on the table
125 126 127 128 129 |
# File 'lib/cequel/schema/table.rb', line 125 def add_list(name, type) List.new(name, type(type)).tap do |column| @data_columns << add_column(column) end end |
#add_map(name, key_type, value_type) ⇒ void
This method returns an undefined value.
Define a map column on the table
156 157 158 159 160 |
# File 'lib/cequel/schema/table.rb', line 156 def add_map(name, key_type, value_type) Map.new(name, type(key_type), type(value_type)).tap do |column| @data_columns << add_column(column) end end |
#add_partition_key(name, type) ⇒ void
This method returns an undefined value.
Define a partition key for the table
80 81 82 83 84 |
# File 'lib/cequel/schema/table.rb', line 80 def add_partition_key(name, type) PartitionKey.new(name, type(type)).tap do |column| @partition_key_columns << add_column(column) end end |
#add_property(name, value) ⇒ void
This method returns an undefined value.
Define a storage property for the table
173 174 175 176 177 |
# File 'lib/cequel/schema/table.rb', line 173 def add_property(name, value) TableProperty.build(name, value).tap do |property| @properties[name] = property end end |
#add_set(name, type) ⇒ void
This method returns an undefined value.
Define a set column on the table
140 141 142 143 144 |
# File 'lib/cequel/schema/table.rb', line 140 def add_set(name, type) Set.new(name, type(type)).tap do |column| @data_columns << add_column(column) end end |
#clustering_column(name) ⇒ ClusteringColumn
Returns clustering column with given name.
255 256 257 |
# File 'lib/cequel/schema/table.rb', line 255 def clustering_column(name) @clustering_columns.find { |column| column.name == name } end |
#clustering_column_count ⇒ Integer
Returns number of clustering columns.
239 240 241 |
# File 'lib/cequel/schema/table.rb', line 239 def clustering_column_count clustering_columns.length end |
#clustering_column_names ⇒ Array<Symbol>
Returns names of clustering columns.
232 233 234 |
# File 'lib/cequel/schema/table.rb', line 232 def clustering_column_names clustering_columns.map { |key| key.name } end |
#column(name) ⇒ Column
Returns column defined on table with given name.
183 184 185 |
# File 'lib/cequel/schema/table.rb', line 183 def column(name) columns_by_name[name.to_sym] end |
#column_names ⇒ Array<Symbol>
Returns the names of all columns.
189 190 191 |
# File 'lib/cequel/schema/table.rb', line 189 def column_names columns.map { |column| column.name } end |
#compact_storage? ⇒ Boolean
Returns ‘true` if this table uses compact storage.
280 281 282 |
# File 'lib/cequel/schema/table.rb', line 280 def compact_storage? !!@compact_storage end |
#data_column(name) ⇒ DataColumn, CollectionColumn
Returns data column or collection column with given name.
264 265 266 267 |
# File 'lib/cequel/schema/table.rb', line 264 def data_column(name) name = name.to_sym @data_columns.find { |column| column.name == name } end |
#key_column_count ⇒ Integer
Returns total number of key columns.
211 212 213 |
# File 'lib/cequel/schema/table.rb', line 211 def key_column_count key_columns.length end |
#key_column_names ⇒ Array<Symbol>
Returns names of all key columns (partition + clustering).
204 205 206 |
# File 'lib/cequel/schema/table.rb', line 204 def key_column_names key_columns.map { |key| key.name } end |
#key_columns ⇒ Array<Column>
Returns all key columns (partition + clustering).
196 197 198 |
# File 'lib/cequel/schema/table.rb', line 196 def key_columns @partition_key_columns + @clustering_columns end |
#partition_key(name) ⇒ PartitionKey
Returns partition key column with given name.
247 248 249 |
# File 'lib/cequel/schema/table.rb', line 247 def partition_key(name) @partition_key_columns.find { |column| column.name == name } end |
#partition_key_column_count ⇒ Integer
Returns number of partition key columns.
225 226 227 |
# File 'lib/cequel/schema/table.rb', line 225 def partition_key_column_count partition_key_columns.length end |
#partition_key_column_names ⇒ Array<Symbol>
Returns names of partition key columns.
218 219 220 |
# File 'lib/cequel/schema/table.rb', line 218 def partition_key_column_names partition_key_columns.map { |key| key.name } end |
#property(name) ⇒ TableProperty
Returns property as defined on table.
273 274 275 |
# File 'lib/cequel/schema/table.rb', line 273 def property(name) @properties[name].try(:value) end |