class Cassandra::Table
Represents a cassandra table @see Cassandra::Keyspace#each_table
@see Cassandra::Keyspace#table
Attributes
@return [Array<Symbol>] an array of order values (`:asc` or `:desc`) that apply to the
`clustering_columns` array.
Public Class Methods
@private
Cassandra::ColumnContainer::new
# File lib/cassandra/table.rb 29 def initialize(keyspace, 30 name, 31 partition_key, 32 clustering_columns, 33 other_columns, 34 options, 35 clustering_order, 36 id) 37 super(keyspace, name, partition_key, clustering_columns, other_columns, options, id) 38 @clustering_order = clustering_order.freeze 39 @indexes = [] 40 @indexes_hash = {} 41 @materialized_views = [] 42 @materialized_views_hash = {} 43 @triggers = [] 44 @triggers_hash = {} 45 end
Public Instance Methods
@private
# File lib/cassandra/table.rb 192 def add_index(index) 193 @indexes << index 194 @indexes_hash[index.name] = index 195 end
@private
# File lib/cassandra/table.rb 204 def add_trigger(trigger) 205 @triggers << trigger 206 @triggers_hash[trigger.name] = trigger 207 end
@private
# File lib/cassandra/table.rb 198 def add_view(view) 199 @materialized_views << view 200 @materialized_views_hash[view.name] = view 201 end
Yield or enumerate each index bound to this table @overload each_index
@yieldparam index [Cassandra::Index] current index @return [Cassandra::Table] self
@overload each_index
@return [Array<Cassandra::Index>] a list of indexes
# File lib/cassandra/table.rb 65 def each_index(&block) 66 if block_given? 67 @indexes.each(&block) 68 self 69 else 70 @indexes.freeze 71 end 72 end
Yield or enumerate each materialized view bound to this table @overload each_materialized_view
@yieldparam materialized_view [Cassandra::MaterializedView] current materialized view @return [Cassandra::Table] self
@overload each_materialized_view
@return [Array<Cassandra::MaterializedView>] a list of materialized views
# File lib/cassandra/table.rb 121 def each_materialized_view(&block) 122 if block_given? 123 @materialized_views.each(&block) 124 self 125 else 126 @materialized_views.freeze 127 end 128 end
Yield or enumerate each trigger bound to this table @overload each_trigger
@yieldparam trigger [Cassandra::Index] current trigger @return [Cassandra::Table] self
@overload each_trigger
@return [Array<Cassandra::Trigger>] a list of triggers
# File lib/cassandra/table.rb 93 def each_trigger(&block) 94 if block_given? 95 @triggers.each(&block) 96 self 97 else 98 @triggers.freeze 99 end 100 end
@private
Cassandra::ColumnContainer#eql?
# File lib/cassandra/table.rb 210 def eql?(other) 211 other.is_a?(Table) && 212 super.eql?(other) && 213 @clustering_order == other.clustering_order && 214 @indexes == other.indexes 215 end
@param name [String] index name @return [Boolean] whether this table has a given index
# File lib/cassandra/table.rb 49 def has_index?(name) 50 @indexes_hash.key?(name) 51 end
@param name [String] materialized view name @return [Boolean] whether this table has a given materialized view
# File lib/cassandra/table.rb 105 def has_materialized_view?(name) 106 @materialized_views_hash.key?(name) 107 end
@param name [String] trigger name @return [Boolean] whether this table has a given trigger
# File lib/cassandra/table.rb 77 def has_trigger?(name) 78 @triggers_hash.key?(name) 79 end
@param name [String] index name @return [Cassandra::Index, nil] an index or nil
# File lib/cassandra/table.rb 55 def index(name) 56 @indexes_hash[name] 57 end
@param name [String] materialized view name @return [Cassandra::MaterializedView, nil] a materialized view or nil
# File lib/cassandra/table.rb 111 def materialized_view(name) 112 @materialized_views_hash[name] 113 end
@return [String] a cql representation of this table
# File lib/cassandra/table.rb 132 def to_cql 133 cql = "CREATE TABLE #{Util.escape_name(@keyspace.name)}.#{Util.escape_name(@name)} (\n" 134 primary_key = @partition_key.first.name if @partition_key.one? && @clustering_columns.empty? 135 136 first = true 137 @columns.each do |column| 138 if first 139 first = false 140 else 141 cql << ",\n" 142 end 143 cql << " #{Util.escape_name(column.name)} #{type_to_cql(column.type, column.frozen?)}" 144 cql << ' PRIMARY KEY' if primary_key && column.name == primary_key 145 end 146 147 unless primary_key 148 cql << ",\n PRIMARY KEY (" 149 if @partition_key.one? 150 cql << Util.escape_name(@partition_key.first.name) 151 else 152 cql << '(' 153 first = true 154 @partition_key.each do |column| 155 if first 156 first = false 157 else 158 cql << ', ' 159 end 160 cql << Util.escape_name(column.name) 161 end 162 cql << ')' 163 end 164 @clustering_columns.each do |column| 165 cql << ", #{Util.escape_name(column.name)}" 166 end 167 cql << ')' 168 end 169 170 cql << "\n)\nWITH " 171 172 if !@clustering_order.empty? && !@clustering_columns.empty? 173 cql << 'CLUSTERING ORDER BY (' 174 first = true 175 @clustering_columns.zip(@clustering_order) do |column, order| 176 if first 177 first = false 178 else 179 cql << ', ' 180 end 181 cql << "#{Util.escape_name(column.name)} #{order.to_s.upcase}" 182 end 183 cql << ")\n AND " 184 end 185 186 cql << @options.to_cql.split("\n").join("\n ") 187 188 cql << ';' 189 end
@param name [String] trigger name @return [Cassandra::Trigger, nil] a trigger or nil
# File lib/cassandra/table.rb 83 def trigger(name) 84 @triggers_hash[name] 85 end
Private Instance Methods
@private
# File lib/cassandra/table.rb 221 def type_to_cql(type, is_frozen) 222 case type.kind 223 when :tuple 224 "frozen <#{type}>" 225 when :udt 226 if @keyspace.name == type.keyspace 227 "frozen <#{Util.escape_name(type.name)}>" 228 else 229 "frozen <#{Util.escape_name(type.keyspace)}.#{Util.escape_name(type.name)}>" 230 end 231 else 232 if is_frozen 233 "frozen <#{type}>" 234 else 235 type.to_s 236 end 237 end 238 end