Class: DatastaxRails::Cql::Base
- Defined in:
- lib/datastax_rails/cql/base.rb
Overview
Base class for CQL generation
Direct Known Subclasses
AlterColumnFamily, CreateColumnFamily, CreateIndex, CreateKeyspace, Delete, DropColumnFamily, DropIndex, DropKeyspace, Insert, Select, Truncate, Update, UseKeyspace
Instance Method Summary collapse
-
#execute ⇒ Object
Generates the CQL and calls Cassandra to execute it.
-
#initialize(klass, *_args) ⇒ Base
constructor
Base initialize that sets the default consistency.
-
#to_cql ⇒ Object
Abstract.
- #using(consistency) ⇒ Object
Constructor Details
#initialize(klass, *_args) ⇒ Base
Base initialize that sets the default consistency.
6 7 8 9 10 11 |
# File 'lib/datastax_rails/cql/base.rb', line 6 def initialize(klass, *_args) @klass = klass @consistency = klass.default_consistency.to_s.downcase.to_sym @keyspace = DatastaxRails::Base.config[:keyspace] @values = [] end |
Instance Method Details
#execute ⇒ Object
Generates the CQL and calls Cassandra to execute it. If you are using this outside of Rails, then DatastaxRails::Base.connection must have already been set up (Rails does this for you).
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/datastax_rails/cql/base.rb', line 26 def execute cql = to_cql.force_encoding('UTF-8') ActiveSupport::Notifications.instrument( 'cql.datastax_rails', name: 'CQL', cql: cql, klass: @klass, connection_id: DatastaxRails::Base.connection.object_id, statement_name: self.class.name, binds: @values) do |payload| digest = Digest::MD5.digest cql try_again = true begin DatastaxRails::Base.reconnect unless DatastaxRails::Base.connection stmt = DatastaxRails::Base.statement_cache[digest] ||= DatastaxRails::Base.connection.prepare(cql) stmt = stmt.bind(@values) if @consistency results = DatastaxRails::Base.connection.execute(stmt, consistency: @consistency) else results = DatastaxRails::Base.connection.execute(stmt) end payload[:result_count] = results.respond_to?(:count) ? results.count : 'No' DatastaxRails::Base.current_server = results.execution_info.hosts.first.ip.to_s results rescue Cassandra::Errors::NoHostsAvailable if try_again Rails.logger.warn('Lost connection to Cassandra. Attempting to reconnect...') try_again = false DatastaxRails::Base.reconnect retry else raise end end end end |
#to_cql ⇒ Object
Abstract. Should be overridden by subclasses
19 20 21 |
# File 'lib/datastax_rails/cql/base.rb', line 19 def to_cql fail NotImplementedError end |
#using(consistency) ⇒ Object
13 14 15 16 |
# File 'lib/datastax_rails/cql/base.rb', line 13 def using(consistency) @consistency = consistency.to_s.downcase.to_sym self end |