Class: Cequel::Schema::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/cequel/schema/table.rb

Overview

An object representation of a CQL3 table schema.

See Also:

Since:

  • 1.0.0

Constant Summary collapse

STORAGE_PROPERTIES =

Since:

  • 1.0.0

%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

Instance Method Summary collapse

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.

Parameters:

  • name (Symbol)

    the name of the table

Since:

  • 1.0.0



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_columnsArray<ClusteringColumn> (readonly)

Returns clustering columns defined on the table.

Returns:

Since:

  • 1.0.0



27
28
29
# File 'lib/cequel/schema/table.rb', line 27

def clustering_columns
  @clustering_columns
end

#columnsArray<Column> (readonly)

Returns all columns defined on the table.

Returns:

  • (Array<Column>)

    all columns defined on the table

Since:

  • 1.0.0



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.

Returns:

  • (Boolean)

    ‘true` if this table is configured with compact storage

Since:

  • 1.0.0



35
36
37
# File 'lib/cequel/schema/table.rb', line 35

def compact_storage=(value)
  @compact_storage = value
end

#data_columnsArray<DataColumn,CollectionColumn> (readonly)

Returns data columns and collection columns defined on the table.

Returns:

Since:

  • 1.0.0



30
31
32
# File 'lib/cequel/schema/table.rb', line 30

def data_columns
  @data_columns
end

#nameSymbol (readonly)

Returns the name of the table.

Returns:

  • (Symbol)

    the name of the table

Since:

  • 1.0.0



19
20
21
# File 'lib/cequel/schema/table.rb', line 19

def name
  @name
end

#partition_key_columnsArray<PartitionKey> (readonly)

Returns partition key columns defined on the table.

Returns:

  • (Array<PartitionKey>)

    partition key columns defined on the table

Since:

  • 1.0.0



24
25
26
# File 'lib/cequel/schema/table.rb', line 24

def partition_key_columns
  @partition_key_columns
end

#propertiesHash (readonly)

Returns storage properties defined on the table.

Returns:

  • (Hash)

    storage properties defined on the table

Since:

  • 1.0.0



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

Parameters:

  • name (Symbol)

    the name of the column

  • type (Symbol, Type)

    the type for the column

  • clustering_order (:asc, :desc) (defaults to: nil)

    whether rows should be in ascending or descending order by this column. Only meaningful for clustering columns.

Since:

  • 1.0.0



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

Parameters:

  • name (Symbol)

    name of the column

  • type (Type)

    type for the column

  • options (Options) (defaults to: {})

    options for the column

Options Hash (options):

  • :index (Boolean, Symbol) — default: nil

    name of a secondary index to apply to the column, or ‘true` to infer an index name by convention

Since:

  • 1.0.0



108
109
110
111
112
113
114
# File 'lib/cequel/schema/table.rb', line 108

def add_data_column(name, type, options = {})
  options = {index: options} unless options.is_a?(Hash)
  index_name = options[: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.

Parameters:

  • name (Symbol)

    the name of the column

  • type (Symbol, Type)

    the type for the column

  • clustering_order (:asc, :desc) (defaults to: nil)

    whether rows should be in ascending or descending order by this column. Only meaningful for clustering columns.

See Also:

Since:

  • 1.0.0



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

Parameters:

  • name (Symbol)

    name of the list

  • type (Symbol, Type)

    type of the list’s elements

See Also:

Since:

  • 1.0.0



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

Parameters:

  • name (Symbol)

    name of the set

  • key_type (Symbol, Type)

    type of the map’s keys

  • value_type (Symbol, Type)

    type of the map’s values

See Also:

Since:

  • 1.0.0



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

Parameters:

  • name (Symbol)

    the name of the column

  • type (Symbol, Type)

    the type for the column

Since:

  • 1.0.0



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

Parameters:

  • name (Symbol)

    name of the property

  • value

    value for the property

See Also:

Since:

  • 1.0.0



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

Parameters:

  • name (Symbol)

    name of the set

  • type (Symbol, Type)

    type of the set’s elements

See Also:

Since:

  • 1.0.0



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.

Parameters:

  • name (Symbol)

    name of clustering column to look up

Returns:

Since:

  • 1.0.0



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_countInteger

Returns number of clustering columns.

Returns:

  • (Integer)

    number of clustering columns

Since:

  • 1.0.0



239
240
241
# File 'lib/cequel/schema/table.rb', line 239

def clustering_column_count
  clustering_columns.length
end

#clustering_column_namesArray<Symbol>

Returns names of clustering columns.

Returns:

  • (Array<Symbol>)

    names of clustering columns

Since:

  • 1.0.0



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.

Parameters:

  • name (Symbol)

    name of column to look up

Returns:

  • (Column)

    column defined on table with given name

Since:

  • 1.0.0



183
184
185
# File 'lib/cequel/schema/table.rb', line 183

def column(name)
  columns_by_name[name.to_sym]
end

#column_namesArray<Symbol>

Returns the names of all columns.

Returns:

  • (Array<Symbol>)

    the names of all columns

Since:

  • 1.0.0



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.

Returns:

  • (Boolean)

    ‘true` if this table uses compact storage

Since:

  • 1.0.0



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.

Parameters:

  • name (Symbol)

    name of data column to look up

Returns:

Since:

  • 1.0.0



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_countInteger

Returns total number of key columns.

Returns:

  • (Integer)

    total number of key columns

Since:

  • 1.0.0



211
212
213
# File 'lib/cequel/schema/table.rb', line 211

def key_column_count
  key_columns.length
end

#key_column_namesArray<Symbol>

Returns names of all key columns (partition + clustering).

Returns:

  • (Array<Symbol>)

    names of all key columns (partition + clustering)

Since:

  • 1.0.0



204
205
206
# File 'lib/cequel/schema/table.rb', line 204

def key_column_names
  key_columns.map { |key| key.name }
end

#key_columnsArray<Column>

Returns all key columns (partition + clustering).

Returns:

  • (Array<Column>)

    all key columns (partition + clustering)

Since:

  • 1.0.0



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.

Parameters:

  • name (Symbol)

    name of partition key column to look up

Returns:

Since:

  • 1.0.0



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_countInteger

Returns number of partition key columns.

Returns:

  • (Integer)

    number of partition key columns

Since:

  • 1.0.0



225
226
227
# File 'lib/cequel/schema/table.rb', line 225

def partition_key_column_count
  partition_key_columns.length
end

#partition_key_column_namesArray<Symbol>

Returns names of partition key columns.

Returns:

  • (Array<Symbol>)

    names of partition key columns

Since:

  • 1.0.0



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.

Parameters:

  • name (Symbol)

    name of property to look up

Returns:

Since:

  • 1.0.0



273
274
275
# File 'lib/cequel/schema/table.rb', line 273

def property(name)
  @properties[name].try(:value)
end