Class: Cassanova::Query
- Inherits:
-
Object
- Object
- Cassanova::Query
- Defined in:
- lib/cassanova.rb
Instance Attribute Summary collapse
-
#queries ⇒ Object
Returns the value of attribute queries.
-
#query ⇒ Object
Returns the value of attribute query.
-
#query_type ⇒ Object
Returns the value of attribute query_type.
-
#table_name ⇒ Object
Returns the value of attribute table_name.
Class Method Summary collapse
Instance Method Summary collapse
- #all ⇒ Object
- #compiled_query ⇒ Object
- #count ⇒ Object (also: #length)
- #destroy ⇒ Object
- #first ⇒ Object
-
#initialize(options = {}) ⇒ Query
constructor
A new instance of Query.
- #limit(i) ⇒ Object
- #select(*attrs) ⇒ Object
- #where(conditions = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Query
Returns a new instance of Query.
130 131 132 133 134 |
# File 'lib/cassanova.rb', line 130 def initialize ={} @query = [:query] @query_type = [:query_type] @table_name = [:table_name] end |
Instance Attribute Details
#queries ⇒ Object
Returns the value of attribute queries.
128 129 130 |
# File 'lib/cassanova.rb', line 128 def queries @queries end |
#query ⇒ Object
Returns the value of attribute query.
128 129 130 |
# File 'lib/cassanova.rb', line 128 def query @query end |
#query_type ⇒ Object
Returns the value of attribute query_type.
128 129 130 |
# File 'lib/cassanova.rb', line 128 def query_type @query_type end |
#table_name ⇒ Object
Returns the value of attribute table_name.
128 129 130 |
# File 'lib/cassanova.rb', line 128 def table_name @table_name end |
Class Method Details
.parse(response, table_name) ⇒ Object
187 188 189 190 191 192 193 194 |
# File 'lib/cassanova.rb', line 187 def self.parse response, table_name table_class = table_name.classify.constantize objs = [] response.each do |d| objs << table_class.new(d) end return objs end |
Instance Method Details
#all ⇒ Object
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/cassanova.rb', line 161 def all results = Cassanova::Model.session.execute(compiled_query) objs = Cassanova::Query.parse(results, table_name) while results.last_page? == false results = results.next_page objs += Cassanova::Query.parse(results, table_name) print "." end return objs end |
#compiled_query ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/cassanova.rb', line 196 def compiled_query wheres = [] selects = [] queries.each do |query| case query.query_type when "where" wheres << query.query when "select" selects << query.query end end query = "select #{selects.present? ? selects.join(',') : '*'} from #{table_name}" if wheres.present? query += " where #{wheres.join(' AND ')}" end end |
#count ⇒ Object Also known as: length
172 173 174 175 176 177 |
# File 'lib/cassanova.rb', line 172 def count cq = compiled_query selects = cq.split("select ")[1].split(" from")[0] cq = cq.gsub(selects, "COUNT(*)") Cassanova::Model.session.execute(cq).rows.first['count'] end |
#destroy ⇒ Object
180 181 182 183 184 185 |
# File 'lib/cassanova.rb', line 180 def destroy cq = compiled_query cq = "delete from " + cq.split("from")[1] result = Cassanova::Model.session.execute(cq) return result.class == Cassandra::Results::Void end |
#first ⇒ Object
157 158 159 |
# File 'lib/cassanova.rb', line 157 def first limit(1).first end |
#limit(i) ⇒ Object
152 153 154 155 |
# File 'lib/cassanova.rb', line 152 def limit i cq = compiled_query + " LIMIT #{i}" Cassanova::Query.parse(Cassanova::Model.session.execute(cq), table_name) end |
#select(*attrs) ⇒ Object
146 147 148 149 150 |
# File 'lib/cassanova.rb', line 146 def select *attrs self.queries ||= [] self.queries << Cassanova::Query.new(:query_type => "select", :query => attrs.join(',')) return self end |
#where(conditions = {}) ⇒ Object
136 137 138 139 140 141 142 143 144 |
# File 'lib/cassanova.rb', line 136 def where conditions={} query = [] conditions.each do |key, val| query << "#{key} = #{val}" end self.queries ||= [] self.queries << Cassanova::Query.new(:query_type => "where", :query => query.join(" AND ")) return self end |