2016-08-31 11:20:36 -04:00
|
|
|
# frozen_string_literal: true
|
2018-02-24 01:45:50 -05:00
|
|
|
|
2018-02-24 02:41:47 -05:00
|
|
|
module Arel # :nodoc: all
|
2010-09-23 18:26:08 -04:00
|
|
|
module Visitors
|
|
|
|
class MySQL < Arel::Visitors::ToSql
|
2010-10-19 14:47:00 -04:00
|
|
|
private
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_Bin(o, collector)
|
|
|
|
collector << "BINARY "
|
|
|
|
visit o.expr, collector
|
|
|
|
end
|
2011-04-11 13:02:28 -04:00
|
|
|
|
2018-09-27 12:03:13 -04:00
|
|
|
def visit_Arel_Nodes_UnqualifiedColumn(o, collector)
|
|
|
|
visit o.expr, collector
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
###
|
|
|
|
# :'(
|
2019-09-05 20:54:16 -04:00
|
|
|
# https://dev.mysql.com/doc/refman/8.0/en/select.html#id3482214
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_SelectStatement(o, collector)
|
|
|
|
if o.offset && !o.limit
|
|
|
|
o.limit = Arel::Nodes::Limit.new(18446744073709551615)
|
|
|
|
end
|
|
|
|
super
|
2014-03-24 19:26:09 -04:00
|
|
|
end
|
2010-10-19 14:47:00 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def visit_Arel_Nodes_SelectCore(o, collector)
|
|
|
|
o.froms ||= Arel.sql("DUAL")
|
|
|
|
super
|
|
|
|
end
|
2010-11-05 17:28:36 -04:00
|
|
|
|
2018-09-30 05:30:47 -04:00
|
|
|
def visit_Arel_Nodes_Concat(o, collector)
|
|
|
|
collector << " CONCAT("
|
|
|
|
visit o.left, collector
|
|
|
|
collector << ", "
|
|
|
|
visit o.right, collector
|
|
|
|
collector << ") "
|
|
|
|
collector
|
|
|
|
end
|
|
|
|
|
2018-11-15 14:49:55 -05:00
|
|
|
def visit_Arel_Nodes_IsNotDistinctFrom(o, collector)
|
|
|
|
collector = visit o.left, collector
|
|
|
|
collector << " <=> "
|
|
|
|
visit o.right, collector
|
|
|
|
end
|
|
|
|
|
|
|
|
def visit_Arel_Nodes_IsDistinctFrom(o, collector)
|
|
|
|
collector << "NOT "
|
|
|
|
visit_Arel_Nodes_IsNotDistinctFrom o, collector
|
|
|
|
end
|
|
|
|
|
2019-07-29 07:04:07 -04:00
|
|
|
def visit_Arel_Nodes_Regexp(o, collector)
|
|
|
|
infix_value o, collector, " REGEXP "
|
|
|
|
end
|
|
|
|
|
|
|
|
def visit_Arel_Nodes_NotRegexp(o, collector)
|
|
|
|
infix_value o, collector, " NOT REGEXP "
|
|
|
|
end
|
|
|
|
|
2018-10-02 16:29:27 -04:00
|
|
|
# In the simple case, MySQL allows us to place JOINs directly into the UPDATE
|
|
|
|
# query. However, this does not allow for LIMIT, OFFSET and ORDER. To support
|
|
|
|
# these, we must use a subquery.
|
|
|
|
def prepare_update_statement(o)
|
2018-10-04 13:16:58 -04:00
|
|
|
if o.offset || has_join_sources?(o) && has_limit_or_offset_or_orders?(o)
|
2018-10-02 16:29:27 -04:00
|
|
|
super
|
|
|
|
else
|
|
|
|
o
|
|
|
|
end
|
|
|
|
end
|
2018-10-09 16:31:20 -04:00
|
|
|
alias :prepare_delete_statement :prepare_update_statement
|
2018-10-02 16:29:27 -04:00
|
|
|
|
|
|
|
# MySQL is too stupid to create a temporary table for use subquery, so we have
|
|
|
|
# to give it some prompting in the form of a subsubquery.
|
2018-09-30 07:17:41 -04:00
|
|
|
def build_subselect(key, o)
|
|
|
|
subselect = super
|
|
|
|
|
|
|
|
# Materialize subquery by adding distinct
|
|
|
|
# to work with MySQL 5.7.6 which sets optimizer_switch='derived_merge=on'
|
2018-10-02 16:29:27 -04:00
|
|
|
unless has_limit_or_offset_or_orders?(subselect)
|
|
|
|
core = subselect.cores.last
|
|
|
|
core.set_quantifier = Arel::Nodes::Distinct.new
|
|
|
|
end
|
2018-09-30 07:17:41 -04:00
|
|
|
|
|
|
|
Nodes::SelectStatement.new.tap do |stmt|
|
|
|
|
core = stmt.cores.last
|
|
|
|
core.froms = Nodes::Grouping.new(subselect).as("__active_record_temp")
|
|
|
|
core.projections = [Arel.sql(quote_column_name(key.name))]
|
|
|
|
end
|
|
|
|
end
|
2010-09-23 18:26:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|