Class: AnyQuery::Adapters::Sql Private

Inherits:
Base
  • Object
show all
Defined in:
lib/any_query/adapters/sql.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

API:

  • private

Defined Under Namespace

Classes: Config

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#fallback_where, #group_join_data, #instantiate_model, #load_single, #map_multi_threaded, #parse_field_type, #parse_field_type_boolean, #parse_field_type_date, #parse_field_type_datetime, #parse_field_type_decimal, #parse_field_type_float, #parse_field_type_integer, #parse_field_type_string, #resolve_join, #resolve_path, #resolve_select, #run_external_join

Constructor Details

#initialize(config) ⇒ Sql

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Sql.

API:

  • private



20
21
22
23
24
25
26
27
28
29
# File 'lib/any_query/adapters/sql.rb', line 20

def initialize(config)
  super(config)
  ActiveRecord::Base.establish_connection(@config[:url])
  table_name = @config[:table]

  @rails_model = declare_model!
  @rails_model.table_name = table_name
  @rails_model.inheritance_column = :_sti_disabled
  Object.const_set("AnyQuery#{table_name.classify}", @rails_model)
end

Instance Attribute Details

#rails_modelObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



44
45
46
# File 'lib/any_query/adapters/sql.rb', line 44

def rails_model
  @rails_model
end

Instance Method Details

#declare_model!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/any_query/adapters/sql.rb', line 31

def declare_model!
  Class.new(ActiveRecord::Base) do
    def dig(key, *other)
      data = public_send(key)
      return data if other.empty?

      return unless data.respond_to?(:dig)

      data.dig(*other)
    end
  end
end

#declare_required_associations!(joins) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/any_query/adapters/sql.rb', line 62

def declare_required_associations!(joins)
  joins&.each do |join|
    next if join[:model]._adapter.url != @config[:url]

    relation = join_relation_name(join)

    if join[:as] == :list
      @rails_model.has_many(relation, class_name: join[:model]._adapter.rails_model.to_s,
                                      foreign_key: join[:foreign_key], primary_key: join[:primary_key])
    else
      @rails_model.belongs_to(relation, class_name: join[:model]._adapter.rails_model.to_s,
                                        foreign_key: join[:foreign_key], primary_key: join[:primary_key])
    end
  end
end

#join_relation_name(join) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



91
92
93
94
95
96
97
# File 'lib/any_query/adapters/sql.rb', line 91

def join_relation_name(join)
  if join[:as] == :list
    join[:model].table_name.pluralize.to_sym
  else
    join[:model].table_name.singularize.to_sym
  end
end

#load(_model, select:, joins:, where:, limit:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/any_query/adapters/sql.rb', line 50

def load(_model, select:, joins:, where:, limit:)
  declare_required_associations!(joins)
  chain = @rails_model.all
  chain = chain.where(*where) if where.present?
  chain = chain.limit(limit) if limit.present?
  chain = resolve_joins(chain, joins) if joins.present?

  chain = resolve_select(chain, select) if select.present?

  chain
end

#resolve_joins(data, joins) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/any_query/adapters/sql.rb', line 78

def resolve_joins(data, joins)
  joins.map do |join|
    if join[:model]._adapter.url == @config[:url]
      relation = join_relation_name(join)

      data = data.eager_load(relation)
    else
      resolve_join(data, join)
    end
  end
  data
end

#urlObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



46
47
48
# File 'lib/any_query/adapters/sql.rb', line 46

def url
  @config[:url]
end