-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathcql-rb-wrapper.rb
66 lines (53 loc) · 1.07 KB
/
cql-rb-wrapper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
63
64
65
66
# encoding: utf-8
require 'cassandra'
require 'ione'
class PreparedStatement
attr_reader :statement
def initialize(client, statement)
@client = client
@statement = statement
end
def execute(*args)
@client.execute(@statement, *args)
end
end
class BatchStatement
def initialize(client, batch)
@client = client
@batch = batch
end
def execute(options = {})
@client.execute(@batch, options)
end
def add(*args)
@batch.add(*args)
self
end
end
class Client
def initialize(session)
@session = session
end
def execute(*args)
@session.execute(*args)
end
def prepare(statement, options = {})
s = @session.prepare(statement, options)
PreparedStatement.new(self, s)
end
def batch(type = :logged, options = {})
batch = BatchStatement.new(self, @session.send(:"#{type}_batch"))
if block_given?
yield(batch)
batch.execute(options)
else
batch
end
end
def close
@session.close
end
end
cluster = Cassandra.cluster
session = cluster.connect
client = Client.new(session)