Class: Cassandra::Table

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

Overview

Represents a cassandra table

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameString (readonly)

Returns table name.

Returns:

  • (String)

    table name



137
138
139
# File 'lib/cassandra/table.rb', line 137

def name
  @name
end

Instance Method Details

#column(name) ⇒ Cassandra::Column?

Returns a column or nil.

Parameters:

  • name (String)

    column name

Returns:



161
162
163
# File 'lib/cassandra/table.rb', line 161

def column(name)
  @columns[name]
end

#each_column {|column| ... } ⇒ Cassandra::Table #each_columnArray<Cassandra::Column> Also known as: columns

Yield or enumerate each column defined in this table

Overloads:



171
172
173
174
175
176
177
178
# File 'lib/cassandra/table.rb', line 171

def each_column(&block)
  if block_given?
    @columns.each_value(&block)
    self
  else
    @columns.values
  end
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns whether this table is equal to the other.

Returns:

  • (Boolean)

    whether this table is equal to the other



241
242
243
244
245
246
247
248
249
250
# File 'lib/cassandra/table.rb', line 241

def eql?(other)
  other.is_a?(Table) &&
    @keyspace == other.keyspace &&
    @name == other.name &&
    @partition_key == other.partition_key &&
    @clustering_columns == other.clustering_columns &&
    @columns == other.raw_columns &&
    @options == other.options &&
    @clustering_order == other.clustering_order
end

#has_column?(name) ⇒ Boolean

Returns whether this table has a given column.

Parameters:

  • name (String)

    column name

Returns:

  • (Boolean)

    whether this table has a given column



155
156
157
# File 'lib/cassandra/table.rb', line 155

def has_column?(name)
  @columns.has_key?(name)
end

#inspectString

Returns a CLI-friendly table representation.

Returns:

  • (String)

    a CLI-friendly table representation



236
237
238
# File 'lib/cassandra/table.rb', line 236

def inspect
  "#<#{self.class.name}:0x#{self.object_id.to_s(16)} @keyspace=#{@keyspace} @name=#{@name}>"
end

#to_cqlString

Returns a cql representation of this table.

Returns:

  • (String)

    a cql representation of this table



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/cassandra/table.rb', line 182

def to_cql
  cql   = "CREATE TABLE #{Util.escape_name(@keyspace)}.#{Util.escape_name(@name)} (\n"
  first = true
  @columns.each do |(_, column)|
    if first
      first = false
    else
      cql << ",\n" unless first
    end
    cql << "  #{column.name} #{type_to_cql(column.type, column.frozen?)}"
  end
  cql << ",\n  PRIMARY KEY ("
  if @partition_key.one?
    cql << @partition_key.first.name
  else
    cql << '('
    first = true
    @partition_key.each do |column|
      if first
        first = false
      else
        cql << ', '
      end
      cql << column.name
    end
    cql << ')'
  end
  @clustering_columns.each do |column|
    cql << ", #{column.name}"
  end
  cql << ')'

  cql << "\n)\nWITH "

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

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

  cql << ';'
end