Class: WorkerPlugins::SelectColumnWithTypeCast

Inherits:
ApplicationService
  • Object
show all
Defined in:
app/services/worker_plugins/select_column_with_type_cast.rb

Instance Method Summary collapse

Methods inherited from ApplicationService

#db_now_value, #postgres?, #quote, #quote_column, #quote_table, #sqlite?

Instance Method Details

#column_to_selectObject



18
19
20
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 18

def column_to_select
  @column_to_select ||= model_class.column_for_attribute(column_name_to_select)
end

#model_classObject



22
23
24
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 22

def model_class
  @model_class ||= query.klass
end

#performObject



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 4

def perform
  return succeed! query.select(column_name_to_select) if same_type?

  if column_to_compare_with.type == :string
    succeed! query_with_varchar
  elsif column_to_compare_with.type == :integer
    succeed! query_with_integer
  else
    raise "Cant handle type cast between types: " \
      "#{model_class.table_name}.#{column_name_to_select} (#{column_to_select.type}) " \
      "#{column_to_compare_with.name} (#{column_to_compare_with.type})"
  end
end

#query_with_integerObject



26
27
28
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 26

def query_with_integer
  query.select("CAST(#{model_class.table_name}.#{column_name_to_select} AS BIGINT)")
end

#query_with_varcharObject



30
31
32
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 30

def query_with_varchar
  query.select("CAST(#{model_class.table_name}.#{column_name_to_select} AS VARCHAR)")
end

#same_type?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 34

def same_type?
  column_to_select.type == column_to_compare_with.type
end