diff options
author | Jeremy Daer <jeremydaer@gmail.com> | 2016-04-19 00:44:10 -0700 |
---|---|---|
committer | Jeremy Daer <jeremydaer@gmail.com> | 2016-04-19 00:44:10 -0700 |
commit | ca13731e617180a0d7fde3c2a3d73dc396b6bda7 (patch) | |
tree | 827e2fde6af38bc812824a9626c6cb37b87d9ec4 /activerecord | |
parent | 7854e69c7445dd9c958783fef47914c745ac53c8 (diff) | |
parent | 072aa06582c26fe0112564b7d0f5b02a8b5eee2a (diff) | |
download | rails-ca13731e617180a0d7fde3c2a3d73dc396b6bda7.tar.gz rails-ca13731e617180a0d7fde3c2a3d73dc396b6bda7.tar.bz2 rails-ca13731e617180a0d7fde3c2a3d73dc396b6bda7.zip |
Merge pull request #23497 from kamipo/extract_schema_qualified_name
Extract `extract_schema_qualified_name` method
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index 5275d8becb..5125a0f06e 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -326,8 +326,7 @@ module ActiveRecord def data_source_exists?(table_name) return false unless table_name.present? - schema, name = table_name.to_s.split('.', 2) - schema, name = @config[:database], schema unless name # A table was provided without a schema + schema, name = extract_schema_qualified_name(table_name) sql = "SELECT table_name FROM information_schema.tables " sql << "WHERE table_schema = #{quote(schema)} AND table_name = #{quote(name)}" @@ -342,8 +341,7 @@ module ActiveRecord def view_exists?(view_name) # :nodoc: return false unless view_name.present? - schema, name = view_name.to_s.split('.', 2) - schema, name = @config[:database], schema unless name # A view was provided without a schema + schema, name = extract_schema_qualified_name(view_name) sql = "SELECT table_name FROM information_schema.tables WHERE table_type = 'VIEW'" sql << " AND table_schema = #{quote(schema)} AND table_name = #{quote(name)}" @@ -488,6 +486,10 @@ module ActiveRecord end def foreign_keys(table_name) + raise ArgumentError unless table_name.present? + + schema, name = extract_schema_qualified_name(table_name) + fk_info = select_all <<-SQL.strip_heredoc SELECT fk.referenced_table_name as 'to_table' ,fk.referenced_column_name as 'primary_key' @@ -495,8 +497,8 @@ module ActiveRecord ,fk.constraint_name as 'name' FROM information_schema.key_column_usage fk WHERE fk.referenced_column_name is not null - AND fk.table_schema = '#{@config[:database]}' - AND fk.table_name = '#{table_name}' + AND fk.table_schema = #{quote(schema)} + AND fk.table_name = #{quote(name)} SQL create_table_info = create_table_info(table_name) @@ -563,8 +565,7 @@ module ActiveRecord def primary_keys(table_name) # :nodoc: raise ArgumentError unless table_name.present? - schema, name = table_name.to_s.split('.', 2) - schema, name = @config[:database], schema unless name # A table was provided without a schema + schema, name = extract_schema_qualified_name(table_name) select_values(<<-SQL.strip_heredoc, 'SCHEMA') SELECT column_name @@ -878,6 +879,12 @@ module ActiveRecord MySQL::TableDefinition.new(*args) end + def extract_schema_qualified_name(string) # :nodoc: + schema, name = string.to_s.scan(/[^`.\s]+|`[^`]*`/) + schema, name = @config[:database], schema unless name + [schema, name] + end + def integer_to_sql(limit) # :nodoc: case limit when 1; 'tinyint' |