Class: Inquery::Query

Inherits:
Object
  • Object
show all
Includes:
Mixins::RawSqlUtils, Mixins::SchemaValidation
Defined in:
lib/inquery/query.rb

Overview

Base query class that encapsulates database queries in reusable classes.

Subclasses must implement the ‘call’ method to define the query logic. Optionally, override ‘process’ to transform the query results.

Example:

class FetchActiveUsers < Inquery::Query
  def call
    User.where(active: true)
  end
end

FetchActiveUsers.run # => Returns active users

Direct Known Subclasses

Chainable

Defined Under Namespace

Classes: Chainable

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Query

Initializes a new query instance with the given parameters.

If a schema is defined using ‘schema’ or ‘schema3’, the params will be validated against it before the query is executed.

Parameters:

  • params (Hash) (defaults to: {})

    Parameters for the query

Raises:

  • (Schemacop::Exceptions::ValidationError)

    if params don’t match schema



52
53
54
55
56
57
58
# File 'lib/inquery/query.rb', line 52

def initialize(params = {})
  @params = params

  if _schema
    @params = _schema.validate!(@params)
  end
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



19
20
21
# File 'lib/inquery/query.rb', line 19

def params
  @params
end

Class Method Details

.call(*args) ⇒ Object

Instantiates the query class with the given params and executes only the ‘call’ method, skipping post-processing.

Parameters:

  • args (Hash)

    Parameters to pass to the query

Returns:

  • (Object)

    The raw query results

Raises:

  • (Schemacop::Exceptions::ValidationError)

    if params don’t match schema



41
42
43
# File 'lib/inquery/query.rb', line 41

def self.call(*args)
  new(*args).call
end

.run(*args) ⇒ Object

Instantiates the query class with the given params and executes both ‘call’ and ‘process’ methods.

This is the primary way to execute queries. It runs the query logic defined in ‘call’ and then passes the results through ‘process’ for any post-processing.

Parameters:

  • args (Hash)

    Parameters to pass to the query

Returns:

  • (Object)

    The processed query results

Raises:

  • (Schemacop::Exceptions::ValidationError)

    if params don’t match schema



31
32
33
# File 'lib/inquery/query.rb', line 31

def self.run(*args)
  new(*args).run
end

Instance Method Details

#callObject

Override this method in subclasses to define the query logic.

Returns:

  • (Object)

    Query results (typically an ActiveRecord::Relation)

Raises:

  • (NotImplementedError)

    if not overridden in subclass



71
72
73
# File 'lib/inquery/query.rb', line 71

def call
  fail NotImplementedError
end

#connectionActiveRecord::ConnectionAdapters::AbstractAdapter

Returns the database connection to use for this query.

Override this method if you need to use a different connection than the default ActiveRecord connection.

Returns:

  • (ActiveRecord::ConnectionAdapters::AbstractAdapter)


106
107
108
# File 'lib/inquery/query.rb', line 106

def connection
  ActiveRecord::Base.connection
end

#osparamsMethodAccessibleHash

Returns the query params wrapped in a MethodAccessibleHash for convenient access using dot notation.

Example:

schema3 { str! :name }
def call
  User.where(name: osparams.name) # Access via dot notation
end

Returns:



96
97
98
# File 'lib/inquery/query.rb', line 96

def osparams
  @osparams ||= MethodAccessibleHash.new(params)
end

#process(results) ⇒ Object

Override this method in subclasses to transform the query results.

By default, returns the results unchanged. Common uses include converting to JSON, counting records, or extracting specific fields.

Parameters:

  • results (Object)

    The results from the ‘call’ method

Returns:

  • (Object)

    The processed results



82
83
84
# File 'lib/inquery/query.rb', line 82

def process(results)
  results
end

#runObject

Executes the query by calling ‘call’ and then ‘process’.

Returns:

  • (Object)

    The processed query results



63
64
65
# File 'lib/inquery/query.rb', line 63

def run
  process(call)
end