diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2017-03-13 19:56:12 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-13 19:56:12 -0400 |
commit | e2e63770f59ce4585944447ee237ec722761e77d (patch) | |
tree | be527e3434eaefebc55cc4f4a8b02df7f942c870 /activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb | |
parent | 82aa2c124f4c2091ca4924735af466c22f0eebb4 (diff) | |
parent | 9d5217db5dce5e61cf3308329d94593a0e1cf6ff (diff) | |
download | rails-e2e63770f59ce4585944447ee237ec722761e77d.tar.gz rails-e2e63770f59ce4585944447ee237ec722761e77d.tar.bz2 rails-e2e63770f59ce4585944447ee237ec722761e77d.zip |
Merge pull request #28068 from kamipo/refactor_data_sources
Extract `data_source_sql` to refactor data source statements
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb new file mode 100644 index 0000000000..10c8bd179a --- /dev/null +++ b/activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb @@ -0,0 +1,33 @@ +module ActiveRecord + module ConnectionAdapters + module MySQL + module SchemaStatements # :nodoc: + private + def data_source_sql(name = nil, type: nil) + scope = quoted_scope(name, type: type) + + sql = "SELECT table_name FROM information_schema.tables" + sql << " WHERE table_schema = #{scope[:schema]}" + sql << " AND table_name = #{scope[:name]}" if scope[:name] + sql << " AND table_type = #{scope[:type]}" if scope[:type] + sql + end + + def quoted_scope(name = nil, type: nil) + schema, name = extract_schema_qualified_name(name) + scope = {} + scope[:schema] = schema ? quote(schema) : "database()" + scope[:name] = quote(name) if name + scope[:type] = quote(type) if type + scope + end + + def extract_schema_qualified_name(string) + schema, name = string.to_s.scan(/[^`.\s]+|`[^`]*`/) + schema, name = nil, schema unless name + [schema, name] + end + end + end + end +end |