Class: WorkerPlugins::SelectColumnWithTypeCast

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationService

#db_now_value, #postgres?, #quote, #sqlite?

Constructor Details

#initialize(column_name_to_select:, column_to_compare_with:, query:) ⇒ SelectColumnWithTypeCast

Returns a new instance of SelectColumnWithTypeCast.



4
5
6
7
8
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 4

def initialize(column_name_to_select:, column_to_compare_with:, query:)
  @column_name_to_select = column_name_to_select
  @column_to_compare_with = column_to_compare_with
  @query = query
end

Instance Attribute Details

#column_name_to_selectObject (readonly)

Returns the value of attribute column_name_to_select.



2
3
4
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 2

def column_name_to_select
  @column_name_to_select
end

#column_to_compare_withObject (readonly)

Returns the value of attribute column_to_compare_with.



2
3
4
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 2

def column_to_compare_with
  @column_to_compare_with
end

#queryObject (readonly)

Returns the value of attribute query.



2
3
4
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 2

def query
  @query
end

Instance Method Details

#column_to_selectObject



24
25
26
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 24

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

#model_classObject



28
29
30
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 28

def model_class
  @model_class ||= query.klass
end

#performObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 10

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



32
33
34
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 32

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

#query_with_varcharObject



36
37
38
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 36

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

#same_type?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 40

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