Module: RelationToStruct::ActiveRecordConnectionAdapterExtension

Defined in:
lib/relation_to_struct/active_record_connection_adapter_extension.rb

Instance Method Summary collapse

Instance Method Details

#pluck_from_sql(sql, binds = []) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/relation_to_struct/active_record_connection_adapter_extension.rb', line 27

def pluck_from_sql(sql, binds=[])
  sanitized_sql = ActiveRecord::Base.sanitize_sql(sql)
  result = ActiveRecord::Base.uncached do
    select_all(sanitized_sql, "Pluck SQL Load", binds)
  end
  result.cast_values()
end

#run_sql(sql, binds = []) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/relation_to_struct/active_record_connection_adapter_extension.rb', line 70

def run_sql(sql, binds=[])
  sanitized_sql = ActiveRecord::Base.sanitize_sql(sql)
  # We don't need to build a result set unnecessarily; using
  # interface this also ensures we're clearing the result set
  # for manually memory managed object (e.g., when using the
  # PostgreSQL adaptor).
  exec_update(sanitized_sql, "Run SQL", binds)
end

#structs_from_sql(struct_class, sql, binds = []) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/relation_to_struct/active_record_connection_adapter_extension.rb', line 2

def structs_from_sql(struct_class, sql, binds=[])
  sanitized_sql = ActiveRecord::Base.sanitize_sql(sql)
  result = ActiveRecord::Base.uncached do
    select_all(sanitized_sql, "Structs SQL Load", binds)
  end

  if result.columns.size != result.columns.uniq.size
    raise ArgumentError, 'Expected column names to be unique'
  end

  if result.columns != struct_class.members.collect(&:to_s)
    raise ArgumentError, 'Expected column names (and their order) to match struct attribute names'
  end

  if result.columns.size == 1
    result.cast_values().map do |tuple|
      struct_class.new(tuple)
    end
  else
    result.cast_values().map do |tuple|
      struct_class.new(*tuple)
    end
  end
end

#tuple_from_sql(sql, binds = []) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/relation_to_struct/active_record_connection_adapter_extension.rb', line 53

def tuple_from_sql(sql, binds=[])
  sanitized_sql = ActiveRecord::Base.sanitize_sql(sql)
  result = ActiveRecord::Base.uncached do
    select_all(sanitized_sql, "Value SQL Load", binds)
  end
  values = result.cast_values()

  case values.size
  when 0
    nil
  when 1
    result.columns.size == 1 ? values : values[0]
  else
    raise ArgumentError, 'Expected only a single result to be returned'
  end
end

#value_from_sql(sql, binds = []) ⇒ Object

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/relation_to_struct/active_record_connection_adapter_extension.rb', line 35

def value_from_sql(sql, binds=[])
  sanitized_sql = ActiveRecord::Base.sanitize_sql(sql)
  result = ActiveRecord::Base.uncached do
    select_all(sanitized_sql, "Value SQL Load", binds)
  end
  raise ArgumentError, 'Expected exactly one column to be selected' unless result.columns.size == 1

  values = result.cast_values()
  case values.size
  when 0
    nil
  when 1
    values[0]
  else
    raise ArgumentError, 'Expected only a single result to be returned'
  end
end