datastax_rails 2.0.12 → 2.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Rakefile +5 -5
- data/lib/blankslate.rb +8 -11
- data/lib/cql-rb_extensions.rb +5 -3
- data/lib/datastax_rails/associations/association.rb +93 -101
- data/lib/datastax_rails/associations/association_scope.rb +7 -7
- data/lib/datastax_rails/associations/belongs_to_association.rb +46 -48
- data/lib/datastax_rails/associations/builder/association.rb +32 -31
- data/lib/datastax_rails/associations/builder/belongs_to.rb +19 -20
- data/lib/datastax_rails/associations/builder/collection_association.rb +32 -32
- data/lib/datastax_rails/associations/builder/has_and_belongs_to_many.rb +21 -21
- data/lib/datastax_rails/associations/builder/has_many.rb +39 -40
- data/lib/datastax_rails/associations/builder/has_one.rb +30 -31
- data/lib/datastax_rails/associations/builder/singular_association.rb +31 -33
- data/lib/datastax_rails/associations/collection_association.rb +129 -135
- data/lib/datastax_rails/associations/collection_proxy.rb +21 -21
- data/lib/datastax_rails/associations/has_and_belongs_to_many_association.rb +26 -26
- data/lib/datastax_rails/associations/has_many_association.rb +38 -38
- data/lib/datastax_rails/associations/has_one_association.rb +31 -32
- data/lib/datastax_rails/associations/singular_association.rb +31 -30
- data/lib/datastax_rails/associations.rb +27 -24
- data/lib/datastax_rails/attribute_assignment.rb +17 -17
- data/lib/datastax_rails/attribute_methods/definition.rb +4 -4
- data/lib/datastax_rails/attribute_methods/dirty.rb +34 -33
- data/lib/datastax_rails/attribute_methods/primary_key.rb +3 -8
- data/lib/datastax_rails/attribute_methods/read.rb +10 -12
- data/lib/datastax_rails/attribute_methods/typecasting.rb +36 -35
- data/lib/datastax_rails/attribute_methods/write.rb +5 -6
- data/lib/datastax_rails/attribute_methods.rb +52 -56
- data/lib/datastax_rails/base.rb +122 -125
- data/lib/datastax_rails/callbacks.rb +15 -9
- data/lib/datastax_rails/cassandra_only_model.rb +6 -6
- data/lib/datastax_rails/collection.rb +5 -7
- data/lib/datastax_rails/column.rb +130 -118
- data/lib/datastax_rails/connection/statement_cache.rb +3 -3
- data/lib/datastax_rails/connection.rb +42 -33
- data/lib/datastax_rails/cql/alter_column_family.rb +19 -21
- data/lib/datastax_rails/cql/base.rb +8 -11
- data/lib/datastax_rails/cql/column_family.rb +11 -10
- data/lib/datastax_rails/cql/consistency.rb +2 -2
- data/lib/datastax_rails/cql/create_column_family.rb +15 -15
- data/lib/datastax_rails/cql/create_index.rb +5 -5
- data/lib/datastax_rails/cql/create_keyspace.rb +7 -7
- data/lib/datastax_rails/cql/delete.rb +16 -29
- data/lib/datastax_rails/cql/drop_column_family.rb +2 -2
- data/lib/datastax_rails/cql/drop_index.rb +2 -2
- data/lib/datastax_rails/cql/drop_keyspace.rb +2 -2
- data/lib/datastax_rails/cql/insert.rb +10 -16
- data/lib/datastax_rails/cql/select.rb +21 -33
- data/lib/datastax_rails/cql/truncate.rb +2 -2
- data/lib/datastax_rails/cql/update.rb +16 -24
- data/lib/datastax_rails/cql/use_keyspace.rb +2 -2
- data/lib/datastax_rails/cql.rb +2 -2
- data/lib/datastax_rails/dynamic_model.rb +32 -29
- data/lib/datastax_rails/errors.rb +6 -6
- data/lib/datastax_rails/grouped_collection.rb +3 -3
- data/lib/datastax_rails/inheritance.rb +9 -9
- data/lib/datastax_rails/payload_model.rb +24 -20
- data/lib/datastax_rails/persistence.rb +116 -110
- data/lib/datastax_rails/railtie.rb +7 -7
- data/lib/datastax_rails/reflection.rb +61 -59
- data/lib/datastax_rails/relation/batches.rb +12 -13
- data/lib/datastax_rails/relation/facet_methods.rb +44 -33
- data/lib/datastax_rails/relation/finder_methods.rb +95 -91
- data/lib/datastax_rails/relation/modification_methods.rb +5 -5
- data/lib/datastax_rails/relation/search_methods.rb +102 -102
- data/lib/datastax_rails/relation/spawn_methods.rb +25 -24
- data/lib/datastax_rails/relation/stats_methods.rb +9 -8
- data/lib/datastax_rails/relation.rb +165 -170
- data/lib/datastax_rails/rsolr_client_wrapper.rb +3 -3
- data/lib/datastax_rails/schema/cassandra.rb +44 -43
- data/lib/datastax_rails/schema/migrator.rb +52 -52
- data/lib/datastax_rails/schema/solr.rb +55 -47
- data/lib/datastax_rails/schema_cache.rb +1 -3
- data/lib/datastax_rails/scoping/default.rb +2 -3
- data/lib/datastax_rails/scoping/named.rb +3 -5
- data/lib/datastax_rails/scoping.rb +11 -12
- data/lib/datastax_rails/serialization.rb +34 -31
- data/lib/datastax_rails/serializers/xml_serializer.rb +178 -175
- data/lib/datastax_rails/timestamps.rb +4 -4
- data/lib/datastax_rails/types/dirty_collection.rb +57 -57
- data/lib/datastax_rails/types/dynamic_list.rb +1 -1
- data/lib/datastax_rails/types/dynamic_map.rb +5 -7
- data/lib/datastax_rails/types/dynamic_set.rb +2 -2
- data/lib/datastax_rails/util/solr_repair.rb +3 -3
- data/lib/datastax_rails/validations/associated.rb +8 -6
- data/lib/datastax_rails/validations/uniqueness.rb +8 -8
- data/lib/datastax_rails/validations.rb +9 -10
- data/lib/datastax_rails/version.rb +2 -1
- data/lib/datastax_rails/wide_storage_model.rb +6 -6
- data/lib/datastax_rails.rb +13 -9
- data/lib/schema_migration.rb +3 -3
- data/spec/datastax_rails/associations/belongs_to_association_spec.rb +2 -2
- data/spec/datastax_rails/associations/collection_association_spec.rb +14 -14
- data/spec/datastax_rails/associations/has_many_association_spec.rb +20 -20
- data/spec/datastax_rails/associations_spec.rb +11 -11
- data/spec/datastax_rails/attribute_methods_spec.rb +25 -25
- data/spec/datastax_rails/base_spec.rb +24 -24
- data/spec/datastax_rails/callbacks_spec.rb +21 -21
- data/spec/datastax_rails/column_spec.rb +133 -132
- data/spec/datastax_rails/connection/statement_cache_spec.rb +2 -2
- data/spec/datastax_rails/cql/base_spec.rb +4 -4
- data/spec/datastax_rails/cql/delete_spec.rb +19 -0
- data/spec/datastax_rails/cql/select_spec.rb +8 -8
- data/spec/datastax_rails/cql/update_spec.rb +8 -10
- data/spec/datastax_rails/dynamic_model_spec.rb +36 -22
- data/spec/datastax_rails/inheritance_spec.rb +11 -14
- data/spec/datastax_rails/persistence_spec.rb +73 -74
- data/spec/datastax_rails/relation/batches_spec.rb +13 -13
- data/spec/datastax_rails/relation/facet_methods_spec.rb +43 -35
- data/spec/datastax_rails/relation/finder_methods_spec.rb +77 -78
- data/spec/datastax_rails/relation/modification_methods_spec.rb +19 -19
- data/spec/datastax_rails/relation/search_methods_spec.rb +160 -160
- data/spec/datastax_rails/relation/spawn_methods_spec.rb +18 -18
- data/spec/datastax_rails/relation_spec.rb +119 -116
- data/spec/datastax_rails/schema/migrator_spec.rb +30 -30
- data/spec/datastax_rails/schema/solr_spec.rb +15 -15
- data/spec/datastax_rails/scoping/default_spec.rb +9 -9
- data/spec/datastax_rails/types/dynamic_list_spec.rb +12 -12
- data/spec/datastax_rails/types/dynamic_map_spec.rb +10 -10
- data/spec/datastax_rails/types/dynamic_set_spec.rb +22 -10
- data/spec/datastax_rails/validations/uniqueness_spec.rb +25 -25
- data/spec/datastax_rails/wide_storage_model_spec.rb +11 -0
- data/spec/datastax_rails_spec.rb +2 -2
- data/spec/dummy/config/application.rb +2 -3
- data/spec/dummy/config/boot.rb +1 -1
- data/spec/dummy/config/environments/development.rb +3 -3
- data/spec/dummy/config/environments/test.rb +1 -1
- data/spec/dummy/config/initializers/session_store.rb +1 -1
- data/spec/dummy/config/initializers/wrap_parameters.rb +1 -1
- data/spec/factories/audit_logs.rb +6 -0
- data/spec/factories/hobbies.rb +6 -0
- data/spec/factories/people.rb +5 -0
- data/spec/feature/dynamic_fields_spec.rb +4 -4
- data/spec/feature/overloaded_tables_spec.rb +11 -12
- data/spec/spec_helper.rb +17 -14
- data/spec/support/datastax_test_hook.rb +2 -2
- data/spec/support/default_consistency_shared_examples.rb +11 -11
- data/spec/support/models.rb +31 -32
- metadata +40 -6
- data/lib/datastax_rails/attribute_methods/before_type_cast.rb +0 -71
- data/lib/datastax_rails/log_subscriber.rb +0 -0
- data/spec/dummy/ks/migrate/20111117224534_models.rb +0 -20
@@ -1,12 +1,12 @@
|
|
1
1
|
module DatastaxRails
|
2
2
|
module StatementCache
|
3
3
|
extend ActiveSupport::Concern
|
4
|
-
|
4
|
+
|
5
5
|
included do
|
6
6
|
class_attribute :statement_cache
|
7
7
|
self.statement_cache = {}
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
module ClassMethods
|
11
11
|
def establish_connection(spec)
|
12
12
|
self.statement_cache = {}
|
@@ -14,4 +14,4 @@ module DatastaxRails
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
17
|
-
end
|
17
|
+
end
|
@@ -8,7 +8,7 @@ module DatastaxRails
|
|
8
8
|
# Datastax Exterprise. This includes both the Cassandra and Solr connections.
|
9
9
|
module Connection
|
10
10
|
extend ActiveSupport::Concern
|
11
|
-
|
11
|
+
|
12
12
|
included do
|
13
13
|
include DatastaxRails::StatementCache
|
14
14
|
class_attribute :connection
|
@@ -17,11 +17,11 @@ module DatastaxRails
|
|
17
17
|
|
18
18
|
module ClassMethods
|
19
19
|
DEFAULT_OPTIONS = {
|
20
|
-
:
|
21
|
-
:
|
22
|
-
:
|
20
|
+
servers: '127.0.0.1:9160',
|
21
|
+
thrift: {},
|
22
|
+
cql_version: '3.0.0'
|
23
23
|
}
|
24
|
-
|
24
|
+
|
25
25
|
# Returns the current server that we are talking to. This is useful when you are talking to a
|
26
26
|
# cluster, and we want to know which server specifically we are connected to.
|
27
27
|
#
|
@@ -31,7 +31,7 @@ module DatastaxRails
|
|
31
31
|
def current_server
|
32
32
|
connection.current_connection.instance_variable_get(:@connection).host
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
# Establish a Cassandra connection to DSE. datastax.yml will be read and the current environment's
|
36
36
|
# settings passed to this method.
|
37
37
|
#
|
@@ -42,7 +42,7 @@ module DatastaxRails
|
|
42
42
|
# port: 9042
|
43
43
|
# keyspace: "datastax_rails_production"
|
44
44
|
# strategy_class: "org.apache.cassandra.locator.NetworkTopologyStrategy"
|
45
|
-
# strategy_options: {"DS1": "3", "DS2": "3", "DS3": "3"}
|
45
|
+
# strategy_options: {"DS1": "3", "DS2": "3", "DS3": "3"}
|
46
46
|
# connection_options:
|
47
47
|
# timeout: 10
|
48
48
|
# solr:
|
@@ -58,8 +58,8 @@ module DatastaxRails
|
|
58
58
|
# will automatically connect to all nodes in the cluster or in the datacenter if you are using multiple
|
59
59
|
# datacenters. You can safely just list all nodes in a particular datacenter if you would like.
|
60
60
|
#
|
61
|
-
# The port to connect to, this port will be used for all nodes. Because the `system.peers` table does not contain
|
62
|
-
# listening on, the port must be the same for all nodes.
|
61
|
+
# The port to connect to, this port will be used for all nodes. Because the `system.peers` table does not contain
|
62
|
+
# the port that the nodes are listening on, the port must be the same for all nodes.
|
63
63
|
#
|
64
64
|
# Since we're using the NetworkTopologyStrategy for our locator, it is important that you configure
|
65
65
|
# cassandra-topology.properties. See the DSE documentation at http://www.datastax.com for more
|
@@ -70,20 +70,27 @@ module DatastaxRails
|
|
70
70
|
#
|
71
71
|
# connection_options are the options that are passed to the thrift layer for the connection to
|
72
72
|
# cassandra.
|
73
|
-
# * *retries* - Number of times a request will be retried. Should likely be the number of servers - 1.
|
73
|
+
# * *retries* - Number of times a request will be retried. Should likely be the number of servers - 1.
|
74
|
+
# Defaults to 0.
|
74
75
|
# * *server_retry_period* - Amount of time to wait before retrying a down server. Defaults to 1.
|
75
|
-
# * *server_max_requests* - Number of requests to make to a server before moving to the next one (helps keep load
|
76
|
+
# * *server_max_requests* - Number of requests to make to a server before moving to the next one (helps keep load
|
77
|
+
# balanced). Default to nil which means cycling does not take place.
|
76
78
|
# * *retry_overrides* - Overrides retries option for individual exceptions.
|
77
79
|
# * *connect_timeout* - The connection timeout on the Thrift socket. Defaults to 0.1.
|
78
80
|
# * *timeout* - The timeout for the transport layer. Defaults to 1.
|
79
81
|
# * *timeout_overrides* - Overrides the timeout value for specific methods (advanced).
|
80
|
-
# * *exception_classes* - List of exceptions for which Thrift will automatically retry a new server in the cluster
|
82
|
+
# * *exception_classes* - List of exceptions for which Thrift will automatically retry a new server in the cluster
|
83
|
+
# (up to retry limit).
|
81
84
|
# Defaults to [IOError, Thrift::Exception, Thrift::ApplicationException, Thrift::TransportException].
|
82
|
-
# * *exception_class_overrides* - List of exceptions which will never cause a retry.
|
83
|
-
#
|
85
|
+
# * *exception_class_overrides* - List of exceptions which will never cause a retry.
|
86
|
+
# Defaults to [CassandraCQL::Thrift::InvalidRequestException].
|
87
|
+
# * *wrapped_exception_options* - List of exceptions that will be automatically wrapped in an exception provided
|
88
|
+
# by client class with the same name (advanced).
|
84
89
|
# Defaults to [Thrift::ApplicationException, Thrift::TransportException].
|
85
|
-
# * *raise* - Whether to raise exceptions or default calls that cause an error (advanced). Defaults to true
|
86
|
-
#
|
90
|
+
# * *raise* - Whether to raise exceptions or default calls that cause an error (advanced). Defaults to true
|
91
|
+
# (raise exceptions).
|
92
|
+
# * *defaults* - When raise is false and an error is encountered, these methods are called to default the return
|
93
|
+
# value (advanced). Should be a hash of method names to values.
|
87
94
|
# * *protocol* - The thrift protocol to use (advanced). Defaults to Thrift::BinaryProtocol.
|
88
95
|
# * *protocol_extra_params* - Any extra parameters to send to the protocol (advanced).
|
89
96
|
# * *transport* - The thrift transport to use (advanced). Defaults to Thrift::Socket.
|
@@ -93,41 +100,43 @@ module DatastaxRails
|
|
93
100
|
def establish_connection(spec)
|
94
101
|
DatastaxRails::Base.config = spec.with_indifferent_access
|
95
102
|
spec.reverse_merge!(DEFAULT_OPTIONS)
|
96
|
-
|
97
|
-
|
103
|
+
self.connection = ::Cql::Client.connect(hosts: spec[:servers],
|
104
|
+
keyspace: spec[:keyspace],
|
105
|
+
connection_timeout: spec[:connection_options][:timeout])
|
98
106
|
end
|
99
|
-
|
107
|
+
|
100
108
|
# Returns the base portion of the URL for connecting to SOLR based on the current Cassandra server.
|
101
109
|
#
|
102
110
|
# @return [String] in the form of 'http://localhost:8983/solr'
|
103
111
|
def solr_base_url
|
104
|
-
DatastaxRails::Base.establish_connection unless
|
112
|
+
DatastaxRails::Base.establish_connection unless connection
|
105
113
|
port = DatastaxRails::Base.config[:solr][:port]
|
106
114
|
path = DatastaxRails::Base.config[:solr][:path]
|
107
|
-
protocol = DatastaxRails::Base.config[:solr].
|
108
|
-
"#{protocol}://#{
|
115
|
+
protocol = DatastaxRails::Base.config[:solr].key?(:ssl) && DatastaxRails::Base.config[:solr][:ssl][:use_ssl] ? 'https' : 'http' # rubocop:disable LineLength
|
116
|
+
"#{protocol}://#{current_server}:#{port}#{path}"
|
109
117
|
end
|
110
|
-
|
118
|
+
|
111
119
|
# Wraps and caches a solr connection object
|
112
120
|
#
|
113
121
|
# @params [Boolean] reconnect force a new connection
|
114
|
-
# @return [DatastaxRails::RSolrClientWrapper] a wrapped RSolr connection
|
122
|
+
# @return [DatastaxRails::RSolrClientWrapper] a wrapped RSolr connection
|
115
123
|
def solr_connection(reconnect = false)
|
116
|
-
if
|
124
|
+
if !@rsolr || reconnect
|
117
125
|
@rsolr = DatastaxRails::RSolrClientWrapper.new(establish_solr_connection, self)
|
118
126
|
end
|
119
127
|
@rsolr
|
120
128
|
end
|
121
|
-
|
122
|
-
# Similar to +establish_connection+, this method creates a connection object for Solr.
|
123
|
-
# actually launch the connection, but it gets everything set up so that RSolr can do its work.
|
124
|
-
# unlike the cassandra connection which is global to all of DSR, each model will have its
|
129
|
+
|
130
|
+
# Similar to +establish_connection+, this method creates a connection object for Solr. Since HTTP is stateless,
|
131
|
+
# this doesn't actually launch the connection, but it gets everything set up so that RSolr can do its work. It's
|
132
|
+
# important to note that unlike the cassandra connection which is global to all of DSR, each model will have its
|
133
|
+
# own solr_connection.
|
125
134
|
#
|
126
135
|
# @return [RSolr::Client] RSolr client object
|
127
136
|
def establish_solr_connection
|
128
|
-
opts = {:
|
129
|
-
if DatastaxRails::Base.config[:solr].
|
130
|
-
DatastaxRails::Base.config[:solr][:ssl].
|
137
|
+
opts = { url: "#{solr_base_url}/#{DatastaxRails::Base.connection.keyspace}.#{column_family}" }
|
138
|
+
if DatastaxRails::Base.config[:solr].key?(:ssl) &&
|
139
|
+
DatastaxRails::Base.config[:solr][:ssl].key?(:cert) &&
|
131
140
|
DatastaxRails::Base.config[:solr][:ssl][:use_ssl]
|
132
141
|
cert = Pathname.new(DatastaxRails::Base.config[:solr][:ssl][:cert])
|
133
142
|
key = Pathname.new(DatastaxRails::Base.config[:solr][:ssl][:key])
|
@@ -137,7 +146,7 @@ module DatastaxRails
|
|
137
146
|
opts[:ssl_cert_file] = cert.to_s
|
138
147
|
opts[:ssl_key_file] = key.to_s
|
139
148
|
opts[:ssl_key_pass] = pass if pass
|
140
|
-
|
149
|
+
|
141
150
|
RSolr::ClientCert.connect opts
|
142
151
|
else
|
143
152
|
RSolr.connect opts
|
@@ -1,56 +1,54 @@
|
|
1
|
-
module DatastaxRails#:nodoc:
|
1
|
+
module DatastaxRails #:nodoc:
|
2
2
|
module Cql #:nodoc:
|
3
3
|
class AlterColumnFamily < Base #:nodoc:
|
4
4
|
def initialize(cf_name)
|
5
5
|
@cf_name = cf_name
|
6
6
|
@action = nil
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
def add(column)
|
10
10
|
set_column(column)
|
11
|
-
@action = 'ADD'
|
11
|
+
@action = 'ADD'
|
12
12
|
self
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
def drop(column)
|
16
16
|
set_column(column)
|
17
17
|
@action = 'DROP'
|
18
|
-
self
|
18
|
+
self
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
def alter(column)
|
22
22
|
set_column(column)
|
23
23
|
@action = 'ALTER'
|
24
|
-
self
|
24
|
+
self
|
25
25
|
end
|
26
|
-
|
27
|
-
def rename(col1,col2)
|
28
|
-
set_column([col1,col2])
|
26
|
+
|
27
|
+
def rename(col1, col2)
|
28
|
+
set_column([col1, col2])
|
29
29
|
@action = 'RENAME'
|
30
30
|
self
|
31
31
|
end
|
32
32
|
|
33
|
-
def set_column(column)
|
34
|
-
if
|
35
|
-
raise ArgumentError, "Only one operation allowed per CQL call"
|
36
|
-
end
|
33
|
+
def set_column(column) # rubocop:disable Style/AccessorMethodName
|
34
|
+
fail ArgumentError, 'Only one operation allowed per CQL call' if @action
|
37
35
|
@column = column
|
38
36
|
end
|
39
|
-
|
37
|
+
|
40
38
|
def to_cql
|
41
39
|
stmt = "ALTER COLUMNFAMILY #{@cf_name} "
|
42
|
-
if(@action == 'ALTER')
|
40
|
+
if (@action == 'ALTER')
|
43
41
|
stmt << "ALTER #{@column.keys.first} TYPE #{@column.values.first}"
|
44
|
-
elsif(@action == 'ADD')
|
42
|
+
elsif (@action == 'ADD')
|
45
43
|
stmt << "ADD #{@column.keys.first} #{@column.values.first}"
|
46
|
-
elsif(@action == 'DROP')
|
44
|
+
elsif (@action == 'DROP')
|
47
45
|
stmt << "DROP #{@column}"
|
48
|
-
elsif(@action == 'RENAME')
|
46
|
+
elsif (@action == 'RENAME')
|
49
47
|
stmt << "RENAME \"#{@column[0]}\" TO \"#{@column[1]}\""
|
50
48
|
end
|
51
|
-
|
49
|
+
|
52
50
|
stmt
|
53
51
|
end
|
54
52
|
end
|
55
53
|
end
|
56
|
-
end
|
54
|
+
end
|
@@ -1,42 +1,39 @@
|
|
1
1
|
module DatastaxRails
|
2
2
|
module Cql
|
3
|
+
# Base class for CQL generation
|
3
4
|
class Base
|
4
5
|
# Base initialize that sets the default consistency.
|
5
|
-
def initialize(klass, *
|
6
|
+
def initialize(klass, *_args)
|
6
7
|
@consistency = klass.default_consistency.to_s.downcase.to_sym
|
7
8
|
@keyspace = DatastaxRails::Base.config[:keyspace]
|
8
9
|
@values = []
|
9
10
|
end
|
10
|
-
|
11
|
+
|
11
12
|
def using(consistency)
|
12
13
|
@consistency = consistency.to_s.downcase.to_sym
|
13
14
|
self
|
14
15
|
end
|
15
|
-
|
16
|
-
def key_name
|
17
|
-
@klass.key_factory.key_columns
|
18
|
-
end
|
19
16
|
|
20
17
|
# Abstract. Should be overridden by subclasses
|
21
18
|
def to_cql
|
22
|
-
|
19
|
+
fail NotImplementedError
|
23
20
|
end
|
24
|
-
|
21
|
+
|
25
22
|
# Generates the CQL and calls Cassandra to execute it.
|
26
23
|
# If you are using this outside of Rails, then DatastaxRails::Base.connection must have
|
27
24
|
# already been set up (Rails does this for you).
|
28
25
|
def execute
|
29
|
-
cql =
|
26
|
+
cql = to_cql
|
30
27
|
puts cql if ENV['DEBUG_CQL'] == 'true'
|
31
28
|
pp @values if ENV['DEBUG_CQL'] == 'true'
|
32
29
|
digest = Digest::MD5.digest cql
|
33
30
|
stmt = DatastaxRails::Base.statement_cache[digest] ||= DatastaxRails::Base.connection.prepare(cql)
|
34
31
|
if @consistency
|
35
|
-
stmt.execute(*@values, :
|
32
|
+
stmt.execute(*@values, consistency: @consistency)
|
36
33
|
else
|
37
34
|
stmt.execute(*@values)
|
38
35
|
end
|
39
36
|
end
|
40
37
|
end
|
41
38
|
end
|
42
|
-
end
|
39
|
+
end
|
@@ -1,38 +1,39 @@
|
|
1
1
|
module DatastaxRails
|
2
2
|
module Cql
|
3
|
+
# Helper class to create CQL-building objects
|
3
4
|
class ColumnFamily
|
4
5
|
def initialize(klass)
|
5
6
|
@klass = klass
|
6
7
|
end
|
7
|
-
|
8
|
+
|
8
9
|
def create_column_family
|
9
10
|
DatastaxRails::Cql::CreateColumnFamily.new(@klass.column_family)
|
10
11
|
end
|
11
|
-
|
12
|
-
def delete(
|
13
|
-
DatastaxRails::Cql::Delete.new(@klass,
|
12
|
+
|
13
|
+
def delete(key)
|
14
|
+
DatastaxRails::Cql::Delete.new(@klass, key)
|
14
15
|
end
|
15
|
-
|
16
|
+
|
16
17
|
def insert
|
17
18
|
DatastaxRails::Cql::Insert.new(@klass)
|
18
19
|
end
|
19
|
-
|
20
|
+
|
20
21
|
def drop_column_family
|
21
22
|
DatastaxRails::Cql::DropColumnFamily.new(@klass.column_family)
|
22
23
|
end
|
23
|
-
|
24
|
+
|
24
25
|
def select(*columns)
|
25
|
-
columns <<
|
26
|
+
columns << '*' if columns.empty?
|
26
27
|
DatastaxRails::Cql::Select.new(@klass, columns.flatten)
|
27
28
|
end
|
28
29
|
|
29
30
|
def truncate
|
30
31
|
DatastaxRails::Cql::Truncate.new(@klass)
|
31
32
|
end
|
32
|
-
|
33
|
+
|
33
34
|
def update(key)
|
34
35
|
DatastaxRails::Cql::Update.new(@klass, key)
|
35
36
|
end
|
36
37
|
end
|
37
38
|
end
|
38
|
-
end
|
39
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module DatastaxRails#:nodoc:
|
1
|
+
module DatastaxRails #:nodoc:
|
2
2
|
module Cql #:nodoc:
|
3
3
|
class CreateColumnFamily < Base #:nodoc:
|
4
4
|
def initialize(cf_name)
|
@@ -12,43 +12,43 @@ module DatastaxRails#:nodoc:
|
|
12
12
|
@primary_key = pk
|
13
13
|
self
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
def with(with)
|
17
17
|
@storage_parameters << with
|
18
18
|
self
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
def columns(columns)
|
22
22
|
@columns.merge! columns
|
23
23
|
self
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
# Migration helpers
|
27
27
|
def comment=(comment)
|
28
|
-
with(
|
28
|
+
with('comment' => comment)
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
def comparator=(comp)
|
32
|
-
with(
|
32
|
+
with('comparator' => comp)
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
def default_validation=(val)
|
36
|
-
with(
|
36
|
+
with('default_validation' => val)
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
def to_cql
|
40
40
|
stmt = "CREATE COLUMNFAMILY #{@cf_name} ("
|
41
|
-
@columns.each do |name,type|
|
41
|
+
@columns.each do |name, type|
|
42
42
|
stmt << "#{name} #{type}, "
|
43
43
|
end
|
44
44
|
stmt << "PRIMARY KEY (#{@primary_key}))"
|
45
45
|
unless @storage_parameters.empty?
|
46
|
-
stmt <<
|
47
|
-
stmt << @storage_parameters.flatten.join(
|
46
|
+
stmt << ' WITH '
|
47
|
+
stmt << @storage_parameters.flatten.join(' AND ')
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
stmt
|
51
51
|
end
|
52
52
|
end
|
53
53
|
end
|
54
|
-
end
|
54
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module DatastaxRails#:nodoc:
|
1
|
+
module DatastaxRails #:nodoc:
|
2
2
|
module Cql #:nodoc:
|
3
3
|
class CreateIndex < Base #:nodoc:
|
4
4
|
def initialize(index_name = nil)
|
@@ -6,20 +6,20 @@ module DatastaxRails#:nodoc:
|
|
6
6
|
@column = nil
|
7
7
|
@index_name = index_name
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
def on(cf_name)
|
11
11
|
@cf_name = cf_name
|
12
12
|
self
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
def column(column)
|
16
16
|
@column = column
|
17
17
|
self
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
def to_cql
|
21
21
|
"CREATE INDEX #{@index_name} ON #{@cf_name} (#{@column})"
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
25
|
-
end
|
25
|
+
end
|