class Cassandra::Function
Represents a cassandra user defined function @see Cassandra::Keyspace#each_function
@see Cassandra::Keyspace#function
@see Cassandra::Keyspace#has_function?
Attributes
@return [String] function body
@private
@return [String] function language
@return [String] function name
@return [Cassandra::Type] function return type
Public Class Methods
@private
# File lib/cassandra/function.rb 37 def initialize(keyspace, name, language, type, arguments, body, called_on_null) 38 @keyspace = keyspace 39 @name = name 40 @language = language 41 @type = type 42 @arguments = arguments 43 @body = body 44 @called_on_null = called_on_null 45 46 # Build up an arguments hash keyed on arg-name. 47 @arguments_hash = @arguments.each_with_object({}) do |arg, h| 48 h[arg.name] = arg 49 end 50 end
Public Instance Methods
@param name [String] argument name @return [Cassandra::Argument, nil] an argument or nil
# File lib/cassandra/function.rb 65 def argument(name) 66 @arguments_hash[name] 67 end
Get the list of argument types for this function. @return [Array<Cassandra::Type>] a list of argument types.
# File lib/cassandra/function.rb 89 def argument_types 90 @arguments.map(&:type) 91 end
@return [Boolean] whether this function will be called on null input
# File lib/cassandra/function.rb 53 def called_on_null? 54 @called_on_null 55 end
Yield or enumerate each argument defined in this function @overload each_argument
@yieldparam argument [Cassandra::Argument] current argument @return [Cassandra::Table] self
@overload each_argument
@return [Array<Cassandra::Argument>] a list of arguments
# File lib/cassandra/function.rb 75 def each_argument(&block) 76 if block_given? 77 @arguments.each(&block) 78 self 79 else 80 # We return a dup of the arguments so that the caller can manipulate 81 # the array however they want without affecting the source. 82 @arguments.dup 83 end 84 end
@private
# File lib/cassandra/function.rb 94 def eql?(other) 95 other.is_a?(Function) && \ 96 @keyspace == other.keyspace && \ 97 @name == other.name && \ 98 @language == other.language && \ 99 @type == other.type && \ 100 @arguments == other.arguments && \ 101 @body == other.body && \ 102 @called_on_null == other.called_on_null? 103 end
@param name [String] argument name @return [Boolean] whether this function has a given argument
# File lib/cassandra/function.rb 59 def has_argument?(name) 60 @arguments_hash.key?(name) 61 end
@private
# File lib/cassandra/function.rb 107 def hash 108 @hash ||= begin 109 h = 17 110 h = 31 * h + @keyspace.hash 111 h = 31 * h + @name.hash 112 h = 31 * h + @language.hash 113 h = 31 * h + @type.hash 114 h = 31 * h + @arguments.hash 115 h = 31 * h + @body.hash 116 h = 31 * h + @called_on_null.hash 117 h 118 end 119 end
@private
# File lib/cassandra/function.rb 122 def inspect 123 "#<Cassandra::Function:0x#{object_id.to_s(16)} " \ 124 "@keyspace=#{@keyspace.inspect}, " \ 125 "@name=#{@name.inspect}, " \ 126 "@language=#{@language.inspect}, " \ 127 "@type=#{@type.inspect}, " \ 128 "@arguments=#{@arguments.inspect} " \ 129 "@body=#{@body.inspect}>" 130 end
@return [String] a cql representation of this function
# File lib/cassandra/function.rb 133 def to_cql 134 cql = "CREATE FUNCTION #{Util.escape_name(@keyspace)}.#{Util.escape_name(@name)}(" 135 first = true 136 @arguments.each do |argument| 137 if first 138 first = false 139 else 140 cql << ', ' 141 end 142 cql << "#{argument.name} #{argument.type}" 143 end 144 cql << ')' 145 cql << if @called_on_null 146 "\n CALLED ON NULL INPUT" 147 else 148 "\n RETURNS NULL ON NULL INPUT" 149 end 150 cql << "\n RETURNS #{@type}" 151 cql << "\n LANGUAGE #{@language}" 152 cql << "\n AS $$#{@body}$$" 153 cql << ';' 154 end