diff --git a/lib/elastic/esql.rb b/lib/elastic/esql.rb index f0087eb..8ff971d 100644 --- a/lib/elastic/esql.rb +++ b/lib/elastic/esql.rb @@ -67,6 +67,23 @@ def initialize @metadata = [] end + # Dinamically define Class methods to allow static instantiation with Source Commands: + # @see ESQL#from + # @see PromQL#promql + # @see Row#row + # @see Show#show + # @see TS#ts + # @example + # Elastic::ESQL.from('sample_data') + # Elastic::ESQL.row({ a: 1, b: 'two' }) + class << self + SOURCE_COMMANDS.each do |command| + define_method(command) do |*params| + new.send(command, *params) + end + end + end + # Function to build the ES|QL formatted query and return it as a String. # @raise [ArgumentError] if the query has no source command # @return [String] The ES|QL query in ES|QL format. @@ -99,41 +116,6 @@ def rerank(column: nil, query: '') @rerank end - # Class method to allow static instantiation. - # @param [String] index_pattern A list of indices, data streams or aliases. Supports wildcards and date math. - # @example - # Elastic::ESQL.from('sample_data') - # @see https://www.elastic.co/docs/reference/query-languages/esql/commands/source-commands#esql-from - def self.from(index_pattern) - new.from(index_pattern) - end - - # Class method to allow static instantiation. - # @see PromQL#promql - def self.promql(params) - new.promql(params) - end - - # The SHOW source command returns information about the deployment and its capabilities. - # @return [String] 'SHOW INFO' - # @see https://www.elastic.co/docs/reference/query-languages/esql/commands/source-commands#esql-show - def self.show - new.show - end - - # Class method to allow static instantiation. - # @param [Hash] params Receives a Hash - # @option params [String] column_name The column name. In case of duplicate column names, only the - # rightmost duplicate creates a column. - # @option params [String] value The value for the column. Can be a literal, an expression, or a function. - def self.row(*params) - new.row(*params) - end - - def self.ts(*params) - new.ts(*params) - end - # Instance method to allow to update +from+ with +esql.from('different_source')+. # @param [String] index_pattern A list of indices, data streams or aliases. Supports wildcards and date math. def from(index_pattern) @@ -148,19 +130,17 @@ def to_s end # rubocop:disable Naming/MethodName, Naming/BinaryOperatorParameterName - def self.🐔(message) - "ROW CHICKEN(\"#{message}\")" + class << self + def 🐔(message) + "ROW CHICKEN(\"#{message}\")" + end + alias chicken 🐔 end def 🐔(message) self.class.🐔(message) end - alias chicken 🐔 - - class << self - alias chicken 🐔 - end # rubocop:enable Naming/MethodName, Naming/BinaryOperatorParameterName def self.branch @@ -176,29 +156,7 @@ def user_agent(params) private - # Function for eval, row, and other functions that have one or more columns with values specified - # as parameters. The hash_or_string function is called with the caller name since it's the same - # logic to use these parameters. - # TODO: Refactor to accept other types when not a Hash - def hash_param(name, params) - raise_hash_error(name) unless params.is_a?(Hash) - - @query[symbolize(name)] = params.map { |k, v| "#{k} = #{v}" }.join(', ') - self - end - - # Error raised when a function expects a Hash and something else is passed in, with explanation - def raise_hash_error(name) - raise ArgumentError, "#{name.to_s.upcase} needs a Hash as a parameter where the keys are the " \ - 'column names and the value is the function or expression to calculate.' - end - - # Used when building the query from hash params function - def symbolize(name) - name.is_a?(Symbol) ? name : name.to_sym - end - - # Check if we have a source command + # Check if there's a source command present in the query def source_command_present? SOURCE_COMMANDS.map { |c| @query.each_key { |k| return true if k == c } } diff --git a/lib/elastic/sort.rb b/lib/elastic/sort.rb index 993c8b1..74f3469 100644 --- a/lib/elastic/sort.rb +++ b/lib/elastic/sort.rb @@ -18,7 +18,7 @@ module Elastic # The SORT processing command sorts a table on one or more columns. module Sort - # @param sort - The column to sort on. + # @param sort [String] The column to sort on. # @see https://www.elastic.co/docs/reference/query-languages/esql/commands/processing-commands#esql-sort def sort!(column) @query[:sort] = column diff --git a/lib/elastic/util.rb b/lib/elastic/util.rb index a99ad20..0ce83e9 100644 --- a/lib/elastic/util.rb +++ b/lib/elastic/util.rb @@ -74,5 +74,27 @@ def caller_method_name(callers) def build_lookup_joins(joins) joins.map { |a| a.map { |k, v| "LOOKUP JOIN #{k} ON #{v}" } }.flatten.join(' | ') end + + # Function for eval, row, and other functions that have one or more columns with values specified + # as parameters. The hash_or_string function is called with the caller name since it's the same + # logic to use these parameters. + # TODO: Refactor to accept other types when not a Hash + def hash_param(name, params) + raise_hash_error(name) unless params.is_a?(Hash) + + @query[symbolize(name)] = params.map { |k, v| "#{k} = #{v}" }.join(', ') + self + end + + # Error raised when a function expects a Hash and something else is passed in, with explanation + def raise_hash_error(name) + raise ArgumentError, "#{name.to_s.upcase} needs a Hash as a parameter where the keys are the " \ + 'column names and the value is the function or expression to calculate.' + end + + # Used when building the query from hash params function + def symbolize(name) + name.is_a?(Symbol) ? name : name.to_sym + end end end