Es hat noch nicht viel Verwendung erhalten, aber ich schrieb den folgenden Code, der das Problem zu lösen scheint. Dieser Affe-Patches der Adapter, ein Verfahren zu haben, um sie zu unterstützen:
module ActiveRecord
module ConnectionAdapters
class AbstractAdapter
# Will return the given strings as a SQL concationation. By default
# uses the SQL-92 syntax:
#
# concat('foo', 'bar') -> "foo || bar"
def concat(*args)
args * " || "
end
end
class AbstractMysqlAdapter < AbstractAdapter
# Will return the given strings as a SQL concationation.
# Uses MySQL format:
#
# concat('foo', 'bar') -> "CONCAT(foo, bar)"
def concat(*args)
"CONCAT(#{args * ', '})"
end
end
class SQLServerAdapter < AbstractAdapter
# Will return the given strings as a SQL concationation.
# Uses MS-SQL format:
#
# concat('foo', 'bar') -> foo + bar
def concat(*args)
args * ' + '
end
end
end
end
Damit Sie sollen in der Lage sein, die folgend in Ihrem Code zu tun:
class User < ActiveRecord::Base
def self.find_by_name(name)
where("#{connection.concat('first_name', 'last_name')} = ?", name)
end
end
Das gibt die folgende SQL-Abfrage auf einem SQL-92-Datenbank (Oracle, SQLite, PostgreSQL):
SELECT * FROM users WHERE first_name || last_name = ?
Für MySQL gibt sie:
SELECT * FROM users WHERE CONCAT(first_name, last_name) = ?
Für SQL Server gibt es
SELECT * FROM users WHERE first_name + last_name = ?
Offensichtlich Sie dieses Konzept auf andere Datenbank-Adapter erweitern könnte.
Ich denke, wenn es existiert, wirst du derjenige sein, der es schreibt :-P –
FYI, ich habe es geschrieben http://github.com/adamcrown/port-a-query. Es gibt noch nicht viel zu tun, aber es funktioniert mit der Verkettung in MySQL und SQLite – aNoble