Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract data_source_sql to refactor data source statements #28068

Merged
merged 2 commits into from
Mar 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def table_alias_for(table_name)
# Returns the relation names useable to back Active Record models.
# For most adapters this means all #tables and #views.
def data_sources
select_values(data_source_sql, "SCHEMA")
rescue NotImplementedError
tables | views
end

Expand All @@ -39,32 +41,38 @@ def data_sources
# data_source_exists?(:ebooks)
#
def data_source_exists?(name)
select_values(data_source_sql(name), "SCHEMA").any? if name.present?
rescue NotImplementedError
data_sources.include?(name.to_s)
end

# Returns an array of table names defined in the database.
def tables
raise NotImplementedError, "#tables is not implemented"
select_values(data_source_sql(type: "BASE TABLE"), "SCHEMA")
end

# Checks to see if the table +table_name+ exists on the database.
#
# table_exists?(:developers)
#
def table_exists?(table_name)
select_values(data_source_sql(table_name, type: "BASE TABLE"), "SCHEMA").any? if table_name.present?
rescue NotImplementedError
tables.include?(table_name.to_s)
end

# Returns an array of view names defined in the database.
def views
raise NotImplementedError, "#views is not implemented"
select_values(data_source_sql(type: "VIEW"), "SCHEMA")
end

# Checks to see if the view +view_name+ exists on the database.
#
# view_exists?(:ebooks)
#
def view_exists?(view_name)
select_values(data_source_sql(view_name, type: "VIEW"), "SCHEMA").any? if view_name.present?
rescue NotImplementedError
views.include?(view_name.to_s)
end

Expand Down Expand Up @@ -1306,6 +1314,14 @@ def extract_new_default_value(default_or_changes)
def can_remove_index_by_name?(options)
options.is_a?(Hash) && options.key?(:name) && options.except(:name, :algorithm).empty?
end

def data_source_sql(name = nil, type: nil)
raise NotImplementedError
end

def quoted_scope(name = nil, type: nil)
raise NotImplementedError
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require "active_record/connection_adapters/mysql/schema_creation"
require "active_record/connection_adapters/mysql/schema_definitions"
require "active_record/connection_adapters/mysql/schema_dumper"
require "active_record/connection_adapters/mysql/schema_statements"
require "active_record/connection_adapters/mysql/type_metadata"

require "active_support/core_ext/string/strip"
Expand All @@ -15,6 +16,7 @@ module ConnectionAdapters
class AbstractMysqlAdapter < AbstractAdapter
include MySQL::Quoting
include MySQL::ColumnDumper
include MySQL::SchemaStatements

def update_table_definition(table_name, base) # :nodoc:
MySQL::Table.new(table_name, base)
Expand Down Expand Up @@ -314,57 +316,6 @@ def collation
show_variable "collation_database"
end

def tables # :nodoc:
sql = "SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE'"
sql << " AND table_schema = #{quote(@config[:database])}"

select_values(sql, "SCHEMA")
end

def views # :nodoc:
select_values("SHOW FULL TABLES WHERE table_type = 'VIEW'", "SCHEMA")
end

def data_sources # :nodoc:
sql = "SELECT table_name FROM information_schema.tables "
sql << "WHERE table_schema = #{quote(@config[:database])}"

select_values(sql, "SCHEMA")
end

def table_exists?(table_name) # :nodoc:
return false unless table_name.present?

schema, name = extract_schema_qualified_name(table_name)

sql = "SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE'"
sql << " AND table_schema = #{quote(schema)} AND table_name = #{quote(name)}"

select_values(sql, "SCHEMA").any?
end

def data_source_exists?(table_name) # :nodoc:
return false unless table_name.present?

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)}"

select_values(sql, "SCHEMA").any?
end

def view_exists?(view_name) # :nodoc:
return false unless view_name.present?

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)}"

select_values(sql, "SCHEMA").any?
end

def truncate(table_name, name = nil)
execute "TRUNCATE TABLE #{quote_table_name(table_name)}", name
end
Expand Down Expand Up @@ -410,13 +361,13 @@ def new_column_from_field(table_name, field) # :nodoc:
end

def table_comment(table_name) # :nodoc:
schema, name = extract_schema_qualified_name(table_name)
scope = quoted_scope(table_name)

select_value(<<-SQL.strip_heredoc, "SCHEMA")
SELECT table_comment
FROM information_schema.tables
WHERE table_schema = #{quote(schema)}
AND table_name = #{quote(name)}
WHERE table_schema = #{scope[:schema]}
AND table_name = #{scope[:name]}
SQL
end

Expand Down Expand Up @@ -516,7 +467,7 @@ def add_sql_comment!(sql, comment) # :nodoc:
def foreign_keys(table_name)
raise ArgumentError unless table_name.present?

schema, name = extract_schema_qualified_name(table_name)
scope = quoted_scope(table_name)

fk_info = select_all(<<-SQL.strip_heredoc, "SCHEMA")
SELECT fk.referenced_table_name AS 'to_table',
Expand All @@ -529,9 +480,9 @@ def foreign_keys(table_name)
JOIN information_schema.referential_constraints rc
USING (constraint_schema, constraint_name)
WHERE fk.referenced_column_name IS NOT NULL
AND fk.table_schema = #{quote(schema)}
AND fk.table_name = #{quote(name)}
AND rc.table_name = #{quote(name)}
AND fk.table_schema = #{scope[:schema]}
AND fk.table_name = #{scope[:name]}
AND rc.table_name = #{scope[:name]}
SQL

fk_info.map do |row|
Expand Down Expand Up @@ -603,14 +554,14 @@ def show_variable(name)
def primary_keys(table_name) # :nodoc:
raise ArgumentError unless table_name.present?

schema, name = extract_schema_qualified_name(table_name)
scope = quoted_scope(table_name)

select_values(<<-SQL.strip_heredoc, "SCHEMA")
SELECT column_name
FROM information_schema.key_column_usage
WHERE constraint_name = 'PRIMARY'
AND table_schema = #{quote(schema)}
AND table_name = #{quote(name)}
AND table_schema = #{scope[:schema]}
AND table_name = #{scope[:name]}
ORDER BY ordinal_position
SQL
end
Expand Down Expand Up @@ -948,12 +899,6 @@ def mismatched_foreign_key(message)
)
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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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