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

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



36
37
38
39
40
41
# File 'lib/cequel/schema/table.rb', line 36

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



22
23
24
# File 'lib/cequel/schema/table.rb', line 22

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



16
17
18
# File 'lib/cequel/schema/table.rb', line 16

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



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

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



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

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



14
15
16
# File 'lib/cequel/schema/table.rb', line 14

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



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

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



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

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



87
88
89
90
# File 'lib/cequel/schema/table.rb', line 87

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



103
104
105
106
107
108
109
# File 'lib/cequel/schema/table.rb', line 103

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



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cequel/schema/table.rb', line 56

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



120
121
122
123
124
# File 'lib/cequel/schema/table.rb', line 120

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



151
152
153
154
155
# File 'lib/cequel/schema/table.rb', line 151

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



75
76
77
78
79
# File 'lib/cequel/schema/table.rb', line 75

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



168
169
170
171
172
# File 'lib/cequel/schema/table.rb', line 168

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



135
136
137
138
139
# File 'lib/cequel/schema/table.rb', line 135

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



250
251
252
# File 'lib/cequel/schema/table.rb', line 250

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



234
235
236
# File 'lib/cequel/schema/table.rb', line 234

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



227
228
229
# File 'lib/cequel/schema/table.rb', line 227

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



178
179
180
# File 'lib/cequel/schema/table.rb', line 178

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



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

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



275
276
277
# File 'lib/cequel/schema/table.rb', line 275

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



259
260
261
262
# File 'lib/cequel/schema/table.rb', line 259

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



206
207
208
# File 'lib/cequel/schema/table.rb', line 206

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



199
200
201
# File 'lib/cequel/schema/table.rb', line 199

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



191
192
193
# File 'lib/cequel/schema/table.rb', line 191

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



242
243
244
# File 'lib/cequel/schema/table.rb', line 242

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



220
221
222
# File 'lib/cequel/schema/table.rb', line 220

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



213
214
215
# File 'lib/cequel/schema/table.rb', line 213

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



268
269
270
# File 'lib/cequel/schema/table.rb', line 268

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