Class: WorkerPlugins::SelectColumnWithTypeCast
Instance Method Summary
collapse
#db_now_value, #mysql?, #postgres?, #quote, #quote_column, #quote_table, #relation_unscoped?, #sqlite?
Instance Method Details
#column_to_select ⇒ Object
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_class ⇒ Object
20
21
22
|
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 20
def model_class
@model_class ||= query.klass
end
|
#mysql_implicit_conversion_safe? ⇒ Boolean
On MySQL / MariaDB, implicit conversion handles comparisons between any string-ish column types (VARCHAR, CHAR, BINARY, UUID, or types AR doesn’t recognize — e.g. MariaDB’s native UUID type when using an older mysql2 adapter). The explicit CAST is only needed when one side is numeric, because MySQL would then force a string → number conversion that loses rows containing non-numeric values.
60
61
62
63
64
|
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 60
def mysql_implicit_conversion_safe?
return false unless mysql?
!numeric_type?(column_to_select.type) && !numeric_type?(column_to_compare_with.type)
end
|
#numeric_type?(type) ⇒ Boolean
66
67
68
|
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 66
def numeric_type?(type)
%i[integer decimal float bigint].include?(type)
end
|
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_integer ⇒ Object
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_uuid ⇒ Object
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_varchar ⇒ Object
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
48
49
50
51
52
|
# File 'app/services/worker_plugins/select_column_with_type_cast.rb', line 48
def same_type?
return true if column_to_select.type == column_to_compare_with.type
mysql_implicit_conversion_safe?
end
|