superstore 1.2.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +6 -13
- data/CHANGELOG.md +16 -0
- data/Gemfile +0 -5
- data/README.md +15 -33
- data/lib/superstore/adapters/jsonb_adapter.rb +245 -0
- data/lib/superstore/associations/association.rb +38 -0
- data/lib/superstore/associations/belongs_to.rb +35 -0
- data/lib/superstore/associations/builder/association.rb +38 -0
- data/lib/superstore/associations/builder/belongs_to.rb +7 -0
- data/lib/superstore/associations/builder/has_many.rb +7 -0
- data/lib/superstore/associations/builder/has_one.rb +7 -0
- data/lib/superstore/associations/has_many.rb +26 -0
- data/lib/superstore/associations/has_one.rb +24 -0
- data/lib/superstore/associations/reflection.rb +65 -0
- data/lib/superstore/associations.rb +72 -0
- data/lib/superstore/attribute_methods/definition.rb +5 -10
- data/lib/superstore/attribute_methods/dirty.rb +12 -2
- data/lib/superstore/attribute_methods/typecasting.rb +6 -12
- data/lib/superstore/base.rb +3 -4
- data/lib/superstore/connection.rb +3 -5
- data/lib/superstore/core.rb +0 -5
- data/lib/superstore/model.rb +32 -33
- data/lib/superstore/persistence.rb +4 -10
- data/lib/superstore/railtie.rb +2 -20
- data/lib/superstore/scope/batches.rb +17 -22
- data/lib/superstore/scope/finder_methods.rb +33 -35
- data/lib/superstore/scope/query_methods.rb +38 -44
- data/lib/superstore/scope.rb +24 -0
- data/lib/superstore/type.rb +3 -3
- data/lib/superstore/types/array_type.rb +2 -9
- data/lib/superstore/types/base_type.rb +4 -7
- data/lib/superstore/types/boolean_type.rb +2 -1
- data/lib/superstore/types/float_type.rb +6 -5
- data/lib/superstore/types/integer_type.rb +3 -3
- data/lib/superstore/types/json_type.rb +0 -21
- data/lib/superstore.rb +16 -5
- data/superstore.gemspec +2 -1
- data/test/support/jsonb.rb +8 -0
- data/test/support/{issue.rb → models.rb} +9 -0
- data/test/support/pg.rb +11 -15
- data/test/test_helper.rb +7 -6
- data/test/unit/{belongs_to_test.rb → associations/belongs_to_test.rb} +1 -10
- data/test/unit/associations/has_many_test.rb +13 -0
- data/test/unit/associations/has_one_test.rb +14 -0
- data/test/unit/{belongs_to → associations}/reflection_test.rb +2 -2
- data/test/unit/attribute_methods/definition_test.rb +6 -3
- data/test/unit/attribute_methods/dirty_test.rb +17 -14
- data/test/unit/attribute_methods/typecasting_test.rb +0 -14
- data/test/unit/base_test.rb +3 -3
- data/test/unit/connection_test.rb +0 -4
- data/test/unit/persistence_test.rb +4 -4
- data/test/unit/schema_test.rb +9 -17
- data/test/unit/scope/query_methods_test.rb +10 -1
- data/test/unit/types/array_type_test.rb +12 -10
- data/test/unit/types/base_type_test.rb +2 -10
- data/test/unit/types/boolean_type_test.rb +15 -13
- data/test/unit/types/date_type_test.rb +3 -3
- data/test/unit/types/float_type_test.rb +14 -7
- data/test/unit/types/integer_type_test.rb +11 -9
- data/test/unit/types/json_type_test.rb +0 -23
- data/test/unit/types/string_type_test.rb +6 -6
- data/test/unit/types/time_type_test.rb +7 -7
- metadata +35 -26
- data/CHANGELOG +0 -0
- data/lib/superstore/adapters/cassandra_adapter.rb +0 -203
- data/lib/superstore/adapters/hstore_adapter.rb +0 -170
- data/lib/superstore/belongs_to/association.rb +0 -65
- data/lib/superstore/belongs_to/builder.rb +0 -40
- data/lib/superstore/belongs_to/reflection.rb +0 -38
- data/lib/superstore/belongs_to.rb +0 -63
- data/lib/superstore/cassandra_schema/statements.rb +0 -52
- data/lib/superstore/cassandra_schema/tasks.rb +0 -47
- data/lib/superstore/cassandra_schema.rb +0 -9
- data/lib/superstore/log_subscriber.rb +0 -44
- data/lib/superstore/railties/controller_runtime.rb +0 -45
- data/lib/superstore/tasks/ks.rake +0 -59
- data/test/support/cassandra.rb +0 -46
- data/test/support/hstore.rb +0 -24
- data/test/support/user.rb +0 -2
- data/test/unit/cassandra_schema/statements_test.rb +0 -47
- data/test/unit/cassandra_schema/tasks_test.rb +0 -31
- data/test/unit/log_subscriber_test.rb +0 -26
- data/test/unit/railties/controller_runtime_test.rb +0 -48
@@ -1,24 +1,26 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class Superstore::Types::BooleanTypeTest < Superstore::Types::TestCase
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
if Superstore::Base.adapter.class.name == 'Superstore::Adapters::CassandraAdapter'
|
5
|
+
test 'encode' do
|
6
|
+
assert_equal '1', type.encode(true)
|
7
|
+
assert_equal '1', type.encode('true')
|
8
|
+
assert_equal '1', type.encode('1')
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
assert_equal '0', type.encode(false)
|
11
|
+
assert_equal '0', type.encode('false')
|
12
|
+
assert_equal '0', type.encode('0')
|
13
|
+
assert_equal '0', type.encode('')
|
13
14
|
|
14
|
-
|
15
|
-
|
15
|
+
assert_raise ArgumentError do
|
16
|
+
type.encode('wtf')
|
17
|
+
end
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
21
|
test 'decode' do
|
20
|
-
assert_equal true,
|
21
|
-
assert_equal false,
|
22
|
-
# assert_nil
|
22
|
+
assert_equal true, type.decode('1')
|
23
|
+
assert_equal false, type.decode('0')
|
24
|
+
# assert_nil type.decode(nil)
|
23
25
|
end
|
24
26
|
end
|
@@ -2,14 +2,14 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
class Superstore::Types::DateTypeTest < Superstore::Types::TestCase
|
4
4
|
test 'encode' do
|
5
|
-
assert_equal '2004-04-25',
|
5
|
+
assert_equal '2004-04-25', type.encode(Date.new(2004, 4, 25))
|
6
6
|
end
|
7
7
|
|
8
8
|
test 'decode' do
|
9
|
-
assert_equal Date.new(2004, 4, 25),
|
9
|
+
assert_equal Date.new(2004, 4, 25), type.decode('2004-04-25')
|
10
10
|
end
|
11
11
|
|
12
12
|
test 'decoding a blank dates' do
|
13
|
-
assert_nil
|
13
|
+
assert_nil type.decode('')
|
14
14
|
end
|
15
15
|
end
|
@@ -1,17 +1,24 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class Superstore::Types::FloatTypeTest < Superstore::Types::TestCase
|
4
|
-
|
5
|
-
|
4
|
+
if Superstore::Base.adapter.class.name == 'Superstore::Adapters::CassandraAdapter'
|
5
|
+
test 'encode' do
|
6
|
+
assert_equal '5.01', type.encode(5.01)
|
6
7
|
|
7
|
-
|
8
|
-
|
8
|
+
assert_raise ArgumentError do
|
9
|
+
type.encode('x')
|
10
|
+
end
|
9
11
|
end
|
10
12
|
end
|
11
13
|
|
12
14
|
test 'decode' do
|
13
|
-
assert_equal 0.0,
|
14
|
-
assert_equal 3.14,
|
15
|
-
assert_equal 5,
|
15
|
+
assert_equal 0.0, type.decode('xyz')
|
16
|
+
assert_equal 3.14, type.decode('3.14')
|
17
|
+
assert_equal 5, type.decode('5')
|
18
|
+
end
|
19
|
+
|
20
|
+
test 'typecast' do
|
21
|
+
assert_equal 1.1, type.typecast('1.1')
|
22
|
+
assert_equal 42.0, type.typecast(42)
|
16
23
|
end
|
17
24
|
end
|
@@ -1,19 +1,21 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class Superstore::Types::IntegerTypeTest < Superstore::Types::TestCase
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
if Superstore::Base.adapter.class.name == 'Superstore::Adapters::CassandraAdapter'
|
5
|
+
test 'encode' do
|
6
|
+
assert_equal '3', type.encode(3)
|
7
|
+
assert_equal '-3', type.encode(-3)
|
7
8
|
|
8
|
-
|
9
|
-
|
9
|
+
assert_raise ArgumentError do
|
10
|
+
type.encode('xx')
|
11
|
+
end
|
10
12
|
end
|
11
13
|
end
|
12
14
|
|
13
15
|
test 'decode' do
|
14
|
-
assert_nil
|
15
|
-
assert_equal 0,
|
16
|
-
assert_equal 3,
|
17
|
-
assert_equal -3,
|
16
|
+
assert_nil type.decode('')
|
17
|
+
assert_equal 0, type.decode('abc')
|
18
|
+
assert_equal 3, type.decode('3')
|
19
|
+
assert_equal -3, type.decode('-3')
|
18
20
|
end
|
19
21
|
end
|
@@ -1,27 +1,4 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class Superstore::Types::JsonTypeTest < Superstore::Types::TestCase
|
4
|
-
test 'encode' do
|
5
|
-
assert_equal({a: 'b'}.to_json, coder.encode(a: 'b'))
|
6
|
-
assert_equal '-3', coder.encode(-3)
|
7
|
-
end
|
8
|
-
|
9
|
-
test 'decode' do
|
10
|
-
assert_equal({'a' => 'b'}, coder.decode({'a' => 'b'}.to_json))
|
11
|
-
end
|
12
|
-
|
13
|
-
test 'encode array' do
|
14
|
-
assert_equal(['a', 'b'].to_json, coder.encode(['a', 'b']))
|
15
|
-
assert_equal '-3', coder.encode(-3)
|
16
|
-
end
|
17
|
-
|
18
|
-
test 'decode array' do
|
19
|
-
assert_equal(['a', 'b'], coder.decode(['a', 'b'].to_json))
|
20
|
-
end
|
21
|
-
|
22
|
-
test 'typecast' do
|
23
|
-
assert_equal({'enabled' => false}, coder.typecast('enabled' => false))
|
24
|
-
assert_equal({'born_at' => "2004-12-24T00:00:00.000Z"}, coder.typecast('born_at' => Time.utc(2004, 12, 24)))
|
25
|
-
assert_equal(["2004-12-24T00:00:00.000Z"], coder.typecast([Time.utc(2004, 12, 24)]))
|
26
|
-
end
|
27
4
|
end
|
@@ -2,29 +2,29 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
class Superstore::Types::StringTypeTest < Superstore::Types::TestCase
|
4
4
|
test 'encode' do
|
5
|
-
assert_equal 'abc',
|
5
|
+
assert_equal 'abc', type.encode('abc')
|
6
6
|
|
7
7
|
assert_raise ArgumentError do
|
8
|
-
|
8
|
+
type.encode(123)
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
test 'encode as utf' do
|
13
13
|
assert_equal(
|
14
14
|
'123'.force_encoding('UTF-8').encoding,
|
15
|
-
|
15
|
+
type.encode('123'.force_encoding('ASCII-8BIT')).encoding
|
16
16
|
)
|
17
17
|
end
|
18
18
|
|
19
19
|
test 'encode frozen as utf' do
|
20
20
|
assert_equal(
|
21
21
|
'123'.force_encoding('UTF-8').encoding,
|
22
|
-
|
22
|
+
type.encode('123'.force_encoding('ASCII-8BIT').freeze).encoding
|
23
23
|
)
|
24
24
|
end
|
25
25
|
|
26
26
|
test 'typecast' do
|
27
|
-
assert_equal '123',
|
28
|
-
assert_equal '123',
|
27
|
+
assert_equal '123', type.typecast(123)
|
28
|
+
assert_equal '123', type.typecast('123')
|
29
29
|
end
|
30
30
|
end
|
@@ -2,20 +2,20 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
class Superstore::Types::TimeTypeTest < Superstore::Types::TestCase
|
4
4
|
test 'encode' do
|
5
|
-
assert_equal '2004-12-24T01:02:03.000000Z',
|
6
|
-
assert_equal '2004-12-24T01:02:03.000000Z',
|
5
|
+
assert_equal '2004-12-24T01:02:03.000000Z', type.encode(Time.utc(2004, 12, 24, 1, 2, 3))
|
6
|
+
assert_equal '2004-12-24T01:02:03.000000Z', type.encode(DateTime.new(2004, 12, 24, 1, 2, 3))
|
7
7
|
assert_raise ArgumentError do
|
8
|
-
|
8
|
+
type.encode 123
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
test 'decode' do
|
13
|
-
assert_nil
|
14
|
-
assert_nil
|
15
|
-
assert_equal Time.utc(2004, 12, 24, 1, 2, 3),
|
13
|
+
assert_nil type.decode(nil)
|
14
|
+
assert_nil type.decode('bad format')
|
15
|
+
assert_equal Time.utc(2004, 12, 24, 1, 2, 3), type.decode('2004-12-24T01:02:03.000000Z')
|
16
16
|
|
17
17
|
Time.use_zone 'Central Time (US & Canada)' do
|
18
|
-
with_zone =
|
18
|
+
with_zone = type.decode('2013-07-18 13:12:46 -0700')
|
19
19
|
assert_equal Time.utc(2013, 07, 18, 20, 12, 46), with_zone
|
20
20
|
assert_equal 'CDT', with_zone.zone
|
21
21
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: superstore
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Koziarski
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-12-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activemodel
|
@@ -39,6 +39,20 @@ dependencies:
|
|
39
39
|
- - ">="
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: oj
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :runtime
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
42
56
|
- !ruby/object:Gem::Dependency
|
43
57
|
name: bundler
|
44
58
|
requirement: !ruby/object:Gem::Requirement
|
@@ -62,7 +76,7 @@ extra_rdoc_files:
|
|
62
76
|
files:
|
63
77
|
- ".gitignore"
|
64
78
|
- ".travis.yml"
|
65
|
-
- CHANGELOG
|
79
|
+
- CHANGELOG.md
|
66
80
|
- Gemfile
|
67
81
|
- LICENSE
|
68
82
|
- MIT-LICENSE
|
@@ -70,40 +84,39 @@ files:
|
|
70
84
|
- Rakefile
|
71
85
|
- lib/superstore.rb
|
72
86
|
- lib/superstore/adapters/abstract_adapter.rb
|
73
|
-
- lib/superstore/adapters/
|
74
|
-
- lib/superstore/
|
87
|
+
- lib/superstore/adapters/jsonb_adapter.rb
|
88
|
+
- lib/superstore/associations.rb
|
89
|
+
- lib/superstore/associations/association.rb
|
90
|
+
- lib/superstore/associations/belongs_to.rb
|
91
|
+
- lib/superstore/associations/builder/association.rb
|
92
|
+
- lib/superstore/associations/builder/belongs_to.rb
|
93
|
+
- lib/superstore/associations/builder/has_many.rb
|
94
|
+
- lib/superstore/associations/builder/has_one.rb
|
95
|
+
- lib/superstore/associations/has_many.rb
|
96
|
+
- lib/superstore/associations/has_one.rb
|
97
|
+
- lib/superstore/associations/reflection.rb
|
75
98
|
- lib/superstore/attribute_methods.rb
|
76
99
|
- lib/superstore/attribute_methods/definition.rb
|
77
100
|
- lib/superstore/attribute_methods/dirty.rb
|
78
101
|
- lib/superstore/attribute_methods/primary_key.rb
|
79
102
|
- lib/superstore/attribute_methods/typecasting.rb
|
80
103
|
- lib/superstore/base.rb
|
81
|
-
- lib/superstore/belongs_to.rb
|
82
|
-
- lib/superstore/belongs_to/association.rb
|
83
|
-
- lib/superstore/belongs_to/builder.rb
|
84
|
-
- lib/superstore/belongs_to/reflection.rb
|
85
104
|
- lib/superstore/caching.rb
|
86
105
|
- lib/superstore/callbacks.rb
|
87
|
-
- lib/superstore/cassandra_schema.rb
|
88
|
-
- lib/superstore/cassandra_schema/statements.rb
|
89
|
-
- lib/superstore/cassandra_schema/tasks.rb
|
90
106
|
- lib/superstore/connection.rb
|
91
107
|
- lib/superstore/core.rb
|
92
108
|
- lib/superstore/errors.rb
|
93
109
|
- lib/superstore/identity.rb
|
94
110
|
- lib/superstore/inspect.rb
|
95
|
-
- lib/superstore/log_subscriber.rb
|
96
111
|
- lib/superstore/model.rb
|
97
112
|
- lib/superstore/persistence.rb
|
98
113
|
- lib/superstore/railtie.rb
|
99
|
-
- lib/superstore/railties/controller_runtime.rb
|
100
114
|
- lib/superstore/schema.rb
|
101
115
|
- lib/superstore/scope.rb
|
102
116
|
- lib/superstore/scope/batches.rb
|
103
117
|
- lib/superstore/scope/finder_methods.rb
|
104
118
|
- lib/superstore/scope/query_methods.rb
|
105
119
|
- lib/superstore/scoping.rb
|
106
|
-
- lib/superstore/tasks/ks.rake
|
107
120
|
- lib/superstore/timestamps.rb
|
108
121
|
- lib/superstore/type.rb
|
109
122
|
- lib/superstore/types.rb
|
@@ -118,33 +131,29 @@ files:
|
|
118
131
|
- lib/superstore/types/time_type.rb
|
119
132
|
- lib/superstore/validations.rb
|
120
133
|
- superstore.gemspec
|
121
|
-
- test/support/
|
122
|
-
- test/support/
|
123
|
-
- test/support/issue.rb
|
134
|
+
- test/support/jsonb.rb
|
135
|
+
- test/support/models.rb
|
124
136
|
- test/support/pg.rb
|
125
|
-
- test/support/user.rb
|
126
137
|
- test/test_helper.rb
|
127
138
|
- test/unit/active_model_test.rb
|
128
139
|
- test/unit/adapters/adapter_test.rb
|
140
|
+
- test/unit/associations/belongs_to_test.rb
|
141
|
+
- test/unit/associations/has_many_test.rb
|
142
|
+
- test/unit/associations/has_one_test.rb
|
143
|
+
- test/unit/associations/reflection_test.rb
|
129
144
|
- test/unit/attribute_methods/definition_test.rb
|
130
145
|
- test/unit/attribute_methods/dirty_test.rb
|
131
146
|
- test/unit/attribute_methods/primary_key_test.rb
|
132
147
|
- test/unit/attribute_methods/typecasting_test.rb
|
133
148
|
- test/unit/attribute_methods_test.rb
|
134
149
|
- test/unit/base_test.rb
|
135
|
-
- test/unit/belongs_to/reflection_test.rb
|
136
|
-
- test/unit/belongs_to_test.rb
|
137
150
|
- test/unit/caching_test.rb
|
138
151
|
- test/unit/callbacks_test.rb
|
139
|
-
- test/unit/cassandra_schema/statements_test.rb
|
140
|
-
- test/unit/cassandra_schema/tasks_test.rb
|
141
152
|
- test/unit/connection_test.rb
|
142
153
|
- test/unit/core_test.rb
|
143
154
|
- test/unit/identity_test.rb
|
144
155
|
- test/unit/inspect_test.rb
|
145
|
-
- test/unit/log_subscriber_test.rb
|
146
156
|
- test/unit/persistence_test.rb
|
147
|
-
- test/unit/railties/controller_runtime_test.rb
|
148
157
|
- test/unit/schema_test.rb
|
149
158
|
- test/unit/scope/batches_test.rb
|
150
159
|
- test/unit/scope/finder_methods_test.rb
|
@@ -181,7 +190,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
181
190
|
version: 1.3.5
|
182
191
|
requirements: []
|
183
192
|
rubyforge_project:
|
184
|
-
rubygems_version: 2.4.
|
193
|
+
rubygems_version: 2.4.8
|
185
194
|
signing_key:
|
186
195
|
specification_version: 4
|
187
196
|
summary: Cassandra ActiveModel
|
data/CHANGELOG
DELETED
File without changes
|
@@ -1,203 +0,0 @@
|
|
1
|
-
gem 'cassandra-cql'
|
2
|
-
require 'cassandra-cql'
|
3
|
-
|
4
|
-
module Superstore
|
5
|
-
module Adapters
|
6
|
-
class CassandraAdapter < AbstractAdapter
|
7
|
-
class QueryBuilder
|
8
|
-
def initialize(adapter, scope)
|
9
|
-
@adapter = adapter
|
10
|
-
@scope = scope
|
11
|
-
end
|
12
|
-
|
13
|
-
def to_query
|
14
|
-
[
|
15
|
-
"SELECT #{select_string} FROM #{@scope.klass.table_name}",
|
16
|
-
@adapter.write_option_string,
|
17
|
-
where_string,
|
18
|
-
limit_string
|
19
|
-
].delete_if(&:blank?) * ' '
|
20
|
-
end
|
21
|
-
|
22
|
-
def select_string
|
23
|
-
if @scope.select_values.any?
|
24
|
-
(['KEY'] | @scope.select_values) * ','
|
25
|
-
else
|
26
|
-
'*'
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def where_string
|
31
|
-
wheres = @scope.where_values.dup
|
32
|
-
if @scope.id_values.any?
|
33
|
-
wheres << @adapter.create_ids_where_clause(@scope.id_values)
|
34
|
-
end
|
35
|
-
|
36
|
-
if wheres.any?
|
37
|
-
"WHERE #{wheres * ' AND '}"
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def limit_string
|
42
|
-
if @scope.limit_value
|
43
|
-
"LIMIT #{@scope.limit_value}"
|
44
|
-
else
|
45
|
-
""
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def primary_key_column
|
51
|
-
'KEY'
|
52
|
-
end
|
53
|
-
|
54
|
-
def connection
|
55
|
-
@connection ||= begin
|
56
|
-
thrift_options = (config[:thrift] || {})
|
57
|
-
CassandraCQL::Database.new(
|
58
|
-
servers,
|
59
|
-
{keyspace: config[:keyspace], username: username, password: password, cql_version: '2.0.0'},
|
60
|
-
thrift_options)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
def servers
|
65
|
-
@servers ||= begin
|
66
|
-
if config[:servers].is_a?(String)
|
67
|
-
config[:servers].split(',')
|
68
|
-
elsif config[:servers].is_a?(Array)
|
69
|
-
config[:servers]
|
70
|
-
else
|
71
|
-
"127.0.0.1:9160"
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
def execute(statement)
|
77
|
-
ActiveSupport::Notifications.instrument("cql.cassandra_object", cql: statement) do
|
78
|
-
connection.execute statement
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
def select(scope)
|
83
|
-
statement = QueryBuilder.new(self, scope).to_query
|
84
|
-
|
85
|
-
execute(statement).fetch do |cql_row|
|
86
|
-
attributes = cql_row.to_hash
|
87
|
-
key = attributes.delete(primary_key_column)
|
88
|
-
yield(key, attributes) unless attributes.empty?
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
def insert(table, id, attributes)
|
93
|
-
write(table, id, attributes)
|
94
|
-
end
|
95
|
-
|
96
|
-
def update(table, id, attributes)
|
97
|
-
write(table, id, attributes)
|
98
|
-
end
|
99
|
-
|
100
|
-
def write(table, id, attributes)
|
101
|
-
if (not_nil_attributes = attributes.reject { |key, value| value.nil? }).any?
|
102
|
-
insert_attributes = {primary_key_column => id}.update(not_nil_attributes)
|
103
|
-
statement = "INSERT INTO #{table} (#{quote_columns(insert_attributes.keys) * ','}) VALUES (#{Array.new(insert_attributes.size, '?') * ','})#{write_option_string}"
|
104
|
-
execute_batchable sanitize(statement, *insert_attributes.values)
|
105
|
-
end
|
106
|
-
|
107
|
-
if (nil_attributes = attributes.select { |key, value| value.nil? }).any?
|
108
|
-
execute_batchable sanitize("DELETE #{quote_columns(nil_attributes.keys) * ','} FROM #{table}#{write_option_string} WHERE #{primary_key_column} = ?", id)
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
def delete(table, ids)
|
113
|
-
statement = "DELETE FROM #{table}#{write_option_string} WHERE #{create_ids_where_clause(ids)}"
|
114
|
-
|
115
|
-
execute_batchable statement
|
116
|
-
end
|
117
|
-
|
118
|
-
def execute_batch(statements)
|
119
|
-
raise 'No can do' if statements.empty?
|
120
|
-
|
121
|
-
stmt = [
|
122
|
-
"BEGIN BATCH#{write_option_string(true)}",
|
123
|
-
statements * "\n",
|
124
|
-
'APPLY BATCH'
|
125
|
-
] * "\n"
|
126
|
-
|
127
|
-
execute stmt
|
128
|
-
end
|
129
|
-
|
130
|
-
# SCHEMA
|
131
|
-
def create_table(table_name, options = {})
|
132
|
-
stmt = "CREATE COLUMNFAMILY #{table_name} " +
|
133
|
-
"(KEY varchar PRIMARY KEY)"
|
134
|
-
|
135
|
-
schema_execute statement_with_options(stmt, options), config[:keyspace]
|
136
|
-
end
|
137
|
-
|
138
|
-
def drop_table(table_name)
|
139
|
-
schema_execute "DROP TABLE #{table_name}", config[:keyspace]
|
140
|
-
end
|
141
|
-
|
142
|
-
def schema_execute(cql, keyspace)
|
143
|
-
schema_db = CassandraCQL::Database.new(
|
144
|
-
servers,
|
145
|
-
{keyspace: keyspace, username: username, password: password, cql_version: '2.0.0'},
|
146
|
-
{connect_timeout: 30, timeout: 30}
|
147
|
-
)
|
148
|
-
schema_db.execute cql
|
149
|
-
end
|
150
|
-
|
151
|
-
def consistency
|
152
|
-
@consistency ||= config[:consistency]
|
153
|
-
end
|
154
|
-
|
155
|
-
def consistency=(val)
|
156
|
-
@consistency = val
|
157
|
-
end
|
158
|
-
|
159
|
-
def write_option_string(ignore_batching = false)
|
160
|
-
if (ignore_batching || !batching?) && consistency
|
161
|
-
" USING CONSISTENCY #{consistency}"
|
162
|
-
end
|
163
|
-
end
|
164
|
-
|
165
|
-
def statement_with_options(stmt, options)
|
166
|
-
if options.any?
|
167
|
-
with_stmt = options.map do |k,v|
|
168
|
-
"#{k} = #{CassandraCQL::Statement.quote(v)}"
|
169
|
-
end.join(' AND ')
|
170
|
-
|
171
|
-
"#{stmt} WITH #{with_stmt}"
|
172
|
-
else
|
173
|
-
stmt
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
177
|
-
def create_ids_where_clause(ids)
|
178
|
-
ids = ids.first if ids.is_a?(Array) && ids.one?
|
179
|
-
sql = ids.is_a?(Array) ? "#{primary_key_column} IN (?)" : "#{primary_key_column} = ?"
|
180
|
-
sanitize(sql, ids)
|
181
|
-
end
|
182
|
-
|
183
|
-
private
|
184
|
-
|
185
|
-
def username
|
186
|
-
config[:username].presence
|
187
|
-
end
|
188
|
-
|
189
|
-
def password
|
190
|
-
config[:password].presence
|
191
|
-
end
|
192
|
-
|
193
|
-
def sanitize(statement, *bind_vars)
|
194
|
-
CassandraCQL::Statement.sanitize(statement, bind_vars).force_encoding(Encoding::UTF_8)
|
195
|
-
end
|
196
|
-
|
197
|
-
def quote_columns(column_names)
|
198
|
-
column_names.map { |name| "'#{name}'" }
|
199
|
-
end
|
200
|
-
|
201
|
-
end
|
202
|
-
end
|
203
|
-
end
|