Skip to content

Commit

Permalink
Merge pull request #11 from backupify/read_support_for_ttls
Browse files Browse the repository at this point in the history
adds tests and read ttl functionality
  • Loading branch information
zackattack01 authored Dec 5, 2016
2 parents 4a40e21 + bbcbe40 commit 6098e64
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
24 changes: 24 additions & 0 deletions lib/cassava/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ def select(table, columns = nil)
StatementBuilder.new(executor).select(table, columns)
end

# @param table [Symbol] the table name
# @param target_attr [String] The attribute to select the TTL for
# @param where_arguments [Hash] Pairs of keys and values for the where clause
def select_ttl(table, target_attr, where_arguments)
statement = select_ttl_statement(table, target_attr, where_arguments)
executor.execute(statement).rows.first["ttl(#{target_attr})"]
end

# @param table [Symbol] the table name
# @param columns [Array<String] A list of columns that will be deleted. If nil, all columns will be deleted.
# @return [StatementBuilder] A statement builder representing the partially completed statement.
Expand Down Expand Up @@ -60,6 +68,22 @@ def insert_statement(table, data, ttl = nil)
statement_cql += " USING TTL #{ttl}" if ttl
executor.prepare(statement_cql)
end

# @param table [Symbol] the table name
# @param target_attr [Symbol] The attribute to select the TTL for
# @param where_arguments [Hash] Pairs of keys and values for the where clause
def select_ttl_statement(table, target_attr, where_arguments)
statement = "SELECT ttl(#{target_attr}) FROM #{table} WHERE "
where_clause = where_arguments.map do |(key, val)|
if val.is_a? Integer
"#{key} = #{val}"
else
"#{key} = '#{val}'"
end
end

statement + where_clause.join(" AND ")
end
end

class StatementBuilder
Expand Down
2 changes: 1 addition & 1 deletion lib/cassava/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Cassava
VERSION = "0.1.2"
VERSION = "0.1.3"
end
27 changes: 27 additions & 0 deletions test/cassava/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,33 @@ def string_keys(hash)
end
end

context 'select_ttl' do
should 'allow an existing ttl to be read' do
ttl = 12345
item = { :id => 'i', :a => 1, :b => 'b', :c => "'\"item(", :d => 1, :ttl => ttl }
@client.insert(:test, item)

resulting_ttl = @client.select_ttl(:test, :d, {:id => 'i'})
assert (1..ttl).include? resulting_ttl
end

should 'return nil if there is no ttl set on a cell' do
item = { :id => 'i', :a => 1, :b => 'b', :c => "'\"item(", :d => 1 }
@client.insert(:test, item)

resulting_ttl = @client.select_ttl(:test, :d, {:id => 'i'})
assert resulting_ttl.nil?
end

should 'handle string vs integer arguments properly' do
where_args = { :id => 'i', :a => 1, :b => 'b', :c => "'\"item(" }
statement = @client.send(:select_ttl_statement, :test, :c, where_args)

assert_match /a\s=\s1/, statement
assert_match /b\s=\s'b'/, statement
end
end

context 'delete' do
setup do
@client.insert(:test, :id => 'i', :a => 2, :b => 'a', :c => '1', :d => 1)
Expand Down

0 comments on commit 6098e64

Please sign in to comment.