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,3 +1,5 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
1
3
|
module DatastaxRails
|
2
4
|
# = Datastax Rails Callbacks
|
3
5
|
#
|
@@ -202,7 +204,8 @@ module DatastaxRails
|
|
202
204
|
# == Ordering callbacks
|
203
205
|
#
|
204
206
|
# Sometimes the code needs that the callbacks execute in a specific order. For example, a +before_destroy+
|
205
|
-
# callback (+log_children+ in this case) should be executed before the children get destroyed by the
|
207
|
+
# callback (+log_children+ in this case) should be executed before the children get destroyed by the
|
208
|
+
# +dependent: destroy+ option.
|
206
209
|
#
|
207
210
|
# Let's look at the code below:
|
208
211
|
#
|
@@ -218,7 +221,8 @@ module DatastaxRails
|
|
218
221
|
# end
|
219
222
|
#
|
220
223
|
# In this case, the problem is that when the +before_destroy+ callback is executed, the children are not available
|
221
|
-
# because the +destroy+ callback gets executed first. You can use the +prepend+ option on the +before_destroy+
|
224
|
+
# because the +destroy+ callback gets executed first. You can use the +prepend+ option on the +before_destroy+
|
225
|
+
# callback to avoid this.
|
222
226
|
#
|
223
227
|
# class Topic < DatastaxRails::Base
|
224
228
|
# has_many :children, dependent: destroy
|
@@ -231,13 +235,14 @@ module DatastaxRails
|
|
231
235
|
# end
|
232
236
|
# end
|
233
237
|
#
|
234
|
-
# This way, the +before_destroy+ gets executed before the <tt>dependent: destroy</tt> is called, and the data is
|
238
|
+
# This way, the +before_destroy+ gets executed before the <tt>dependent: destroy</tt> is called, and the data is
|
239
|
+
# still available.
|
235
240
|
#
|
236
241
|
# == Debugging callbacks
|
237
242
|
#
|
238
243
|
# The callback chain is accessible via the <tt>_*_callbacks</tt> method on an object. ActiveModel Callbacks support
|
239
|
-
# <tt>:before</tt>, <tt>:after</tt> and <tt>:around</tt> as values for the <tt>kind</tt> property. The <tt>kind</tt>
|
240
|
-
# defines what part of the chain the callback runs in.
|
244
|
+
# <tt>:before</tt>, <tt>:after</tt> and <tt>:around</tt> as values for the <tt>kind</tt> property. The <tt>kind</tt>
|
245
|
+
# property defines what part of the chain the callback runs in.
|
241
246
|
#
|
242
247
|
# To find all callbacks in the before_save callback chain:
|
243
248
|
#
|
@@ -245,7 +250,8 @@ module DatastaxRails
|
|
245
250
|
#
|
246
251
|
# Returns an array of callback objects that form the before_save chain.
|
247
252
|
#
|
248
|
-
# To further check if the before_save chain contains a proc defined as <tt>rest_when_dead</tt> use the
|
253
|
+
# To further check if the before_save chain contains a proc defined as <tt>rest_when_dead</tt> use the
|
254
|
+
# <tt>filter</tt> property of the callback object:
|
249
255
|
#
|
250
256
|
# Topic._save_callbacks.select { |cb| cb.kind.eql?(:before) }.collect(&:filter).include?(:rest_when_dead)
|
251
257
|
#
|
@@ -268,7 +274,7 @@ module DatastaxRails
|
|
268
274
|
included do
|
269
275
|
include ActiveModel::Validations::Callbacks
|
270
276
|
|
271
|
-
define_model_callbacks :initialize, :find, :touch, :
|
277
|
+
define_model_callbacks :initialize, :find, :touch, only: :after
|
272
278
|
define_model_callbacks :save, :create, :update, :destroy
|
273
279
|
end
|
274
280
|
|
@@ -277,10 +283,10 @@ module DatastaxRails
|
|
277
283
|
end
|
278
284
|
|
279
285
|
# def touch(*) #:nodoc:
|
280
|
-
|
286
|
+
# run_callbacks(:touch) { super }
|
281
287
|
# end
|
282
288
|
|
283
|
-
|
289
|
+
private
|
284
290
|
|
285
291
|
def _create_or_update(*) #:nodoc:
|
286
292
|
run_callbacks(:save) { super }
|
@@ -17,17 +17,17 @@ module DatastaxRails
|
|
17
17
|
extend ActiveSupport::Concern
|
18
18
|
|
19
19
|
included do
|
20
|
-
default_scope -> {with_cassandra}
|
20
|
+
default_scope -> { with_cassandra }
|
21
21
|
self.storage_method = :cql
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
module ClassMethods
|
25
25
|
def attribute(name, options)
|
26
|
-
opts = options.update(:
|
27
|
-
:
|
28
|
-
:
|
26
|
+
opts = options.update(solr_index: false, solr_store: false,
|
27
|
+
multi_valued: false, sortable: false,
|
28
|
+
tokenized: false, fulltext: false)
|
29
29
|
super(name, opts)
|
30
30
|
end
|
31
31
|
end
|
32
32
|
end
|
33
|
-
end
|
33
|
+
end
|
@@ -2,23 +2,21 @@ module DatastaxRails
|
|
2
2
|
class Collection < Array
|
3
3
|
# @!attribute [r] total_entries
|
4
4
|
# @return [Fixnum] the total number of entries that match the search
|
5
|
-
# @!attribute [r] last_column_name
|
6
|
-
# @return [Fixnum] the last column that was returned in the search in case you limited the number of columns (not supported)
|
7
5
|
# @!attribute [r] per_page
|
8
6
|
# @return [Fixnum] the per page value of the search that produced these results (used by will_paginate)
|
9
7
|
# @!attribute [r] current_page
|
10
8
|
# @return [Fixnum] the current page of the search that produced these results (used by will_paginate)
|
11
9
|
# @!attribute [r] facets
|
12
|
-
# @return [Hash] the facet
|
13
|
-
attr_accessor :
|
14
|
-
|
10
|
+
# @return [Hash] the facet results (field and/or range)
|
11
|
+
attr_accessor :total_entries, :per_page, :current_page, :facets, :highlights
|
12
|
+
|
15
13
|
def inspect
|
16
14
|
"<DatastaxRails::Collection##{object_id} contents: #{super} last_column_name: #{last_column_name.inspect}>"
|
17
15
|
end
|
18
|
-
|
16
|
+
|
19
17
|
def total_pages
|
20
18
|
return 1 unless per_page
|
21
19
|
(total_entries / per_page.to_f).ceil
|
22
20
|
end
|
23
21
|
end
|
24
|
-
end
|
22
|
+
end
|
@@ -1,20 +1,22 @@
|
|
1
1
|
require 'set'
|
2
2
|
|
3
|
+
# TODO: Break this up into manageable pieces
|
4
|
+
# rubocop:disable Style/RescueModifier
|
3
5
|
module DatastaxRails
|
4
|
-
class Column
|
6
|
+
class Column # rubocop:disable Style/ClassLength
|
5
7
|
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON'].to_set
|
6
8
|
FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF'].to_set
|
7
|
-
|
9
|
+
|
8
10
|
module Format
|
9
11
|
ISO_DATE = /\A(\d{4})-(\d\d)-(\d\d)\z/
|
10
12
|
ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?\z/
|
11
|
-
SOLR_TIME_FORMAT =
|
13
|
+
SOLR_TIME_FORMAT = '%Y-%m-%dT%H:%M:%SZ'.force_encoding('utf-8').freeze
|
12
14
|
end
|
13
15
|
|
14
16
|
attr_reader :name, :type, :cql_type, :solr_type, :options
|
15
17
|
attr_accessor :primary, :coder, :default
|
16
18
|
|
17
|
-
|
19
|
+
alias_method :encoded?, :coder
|
18
20
|
|
19
21
|
# Instantiates a new column in the table.
|
20
22
|
#
|
@@ -30,7 +32,7 @@ module DatastaxRails
|
|
30
32
|
def initialize(name, default, type, options = {})
|
31
33
|
@name = name
|
32
34
|
@type = type.to_sym
|
33
|
-
|
35
|
+
fail ArgumentError, "Unknown type #{type}" unless klass
|
34
36
|
options[:holds] = 'string' if collection? && options[:holds].blank?
|
35
37
|
@options = configure_options(@type, options).with_indifferent_access
|
36
38
|
@cql_type = compute_cql_type(@type, @options)
|
@@ -39,36 +41,36 @@ module DatastaxRails
|
|
39
41
|
@primary = nil
|
40
42
|
@coder = nil
|
41
43
|
end
|
42
|
-
|
44
|
+
|
43
45
|
def configure_options(type, options)
|
44
46
|
case type.to_sym
|
45
47
|
when :set, :list, :map then
|
46
|
-
configure_options(options[:holds], options).merge(:
|
48
|
+
configure_options(options[:holds], options).merge(multi_valued: true)
|
47
49
|
when :binary then
|
48
|
-
{:
|
49
|
-
|
50
|
-
|
51
|
-
|
50
|
+
{ solr_index: false, solr_store: false,
|
51
|
+
multi_valued: false, sortable: false,
|
52
|
+
tokenized: false, fulltext: false,
|
53
|
+
cql_index: false }
|
52
54
|
when :boolean, :date, :time, :timestamp, :datetime, :float, :integer, :uuid then
|
53
|
-
{:
|
54
|
-
|
55
|
-
|
56
|
-
|
55
|
+
{ solr_index: true, solr_store: true,
|
56
|
+
multi_valued: false, sortable: true,
|
57
|
+
tokenized: false, fulltext: false,
|
58
|
+
cql_index: false }
|
57
59
|
when :string then
|
58
|
-
{:
|
59
|
-
|
60
|
-
|
61
|
-
|
60
|
+
{ solr_index: true, solr_store: true,
|
61
|
+
multi_valued: false, sortable: true,
|
62
|
+
tokenized: false, fulltext: true,
|
63
|
+
cql_index: false }
|
62
64
|
when :text then
|
63
|
-
{:
|
64
|
-
|
65
|
-
|
66
|
-
|
65
|
+
{ solr_index: true, solr_store: true,
|
66
|
+
multi_valued: false, sortable: false,
|
67
|
+
tokenized: true, fulltext: true,
|
68
|
+
cql_index: false }
|
67
69
|
else
|
68
|
-
|
70
|
+
fail ArgumentError, "Unknown Type: #{type}"
|
69
71
|
end.merge(options)
|
70
72
|
end
|
71
|
-
|
73
|
+
|
72
74
|
# Returns +true+ if the column is either of type ascii or text.
|
73
75
|
def text?
|
74
76
|
[:ascii, :text].include?(type)
|
@@ -78,18 +80,18 @@ module DatastaxRails
|
|
78
80
|
def number?
|
79
81
|
[:decimal, :double, :float, :integer].include?(type)
|
80
82
|
end
|
81
|
-
|
83
|
+
|
82
84
|
# Returns +true+ if the column is of type binary
|
83
85
|
def binary?
|
84
86
|
[:binary].include?(type)
|
85
87
|
end
|
86
|
-
|
88
|
+
|
87
89
|
def collection?
|
88
90
|
[:set, :list, :map].include?(type)
|
89
91
|
end
|
90
92
|
|
91
|
-
def
|
92
|
-
|
93
|
+
def default?
|
94
|
+
default.present?
|
93
95
|
end
|
94
96
|
|
95
97
|
# Returns the Ruby class that corresponds to the abstract data type.
|
@@ -110,7 +112,7 @@ module DatastaxRails
|
|
110
112
|
end
|
111
113
|
|
112
114
|
# Casts value (which can be a String) to an appropriate instance.
|
113
|
-
def type_cast(value, record = nil, dest_type = nil)
|
115
|
+
def type_cast(value, record = nil, dest_type = nil) # rubocop:disable Style/CyclomaticComplexity
|
114
116
|
return nil if value.nil?
|
115
117
|
return coder.load(value) if encoded?
|
116
118
|
|
@@ -128,23 +130,26 @@ module DatastaxRails
|
|
128
130
|
when :binary then klass.binary_to_string(value)
|
129
131
|
when :boolean then klass.value_to_boolean(value)
|
130
132
|
when :uuid, :timeuuid then klass.value_to_uuid(value)
|
131
|
-
when :list, :set then wrap_collection(value.
|
132
|
-
when :map
|
133
|
+
when :list, :set then wrap_collection(value.map { |v| type_cast(v, record, @options[:holds]) }, record)
|
134
|
+
when :map
|
135
|
+
wrap_collection(value.each { |k, v| value[k] = type_cast(v, record, @options[:holds]) }.stringify_keys, record)
|
133
136
|
else value
|
134
137
|
end
|
135
138
|
end
|
136
|
-
|
139
|
+
|
137
140
|
def wrap_collection(collection, record)
|
138
|
-
|
141
|
+
Types::DirtyCollection.ignore_modifications do
|
142
|
+
klass.new(record, name, collection)
|
143
|
+
end
|
139
144
|
end
|
140
|
-
|
145
|
+
|
141
146
|
# Cql-rb does a really good job of typecasting, so for the most part we
|
142
147
|
# just pass in the native types. The only exceptions are for UUIDs that
|
143
148
|
# are passed in as strings and dates.
|
144
149
|
def type_cast_for_cql3(value, dest_type = nil)
|
145
150
|
return nil if value.nil?
|
146
151
|
return coder.dump(value) if encoded?
|
147
|
-
|
152
|
+
|
148
153
|
case (dest_type || type)
|
149
154
|
when :uuid then value.is_a?(::Cql::Uuid) ? value : self.class.value_to_uuid(value)
|
150
155
|
when :time, :datetime, :timestamp then value.to_time.utc
|
@@ -154,37 +159,37 @@ module DatastaxRails
|
|
154
159
|
else value
|
155
160
|
end
|
156
161
|
end
|
157
|
-
|
162
|
+
|
158
163
|
# By contrast, since Solr isn't doing things like prepared statements
|
159
164
|
# it doesn't know what the types are so we have to handle any casting
|
160
165
|
# or encoding ourselves.
|
161
166
|
def type_cast_for_solr(value, dest_type = nil)
|
162
167
|
return nil if value.nil?
|
163
168
|
return coder.dump(value) if encoded?
|
164
|
-
|
169
|
+
|
165
170
|
case (dest_type || type)
|
166
171
|
when :boolean then value ? 'true' : 'false'
|
167
172
|
when :date, :time, :datetime, :timestamp then value.to_time.utc.strftime(Format::SOLR_TIME_FORMAT)
|
168
|
-
when :list, :set then
|
169
|
-
when :map then
|
173
|
+
when :list, :set then list_to_solr_value(value)
|
174
|
+
when :map then map_to_solr_value(value)
|
170
175
|
else value
|
171
176
|
end
|
172
177
|
end
|
173
|
-
|
178
|
+
|
174
179
|
def list_to_solr_value(value)
|
175
|
-
value.map {|v| type_cast_for_solr(v, @options[:holds].to_sym)}
|
180
|
+
value.map { |v| type_cast_for_solr(v, @options[:holds].to_sym) }
|
176
181
|
end
|
177
|
-
|
182
|
+
|
178
183
|
def map_to_solr_value(value)
|
179
|
-
value.each {|k,v| value[k] = type_cast_for_solr(v, @options[:holds].to_sym)}
|
184
|
+
value.each { |k, v| value[k] = type_cast_for_solr(v, @options[:holds].to_sym) }
|
180
185
|
end
|
181
|
-
|
186
|
+
|
182
187
|
def list_to_cql3_value(value)
|
183
|
-
value.map {|v| type_cast_for_cql3(v, @options[:holds].to_sym)}
|
188
|
+
value.map { |v| type_cast_for_cql3(v, @options[:holds].to_sym) }
|
184
189
|
end
|
185
|
-
|
190
|
+
|
186
191
|
def map_to_cql3_value(value)
|
187
|
-
value.dup.each {|k,v| value[k] = type_cast_for_cql3(v, @options[:holds].to_sym)}
|
192
|
+
value.dup.each { |k, v| value[k] = type_cast_for_cql3(v, @options[:holds].to_sym) }
|
188
193
|
value
|
189
194
|
end
|
190
195
|
|
@@ -198,9 +203,9 @@ module DatastaxRails
|
|
198
203
|
|
199
204
|
def extract_default(default)
|
200
205
|
case type
|
201
|
-
when :map then {}#lambda {|rec| DatastaxRails::Types::DynamicMap.new(rec, self.name.to_s, {})}
|
202
|
-
when :list then []#lambda {|rec| DatastaxRails::Types::DynamicList.new(rec, self.name.to_s, [])}
|
203
|
-
when :set then Set.new#lambda {|set| DatastaxRails::Types::DynamicSet.new(rec, self.name.to_s, Set.new)}
|
206
|
+
when :map then {} # lambda {|rec| DatastaxRails::Types::DynamicMap.new(rec, self.name.to_s, {})}
|
207
|
+
when :list then [] # lambda {|rec| DatastaxRails::Types::DynamicList.new(rec, self.name.to_s, [])}
|
208
|
+
when :set then Set.new # lambda {|set| DatastaxRails::Types::DynamicSet.new(rec, self.name.to_s, Set.new)}
|
204
209
|
else default
|
205
210
|
end
|
206
211
|
end
|
@@ -209,9 +214,9 @@ module DatastaxRails
|
|
209
214
|
def string_to_binary(value)
|
210
215
|
self.class.string_to_binary(value)
|
211
216
|
end
|
212
|
-
|
217
|
+
|
213
218
|
def full_solr_range
|
214
|
-
if %w
|
219
|
+
if %w(date uuid).include? solr_type
|
215
220
|
'[* TO *]'
|
216
221
|
else
|
217
222
|
'[\"\" TO *]'
|
@@ -248,7 +253,7 @@ module DatastaxRails
|
|
248
253
|
|
249
254
|
fast_string_to_time(string) || fallback_string_to_time(string)
|
250
255
|
end
|
251
|
-
|
256
|
+
|
252
257
|
def string_to_dummy_time(string)
|
253
258
|
return string unless string.is_a?(String)
|
254
259
|
return nil if string.empty?
|
@@ -294,7 +299,7 @@ module DatastaxRails
|
|
294
299
|
value.to_s.to_d
|
295
300
|
end
|
296
301
|
end
|
297
|
-
|
302
|
+
|
298
303
|
# convert something to a TimeUuid
|
299
304
|
def value_to_uuid(value)
|
300
305
|
if value.is_a?(::Cql::Uuid)
|
@@ -303,84 +308,91 @@ module DatastaxRails
|
|
303
308
|
::Cql::TimeUuid.new(value) rescue nil
|
304
309
|
end
|
305
310
|
end
|
306
|
-
|
311
|
+
|
307
312
|
protected
|
308
|
-
# '0.123456' -> 123456
|
309
|
-
# '1.123456' -> 123456
|
310
|
-
def microseconds(time)
|
311
|
-
time[:sec_fraction] ? (time[:sec_fraction] * 1_000_000).to_i : 0
|
312
|
-
end
|
313
313
|
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
314
|
+
# '0.123456' -> 123456
|
315
|
+
# '1.123456' -> 123456
|
316
|
+
def microseconds(time)
|
317
|
+
time[:sec_fraction] ? (time[:sec_fraction] * 1_000_000).to_i : 0
|
318
|
+
end
|
319
319
|
|
320
|
-
|
321
|
-
|
322
|
-
|
320
|
+
def new_date(year, mon, mday)
|
321
|
+
year && year != 0 && Date.new(year, mon, mday) rescue nil
|
322
|
+
end
|
323
323
|
|
324
|
-
|
325
|
-
|
326
|
-
|
324
|
+
def new_time(year, mon, mday, hour, min, sec, microsec, offset = nil) # rubocop:disable Style/ParameterLists
|
325
|
+
# Treat 0000-00-00 00:00:00 as nil.
|
326
|
+
return nil if year.nil? || (year == 0 && mon == 0 && mday == 0)
|
327
327
|
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
Time.public_send(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil
|
332
|
-
end
|
333
|
-
end
|
328
|
+
if offset
|
329
|
+
time = Time.utc(year, mon, mday, hour, min, sec, microsec) rescue nil
|
330
|
+
return nil unless time
|
334
331
|
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
332
|
+
time -= offset
|
333
|
+
Base.default_timezone == :utc ? time : time.getlocal
|
334
|
+
else
|
335
|
+
Time.public_send(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil
|
339
336
|
end
|
337
|
+
end
|
340
338
|
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
microsec = ($7.to_r * 1_000_000).to_i
|
345
|
-
new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec
|
346
|
-
end
|
339
|
+
def fast_string_to_date(string)
|
340
|
+
if string =~ Format::ISO_DATE
|
341
|
+
new_date Regexp.last_match[1].to_i, Regexp.last_match[2].to_i, Regexp.last_match[3].to_i
|
347
342
|
end
|
343
|
+
end
|
348
344
|
|
349
|
-
|
350
|
-
|
351
|
-
|
345
|
+
# Doesn't handle time zones.
|
346
|
+
def fast_string_to_time(string)
|
347
|
+
return unless string =~ Format::ISO_DATETIME
|
348
|
+
microsec = (Regexp.last_match[7].to_r * 1_000_000).to_i
|
349
|
+
new_time(Regexp.last_match[1].to_i,
|
350
|
+
Regexp.last_match[2].to_i,
|
351
|
+
Regexp.last_match[3].to_i,
|
352
|
+
Regexp.last_match[4].to_i,
|
353
|
+
Regexp.last_match[5].to_i,
|
354
|
+
Regexp.last_match[6].to_i,
|
355
|
+
microsec
|
356
|
+
)
|
357
|
+
end
|
352
358
|
|
353
|
-
|
354
|
-
|
355
|
-
|
359
|
+
def fallback_string_to_date(string)
|
360
|
+
new_date(*::Date._parse(string, false).values_at(:year, :mon, :mday))
|
361
|
+
end
|
356
362
|
|
357
|
-
|
358
|
-
|
363
|
+
def fallback_string_to_time(string)
|
364
|
+
time_hash = Date._parse(string)
|
365
|
+
time_hash[:sec_fraction] = microseconds(time_hash)
|
366
|
+
|
367
|
+
new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction))
|
368
|
+
end
|
359
369
|
end
|
360
|
-
|
370
|
+
|
361
371
|
private
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
372
|
+
|
373
|
+
def compute_cql_type(field_type, options)
|
374
|
+
options[:cql_type] ||
|
375
|
+
case field_type.to_sym
|
376
|
+
when :integer then 'int'
|
377
|
+
when :time, :date, :timestamp, :datetime then 'timestamp'
|
378
|
+
when :binary then 'blob'
|
379
|
+
when :list then "list<#{compute_cql_type(options[:holds], options)}>"
|
380
|
+
when :set then "set<#{compute_cql_type(options[:holds], options)}>"
|
381
|
+
when :map then "map<text, #{compute_cql_type(options[:holds], options)}>"
|
382
|
+
when :string then 'text'
|
383
|
+
else field_type.to_s
|
374
384
|
end
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
385
|
+
end
|
386
|
+
|
387
|
+
def compute_solr_type(field_type, options)
|
388
|
+
options[:solr_type] ||
|
389
|
+
case field_type.to_sym
|
390
|
+
when :integer then 'int'
|
391
|
+
when :decimal then 'double'
|
392
|
+
when :timestamp, :time, :datetime then 'date'
|
393
|
+
when :list, :set, :map then compute_solr_type(options[:holds], options)
|
394
|
+
else field_type.to_s
|
384
395
|
end
|
396
|
+
end
|
385
397
|
end
|
386
398
|
end
|