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, #mysql?, #postgres?, #quote, #quote_column, #quote_table, #sqlite?

Instance Method Details

#column_to_selectObject



16
17
18
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 16

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

#model_classObject



20
21
22
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 20

def model_class
  @model_class ||= query.klass
end

#performObject



4
5
6
7
8
9
10
11
12
13
14
# 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 == :integer
    succeed! query_with_integer
  elsif column_to_compare_with.type == :uuid
    succeed! query_with_uuid
  else
    succeed! query_with_varchar
  end
end

#query_with_integerObject



24
25
26
27
28
29
30
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 24

def query_with_integer
  if mysql?
    query.select("CAST(#{model_class.table_name}.#{column_name_to_select} AS SIGNED)")
  else
    query.select("CAST(#{model_class.table_name}.#{column_name_to_select} AS BIGINT)")
  end
end

#query_with_uuidObject



40
41
42
43
44
45
46
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 40

def query_with_uuid
  if postgres?
    query.select("CAST(#{model_class.table_name}.#{column_name_to_select} AS UUID)")
  else
    query_with_varchar
  end
end

#query_with_varcharObject



32
33
34
35
36
37
38
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 32

def query_with_varchar
  if mysql?
    query.select("CAST(#{model_class.table_name}.#{column_name_to_select} AS CHAR)")
  else
    query.select("CAST(#{model_class.table_name}.#{column_name_to_select} AS VARCHAR)")
  end
end

#same_type?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 48

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