Class: RailsNewsfeed::Connection
- Inherits:
-
Object
- Object
- RailsNewsfeed::Connection
- Defined in:
- lib/rails_newsfeed/connection.rb
Class Method Summary collapse
-
.batch_cqls(cqls, options = {}) ⇒ Object
executes batch.
-
.config ⇒ Object
gets config.
-
.connection ⇒ Object
gets cassandra connection.
-
.delete(tbl, schema, conditions, to_cql = false) ⇒ Object
deletes.
-
.exec_cql(cql, options = {}) ⇒ Object
executes cql.
-
.insert(tbl, schema, vals, to_cql = false) ⇒ Object
inserts.
-
.select(tbl, schema = {}, columns = '*', conditions = {}, options = {}) ⇒ Object
selects.
-
.update(tbl, schema, conditions, vals, to_cql = false) ⇒ Object
updates.
Class Method Details
.batch_cqls(cqls, options = {}) ⇒ Object
executes batch
54 55 56 57 58 59 60 61 62 |
# File 'lib/rails_newsfeed/connection.rb', line 54 def self.batch_cqls(cqls, = {}) return true if cqls.empty? batch = connection.batch do |b| cqls.each do |cql| b.add(cql) end end exec_cql(batch, ) end |
.config ⇒ Object
gets config
37 38 39 |
# File 'lib/rails_newsfeed/connection.rb', line 37 def self.config YAML.load_file('config/cassandra.yml')[Rails.env] end |
.connection ⇒ Object
gets cassandra connection
42 43 44 45 46 |
# File 'lib/rails_newsfeed/connection.rb', line 42 def self.connection return @connection if @connection cfg = config @connection = Cassandra.cluster(cfg || {}).connect(cfg['keyspace']) end |
.delete(tbl, schema, conditions, to_cql = false) ⇒ Object
deletes
84 85 86 87 88 89 |
# File 'lib/rails_newsfeed/connection.rb', line 84 def self.delete(tbl, schema, conditions, to_cql = false) cql = "DELETE FROM #{tbl} WHERE #{exported_col_val(schema, conditions).join(' AND ')}" return cql if to_cql exec_cql(cql) true end |
.exec_cql(cql, options = {}) ⇒ Object
executes cql
49 50 51 |
# File 'lib/rails_newsfeed/connection.rb', line 49 def self.exec_cql(cql, = {}) connection.execute(cql, ) end |
.insert(tbl, schema, vals, to_cql = false) ⇒ Object
inserts
65 66 67 68 69 70 |
# File 'lib/rails_newsfeed/connection.rb', line 65 def self.insert(tbl, schema, vals, to_cql = false) cql = "INSERT INTO #{tbl} (#{vals.keys.join(',')}) VALUES (#{cass_vals(schema, vals)})" return cql if to_cql exec_cql(cql) true end |
.select(tbl, schema = {}, columns = '*', conditions = {}, options = {}) ⇒ Object
selects
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rails_newsfeed/connection.rb', line 92 def self.select(tbl, schema = {}, columns = '*', conditions = {}, = {}) cql = 'SELECT ' cql += columns unless columns.is_a?(Array) cql += columns.join(',') if columns.is_a?(Array) cql += " FROM #{tbl}" cql += " WHERE #{exported_col_val(schema, conditions).join(' AND ')}" unless conditions.empty? if [:filtering] cql += ' ALLOW FILTERING' .delete(:filtering) end exec_cql(cql, ) end |
.update(tbl, schema, conditions, vals, to_cql = false) ⇒ Object
updates
73 74 75 76 77 78 79 80 81 |
# File 'lib/rails_newsfeed/connection.rb', line 73 def self.update(tbl, schema, conditions, vals, to_cql = false) # unsets primary keys conditions.keys.each { |k| vals.delete(k) } val_s = exported_col_val(schema, vals).join(',') cql = "UPDATE #{tbl} SET #{val_s} WHERE #{exported_col_val(schema, conditions).join(' AND ')}" return cql if to_cql exec_cql(cql) true end |