Class: Cassanova::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/cassanova.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Query

Returns a new instance of Query.



130
131
132
133
134
# File 'lib/cassanova.rb', line 130

def initialize options={}
  @query = options[:query]
  @query_type = options[:query_type]
  @table_name = options[:table_name]
end

Instance Attribute Details

#queriesObject

Returns the value of attribute queries.



128
129
130
# File 'lib/cassanova.rb', line 128

def queries
  @queries
end

#queryObject

Returns the value of attribute query.



128
129
130
# File 'lib/cassanova.rb', line 128

def query
  @query
end

#query_typeObject

Returns the value of attribute query_type.



128
129
130
# File 'lib/cassanova.rb', line 128

def query_type
  @query_type
end

#table_nameObject

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

#allObject



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_queryObject



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

#countObject 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

#destroyObject



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

#firstObject



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