Class: Cassandra::Table

Inherits:
ColumnContainer show all
Defined in:
lib/cassandra/table.rb

Overview

Represents a cassandra table

Instance Attribute Summary collapse

Attributes inherited from ColumnContainer

#clustering_columns, #id, #keyspace, #name, #options, #partition_key, #primary_key

Instance Method Summary collapse

Methods inherited from ColumnContainer

#column, #each_column, #has_column?

Instance Attribute Details

#clustering_orderArray<Symbol> (readonly)

Returns an array of order values (:asc or :desc) that apply to the clustering_columns array.

Returns:

  • (Array<Symbol>)

    an array of order values (:asc or :desc) that apply to the clustering_columns array.



26
27
28
# File 'lib/cassandra/table.rb', line 26

def clustering_order
  @clustering_order
end

Instance Method Details

#each_index {|index| ... } ⇒ Cassandra::Table #each_indexArray<Cassandra::Index> Also known as: indexes

Yield or enumerate each index bound to this table

Overloads:



65
66
67
68
69
70
71
72
# File 'lib/cassandra/table.rb', line 65

def each_index(&block)
  if block_given?
    @indexes.each(&block)
    self
  else
    @indexes.freeze
  end
end

#each_materialized_view {|materialized_view| ... } ⇒ Cassandra::Table #each_materialized_viewArray<Cassandra::MaterializedView> Also known as: materialized_views

Yield or enumerate each materialized view bound to this table

Overloads:



121
122
123
124
125
126
127
128
# File 'lib/cassandra/table.rb', line 121

def each_materialized_view(&block)
  if block_given?
    @materialized_views.each(&block)
    self
  else
    @materialized_views.freeze
  end
end

#each_trigger {|trigger| ... } ⇒ Cassandra::Table #each_triggerArray<Cassandra::Trigger> Also known as: triggers

Yield or enumerate each trigger bound to this table

Overloads:



93
94
95
96
97
98
99
100
# File 'lib/cassandra/table.rb', line 93

def each_trigger(&block)
  if block_given?
    @triggers.each(&block)
    self
  else
    @triggers.freeze
  end
end

#has_index?(name) ⇒ Boolean

Returns whether this table has a given index.

Parameters:

  • name (String)

    index name

Returns:

  • (Boolean)

    whether this table has a given index



49
50
51
# File 'lib/cassandra/table.rb', line 49

def has_index?(name)
  @indexes_hash.key?(name)
end

#has_materialized_view?(name) ⇒ Boolean

Returns whether this table has a given materialized view.

Parameters:

  • name (String)

    materialized view name

Returns:

  • (Boolean)

    whether this table has a given materialized view



105
106
107
# File 'lib/cassandra/table.rb', line 105

def has_materialized_view?(name)
  @materialized_views_hash.key?(name)
end

#has_trigger?(name) ⇒ Boolean

Returns whether this table has a given trigger.

Parameters:

  • name (String)

    trigger name

Returns:

  • (Boolean)

    whether this table has a given trigger



77
78
79
# File 'lib/cassandra/table.rb', line 77

def has_trigger?(name)
  @triggers_hash.key?(name)
end

#index(name) ⇒ Cassandra::Index?

Returns an index or nil.

Parameters:

  • name (String)

    index name

Returns:



55
56
57
# File 'lib/cassandra/table.rb', line 55

def index(name)
  @indexes_hash[name]
end

#materialized_view(name) ⇒ Cassandra::MaterializedView?

Returns a materialized view or nil.

Parameters:

  • name (String)

    materialized view name

Returns:



111
112
113
# File 'lib/cassandra/table.rb', line 111

def materialized_view(name)
  @materialized_views_hash[name]
end

#to_cqlString

Returns a cql representation of this table.

Returns:

  • (String)

    a cql representation of this table



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/cassandra/table.rb', line 132

def to_cql
  cql = "CREATE TABLE #{Util.escape_name(@keyspace.name)}.#{Util.escape_name(@name)} (\n"
  primary_key = @partition_key.first.name if @partition_key.one? && @clustering_columns.empty?

  first = true
  @columns.each do |column|
    if first
      first = false
    else
      cql << ",\n"
    end
    cql << "  #{Util.escape_name(column.name)} #{type_to_cql(column.type, column.frozen?)}"
    cql << ' PRIMARY KEY' if primary_key && column.name == primary_key
  end

  unless primary_key
    cql << ",\n  PRIMARY KEY ("
    if @partition_key.one?
      cql << Util.escape_name(@partition_key.first.name)
    else
      cql << '('
      first = true
      @partition_key.each do |column|
        if first
          first = false
        else
          cql << ', '
        end
        cql << Util.escape_name(column.name)
      end
      cql << ')'
    end
    @clustering_columns.each do |column|
      cql << ", #{Util.escape_name(column.name)}"
    end
    cql << ')'
  end

  cql << "\n)\nWITH "

  if !@clustering_order.empty? && !@clustering_columns.empty?
    cql << 'CLUSTERING ORDER BY ('
    first = true
    @clustering_columns.zip(@clustering_order) do |column, order|
      if first
        first = false
      else
        cql << ', '
      end
      cql << "#{Util.escape_name(column.name)} #{order.to_s.upcase}"
    end
    cql << ")\n AND "
  end

  cql << @options.to_cql.split("\n").join("\n ")

  cql << ';'
end

#trigger(name) ⇒ Cassandra::Trigger?

Returns a trigger or nil.

Parameters:

  • name (String)

    trigger name

Returns:



83
84
85
# File 'lib/cassandra/table.rb', line 83

def trigger(name)
  @triggers_hash[name]
end