Class: CassandraModel::TableDefinition
- Inherits:
-
Object
- Object
- CassandraModel::TableDefinition
- Defined in:
- lib/cassandra_model/table_definition.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#name_in_cassandra ⇒ Object
readonly
Returns the value of attribute name_in_cassandra.
-
#table_id ⇒ Object
readonly
Returns the value of attribute table_id.
Class Method Summary collapse
Instance Method Summary collapse
- #==(rhs) ⇒ Object
-
#initialize(options) ⇒ TableDefinition
constructor
A new instance of TableDefinition.
- #to_cql(options = {}) ⇒ Object
Constructor Details
#initialize(options) ⇒ TableDefinition
Returns a new instance of TableDefinition.
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/cassandra_model/table_definition.rb', line 61 def initialize() @partition_key = [:partition_key].keys @clustering_columns = [:clustering_columns].keys @name = [:name] @columns = [:partition_key].merge([:clustering_columns].merge([:remaining_columns])) @table_id = generate_table_id @name_in_cassandra = "#{name}_#{table_id}" @properties = ([:properties] || {}).select do |name, _| [:compaction, :clustering_order].include?(name) end end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/cassandra_model/table_definition.rb', line 3 def name @name end |
#name_in_cassandra ⇒ Object (readonly)
Returns the value of attribute name_in_cassandra.
59 60 61 |
# File 'lib/cassandra_model/table_definition.rb', line 59 def name_in_cassandra @name_in_cassandra end |
#table_id ⇒ Object (readonly)
Returns the value of attribute table_id.
59 60 61 |
# File 'lib/cassandra_model/table_definition.rb', line 59 def table_id @table_id end |
Class Method Details
.from_data_model(table_name, inquirer, data_set, properties) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/cassandra_model/table_definition.rb', line 7 def from_data_model(table_name, inquirer, data_set, properties) partition_key = inquirer_partition_key(inquirer) if inquirer.shard_column if inquirer.shard_column.is_a?(Hash) column_name, type = inquirer.shard_column.first partition_key.merge!(:"rk_#{column_name}" => type) else partition_key.merge!(:"rk_#{inquirer.shard_column}" => :int) end end clustering_columns = table_set_clustering_columns(data_set) remaining_columns = table_set_remaining_columns(data_set) updated_properties = table_properties_from_data_set(data_set, properties) new(name: table_name, partition_key: partition_key, clustering_columns: clustering_columns, remaining_columns: remaining_columns, properties: updated_properties) end |
Instance Method Details
#==(rhs) ⇒ Object
92 93 94 |
# File 'lib/cassandra_model/table_definition.rb', line 92 def ==(rhs) to_cql == rhs.to_cql end |
#to_cql(options = {}) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/cassandra_model/table_definition.rb', line 73 def to_cql( = {}) table_name = [:no_id] ? name : name_in_cassandra exists = if [:check_exists] 'IF NOT EXISTS ' end properties = if @properties.present? property_values = @properties.map do |property, definition| case property when :compaction "COMPACTION = #{to_property_string(definition)}" when :clustering_order "CLUSTERING ORDER BY #{to_clustering_order_string(definition)}" end end * ' AND ' " WITH #{property_values}" end "CREATE TABLE #{exists}#{table_name} (#{columns}, PRIMARY KEY #{primary_key})#{properties}" end |