yugabyte-ycql-driver 3.2.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.yardopts +13 -0
- data/README.md +242 -0
- data/ext/cassandra_murmur3/cassandra_murmur3.c +178 -0
- data/ext/cassandra_murmur3/extconf.rb +2 -0
- data/lib/cassandra/address_resolution.rb +36 -0
- data/lib/cassandra/address_resolution/policies.rb +2 -0
- data/lib/cassandra/address_resolution/policies/ec2_multi_region.rb +56 -0
- data/lib/cassandra/address_resolution/policies/none.rb +35 -0
- data/lib/cassandra/aggregate.rb +123 -0
- data/lib/cassandra/argument.rb +51 -0
- data/lib/cassandra/attr_boolean.rb +33 -0
- data/lib/cassandra/auth.rb +100 -0
- data/lib/cassandra/auth/providers.rb +17 -0
- data/lib/cassandra/auth/providers/password.rb +65 -0
- data/lib/cassandra/cassandra_logger.rb +80 -0
- data/lib/cassandra/cluster.rb +331 -0
- data/lib/cassandra/cluster/client.rb +1612 -0
- data/lib/cassandra/cluster/connection_pool.rb +78 -0
- data/lib/cassandra/cluster/connector.rb +372 -0
- data/lib/cassandra/cluster/control_connection.rb +962 -0
- data/lib/cassandra/cluster/failed_connection.rb +35 -0
- data/lib/cassandra/cluster/metadata.rb +142 -0
- data/lib/cassandra/cluster/options.rb +145 -0
- data/lib/cassandra/cluster/registry.rb +284 -0
- data/lib/cassandra/cluster/schema.rb +405 -0
- data/lib/cassandra/cluster/schema/cql_type_parser.rb +112 -0
- data/lib/cassandra/cluster/schema/fetchers.rb +1627 -0
- data/lib/cassandra/cluster/schema/fqcn_type_parser.rb +175 -0
- data/lib/cassandra/cluster/schema/partitioners.rb +21 -0
- data/lib/cassandra/cluster/schema/partitioners/murmur3.rb +45 -0
- data/lib/cassandra/cluster/schema/partitioners/ordered.rb +37 -0
- data/lib/cassandra/cluster/schema/partitioners/random.rb +37 -0
- data/lib/cassandra/cluster/schema/replication_strategies.rb +21 -0
- data/lib/cassandra/cluster/schema/replication_strategies/network_topology.rb +102 -0
- data/lib/cassandra/cluster/schema/replication_strategies/none.rb +39 -0
- data/lib/cassandra/cluster/schema/replication_strategies/simple.rb +44 -0
- data/lib/cassandra/column.rb +66 -0
- data/lib/cassandra/column_container.rb +326 -0
- data/lib/cassandra/compression.rb +69 -0
- data/lib/cassandra/compression/compressors/lz4.rb +73 -0
- data/lib/cassandra/compression/compressors/snappy.rb +69 -0
- data/lib/cassandra/custom_data.rb +53 -0
- data/lib/cassandra/driver.rb +260 -0
- data/lib/cassandra/errors.rb +784 -0
- data/lib/cassandra/execution/info.rb +69 -0
- data/lib/cassandra/execution/options.rb +267 -0
- data/lib/cassandra/execution/profile.rb +153 -0
- data/lib/cassandra/execution/profile_manager.rb +71 -0
- data/lib/cassandra/execution/trace.rb +192 -0
- data/lib/cassandra/executors.rb +113 -0
- data/lib/cassandra/function.rb +156 -0
- data/lib/cassandra/function_collection.rb +85 -0
- data/lib/cassandra/future.rb +794 -0
- data/lib/cassandra/host.rb +102 -0
- data/lib/cassandra/index.rb +118 -0
- data/lib/cassandra/keyspace.rb +473 -0
- data/lib/cassandra/listener.rb +87 -0
- data/lib/cassandra/load_balancing.rb +121 -0
- data/lib/cassandra/load_balancing/policies.rb +20 -0
- data/lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb +172 -0
- data/lib/cassandra/load_balancing/policies/round_robin.rb +141 -0
- data/lib/cassandra/load_balancing/policies/token_aware.rb +149 -0
- data/lib/cassandra/load_balancing/policies/white_list.rb +100 -0
- data/lib/cassandra/materialized_view.rb +92 -0
- data/lib/cassandra/null_logger.rb +56 -0
- data/lib/cassandra/protocol.rb +102 -0
- data/lib/cassandra/protocol/coder.rb +1085 -0
- data/lib/cassandra/protocol/cql_byte_buffer.rb +418 -0
- data/lib/cassandra/protocol/cql_protocol_handler.rb +448 -0
- data/lib/cassandra/protocol/request.rb +41 -0
- data/lib/cassandra/protocol/requests/auth_response_request.rb +51 -0
- data/lib/cassandra/protocol/requests/batch_request.rb +117 -0
- data/lib/cassandra/protocol/requests/credentials_request.rb +51 -0
- data/lib/cassandra/protocol/requests/execute_request.rb +122 -0
- data/lib/cassandra/protocol/requests/options_request.rb +39 -0
- data/lib/cassandra/protocol/requests/prepare_request.rb +59 -0
- data/lib/cassandra/protocol/requests/query_request.rb +112 -0
- data/lib/cassandra/protocol/requests/register_request.rb +38 -0
- data/lib/cassandra/protocol/requests/startup_request.rb +49 -0
- data/lib/cassandra/protocol/requests/void_query_request.rb +24 -0
- data/lib/cassandra/protocol/response.rb +28 -0
- data/lib/cassandra/protocol/responses/already_exists_error_response.rb +50 -0
- data/lib/cassandra/protocol/responses/auth_challenge_response.rb +36 -0
- data/lib/cassandra/protocol/responses/auth_success_response.rb +36 -0
- data/lib/cassandra/protocol/responses/authenticate_response.rb +36 -0
- data/lib/cassandra/protocol/responses/error_response.rb +142 -0
- data/lib/cassandra/protocol/responses/event_response.rb +30 -0
- data/lib/cassandra/protocol/responses/function_failure_error_response.rb +52 -0
- data/lib/cassandra/protocol/responses/prepared_result_response.rb +62 -0
- data/lib/cassandra/protocol/responses/raw_rows_result_response.rb +59 -0
- data/lib/cassandra/protocol/responses/read_failure_error_response.rb +71 -0
- data/lib/cassandra/protocol/responses/read_timeout_error_response.rb +61 -0
- data/lib/cassandra/protocol/responses/ready_response.rb +43 -0
- data/lib/cassandra/protocol/responses/result_response.rb +42 -0
- data/lib/cassandra/protocol/responses/rows_result_response.rb +39 -0
- data/lib/cassandra/protocol/responses/schema_change_event_response.rb +73 -0
- data/lib/cassandra/protocol/responses/schema_change_result_response.rb +70 -0
- data/lib/cassandra/protocol/responses/set_keyspace_result_response.rb +37 -0
- data/lib/cassandra/protocol/responses/status_change_event_response.rb +39 -0
- data/lib/cassandra/protocol/responses/supported_response.rb +36 -0
- data/lib/cassandra/protocol/responses/topology_change_event_response.rb +33 -0
- data/lib/cassandra/protocol/responses/unavailable_error_response.rb +58 -0
- data/lib/cassandra/protocol/responses/unprepared_error_response.rb +48 -0
- data/lib/cassandra/protocol/responses/void_result_response.rb +34 -0
- data/lib/cassandra/protocol/responses/write_failure_error_response.rb +73 -0
- data/lib/cassandra/protocol/responses/write_timeout_error_response.rb +63 -0
- data/lib/cassandra/protocol/v1.rb +326 -0
- data/lib/cassandra/protocol/v3.rb +358 -0
- data/lib/cassandra/protocol/v4.rb +478 -0
- data/lib/cassandra/reconnection.rb +49 -0
- data/lib/cassandra/reconnection/policies.rb +20 -0
- data/lib/cassandra/reconnection/policies/constant.rb +46 -0
- data/lib/cassandra/reconnection/policies/exponential.rb +79 -0
- data/lib/cassandra/result.rb +276 -0
- data/lib/cassandra/retry.rb +154 -0
- data/lib/cassandra/retry/policies.rb +21 -0
- data/lib/cassandra/retry/policies/default.rb +53 -0
- data/lib/cassandra/retry/policies/downgrading_consistency.rb +73 -0
- data/lib/cassandra/retry/policies/fallthrough.rb +39 -0
- data/lib/cassandra/session.rb +270 -0
- data/lib/cassandra/statement.rb +32 -0
- data/lib/cassandra/statements.rb +23 -0
- data/lib/cassandra/statements/batch.rb +146 -0
- data/lib/cassandra/statements/bound.rb +65 -0
- data/lib/cassandra/statements/prepared.rb +235 -0
- data/lib/cassandra/statements/simple.rb +118 -0
- data/lib/cassandra/statements/void.rb +38 -0
- data/lib/cassandra/table.rb +240 -0
- data/lib/cassandra/time.rb +103 -0
- data/lib/cassandra/time_uuid.rb +78 -0
- data/lib/cassandra/timestamp_generator.rb +37 -0
- data/lib/cassandra/timestamp_generator/simple.rb +38 -0
- data/lib/cassandra/timestamp_generator/ticking_on_duplicate.rb +58 -0
- data/lib/cassandra/trigger.rb +67 -0
- data/lib/cassandra/tuple.rb +131 -0
- data/lib/cassandra/types.rb +1704 -0
- data/lib/cassandra/udt.rb +443 -0
- data/lib/cassandra/util.rb +464 -0
- data/lib/cassandra/uuid.rb +110 -0
- data/lib/cassandra/uuid/generator.rb +212 -0
- data/lib/cassandra/version.rb +21 -0
- data/lib/datastax/cassandra.rb +47 -0
- data/lib/ycql.rb +842 -0
- metadata +243 -0
@@ -0,0 +1,78 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
# Copyright DataStax, Inc.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#++
|
18
|
+
|
19
|
+
module Cassandra
|
20
|
+
# A variant of UUID which can extract its time component.
|
21
|
+
#
|
22
|
+
# You can use {Cassandra::Uuid::Generator} to generate {Cassandra::TimeUuid}s
|
23
|
+
class TimeUuid < Uuid
|
24
|
+
include Comparable
|
25
|
+
|
26
|
+
# @private
|
27
|
+
LOWER_HALF_MASK = 0xffffffff_ffffffff
|
28
|
+
# @private
|
29
|
+
GREGORIAN_OFFSET = 122192928000000000
|
30
|
+
|
31
|
+
# Returns the time component from this UUID as a Time.
|
32
|
+
#
|
33
|
+
# @return [Time]
|
34
|
+
def to_time
|
35
|
+
t = time_bits - GREGORIAN_OFFSET
|
36
|
+
seconds = t / 10_000_000
|
37
|
+
microseconds = (t - seconds * 10_000_000) / 10.0
|
38
|
+
|
39
|
+
::Time.at(seconds, microseconds).utc
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns the date component from this UUID as Date.
|
43
|
+
#
|
44
|
+
# This just sugar around {#to_time}
|
45
|
+
#
|
46
|
+
# @return [Date]
|
47
|
+
def to_date
|
48
|
+
to_time.to_date
|
49
|
+
end
|
50
|
+
|
51
|
+
# Compares this timeuuid with another timeuuid
|
52
|
+
#
|
53
|
+
# @param other [Cassandra::TimeUuid] another timeuuid to compare
|
54
|
+
# @see Comparable
|
55
|
+
#
|
56
|
+
# @return [nil] when other is not a {Cassandra::Uuid}
|
57
|
+
# @return [Integer] `-1` when less than `other`, `0` when equal to `other`
|
58
|
+
# and `1` when greater than `other`
|
59
|
+
def <=>(other)
|
60
|
+
return nil unless other.is_a?(Cassandra::Uuid)
|
61
|
+
c = value <=> other.value
|
62
|
+
return c if c == 0 || !other.is_a?(Cassandra::TimeUuid)
|
63
|
+
time_bits <=> other.time_bits
|
64
|
+
end
|
65
|
+
|
66
|
+
protected
|
67
|
+
|
68
|
+
# @private
|
69
|
+
def time_bits
|
70
|
+
n = (value >> 64)
|
71
|
+
t = 0
|
72
|
+
t |= (n & 0x0000000000000fff) << 48
|
73
|
+
t |= (n & 0x00000000ffff0000) << 16
|
74
|
+
t |= (n & 0xffffffff00000000) >> 32
|
75
|
+
t
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
# Copyright DataStax, Inc.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#++
|
18
|
+
|
19
|
+
module Cassandra
|
20
|
+
# A generator is used to create client-timestamps (in the form of long integers) to send with C* requests when
|
21
|
+
# the `:client_timestamps` cluster option is set to true.
|
22
|
+
#
|
23
|
+
# @abstract A timestamp generator given to {Cassandra.cluster} doesn't need to include this module, but needs to
|
24
|
+
# implement the same methods. This module exists only for documentation purposes.
|
25
|
+
module TimestampGenerator
|
26
|
+
# Create a new timestamp, as a 64-bit integer. Calls must return monotonically increasing values.
|
27
|
+
#
|
28
|
+
# @return [Integer] an integer representing a timestamp in microseconds.
|
29
|
+
# @raise [NotImplementedError] if a class including this module does not define this method.
|
30
|
+
def next
|
31
|
+
raise NotImplementedError, "#{self.class} class must implement the 'next' method"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
require 'cassandra/timestamp_generator/ticking_on_duplicate'
|
37
|
+
require 'cassandra/timestamp_generator/simple'
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
# Copyright DataStax, Inc.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#++
|
18
|
+
|
19
|
+
module Cassandra
|
20
|
+
module TimestampGenerator
|
21
|
+
# Generate long integer timestamps from current time. This implementation relies on the {::Time} class to return
|
22
|
+
# microsecond precision time.
|
23
|
+
# @note It is not appropriate for use with JRuby because its {::Time#now} returns millisecond precision time.
|
24
|
+
class Simple
|
25
|
+
include TimestampGenerator
|
26
|
+
|
27
|
+
# Create a new timestamp, as a 64-bit integer. This is just a wrapper around Time::now.
|
28
|
+
#
|
29
|
+
# @return [Integer] an integer representing a timestamp in microseconds.
|
30
|
+
def next
|
31
|
+
# Use Time.now, which has microsecond precision on MRI (and probably Rubinius) to make an int representing
|
32
|
+
# client timestamp in protocol requests.
|
33
|
+
timestamp = ::Time.now
|
34
|
+
timestamp.tv_sec * 1000000 + timestamp.tv_usec
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
# Copyright DataStax, Inc.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#++
|
18
|
+
|
19
|
+
module Cassandra
|
20
|
+
module TimestampGenerator
|
21
|
+
# In JRuby, {::Time} has millisecond precision. We require client timestamps to have microsecond precision to
|
22
|
+
# minimize clashes in C*. This generator keeps track of the last generated timestamp, and if the current-time
|
23
|
+
# is within the same millisecond as the last, it fills the microsecond portion of the new timestamp with the
|
24
|
+
# value of an incrementing counter.
|
25
|
+
#
|
26
|
+
# For example, if the generator triggers twice at time 12345678000 (microsecond granularity, but ms precisions
|
27
|
+
# as shown by 0's for the three least-significant digits), it'll return 12345678000 and 12345678001.
|
28
|
+
class TickingOnDuplicate
|
29
|
+
include MonitorMixin
|
30
|
+
include TimestampGenerator
|
31
|
+
|
32
|
+
# @private
|
33
|
+
def initialize
|
34
|
+
mon_initialize
|
35
|
+
@last = 0
|
36
|
+
end
|
37
|
+
|
38
|
+
# Create a new timestamp, as a 64-bit integer.
|
39
|
+
#
|
40
|
+
# @return [Integer] an integer representing a timestamp in microseconds.
|
41
|
+
def next
|
42
|
+
now = ::Time.now
|
43
|
+
now_millis = now.tv_sec * 1000 + now.tv_usec / 1000
|
44
|
+
synchronize do
|
45
|
+
millis = @last / 1000
|
46
|
+
counter = @last % 1000
|
47
|
+
if millis >= now_millis
|
48
|
+
counter += 1
|
49
|
+
else
|
50
|
+
millis = now_millis
|
51
|
+
counter = 0
|
52
|
+
end
|
53
|
+
@last = millis * 1000 + counter
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
# Copyright DataStax, Inc.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#++
|
18
|
+
|
19
|
+
module Cassandra
|
20
|
+
# Represents a trigger on a cassandra table
|
21
|
+
class Trigger
|
22
|
+
# @return [Cassandra::Table] table that the trigger applies to.
|
23
|
+
attr_reader :table
|
24
|
+
# @return [String] name of the trigger.
|
25
|
+
attr_reader :name
|
26
|
+
# @return [Hash] options of the trigger.
|
27
|
+
attr_reader :options
|
28
|
+
|
29
|
+
# @private
|
30
|
+
def initialize(table,
|
31
|
+
name,
|
32
|
+
options)
|
33
|
+
@table = table
|
34
|
+
@name = name.freeze
|
35
|
+
@options = options.freeze
|
36
|
+
end
|
37
|
+
|
38
|
+
# @return [String] name of the trigger class
|
39
|
+
def custom_class_name
|
40
|
+
@options['class']
|
41
|
+
end
|
42
|
+
|
43
|
+
# @return [String] a cql representation of this trigger
|
44
|
+
def to_cql
|
45
|
+
keyspace_name = Util.escape_name(@table.keyspace.name)
|
46
|
+
table_name = Util.escape_name(@table.name)
|
47
|
+
trigger_name = Util.escape_name(@name)
|
48
|
+
|
49
|
+
"CREATE TRIGGER #{trigger_name} ON #{keyspace_name}.#{table_name} USING '#{@options['class']}';"
|
50
|
+
end
|
51
|
+
|
52
|
+
# @private
|
53
|
+
def eql?(other)
|
54
|
+
other.is_a?(Trigger) &&
|
55
|
+
@table == other.table &&
|
56
|
+
@name == other.name &&
|
57
|
+
@options == other.options
|
58
|
+
end
|
59
|
+
alias == eql?
|
60
|
+
|
61
|
+
# @private
|
62
|
+
def inspect
|
63
|
+
"#<#{self.class.name}:0x#{object_id.to_s(16)} " \
|
64
|
+
"@name=#{@name.inspect} @table=#{@table.inspect} @options=#{@options.inspect}>"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
# Copyright DataStax, Inc.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#++
|
18
|
+
|
19
|
+
module Cassandra
|
20
|
+
class Tuple
|
21
|
+
# @private
|
22
|
+
class Strict < Tuple
|
23
|
+
attr_reader :types
|
24
|
+
|
25
|
+
def initialize(types, values)
|
26
|
+
@types = types
|
27
|
+
@values = values
|
28
|
+
end
|
29
|
+
|
30
|
+
def each(&block)
|
31
|
+
@types.size.times do |i|
|
32
|
+
yield(@values[i])
|
33
|
+
end
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
def [](i)
|
38
|
+
@values[Integer(i)]
|
39
|
+
end
|
40
|
+
|
41
|
+
def fetch(i)
|
42
|
+
i = Integer(i)
|
43
|
+
raise ::IndexError, "index #{i} is outside of tuple, size: #{@types.size}" if i < 0 || i >= @types.size
|
44
|
+
@values[i]
|
45
|
+
end
|
46
|
+
|
47
|
+
def []=(i, value)
|
48
|
+
raise ::IndexError, "index #{i} is outside of tuple, size: #{@types.size}" if i < 0 || i >= @types.size
|
49
|
+
Util.assert_type(@types[i], value)
|
50
|
+
@values[i] = value
|
51
|
+
end
|
52
|
+
|
53
|
+
def size
|
54
|
+
@types.size
|
55
|
+
end
|
56
|
+
|
57
|
+
def inspect
|
58
|
+
"#<Cassandra::Tuple:0x#{object_id.to_s(16)} types=#{@types.inspect}, []=#{@values.inspect}>"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
include Enumerable
|
63
|
+
|
64
|
+
# Constructs a tuple with given values
|
65
|
+
def initialize(*values)
|
66
|
+
@values = values
|
67
|
+
end
|
68
|
+
|
69
|
+
# Iterates over all values of the tuple
|
70
|
+
# @yieldparam value [Object] current value
|
71
|
+
def each(&block)
|
72
|
+
@values.each(&block)
|
73
|
+
self
|
74
|
+
end
|
75
|
+
|
76
|
+
# @param i [Integer] numeric index of the value inside the tuple, must
|
77
|
+
# be `0 < i < tuple.size`
|
78
|
+
# @return [Object] value of the tuple at position `i`
|
79
|
+
def [](i)
|
80
|
+
@values[Integer(i)]
|
81
|
+
end
|
82
|
+
|
83
|
+
# @param i [Integer] numeric index of the value inside the tuple, must
|
84
|
+
# be `0 < i < tuple.size`
|
85
|
+
# @raise [IndexError] when index is outside of tuple bounds
|
86
|
+
# @return [Object] value of the tuple at position `i`
|
87
|
+
def fetch(i)
|
88
|
+
i = Integer(i)
|
89
|
+
raise ::IndexError, "index #{i} is outside of tuple, size: #{@values.size}" if i < 0 || i >= @values.size
|
90
|
+
@values[i]
|
91
|
+
end
|
92
|
+
|
93
|
+
# @param i [Integer] numeric index of the value inside the tuple, must
|
94
|
+
# be `0 < i < tuple.size`
|
95
|
+
# @param value [Object] a value to assign at position `i`
|
96
|
+
# @raise [IndexError] when index is outside of tuple bounds
|
97
|
+
# @return [Object] value of the tuple at position `i`
|
98
|
+
def []=(i, value)
|
99
|
+
i = Integer(i)
|
100
|
+
raise ::IndexError, "index #{i} is outside of tuple, size: #{@values.size}" if i < 0 || i >= @values.size
|
101
|
+
@values[i] = value
|
102
|
+
end
|
103
|
+
|
104
|
+
# Returns tuple size
|
105
|
+
# @return [Integer] tuple size
|
106
|
+
def size
|
107
|
+
@values.size
|
108
|
+
end
|
109
|
+
|
110
|
+
# String representation of the tuple
|
111
|
+
def to_s
|
112
|
+
"(#{@values.map(&:to_s).join(', ')})"
|
113
|
+
end
|
114
|
+
|
115
|
+
# @private
|
116
|
+
def inspect
|
117
|
+
"#<Cassandra::Tuple:0x#{object_id.to_s(16)} []=#{@values.inspect}>"
|
118
|
+
end
|
119
|
+
|
120
|
+
# @private
|
121
|
+
def eql?(other)
|
122
|
+
other == @values
|
123
|
+
end
|
124
|
+
alias == eql?
|
125
|
+
|
126
|
+
# @private
|
127
|
+
def hash
|
128
|
+
@values.inject(17) {|h, v| 31 * h + v.hash}
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
@@ -0,0 +1,1704 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
# Copyright DataStax, Inc.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#++
|
18
|
+
|
19
|
+
module Cassandra
|
20
|
+
# Base class for all cassandra types.
|
21
|
+
# @abstract This class exists for documentation purposes only
|
22
|
+
class Type
|
23
|
+
# @return [Symbol] shorthand type name
|
24
|
+
attr_reader :kind
|
25
|
+
|
26
|
+
def initialize(kind)
|
27
|
+
@kind = kind
|
28
|
+
end
|
29
|
+
|
30
|
+
# Coerces a given value to this type
|
31
|
+
#
|
32
|
+
# @param values [*Object] value to be coerced
|
33
|
+
# @return [Object] a value of this type
|
34
|
+
def new(*values)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Asserts that a given value is of this type
|
38
|
+
# @param value [Object] value to be validated
|
39
|
+
# @param message [String] error message to use when assertion fails
|
40
|
+
# @yieldreturn [String] error message to use when assertion fails
|
41
|
+
# @raise [ArgumentError] if the value is invalid
|
42
|
+
# @return [void]
|
43
|
+
def assert(value, message = nil, &block)
|
44
|
+
end
|
45
|
+
|
46
|
+
# @return [String] a cassandra representation of this type
|
47
|
+
def to_s
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
module Types
|
52
|
+
# If we use module_function, the yard docs end up showing duplicates of all
|
53
|
+
# methods: one for self, the other as instance methods.
|
54
|
+
#
|
55
|
+
# rubocop:disable Style/ModuleFunction
|
56
|
+
extend self
|
57
|
+
|
58
|
+
# @private
|
59
|
+
class Simple < Type
|
60
|
+
def new(value)
|
61
|
+
__send__(:"new_#{@kind}", value)
|
62
|
+
end
|
63
|
+
|
64
|
+
def assert(value, message = nil, &block)
|
65
|
+
__send__(:"assert_#{@kind}", value, message, &block)
|
66
|
+
nil
|
67
|
+
end
|
68
|
+
|
69
|
+
def to_s
|
70
|
+
@kind.to_s
|
71
|
+
end
|
72
|
+
|
73
|
+
def hash
|
74
|
+
@hash ||= 31 * 17 + @kind.hash
|
75
|
+
end
|
76
|
+
|
77
|
+
def eql?(other)
|
78
|
+
other.is_a?(Simple) && @kind == other.kind
|
79
|
+
end
|
80
|
+
|
81
|
+
alias == eql?
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
def new_varchar(value)
|
86
|
+
String(value)
|
87
|
+
end
|
88
|
+
|
89
|
+
def assert_varchar(value, message, &block)
|
90
|
+
Util.assert_instance_of(::String, value, message, &block)
|
91
|
+
end
|
92
|
+
|
93
|
+
def new_text(value)
|
94
|
+
String(value)
|
95
|
+
end
|
96
|
+
|
97
|
+
def assert_text(value, message, &block)
|
98
|
+
Util.assert_instance_of(::String, value, message, &block)
|
99
|
+
end
|
100
|
+
|
101
|
+
def new_blob(value)
|
102
|
+
String(value)
|
103
|
+
end
|
104
|
+
|
105
|
+
def assert_blob(value, message, &block)
|
106
|
+
Util.assert_instance_of(::String, value, message, &block)
|
107
|
+
end
|
108
|
+
|
109
|
+
def new_ascii(value)
|
110
|
+
String(value)
|
111
|
+
end
|
112
|
+
|
113
|
+
def assert_ascii(value, message, &block)
|
114
|
+
Util.assert_instance_of(::String, value, message, &block)
|
115
|
+
end
|
116
|
+
|
117
|
+
def new_bigint(value)
|
118
|
+
Integer(value)
|
119
|
+
end
|
120
|
+
|
121
|
+
def assert_bigint(value, message, &block)
|
122
|
+
Util.assert_instance_of(::Integer, value, message, &block)
|
123
|
+
end
|
124
|
+
|
125
|
+
def new_counter(value)
|
126
|
+
Integer(value)
|
127
|
+
end
|
128
|
+
|
129
|
+
def assert_counter(value, message, &block)
|
130
|
+
Util.assert_instance_of(::Integer, value, message, &block)
|
131
|
+
end
|
132
|
+
|
133
|
+
def new_int(value)
|
134
|
+
Integer(value)
|
135
|
+
end
|
136
|
+
|
137
|
+
def assert_int(value, message, &block)
|
138
|
+
Util.assert_instance_of(::Integer, value, message, &block)
|
139
|
+
end
|
140
|
+
|
141
|
+
def new_varint(value)
|
142
|
+
Integer(value)
|
143
|
+
end
|
144
|
+
|
145
|
+
def assert_varint(value, message, &block)
|
146
|
+
Util.assert_instance_of(::Integer, value, message, &block)
|
147
|
+
end
|
148
|
+
|
149
|
+
def new_boolean(value)
|
150
|
+
!value.nil? && value != false
|
151
|
+
end
|
152
|
+
|
153
|
+
def assert_boolean(value, message, &block)
|
154
|
+
Util.assert_instance_of_one_of([::TrueClass, ::FalseClass],
|
155
|
+
value,
|
156
|
+
message,
|
157
|
+
&block)
|
158
|
+
end
|
159
|
+
|
160
|
+
def new_decimal(value)
|
161
|
+
::BigDecimal.new(value)
|
162
|
+
end
|
163
|
+
|
164
|
+
def assert_decimal(value, message, &block)
|
165
|
+
Util.assert_instance_of(::BigDecimal, value, message, &block)
|
166
|
+
end
|
167
|
+
|
168
|
+
def new_double(value)
|
169
|
+
Float(value)
|
170
|
+
end
|
171
|
+
|
172
|
+
def assert_double(value, message, &block)
|
173
|
+
Util.assert_instance_of(::Float, value, message, &block)
|
174
|
+
end
|
175
|
+
|
176
|
+
def new_float(value)
|
177
|
+
Float(value)
|
178
|
+
end
|
179
|
+
|
180
|
+
def assert_float(value, message, &block)
|
181
|
+
Util.assert_instance_of(::Float, value, message, &block)
|
182
|
+
end
|
183
|
+
|
184
|
+
def new_inet(value)
|
185
|
+
::IPAddr.new(value)
|
186
|
+
end
|
187
|
+
|
188
|
+
def assert_inet(value, message, &block)
|
189
|
+
Util.assert_instance_of(::IPAddr, value, message, &block)
|
190
|
+
end
|
191
|
+
|
192
|
+
def new_timestamp(value)
|
193
|
+
case value
|
194
|
+
when ::Time
|
195
|
+
value
|
196
|
+
else
|
197
|
+
return value.to_time if value.respond_to?(:to_time)
|
198
|
+
raise ::ArgumentError, "cannot convert #{value.inspect} to timestamp"
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
def assert_timestamp(value, message, &block)
|
203
|
+
Util.assert_instance_of(::Time, value, message, &block)
|
204
|
+
end
|
205
|
+
|
206
|
+
def new_uuid(value)
|
207
|
+
Cassandra::Uuid.new(value)
|
208
|
+
end
|
209
|
+
|
210
|
+
def assert_uuid(value, message, &block)
|
211
|
+
Util.assert_instance_of(Cassandra::Uuid, value, message, &block)
|
212
|
+
end
|
213
|
+
|
214
|
+
def new_timeuuid(value)
|
215
|
+
Cassandra::TimeUuid.new(value)
|
216
|
+
end
|
217
|
+
|
218
|
+
def assert_timeuuid(value, message, &block)
|
219
|
+
Util.assert_instance_of(Cassandra::TimeUuid, value, message, &block)
|
220
|
+
end
|
221
|
+
|
222
|
+
def new_date(value)
|
223
|
+
case value
|
224
|
+
when ::Date
|
225
|
+
value
|
226
|
+
else
|
227
|
+
return value.to_date if value.respond_to?(:to_date)
|
228
|
+
raise ::ArgumentError, "cannot convert #{value.inspect} to date"
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
def assert_date(value, message, &block)
|
233
|
+
Util.assert_instance_of(::Date, value, message, &block)
|
234
|
+
end
|
235
|
+
|
236
|
+
def new_smallint(value)
|
237
|
+
Integer(value)
|
238
|
+
end
|
239
|
+
|
240
|
+
def assert_smallint(value, message, &block)
|
241
|
+
Util.assert_instance_of(::Integer, value, message, &block)
|
242
|
+
Util.assert(value <= 32767 && value >= -32768, message, &block)
|
243
|
+
end
|
244
|
+
|
245
|
+
def new_time(value)
|
246
|
+
case value
|
247
|
+
when ::Integer
|
248
|
+
Time.new(value)
|
249
|
+
else
|
250
|
+
return Time.new(value.to_nanoseconds) if value.respond_to?(:to_nanoseconds)
|
251
|
+
raise ::ArgumentError, "cannot convert #{value.inspect} to time"
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
def assert_time(value, message, &block)
|
256
|
+
Util.assert_instance_of(Cassandra::Time, value, message, &block)
|
257
|
+
end
|
258
|
+
|
259
|
+
def new_tinyint(value)
|
260
|
+
Integer(value)
|
261
|
+
end
|
262
|
+
|
263
|
+
def assert_tinyint(value, message, &block)
|
264
|
+
Util.assert_instance_of(::Integer, value, message, &block)
|
265
|
+
Util.assert(value <= 127 && value >= -128, message, &block)
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
# @!parse
|
270
|
+
# class Ascii < Type
|
271
|
+
# # @return [Symbol] `:ascii`
|
272
|
+
# # @see Cassandra::Type#kind
|
273
|
+
# def kind
|
274
|
+
# end
|
275
|
+
#
|
276
|
+
# # Coerces the value to String
|
277
|
+
# # @param value [Object] original value
|
278
|
+
# # @return [String] value
|
279
|
+
# # @see Cassandra::Type#new
|
280
|
+
# def new(value)
|
281
|
+
# end
|
282
|
+
#
|
283
|
+
# # Asserts that a given value is a String
|
284
|
+
# # @param value [Object] value to be validated
|
285
|
+
# # @param message [String] error message to use when assertion
|
286
|
+
# # fails
|
287
|
+
# # @yieldreturn [String] error message to use when assertion fails
|
288
|
+
# # @raise [ArgumentError] if the value is not a String
|
289
|
+
# # @return [void]
|
290
|
+
# # @see Cassandra::Type#assert
|
291
|
+
# def assert(value, message = nil, &block)
|
292
|
+
# end
|
293
|
+
#
|
294
|
+
# # @return [String] `"ascii"`
|
295
|
+
# # @see Cassandra::Type#to_s
|
296
|
+
# def to_s
|
297
|
+
# end
|
298
|
+
# end
|
299
|
+
const_set('Ascii', Simple.new(:ascii))
|
300
|
+
|
301
|
+
# @!parse
|
302
|
+
# class Bigint < Type
|
303
|
+
# # @return [Symbol] `:bigint`
|
304
|
+
# # @see Cassandra::Type#kind
|
305
|
+
# def kind
|
306
|
+
# end
|
307
|
+
#
|
308
|
+
# # Coerces the value to Integer
|
309
|
+
# # @param value [Object] original value
|
310
|
+
# # @return [Integer] value
|
311
|
+
# # @see Cassandra::Type#new
|
312
|
+
# def new(value)
|
313
|
+
# end
|
314
|
+
#
|
315
|
+
# # Asserts that a given value is an Integer
|
316
|
+
# # @param value [Object] value to be validated
|
317
|
+
# # @param message [String] error message to use when assertion
|
318
|
+
# # fails
|
319
|
+
# # @yieldreturn [String] error message to use when assertion fails
|
320
|
+
# # @raise [ArgumentError] if the value is not an Integer
|
321
|
+
# # @return [void]
|
322
|
+
# # @see Cassandra::Type#assert
|
323
|
+
# def assert(value, message = nil, &block)
|
324
|
+
# end
|
325
|
+
#
|
326
|
+
# # @return [String] `"bigint"`
|
327
|
+
# # @see Cassandra::Type#to_s
|
328
|
+
# def to_s
|
329
|
+
# end
|
330
|
+
# end
|
331
|
+
const_set('Bigint', Simple.new(:bigint))
|
332
|
+
|
333
|
+
# @!parse
|
334
|
+
# class Blob < Type
|
335
|
+
# # @return [Symbol] `:blob`
|
336
|
+
# # @see Cassandra::Type#kind
|
337
|
+
# def kind
|
338
|
+
# end
|
339
|
+
#
|
340
|
+
# # Coerces the value to String
|
341
|
+
# # @param value [Object] original value
|
342
|
+
# # @return [String] value
|
343
|
+
# # @see Cassandra::Type#new
|
344
|
+
# def new(value)
|
345
|
+
# end
|
346
|
+
#
|
347
|
+
# # Asserts that a given value is a String
|
348
|
+
# # @param value [Object] value to be validated
|
349
|
+
# # @param message [String] error message to use when assertion
|
350
|
+
# # fails
|
351
|
+
# # @yieldreturn [String] error message to use when assertion fails
|
352
|
+
# # @raise [ArgumentError] if the value is not a String
|
353
|
+
# # @return [void]
|
354
|
+
# # @see Cassandra::Type#assert
|
355
|
+
# def assert(value, message = nil, &block)
|
356
|
+
# end
|
357
|
+
#
|
358
|
+
# # @return [String] `"blob"`
|
359
|
+
# # @see Cassandra::Type#to_s
|
360
|
+
# def to_s
|
361
|
+
# end
|
362
|
+
# end
|
363
|
+
const_set('Blob', Simple.new(:blob))
|
364
|
+
|
365
|
+
# @!parse
|
366
|
+
# class Boolean < Type
|
367
|
+
# # @return [Symbol] `:boolean`
|
368
|
+
# # @see Cassandra::Type#kind
|
369
|
+
# def kind
|
370
|
+
# end
|
371
|
+
#
|
372
|
+
# # Coerces the value to `true` or `false`
|
373
|
+
# # @param value [Object] original value
|
374
|
+
# # @return [Boolean] value
|
375
|
+
# # @see Cassandra::Type#new
|
376
|
+
# def new(value)
|
377
|
+
# end
|
378
|
+
#
|
379
|
+
# # Asserts that a given value is a `true` or `false`
|
380
|
+
# # @param value [Object] value to be validated
|
381
|
+
# # @param message [String] error message to use when assertion
|
382
|
+
# # fails
|
383
|
+
# # @yieldreturn [String] error message to use when assertion fails
|
384
|
+
# # @raise [ArgumentError] if the value is not `true` or `false`
|
385
|
+
# # @return [void]
|
386
|
+
# # @see Cassandra::Type#assert
|
387
|
+
# def assert(value, message = nil, &block)
|
388
|
+
# end
|
389
|
+
#
|
390
|
+
# # @return [String] `"boolean"`
|
391
|
+
# # @see Cassandra::Type#to_s
|
392
|
+
# def to_s
|
393
|
+
# end
|
394
|
+
# end
|
395
|
+
const_set('Boolean', Simple.new(:boolean))
|
396
|
+
|
397
|
+
# @!parse
|
398
|
+
# class Counter < Type
|
399
|
+
# # @return [Symbol] `:counter`
|
400
|
+
# # @see Cassandra::Type#kind
|
401
|
+
# def kind
|
402
|
+
# end
|
403
|
+
#
|
404
|
+
# # Coerces the value to Integer
|
405
|
+
# # @param value [Object] original value
|
406
|
+
# # @return [Integer] value
|
407
|
+
# # @see Cassandra::Type#new
|
408
|
+
# def new(value)
|
409
|
+
# end
|
410
|
+
#
|
411
|
+
# # Asserts that a given value is an Integer
|
412
|
+
# # @param value [Object] value to be validated
|
413
|
+
# # @param message [String] error message to use when assertion
|
414
|
+
# # fails
|
415
|
+
# # @yieldreturn [String] error message to use when assertion fails
|
416
|
+
# # @raise [ArgumentError] if the value is not an Integer
|
417
|
+
# # @return [void]
|
418
|
+
# # @see Cassandra::Type#assert
|
419
|
+
# def assert(value, message = nil, &block)
|
420
|
+
# end
|
421
|
+
#
|
422
|
+
# # @return [String] `"counter"`
|
423
|
+
# # @see Cassandra::Type#to_s
|
424
|
+
# def to_s
|
425
|
+
# end
|
426
|
+
# end
|
427
|
+
const_set('Counter', Simple.new(:counter))
|
428
|
+
|
429
|
+
# @!parse
|
430
|
+
# class Date < Type
|
431
|
+
# # @return [Symbol] `:date`
|
432
|
+
# # @see Cassandra::Type#kind
|
433
|
+
# def kind
|
434
|
+
# end
|
435
|
+
#
|
436
|
+
# # Coerces the value to Date
|
437
|
+
# # @param value [Object] original value
|
438
|
+
# # @return [Date] value
|
439
|
+
# # @see Cassandra::Type#new
|
440
|
+
# def new(value)
|
441
|
+
# end
|
442
|
+
#
|
443
|
+
# # Asserts that a given value is a Date
|
444
|
+
# # @param value [Object] value to be validated
|
445
|
+
# # @param message [String] error message to use when assertion
|
446
|
+
# # fails
|
447
|
+
# # @yieldreturn [String] error message to use when assertion fails
|
448
|
+
# # @raise [ArgumentError] if the value is not a Date
|
449
|
+
# # @return [void]
|
450
|
+
# # @see Cassandra::Type#assert
|
451
|
+
# def assert(value, message = nil, &block)
|
452
|
+
# end
|
453
|
+
#
|
454
|
+
# # @return [String] `"date"`
|
455
|
+
# # @see Cassandra::Type#to_s
|
456
|
+
# def to_s
|
457
|
+
# end
|
458
|
+
# end
|
459
|
+
const_set('Date', Simple.new(:date))
|
460
|
+
|
461
|
+
# @!parse
|
462
|
+
# class Decimal < Type
|
463
|
+
# # @return [Symbol] `:decimal`
|
464
|
+
# # @see Cassandra::Type#kind
|
465
|
+
# def kind
|
466
|
+
# end
|
467
|
+
#
|
468
|
+
# # Coerces the value to BigDecimal
|
469
|
+
# # @param value [Object] original value
|
470
|
+
# # @return [BigDecimal] value
|
471
|
+
# # @see Cassandra::Type#new
|
472
|
+
# def new(value)
|
473
|
+
# end
|
474
|
+
#
|
475
|
+
# # Asserts that a given value is a BigDecimal
|
476
|
+
# # @param value [Object] value to be validated
|
477
|
+
# # @param message [String] error message to use when assertion
|
478
|
+
# # fails
|
479
|
+
# # @yieldreturn [String] error message to use when assertion fails
|
480
|
+
# # @raise [ArgumentError] if the value is not a BigDecimal
|
481
|
+
# # @return [void]
|
482
|
+
# # @see Cassandra::Type#assert
|
483
|
+
# def assert(value, message = nil, &block)
|
484
|
+
# end
|
485
|
+
#
|
486
|
+
# # @return [String] `"decimal"`
|
487
|
+
# # @see Cassandra::Type#to_s
|
488
|
+
# def to_s
|
489
|
+
# end
|
490
|
+
# end
|
491
|
+
const_set('Decimal', Simple.new(:decimal))
|
492
|
+
|
493
|
+
# @!parse
|
494
|
+
# class Double < Type
|
495
|
+
# # @return [Symbol] `:double`
|
496
|
+
# # @see Cassandra::Type#kind
|
497
|
+
# def kind
|
498
|
+
# end
|
499
|
+
#
|
500
|
+
# # Coerces the value to Float
|
501
|
+
# # @param value [Object] original value
|
502
|
+
# # @return [Float] value
|
503
|
+
# # @see Cassandra::Type#new
|
504
|
+
# def new(value)
|
505
|
+
# end
|
506
|
+
#
|
507
|
+
# # Asserts that a given value is a Float
|
508
|
+
# # @param value [Object] value to be validated
|
509
|
+
# # @param message [String] error message to use when assertion
|
510
|
+
# # fails
|
511
|
+
# # @yieldreturn [String] error message to use when assertion fails
|
512
|
+
# # @raise [ArgumentError] if the value is not a Float
|
513
|
+
# # @return [void]
|
514
|
+
# # @see Cassandra::Type#assert
|
515
|
+
# def assert(value, message = nil, &block)
|
516
|
+
# end
|
517
|
+
#
|
518
|
+
# # @return [String] `"double"`
|
519
|
+
# # @see Cassandra::Type#to_s
|
520
|
+
# def to_s
|
521
|
+
# end
|
522
|
+
# end
|
523
|
+
const_set('Double', Simple.new(:double))
|
524
|
+
|
525
|
+
# @!parse
|
526
|
+
# class Float < Type
|
527
|
+
# # @return [Symbol] `:float`
|
528
|
+
# # @see Cassandra::Type#kind
|
529
|
+
# def kind
|
530
|
+
# end
|
531
|
+
#
|
532
|
+
# # Coerces the value to Float
|
533
|
+
# # @param value [Object] original value
|
534
|
+
# # @return [Float] value
|
535
|
+
# # @see Cassandra::Type#new
|
536
|
+
# def new(value)
|
537
|
+
# end
|
538
|
+
#
|
539
|
+
# # Asserts that a given value is a Float
|
540
|
+
# # @param value [Object] value to be validated
|
541
|
+
# # @param message [String] error message to use when assertion
|
542
|
+
# # fails
|
543
|
+
# # @yieldreturn [String] error message to use when assertion fails
|
544
|
+
# # @raise [ArgumentError] if the value is not a Float
|
545
|
+
# # @return [void]
|
546
|
+
# # @see Cassandra::Type#assert
|
547
|
+
# def assert(value, message = nil, &block)
|
548
|
+
# end
|
549
|
+
#
|
550
|
+
# # @return [String] `"float"`
|
551
|
+
# # @see Cassandra::Type#to_s
|
552
|
+
# def to_s
|
553
|
+
# end
|
554
|
+
# end
|
555
|
+
const_set('Float', Simple.new(:float))
|
556
|
+
|
557
|
+
# @!parse
|
558
|
+
# class Inet < Type
|
559
|
+
# # @return [Symbol] `:inet`
|
560
|
+
# # @see Cassandra::Type#kind
|
561
|
+
# def kind
|
562
|
+
# end
|
563
|
+
#
|
564
|
+
# # Coerces the value to IPAddr
|
565
|
+
# # @param value [Object] original value
|
566
|
+
# # @return [IPAddr] value
|
567
|
+
# # @see Cassandra::Type#new
|
568
|
+
# def new(value)
|
569
|
+
# end
|
570
|
+
#
|
571
|
+
# # Asserts that a given value is an IPAddr
|
572
|
+
# # @param value [Object] value to be validated
|
573
|
+
# # @param message [String] error message to use when assertion
|
574
|
+
# # fails
|
575
|
+
# # @yieldreturn [String] error message to use when assertion fails
|
576
|
+
# # @raise [ArgumentError] if the value is not an IPAddr
|
577
|
+
# # @return [void]
|
578
|
+
# # @see Cassandra::Type#assert
|
579
|
+
# def assert(value, message = nil, &block)
|
580
|
+
# end
|
581
|
+
#
|
582
|
+
# # @return [String] `"inet"`
|
583
|
+
# # @see Cassandra::Type#to_s
|
584
|
+
# def to_s
|
585
|
+
# end
|
586
|
+
# end
|
587
|
+
const_set('Inet', Simple.new(:inet))
|
588
|
+
|
589
|
+
# @!parse
|
590
|
+
# class Int < Type
|
591
|
+
# # @return [Symbol] `:int`
|
592
|
+
# # @see Cassandra::Type#kind
|
593
|
+
# def kind
|
594
|
+
# end
|
595
|
+
#
|
596
|
+
# # Coerces the value to Integer
|
597
|
+
# # @param value [Object] original value
|
598
|
+
# # @return [Integer] value
|
599
|
+
# # @see Cassandra::Type#new
|
600
|
+
# def new(value)
|
601
|
+
# end
|
602
|
+
#
|
603
|
+
# # Asserts that a given value is an Integer
|
604
|
+
# # @param value [Object] value to be validated
|
605
|
+
# # @param message [String] error message to use when assertion
|
606
|
+
# # fails
|
607
|
+
# # @yieldreturn [String] error message to use when assertion fails
|
608
|
+
# # @raise [ArgumentError] if the value is not an Integer
|
609
|
+
# # @return [void]
|
610
|
+
# # @see Cassandra::Type#assert
|
611
|
+
# def assert(value, message = nil, &block)
|
612
|
+
# end
|
613
|
+
#
|
614
|
+
# # @return [String] `"int"`
|
615
|
+
# # @see Cassandra::Type#to_s
|
616
|
+
# def to_s
|
617
|
+
# end
|
618
|
+
# end
|
619
|
+
const_set('Int', Simple.new(:int))
|
620
|
+
|
621
|
+
class List < Type
|
622
|
+
# @private
|
623
|
+
attr_reader :value_type
|
624
|
+
|
625
|
+
# @private
|
626
|
+
def initialize(value_type)
|
627
|
+
super(:list)
|
628
|
+
@value_type = value_type
|
629
|
+
end
|
630
|
+
|
631
|
+
# Coerces the value to Array
|
632
|
+
# @param value [Object] original value
|
633
|
+
# @return [Array] value
|
634
|
+
# @see Cassandra::Type#new
|
635
|
+
def new(*value)
|
636
|
+
value = Array(value.first) if value.one?
|
637
|
+
|
638
|
+
value.each do |v|
|
639
|
+
Util.assert_type(@value_type, v)
|
640
|
+
end
|
641
|
+
value
|
642
|
+
end
|
643
|
+
|
644
|
+
# Asserts that a given value is an Array
|
645
|
+
# @param value [Object] value to be validated
|
646
|
+
# @param message [String] error message to use when assertion fails
|
647
|
+
# @yieldreturn [String] error message to use when assertion fails
|
648
|
+
# @raise [ArgumentError] if the value is not an Array
|
649
|
+
# @return [void]
|
650
|
+
# @see Cassandra::Type#assert
|
651
|
+
def assert(value, message = nil, &block)
|
652
|
+
Util.assert_instance_of(::Array, value, message, &block)
|
653
|
+
value.each do |v|
|
654
|
+
Util.assert_type(@value_type, v, message, &block)
|
655
|
+
end
|
656
|
+
nil
|
657
|
+
end
|
658
|
+
|
659
|
+
# @return [String] `"list<type>"`
|
660
|
+
# @see Cassandra::Type#to_s
|
661
|
+
def to_s
|
662
|
+
"list<#{@value_type}>"
|
663
|
+
end
|
664
|
+
|
665
|
+
def hash
|
666
|
+
@hash ||= begin
|
667
|
+
h = 17
|
668
|
+
h = 31 * h + @kind.hash
|
669
|
+
h = 31 * h + @value_type.hash
|
670
|
+
h
|
671
|
+
end
|
672
|
+
end
|
673
|
+
|
674
|
+
def eql?(other)
|
675
|
+
other.is_a?(List) && @value_type == other.value_type
|
676
|
+
end
|
677
|
+
|
678
|
+
alias == eql?
|
679
|
+
end
|
680
|
+
|
681
|
+
class Map < Type
|
682
|
+
# @private
|
683
|
+
attr_reader :key_type, :value_type
|
684
|
+
|
685
|
+
# @private
|
686
|
+
def initialize(key_type, value_type)
|
687
|
+
super(:map)
|
688
|
+
@key_type = key_type
|
689
|
+
@value_type = value_type
|
690
|
+
end
|
691
|
+
|
692
|
+
# Coerces the value to Hash
|
693
|
+
# @param value [Object] original value
|
694
|
+
# @return [Hash] value
|
695
|
+
# @see Cassandra::Type#new
|
696
|
+
def new(*value)
|
697
|
+
value = value.first if value.one?
|
698
|
+
|
699
|
+
case value
|
700
|
+
when ::Hash
|
701
|
+
value.each do |k, v|
|
702
|
+
Util.assert_type(@key_type, k)
|
703
|
+
Util.assert_type(@value_type, v)
|
704
|
+
end
|
705
|
+
value
|
706
|
+
when ::Array
|
707
|
+
result = ::Hash.new
|
708
|
+
value.each_slice(2) do |(k, v)|
|
709
|
+
Util.assert_type(@key_type, k)
|
710
|
+
Util.assert_type(@value_type, v)
|
711
|
+
result[k] = v
|
712
|
+
end
|
713
|
+
result
|
714
|
+
else
|
715
|
+
raise ::ArgumentError, "cannot convert #{value.inspect} to #{self}"
|
716
|
+
end
|
717
|
+
end
|
718
|
+
|
719
|
+
# Asserts that a given value is a Hash
|
720
|
+
# @param value [Object] value to be validated
|
721
|
+
# @param message [String] error message to use when assertion fails
|
722
|
+
# @yieldreturn [String] error message to use when assertion fails
|
723
|
+
# @raise [ArgumentError] if the value is not a Hash
|
724
|
+
# @return [void]
|
725
|
+
# @see Cassandra::Type#assert
|
726
|
+
def assert(value, message = nil, &block)
|
727
|
+
Util.assert_instance_of(::Hash, value, message, &block)
|
728
|
+
value.each do |k, v|
|
729
|
+
Util.assert_type(@key_type, k, message, &block)
|
730
|
+
Util.assert_type(@value_type, v, message, &block)
|
731
|
+
end
|
732
|
+
nil
|
733
|
+
end
|
734
|
+
|
735
|
+
# @return [String] `"map<type, type>"`
|
736
|
+
# @see Cassandra::Type#to_s
|
737
|
+
def to_s
|
738
|
+
"map<#{@key_type}, #{@value_type}>"
|
739
|
+
end
|
740
|
+
|
741
|
+
def hash
|
742
|
+
@hash ||= begin
|
743
|
+
h = 17
|
744
|
+
h = 31 * h + @kind.hash
|
745
|
+
h = 31 * h + @key_type.hash
|
746
|
+
h = 31 * h + @value_type.hash
|
747
|
+
h
|
748
|
+
end
|
749
|
+
end
|
750
|
+
|
751
|
+
def eql?(other)
|
752
|
+
other.is_a?(Map) &&
|
753
|
+
@key_type == other.key_type &&
|
754
|
+
@value_type == other.value_type
|
755
|
+
end
|
756
|
+
|
757
|
+
alias == eql?
|
758
|
+
end
|
759
|
+
|
760
|
+
class Set < Type
|
761
|
+
# @private
|
762
|
+
attr_reader :value_type
|
763
|
+
|
764
|
+
# @private
|
765
|
+
def initialize(value_type)
|
766
|
+
super(:set)
|
767
|
+
@value_type = value_type
|
768
|
+
end
|
769
|
+
|
770
|
+
# Coerces the value to Set
|
771
|
+
# @param value [Object] original value
|
772
|
+
# @return [Set] value
|
773
|
+
# @see Cassandra::Type#new
|
774
|
+
# @example Creating a set using splat arguments
|
775
|
+
# include Cassandra::Types
|
776
|
+
#
|
777
|
+
# set(varchar).new('Jane', 'Alice', 'Loren') => #<Set: {"Jane", "Alice", "Loren"}>
|
778
|
+
#
|
779
|
+
# @example Coercing an existing set
|
780
|
+
# include Cassandra::Types
|
781
|
+
#
|
782
|
+
# set(varchar).new(Set['Jane', 'Alice', 'Loren']) => #<Set: {"Jane", "Alice", "Loren"}>
|
783
|
+
#
|
784
|
+
# @example Coercing an array
|
785
|
+
# include Cassandra::Types
|
786
|
+
#
|
787
|
+
# set(varchar).new(['Jane', 'Alice', 'Loren']) => #<Set: {"Jane", "Alice", "Loren"}>
|
788
|
+
def new(*value)
|
789
|
+
value = value.first if value.one?
|
790
|
+
|
791
|
+
case value
|
792
|
+
when ::Array
|
793
|
+
result = ::Set.new
|
794
|
+
value.each do |v|
|
795
|
+
Util.assert_type(@value_type, v)
|
796
|
+
result << v
|
797
|
+
end
|
798
|
+
result
|
799
|
+
when ::Set
|
800
|
+
value.each do |v|
|
801
|
+
Util.assert_type(@value_type, v)
|
802
|
+
end
|
803
|
+
value
|
804
|
+
else
|
805
|
+
Util.assert_type(@value_type, value)
|
806
|
+
::Set[value]
|
807
|
+
end
|
808
|
+
end
|
809
|
+
|
810
|
+
# Asserts that a given value is an Set
|
811
|
+
# @param value [Object] value to be validated
|
812
|
+
# @param message [String] error message to use when assertion fails
|
813
|
+
# @yieldreturn [String] error message to use when assertion fails
|
814
|
+
# @raise [ArgumentError] if the value is not an Set
|
815
|
+
# @return [void]
|
816
|
+
# @see Cassandra::Type#assert
|
817
|
+
def assert(value, message = nil, &block)
|
818
|
+
Util.assert_instance_of(::Set, value, message, &block)
|
819
|
+
value.each do |v|
|
820
|
+
Util.assert_type(@value_type, v, message, &block)
|
821
|
+
end
|
822
|
+
nil
|
823
|
+
end
|
824
|
+
|
825
|
+
# @return [String] `"set<type>"`
|
826
|
+
# @see Cassandra::Type#to_s
|
827
|
+
def to_s
|
828
|
+
"set<#{@value_type}>"
|
829
|
+
end
|
830
|
+
|
831
|
+
def hash
|
832
|
+
@hash ||= begin
|
833
|
+
h = 17
|
834
|
+
h = 31 * h + @kind.hash
|
835
|
+
h = 31 * h + @value_type.hash
|
836
|
+
h
|
837
|
+
end
|
838
|
+
end
|
839
|
+
|
840
|
+
def eql?(other)
|
841
|
+
other.is_a?(Set) && @value_type == other.value_type
|
842
|
+
end
|
843
|
+
|
844
|
+
alias == eql?
|
845
|
+
end
|
846
|
+
|
847
|
+
# @!parse
|
848
|
+
# class Smallint < Type
|
849
|
+
# # @return [Symbol] `:smallint`
|
850
|
+
# # @see Cassandra::Type#kind
|
851
|
+
# def kind
|
852
|
+
# end
|
853
|
+
#
|
854
|
+
# # Coerces the value to an Integer
|
855
|
+
# # @param value [Object] original value
|
856
|
+
# # @return [Integer] value
|
857
|
+
# # @see Cassandra::Type#new
|
858
|
+
# def new(value)
|
859
|
+
# end
|
860
|
+
#
|
861
|
+
# # Asserts that a given value is an Integer
|
862
|
+
# # @param value [Object] value to be validated
|
863
|
+
# # @param message [String] error message to use when assertion
|
864
|
+
# # fails
|
865
|
+
# # @yieldreturn [String] error message to use when assertion fails
|
866
|
+
# # @raise [ArgumentError] if the value is not an Integer
|
867
|
+
# # @return [void]
|
868
|
+
# # @see Cassandra::Type#assert
|
869
|
+
# def assert(value, message = nil, &block)
|
870
|
+
# end
|
871
|
+
#
|
872
|
+
# # @return [String] `"smallint"`
|
873
|
+
# # @see Cassandra::Type#to_s
|
874
|
+
# def to_s
|
875
|
+
# end
|
876
|
+
# end
|
877
|
+
const_set('Smallint', Simple.new(:smallint))
|
878
|
+
|
879
|
+
# @!parse
|
880
|
+
# class Text < Type
|
881
|
+
# # @return [Symbol] `:text`
|
882
|
+
# # @see Cassandra::Type#kind
|
883
|
+
# def kind
|
884
|
+
# end
|
885
|
+
#
|
886
|
+
# # Coerces the value to String
|
887
|
+
# # @param value [Object] original value
|
888
|
+
# # @return [String] value
|
889
|
+
# # @see Cassandra::Type#new
|
890
|
+
# def new(value)
|
891
|
+
# end
|
892
|
+
#
|
893
|
+
# # Asserts that a given value is a String
|
894
|
+
# # @param value [Object] value to be validated
|
895
|
+
# # @param message [String] error message to use when assertion
|
896
|
+
# # fails
|
897
|
+
# # @yieldreturn [String] error message to use when assertion fails
|
898
|
+
# # @raise [ArgumentError] if the value is not a String
|
899
|
+
# # @return [void]
|
900
|
+
# # @see Cassandra::Type#assert
|
901
|
+
# def assert(value, message = nil, &block)
|
902
|
+
# end
|
903
|
+
#
|
904
|
+
# # @return [String] `"text"`
|
905
|
+
# # @see Cassandra::Type#to_s
|
906
|
+
# def to_s
|
907
|
+
# end
|
908
|
+
# end
|
909
|
+
const_set('Text', Simple.new(:text))
|
910
|
+
|
911
|
+
# @!parse
|
912
|
+
# class Time < Type
|
913
|
+
# # @return [Symbol] `:time`
|
914
|
+
# # @see Cassandra::Type#kind
|
915
|
+
# def kind
|
916
|
+
# end
|
917
|
+
#
|
918
|
+
# # Coerces the value to Time
|
919
|
+
# # @param value [Object] original value
|
920
|
+
# # @return [Time] value
|
921
|
+
# # @see Cassandra::Type#new
|
922
|
+
# def new(value)
|
923
|
+
# end
|
924
|
+
#
|
925
|
+
# # Asserts that a given value is a Time
|
926
|
+
# # @param value [Object] value to be validated
|
927
|
+
# # @param message [String] error message to use when assertion
|
928
|
+
# # fails
|
929
|
+
# # @yieldreturn [String] error message to use when assertion fails
|
930
|
+
# # @raise [ArgumentError] if the value is not a String
|
931
|
+
# # @return [void]
|
932
|
+
# # @see Cassandra::Type#assert
|
933
|
+
# def assert(value, message = nil, &block)
|
934
|
+
# end
|
935
|
+
#
|
936
|
+
# # @return [String] `"time"`
|
937
|
+
# # @see Cassandra::Type#to_s
|
938
|
+
# def to_s
|
939
|
+
# end
|
940
|
+
# end
|
941
|
+
const_set('Time', Simple.new(:time))
|
942
|
+
|
943
|
+
# @!parse
|
944
|
+
# class Timestamp < Type
|
945
|
+
# # @return [Symbol] `:timestamp`
|
946
|
+
# # @see Cassandra::Type#kind
|
947
|
+
# def kind
|
948
|
+
# end
|
949
|
+
#
|
950
|
+
# # Coerces the value to Time
|
951
|
+
# # @param value [Object] original value
|
952
|
+
# # @return [Time] value
|
953
|
+
# # @see Cassandra::Type#new
|
954
|
+
# def new(value)
|
955
|
+
# end
|
956
|
+
#
|
957
|
+
# # Asserts that a given value is a Time
|
958
|
+
# # @param value [Object] value to be validated
|
959
|
+
# # @param message [String] error message to use when assertion
|
960
|
+
# # fails
|
961
|
+
# # @yieldreturn [String] error message to use when assertion fails
|
962
|
+
# # @raise [ArgumentError] if the value is not a Time
|
963
|
+
# # @return [void]
|
964
|
+
# # @see Cassandra::Type#assert
|
965
|
+
# def assert(value, message = nil, &block)
|
966
|
+
# end
|
967
|
+
#
|
968
|
+
# # @return [String] `"timestamp"`
|
969
|
+
# # @see Cassandra::Type#to_s
|
970
|
+
# def to_s
|
971
|
+
# end
|
972
|
+
# end
|
973
|
+
const_set('Timestamp', Simple.new(:timestamp))
|
974
|
+
|
975
|
+
# @!parse
|
976
|
+
# class Timeuuid < Type
|
977
|
+
# # @return [Symbol] `:timeuuid`
|
978
|
+
# # @see Cassandra::Type#kind
|
979
|
+
# def kind
|
980
|
+
# end
|
981
|
+
#
|
982
|
+
# # Coerces the value to Cassandra::Timeuuid
|
983
|
+
# # @param value [Object] original value
|
984
|
+
# # @return [Cassandra::Timeuuid] value
|
985
|
+
# # @see Cassandra::Type#new
|
986
|
+
# def new(value)
|
987
|
+
# end
|
988
|
+
#
|
989
|
+
# # Asserts that a given value is a Cassandra::Timeuuid
|
990
|
+
# # @param value [Object] value to be validated
|
991
|
+
# # @param message [String] error message to use when assertion
|
992
|
+
# # fails
|
993
|
+
# # @yieldreturn [String] error message to use when assertion fails
|
994
|
+
# # @raise [ArgumentError] if the value is not a Cassandra::Timeuuid
|
995
|
+
# # @return [void]
|
996
|
+
# # @see Cassandra::Type#assert
|
997
|
+
# def assert(value, message = nil, &block)
|
998
|
+
# end
|
999
|
+
#
|
1000
|
+
# # @return [String] `"timeuuid"`
|
1001
|
+
# # @see Cassandra::Type#to_s
|
1002
|
+
# def to_s
|
1003
|
+
# end
|
1004
|
+
# end
|
1005
|
+
const_set('Timeuuid', Simple.new(:timeuuid))
|
1006
|
+
|
1007
|
+
# @!parse
|
1008
|
+
# class Tinyint < Type
|
1009
|
+
# # @return [Symbol] `:tinyint`
|
1010
|
+
# # @see Cassandra::Type#kind
|
1011
|
+
# def kind
|
1012
|
+
# end
|
1013
|
+
#
|
1014
|
+
# # Coerces the value to Integer
|
1015
|
+
# # @param value [Object] original value
|
1016
|
+
# # @return [Integer] value
|
1017
|
+
# # @see Cassandra::Type#new
|
1018
|
+
# def new(value)
|
1019
|
+
# end
|
1020
|
+
#
|
1021
|
+
# # Asserts that a given value is an Integer
|
1022
|
+
# # @param value [Object] value to be validated
|
1023
|
+
# # @param message [String] error message to use when assertion
|
1024
|
+
# # fails
|
1025
|
+
# # @yieldreturn [String] error message to use when assertion fails
|
1026
|
+
# # @raise [ArgumentError] if the value is not an Integer
|
1027
|
+
# # @return [void]
|
1028
|
+
# # @see Cassandra::Type#assert
|
1029
|
+
# def assert(value, message = nil, &block)
|
1030
|
+
# end
|
1031
|
+
#
|
1032
|
+
# # @return [String] `"tinyint"`
|
1033
|
+
# # @see Cassandra::Type#to_s
|
1034
|
+
# def to_s
|
1035
|
+
# end
|
1036
|
+
# end
|
1037
|
+
const_set('Tinyint', Simple.new(:tinyint))
|
1038
|
+
|
1039
|
+
class Tuple < Type
|
1040
|
+
# @private
|
1041
|
+
attr_reader :members
|
1042
|
+
|
1043
|
+
# @private
|
1044
|
+
def initialize(*members)
|
1045
|
+
super(:tuple)
|
1046
|
+
@members = members
|
1047
|
+
end
|
1048
|
+
|
1049
|
+
# Coerces the value to Cassandra::Tuple
|
1050
|
+
# @param values [*Object] tuple values
|
1051
|
+
# @return [Cassandra::Tuple] value
|
1052
|
+
# @see Cassandra::Type#new
|
1053
|
+
# @example Creating a tuple
|
1054
|
+
# include Cassandra::Types
|
1055
|
+
#
|
1056
|
+
# tuple(varchar, varchar, int).new('Jane', 'Smith', 38) # => (Jane, Smith, 38)
|
1057
|
+
def new(*values)
|
1058
|
+
Util.assert(values.size <= @members.size) do
|
1059
|
+
"too many values: #{values.size} out of max #{@members.size}"
|
1060
|
+
end
|
1061
|
+
values.each_with_index do |v, i|
|
1062
|
+
Util.assert_type(@members[i], v)
|
1063
|
+
end
|
1064
|
+
Cassandra::Tuple::Strict.new(@members, values)
|
1065
|
+
end
|
1066
|
+
|
1067
|
+
# Asserts that a given value is an Cassandra::Tuple
|
1068
|
+
# @param value [Object] value to be validated
|
1069
|
+
# @param message [String] error message to use when assertion fails
|
1070
|
+
# @yieldreturn [String] error message to use when assertion fails
|
1071
|
+
# @raise [ArgumentError] if the value is not an Cassandra::Tuple
|
1072
|
+
# @return [void]
|
1073
|
+
# @see Cassandra::Type#assert
|
1074
|
+
def assert(value, message = nil, &block)
|
1075
|
+
Util.assert_instance_of(Cassandra::Tuple, value, message, &block)
|
1076
|
+
Util.assert(value.size <= @members.size, message, &block)
|
1077
|
+
@members.each_with_index do |type, i|
|
1078
|
+
Util.assert_type(type, value[i], message, &block)
|
1079
|
+
end
|
1080
|
+
nil
|
1081
|
+
end
|
1082
|
+
|
1083
|
+
# @return [String] `"tuple<type, type, type...>"`
|
1084
|
+
# @see Cassandra::Type#to_s
|
1085
|
+
def to_s
|
1086
|
+
"tuple<#{@members.map(&:to_s).join(', ')}>"
|
1087
|
+
end
|
1088
|
+
|
1089
|
+
def hash
|
1090
|
+
@hash ||= begin
|
1091
|
+
h = 17
|
1092
|
+
h = 31 * h + @kind.hash
|
1093
|
+
h = 31 * h + @members.hash
|
1094
|
+
h
|
1095
|
+
end
|
1096
|
+
end
|
1097
|
+
|
1098
|
+
def eql?(other)
|
1099
|
+
other.is_a?(Tuple) && @members == other.members
|
1100
|
+
end
|
1101
|
+
|
1102
|
+
alias == eql?
|
1103
|
+
end
|
1104
|
+
|
1105
|
+
# @!parse
|
1106
|
+
# class Uuid < Type
|
1107
|
+
# # @return [Symbol] `:uuid`
|
1108
|
+
# # @see Cassandra::Type#kind
|
1109
|
+
# def kind
|
1110
|
+
# end
|
1111
|
+
#
|
1112
|
+
# # Coerces the value to Cassandra::Uuid
|
1113
|
+
# # @param value [Object] original value
|
1114
|
+
# # @return [Cassandra::Uuid] value
|
1115
|
+
# # @see Cassandra::Type#new
|
1116
|
+
# def new(value)
|
1117
|
+
# end
|
1118
|
+
#
|
1119
|
+
# # Asserts that a given value is a Cassandra::Uuid
|
1120
|
+
# # @param value [Object] value to be validated
|
1121
|
+
# # @param message [String] error message to use when assertion
|
1122
|
+
# # fails
|
1123
|
+
# # @yieldreturn [String] error message to use when assertion fails
|
1124
|
+
# # @raise [ArgumentError] if the value is not a Cassandra::Uuid
|
1125
|
+
# # @return [void]
|
1126
|
+
# # @see Cassandra::Type#assert
|
1127
|
+
# def assert(value, message = nil, &block)
|
1128
|
+
# end
|
1129
|
+
#
|
1130
|
+
# # @return [String] `"uuid"`
|
1131
|
+
# # @see Cassandra::Type#to_s
|
1132
|
+
# def to_s
|
1133
|
+
# end
|
1134
|
+
# end
|
1135
|
+
const_set('Uuid', Simple.new(:uuid))
|
1136
|
+
|
1137
|
+
# @!parse
|
1138
|
+
# class Varint < Type
|
1139
|
+
# # @return [Symbol] `:varint`
|
1140
|
+
# # @see Cassandra::Type#kind
|
1141
|
+
# def kind
|
1142
|
+
# end
|
1143
|
+
#
|
1144
|
+
# # Coerces the value to Integer
|
1145
|
+
# # @param value [Object] original value
|
1146
|
+
# # @return [Integer] value
|
1147
|
+
# # @see Cassandra::Type#new
|
1148
|
+
# def new(value)
|
1149
|
+
# end
|
1150
|
+
#
|
1151
|
+
# # Asserts that a given value is an Integer
|
1152
|
+
# # @param value [Object] value to be validated
|
1153
|
+
# # @param message [String] error message to use when assertion
|
1154
|
+
# # fails
|
1155
|
+
# # @yieldreturn [String] error message to use when assertion fails
|
1156
|
+
# # @raise [ArgumentError] if the value is not an Integer
|
1157
|
+
# # @return [void]
|
1158
|
+
# # @see Cassandra::Type#assert
|
1159
|
+
# def assert(value, message = nil, &block)
|
1160
|
+
# end
|
1161
|
+
#
|
1162
|
+
# # @return [String] `"varint"`
|
1163
|
+
# # @see Cassandra::Type#to_s
|
1164
|
+
# def to_s
|
1165
|
+
# end
|
1166
|
+
# end
|
1167
|
+
const_set('Varint', Simple.new(:varint))
|
1168
|
+
|
1169
|
+
class UserDefined < Type
|
1170
|
+
class Field
|
1171
|
+
# @return [String] name of this field
|
1172
|
+
attr_reader :name
|
1173
|
+
# @return [Cassandra::Type] type of this field
|
1174
|
+
attr_reader :type
|
1175
|
+
|
1176
|
+
# @private
|
1177
|
+
def initialize(name, type)
|
1178
|
+
@name = name
|
1179
|
+
@type = type
|
1180
|
+
end
|
1181
|
+
|
1182
|
+
# String representation of the field
|
1183
|
+
# @return [String] String representation of the field
|
1184
|
+
def to_s
|
1185
|
+
"#{@name} #{@type}"
|
1186
|
+
end
|
1187
|
+
|
1188
|
+
def hash
|
1189
|
+
@hash ||= begin
|
1190
|
+
h = 17
|
1191
|
+
h = 31 * h + @name.hash
|
1192
|
+
h = 31 * h + @type.hash
|
1193
|
+
h
|
1194
|
+
end
|
1195
|
+
end
|
1196
|
+
|
1197
|
+
def eql?(other)
|
1198
|
+
other.is_a?(Field) &&
|
1199
|
+
@name == other.name &&
|
1200
|
+
@type == other.type
|
1201
|
+
end
|
1202
|
+
|
1203
|
+
alias == eql?
|
1204
|
+
end
|
1205
|
+
|
1206
|
+
# @return [String] keyspace where this type is defined
|
1207
|
+
attr_reader :keyspace
|
1208
|
+
|
1209
|
+
# @return [String] name of this type
|
1210
|
+
attr_reader :name
|
1211
|
+
|
1212
|
+
# @private
|
1213
|
+
attr_reader :fields
|
1214
|
+
|
1215
|
+
# @private
|
1216
|
+
def initialize(keyspace, name, fields)
|
1217
|
+
super(:udt)
|
1218
|
+
@keyspace = keyspace
|
1219
|
+
@name = name
|
1220
|
+
@fields = fields
|
1221
|
+
end
|
1222
|
+
|
1223
|
+
# @param name [String] field name
|
1224
|
+
# @return [Boolean] whether this type has a given field
|
1225
|
+
def has_field?(name)
|
1226
|
+
@fields.any? { |f| f.name == name }
|
1227
|
+
end
|
1228
|
+
|
1229
|
+
# Yield or enumerate each field defined in this type
|
1230
|
+
# @overload each_field
|
1231
|
+
# @yieldparam field [Cassandra::UserDefined::Field] field
|
1232
|
+
# @return [Cassandra::Types::UserDefined] self
|
1233
|
+
# @overload each_field
|
1234
|
+
# @return [Array<Array<String, Cassandra::Type>>] a list of fields
|
1235
|
+
def each_field(&block)
|
1236
|
+
if block_given?
|
1237
|
+
@fields.each(&block)
|
1238
|
+
self
|
1239
|
+
else
|
1240
|
+
@fields.dup
|
1241
|
+
end
|
1242
|
+
end
|
1243
|
+
|
1244
|
+
alias fields each_field
|
1245
|
+
|
1246
|
+
# @param name [String] field name
|
1247
|
+
# @return [Cassandra::UserDefined::Field, nil] a field with this name or
|
1248
|
+
# nil
|
1249
|
+
def field(name)
|
1250
|
+
@fields.find { |f| f.name == name }
|
1251
|
+
end
|
1252
|
+
|
1253
|
+
# Coerces the value to Cassandra::UDT
|
1254
|
+
# @param value [Object] original value
|
1255
|
+
# @return [Cassandra::UDT] value
|
1256
|
+
# @see Cassandra::Type#new
|
1257
|
+
def new(*value)
|
1258
|
+
value = value.first if value.one?
|
1259
|
+
value = Array(value) unless value.is_a?(::Hash)
|
1260
|
+
|
1261
|
+
Util.assert(value.size <= @fields.size) do
|
1262
|
+
"too many values: #{value.size} out of #{@fields.size}"
|
1263
|
+
end
|
1264
|
+
|
1265
|
+
case value
|
1266
|
+
when ::Array
|
1267
|
+
result = ::Hash.new
|
1268
|
+
value.each_with_index do |v, i|
|
1269
|
+
f = @fields[i]
|
1270
|
+
Util.assert_type(f.type, v)
|
1271
|
+
result[f.name] = v
|
1272
|
+
end
|
1273
|
+
when ::Hash
|
1274
|
+
result = ::Hash.new
|
1275
|
+
@fields.each do |f|
|
1276
|
+
n = f.name
|
1277
|
+
v = value[n]
|
1278
|
+
Util.assert_type(f.type, v)
|
1279
|
+
result[n] = v
|
1280
|
+
end
|
1281
|
+
end
|
1282
|
+
|
1283
|
+
Cassandra::UDT::Strict.new(@keyspace, @name, @fields, result)
|
1284
|
+
end
|
1285
|
+
|
1286
|
+
# Asserts that a given value is an Cassandra::UDT
|
1287
|
+
# @param value [Object] value to be validated
|
1288
|
+
# @param message [String] error message to use when assertion fails
|
1289
|
+
# @yieldreturn [String] error message to use when assertion fails
|
1290
|
+
# @raise [ArgumentError] if the value is not an Cassandra::UDT
|
1291
|
+
# @return [void]
|
1292
|
+
# @see Cassandra::Type#assert
|
1293
|
+
def assert(value, message = nil, &block)
|
1294
|
+
Util.assert_instance_of(Cassandra::UDT, value, message, &block)
|
1295
|
+
Util.assert(value.size <= @fields.size, message, &block)
|
1296
|
+
@fields.each do |field|
|
1297
|
+
Util.assert_type(field.type, value[field.name], message, &block)
|
1298
|
+
end
|
1299
|
+
nil
|
1300
|
+
end
|
1301
|
+
|
1302
|
+
# @return [String] `"keyspace.name"`
|
1303
|
+
# @see Cassandra::Type#to_s
|
1304
|
+
def to_s
|
1305
|
+
"#{Util.escape_name(@keyspace)}.#{Util.escape_name(@name)} " \
|
1306
|
+
"{#{@fields.join(', ')}}"
|
1307
|
+
end
|
1308
|
+
|
1309
|
+
def hash
|
1310
|
+
@hash ||= begin
|
1311
|
+
h = 17
|
1312
|
+
h = 31 * h + @kind.hash
|
1313
|
+
h = 31 * h + @keyspace.hash
|
1314
|
+
h = 31 * h + @name.hash
|
1315
|
+
h = 31 * h + @fields.hash
|
1316
|
+
h
|
1317
|
+
end
|
1318
|
+
end
|
1319
|
+
|
1320
|
+
def eql?(other)
|
1321
|
+
other.is_a?(UserDefined) &&
|
1322
|
+
@keyspace == other.keyspace &&
|
1323
|
+
@name == other.name &&
|
1324
|
+
@fields == other.fields
|
1325
|
+
end
|
1326
|
+
|
1327
|
+
alias == eql?
|
1328
|
+
|
1329
|
+
# Output this type in CQL
|
1330
|
+
def to_cql
|
1331
|
+
cql = "CREATE TYPE #{Util.escape_name(@keyspace)}.#{Util.escape_name(@name)} " \
|
1332
|
+
"(\n"
|
1333
|
+
first = true
|
1334
|
+
|
1335
|
+
@fields.each do |field|
|
1336
|
+
if first
|
1337
|
+
first = false
|
1338
|
+
else
|
1339
|
+
cql << ",\n" unless first
|
1340
|
+
end
|
1341
|
+
cql << " #{field.name} #{type_to_cql(field.type)}"
|
1342
|
+
end
|
1343
|
+
|
1344
|
+
cql << "\n);"
|
1345
|
+
|
1346
|
+
cql
|
1347
|
+
end
|
1348
|
+
|
1349
|
+
private
|
1350
|
+
|
1351
|
+
# @private
|
1352
|
+
def type_to_cql(type)
|
1353
|
+
case type.kind
|
1354
|
+
when :tuple
|
1355
|
+
"frozen <#{type}>"
|
1356
|
+
when :udt
|
1357
|
+
if @keyspace == type.keyspace
|
1358
|
+
"frozen <#{Util.escape_name(type.name)}>"
|
1359
|
+
else
|
1360
|
+
"frozen <#{Util.escape_name(type.keyspace)}.#{Util.escape_name(type.name)}>"
|
1361
|
+
end
|
1362
|
+
else
|
1363
|
+
type.to_s
|
1364
|
+
end
|
1365
|
+
end
|
1366
|
+
end
|
1367
|
+
|
1368
|
+
class Custom < Type
|
1369
|
+
attr_reader :name
|
1370
|
+
|
1371
|
+
def initialize(name)
|
1372
|
+
super(:custom)
|
1373
|
+
@name = name
|
1374
|
+
end
|
1375
|
+
|
1376
|
+
# Coerces a given value to this type
|
1377
|
+
#
|
1378
|
+
# @param value [*Object] value to be coerced
|
1379
|
+
# @return [Object] a value of this type
|
1380
|
+
def new(*value)
|
1381
|
+
raise ::NotImplementedError,
|
1382
|
+
"unable to create a value for custom type: #{@name.inspect}"
|
1383
|
+
end
|
1384
|
+
|
1385
|
+
# Asserts that a given value is of this type
|
1386
|
+
# @param value [Object] value to be validated
|
1387
|
+
# @param message [String] error message to use when assertion fails
|
1388
|
+
# @yieldreturn [String] error message to use when assertion fails
|
1389
|
+
# @raise [ArgumentError] if the value is invalid
|
1390
|
+
# @return [void]
|
1391
|
+
def assert(value, message = nil, &block)
|
1392
|
+
Util.assert_instance_of(CustomData, value, message, &block)
|
1393
|
+
Util.assert_equal(self, value.class.type, message, &block)
|
1394
|
+
end
|
1395
|
+
|
1396
|
+
# @return [String] a cassandra representation of this type
|
1397
|
+
def to_s
|
1398
|
+
"'#{@name}'"
|
1399
|
+
end
|
1400
|
+
|
1401
|
+
def hash
|
1402
|
+
@hash ||= begin
|
1403
|
+
h = 17
|
1404
|
+
h = 31 * h + @kind.hash
|
1405
|
+
h = 31 * h + @name.hash
|
1406
|
+
h
|
1407
|
+
end
|
1408
|
+
end
|
1409
|
+
|
1410
|
+
def eql?(other)
|
1411
|
+
other.is_a?(Custom) && @name == other.name
|
1412
|
+
end
|
1413
|
+
|
1414
|
+
alias == eql?
|
1415
|
+
end
|
1416
|
+
|
1417
|
+
class Frozen < Type
|
1418
|
+
# @private
|
1419
|
+
attr_reader :value_type
|
1420
|
+
|
1421
|
+
# @private
|
1422
|
+
def initialize(value_type)
|
1423
|
+
super(:frozen)
|
1424
|
+
@value_type = value_type
|
1425
|
+
end
|
1426
|
+
|
1427
|
+
# Coerces the value to Array
|
1428
|
+
# @param value [Object] original value
|
1429
|
+
# @return [Array] value
|
1430
|
+
# @see Cassandra::Type#new
|
1431
|
+
def new(*value)
|
1432
|
+
value = Array(value.first) if value.one?
|
1433
|
+
|
1434
|
+
value.each do |v|
|
1435
|
+
Util.assert_type(@value_type, v)
|
1436
|
+
end
|
1437
|
+
value
|
1438
|
+
end
|
1439
|
+
|
1440
|
+
# Asserts that a given value is an Array
|
1441
|
+
# @param value [Object] value to be validated
|
1442
|
+
# @param message [String] error message to use when assertion fails
|
1443
|
+
# @yieldreturn [String] error message to use when assertion fails
|
1444
|
+
# @raise [ArgumentError] if the value is not an Array
|
1445
|
+
# @return [void]
|
1446
|
+
# @see Cassandra::Type#assert
|
1447
|
+
def assert(value, message = nil, &block)
|
1448
|
+
Util.assert_instance_of(::Array, value, message, &block)
|
1449
|
+
value.each do |v|
|
1450
|
+
Util.assert_type(@value_type, v, message, &block)
|
1451
|
+
end
|
1452
|
+
nil
|
1453
|
+
end
|
1454
|
+
|
1455
|
+
# @return [String] `"list<type>"`
|
1456
|
+
# @see Cassandra::Type#to_s
|
1457
|
+
def to_s
|
1458
|
+
"frozen<#{@value_type}>"
|
1459
|
+
end
|
1460
|
+
|
1461
|
+
def hash
|
1462
|
+
@hash ||= begin
|
1463
|
+
h = 17
|
1464
|
+
h = 31 * h + @kind.hash
|
1465
|
+
h = 31 * h + @value_type.hash
|
1466
|
+
h
|
1467
|
+
end
|
1468
|
+
end
|
1469
|
+
|
1470
|
+
def eql?(other)
|
1471
|
+
other.is_a?(List) && @value_type == other.value_type
|
1472
|
+
end
|
1473
|
+
|
1474
|
+
alias == eql?
|
1475
|
+
end
|
1476
|
+
|
1477
|
+
def frozen(value_type)
|
1478
|
+
Util.assert_instance_of(Cassandra::Type, value_type,
|
1479
|
+
"frozen type must be a Cassandra::Type, #{value_type.inspect} given")
|
1480
|
+
|
1481
|
+
Frozen.new(value_type)
|
1482
|
+
end
|
1483
|
+
|
1484
|
+
# @return [Cassandra::Types::Text] text type since varchar is an alias for text
|
1485
|
+
def varchar
|
1486
|
+
Text
|
1487
|
+
end
|
1488
|
+
|
1489
|
+
# @return [Cassandra::Types::Text] text type
|
1490
|
+
def text
|
1491
|
+
Text
|
1492
|
+
end
|
1493
|
+
|
1494
|
+
# @return [Cassandra::Types::Blob] blob type
|
1495
|
+
def blob
|
1496
|
+
Blob
|
1497
|
+
end
|
1498
|
+
|
1499
|
+
# @return [Cassandra::Types::Ascii] ascii type
|
1500
|
+
def ascii
|
1501
|
+
Ascii
|
1502
|
+
end
|
1503
|
+
|
1504
|
+
# @return [Cassandra::Types::Bigint] bigint type
|
1505
|
+
def bigint
|
1506
|
+
Bigint
|
1507
|
+
end
|
1508
|
+
|
1509
|
+
# @return [Cassandra::Types::Counter] counter type
|
1510
|
+
def counter
|
1511
|
+
Counter
|
1512
|
+
end
|
1513
|
+
|
1514
|
+
# @return [Cassandra::Types::Int] int type
|
1515
|
+
def int
|
1516
|
+
Int
|
1517
|
+
end
|
1518
|
+
|
1519
|
+
# @return [Cassandra::Types::Varint] varint type
|
1520
|
+
def varint
|
1521
|
+
Varint
|
1522
|
+
end
|
1523
|
+
|
1524
|
+
# @return [Cassandra::Types::Boolean] boolean type
|
1525
|
+
def boolean
|
1526
|
+
Boolean
|
1527
|
+
end
|
1528
|
+
|
1529
|
+
# @return [Cassandra::Types::Decimal] decimal type
|
1530
|
+
def decimal
|
1531
|
+
Decimal
|
1532
|
+
end
|
1533
|
+
|
1534
|
+
# @return [Cassandra::Types::Double] double type
|
1535
|
+
def double
|
1536
|
+
Double
|
1537
|
+
end
|
1538
|
+
|
1539
|
+
# @return [Cassandra::Types::Float] float type
|
1540
|
+
def float
|
1541
|
+
Float
|
1542
|
+
end
|
1543
|
+
|
1544
|
+
# @return [Cassandra::Types::Inet] inet type
|
1545
|
+
def inet
|
1546
|
+
Inet
|
1547
|
+
end
|
1548
|
+
|
1549
|
+
# @return [Cassandra::Types::Timestamp] timestamp type
|
1550
|
+
def timestamp
|
1551
|
+
Timestamp
|
1552
|
+
end
|
1553
|
+
|
1554
|
+
# @return [Cassandra::Types::Uuid] uuid type
|
1555
|
+
def uuid
|
1556
|
+
Uuid
|
1557
|
+
end
|
1558
|
+
|
1559
|
+
# @return [Cassandra::Types::Timeuuid] timeuuid type
|
1560
|
+
def timeuuid
|
1561
|
+
Timeuuid
|
1562
|
+
end
|
1563
|
+
|
1564
|
+
# @return [Cassandra::Types::Date] date type
|
1565
|
+
def date
|
1566
|
+
Date
|
1567
|
+
end
|
1568
|
+
|
1569
|
+
# @return [Cassandra::Types::Time] time type
|
1570
|
+
def time
|
1571
|
+
Time
|
1572
|
+
end
|
1573
|
+
|
1574
|
+
# @return [Cassandra::Types::Smallint] smallint type
|
1575
|
+
def smallint
|
1576
|
+
Smallint
|
1577
|
+
end
|
1578
|
+
|
1579
|
+
# @return [Cassandra::Types::Tinyint] tinyint type
|
1580
|
+
def tinyint
|
1581
|
+
Tinyint
|
1582
|
+
end
|
1583
|
+
|
1584
|
+
# @param value_type [Cassandra::Type] the type of elements in this list
|
1585
|
+
# @return [Cassandra::Types::List] list type
|
1586
|
+
def list(value_type)
|
1587
|
+
Util.assert_instance_of(Cassandra::Type, value_type,
|
1588
|
+
"list type must be a Cassandra::Type, #{value_type.inspect} given")
|
1589
|
+
|
1590
|
+
List.new(value_type)
|
1591
|
+
end
|
1592
|
+
|
1593
|
+
# @param key_type [Cassandra::Type] the type of keys in this map
|
1594
|
+
# @param value_type [Cassandra::Type] the type of values in this map
|
1595
|
+
# @return [Cassandra::Types::Map] map type
|
1596
|
+
def map(key_type, value_type)
|
1597
|
+
Util.assert_instance_of(Cassandra::Type, key_type,
|
1598
|
+
"map key type must be a Cassandra::Type, #{key_type.inspect} given")
|
1599
|
+
Util.assert_instance_of(Cassandra::Type, value_type,
|
1600
|
+
"map value type must be a Cassandra::Type, #{value_type.inspect} given")
|
1601
|
+
|
1602
|
+
Map.new(key_type, value_type)
|
1603
|
+
end
|
1604
|
+
|
1605
|
+
# @param value_type [Cassandra::Type] the type of values in this set
|
1606
|
+
# @return [Cassandra::Types::Set] set type
|
1607
|
+
def set(value_type)
|
1608
|
+
Util.assert_instance_of(Cassandra::Type, value_type,
|
1609
|
+
"set type must be a Cassandra::Type, #{value_type.inspect} given")
|
1610
|
+
|
1611
|
+
Set.new(value_type)
|
1612
|
+
end
|
1613
|
+
|
1614
|
+
# @param members [*Cassandra::Type] types of members of this tuple
|
1615
|
+
# @return [Cassandra::Types::Tuple] tuple type
|
1616
|
+
def tuple(*members)
|
1617
|
+
Util.assert_not_empty(members, 'tuple must contain at least one member')
|
1618
|
+
members.each do |member|
|
1619
|
+
Util.assert_instance_of(Cassandra::Type, member,
|
1620
|
+
'each tuple member must be a Cassandra::Type, ' \
|
1621
|
+
"#{member.inspect} given")
|
1622
|
+
end
|
1623
|
+
|
1624
|
+
Tuple.new(*members)
|
1625
|
+
end
|
1626
|
+
|
1627
|
+
# Creates a User Defined Type instance
|
1628
|
+
# @example Various ways of defining the same UDT
|
1629
|
+
# include Cassandra::Types
|
1630
|
+
#
|
1631
|
+
# udt('simplex', 'address', {'street' => varchar,
|
1632
|
+
# 'city' => varchar,
|
1633
|
+
# 'state' => varchar,
|
1634
|
+
# 'zip' => varchar}) #=> simplex.address
|
1635
|
+
#
|
1636
|
+
# udt('simplex', 'address', [['street', varchar],
|
1637
|
+
# ['city', varchar],
|
1638
|
+
# ['state', varchar],
|
1639
|
+
# ['zip', varchar]]) #=> simplex.address
|
1640
|
+
#
|
1641
|
+
# udt('simplex', 'address', ['street', varchar],
|
1642
|
+
# ['city', varchar],
|
1643
|
+
# ['state', varchar],
|
1644
|
+
# ['zip', varchar]) #=> simplex.address
|
1645
|
+
#
|
1646
|
+
# udt('simplex', 'address', 'street', varchar,
|
1647
|
+
# 'city', varchar,
|
1648
|
+
# 'state', varchar,
|
1649
|
+
# 'zip', varchar) #=> simplex.address
|
1650
|
+
# @param keyspace [String] name of the keyspace that this UDT is defined in
|
1651
|
+
# @param name [String] name of this UDT
|
1652
|
+
# @param fields [Hash<String, Cassandra::Type>,
|
1653
|
+
# Array<Array<String, Cassandra::Type>>,
|
1654
|
+
# *(String, Cassandra::Type),
|
1655
|
+
# *Array<String, Cassandra::Type>] UDT field types
|
1656
|
+
# @return [Cassandra::Types::UserDefined] user defined type
|
1657
|
+
def udt(keyspace, name, *fields)
|
1658
|
+
keyspace = String(keyspace)
|
1659
|
+
name = String(name)
|
1660
|
+
fields = Array(fields.first) if fields.one?
|
1661
|
+
|
1662
|
+
Util.assert_not_empty(fields,
|
1663
|
+
'user-defined type must contain at least one field')
|
1664
|
+
|
1665
|
+
if fields.first.is_a?(::Array)
|
1666
|
+
fields = fields.map do |pair|
|
1667
|
+
Util.assert(pair.size == 2,
|
1668
|
+
'fields of a user-defined type must be an Array of name and ' \
|
1669
|
+
"value pairs, #{pair.inspect} given")
|
1670
|
+
Util.assert_instance_of(::String, pair[0],
|
1671
|
+
'each field name for a user-defined type must be a String, ' \
|
1672
|
+
"#{pair[0].inspect} given")
|
1673
|
+
Util.assert_instance_of(Cassandra::Type, pair[1],
|
1674
|
+
'each field type for a user-defined type must be a ' \
|
1675
|
+
"Cassandra::Type, #{pair[1].inspect} given")
|
1676
|
+
|
1677
|
+
UserDefined::Field.new(*pair)
|
1678
|
+
end
|
1679
|
+
else
|
1680
|
+
Util.assert(fields.size.even?,
|
1681
|
+
'fields of a user-defined type must be an Array of alternating ' \
|
1682
|
+
"names and values pairs, #{fields.inspect} given")
|
1683
|
+
fields = fields.each_slice(2).map do |field_name, field_type|
|
1684
|
+
Util.assert_instance_of(::String, field_name,
|
1685
|
+
'each field name for a user-defined type must be a String, ' \
|
1686
|
+
"#{field_name.inspect} given")
|
1687
|
+
Util.assert_instance_of(Cassandra::Type, field_type,
|
1688
|
+
'each field type for a user-defined type must be a ' \
|
1689
|
+
"Cassandra::Type, #{field_type.inspect} given")
|
1690
|
+
|
1691
|
+
UserDefined::Field.new(field_name, field_type)
|
1692
|
+
end
|
1693
|
+
end
|
1694
|
+
|
1695
|
+
UserDefined.new(keyspace, name, fields)
|
1696
|
+
end
|
1697
|
+
|
1698
|
+
# @param name [String] name of the custom type
|
1699
|
+
# @return [Cassandra::Types::Custom] custom type
|
1700
|
+
def custom(name)
|
1701
|
+
Custom.new(name)
|
1702
|
+
end
|
1703
|
+
end
|
1704
|
+
end
|