Class: Cql::Model::Query::InsertStatement
- Inherits:
-
MutationStatement
- Object
- Statement
- MutationStatement
- Cql::Model::Query::InsertStatement
- Defined in:
- lib/cql/model/query/insert_statement.rb
Overview
INSERT statement DSL << An INSERT writes one or more columns to a record in a Cassandra column family. No results are returned.
The first column name in the INSERT list must be the name of the column family key >>
(from: www.datastax.com/docs/1.1/references/cql/INSERT)
Ex: Model.create(:key => ‘val’, :col1 => ‘value’, :col2 => 42) # Simple insert Model.create(:key => ‘val’, :key2 => 64, :col1 => ‘value’, :col2 => 42) # Composite keys Model.create(:key => ‘val’, :col => ‘value’).ttl(3600) # TTL in seconds Model.create(:key => ‘val’, :col => ‘value’).timestamp(1366057256324) # Milliseconds since epoch timestamp Model.create(:key => ‘val’, :col => ‘value’).timestamp(‘2013-04-15 13:21:48’) # ISO 8601 timestamp Model.create(:key => ‘val’, :col => ‘value’).consistency(‘ONE’) # Custom consistency (default is ‘LOCAL_QUORUM’) Model.create(:key => ‘val’, :col => ‘value’).ttl(3600).timestamp(1366057256324).consistency(‘ONE’) # Multiple options
Instance Method Summary collapse
-
#insert(values) ⇒ Object
(also: #create)
Specify names and values to insert.
-
#to_s ⇒ String
Build a string representation of this CQL statement, suitable for execution by a CQL client.
Methods inherited from MutationStatement
#execute, #initialize, #timestamp, #ttl
Methods inherited from Statement
#consistency, #execute, #initialize
Constructor Details
This class inherits a constructor from Cql::Model::Query::MutationStatement
Instance Method Details
#insert(values) ⇒ Object Also known as: create
Specify names and values to insert.
21 22 23 24 25 |
# File 'lib/cql/model/query/insert_statement.rb', line 21 def insert(values) raise ArgumentError, "Cannot specify INSERT values twice" unless @values.nil? @values = values self end |
#to_s ⇒ String
Build a string representation of this CQL statement, suitable for execution by a CQL client. Do not validate the statement for completeness; Cassnadra will raise an error if a key component is missing.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/cql/model/query/insert_statement.rb', line 34 def to_s keys = @klass.primary_key.inject([]) { |h, k| h << [k, @values.delete(k)]; h } if keys.any? { |k| k[1].nil? } raise Cql::Model::MissingKey.new("Missing primary key(s) in INSERT statement: #{keys.select { |k| k[1].nil? }.map(&:first).map(&:inspect).join(', ')}") end s = "INSERT INTO #{@klass.table_name} (#{keys.map { |k| k[0] }.join(', ')}, #{@values.keys.join(', ')})" s << " VALUES (#{keys.map { |k| ::Cql::Model::Query.cql_value(k[1]) }.join(', ')}, #{@values.values.map { |v| ::Cql::Model::Query.cql_value(v) }.join(', ')})" = [] << "CONSISTENCY #{@consistency || @klass.write_consistency}" << "TIMESTAMP #{@timestamp}" unless @timestamp.nil? << "TTL #{@ttl}" unless @ttl.nil? s << " USING #{.join(' AND ')}" s << ';' s end |